function showImg(thId){
    var a = photogalleryA;
	for(var i = 0; i < a.length; i++){
        if(oDiv = document.getElementById('div' + a[i])){
            oThumb = document.getElementById('th' + a[i]);
            oPreview = document.getElementById('preview' + a[i]);
            if(a[i] == thId){
                oDiv.style.display = '';
                oThumb.className = 'sel';
                oPreview.src = photogallerySources[i];
            } else {
                oDiv.style.display = 'none';
                oThumb.className = '';
            }
        }
    }
    if(oThumbs){
        checkSelectedPosition();
        resizeThumbs();
    }
}

function showPreview(iThumbId, aIds, sNewSrc){
    var oThumb;
    var oPreview;
    for(var i = 0; i < aIds.length; i++){
        if(oThumb = document.getElementById('ph_thumb' + aIds[i])){
            if(aIds[i] == iThumbId){
                if(oPreview = document.getElementById('ph_preview')){
                    oPreview.src = sNewSrc;
                }
                oThumb.className = 'sel';
            } else {
                oThumb.className = '';
            }
        }
    }
}

var bSS = false;
var iScrollTimeout = 1;
var iTimeOut = window.addEventListener ? iScrollTimeout * 2 : iScrollTimeout;
var iScrollKoef = 15;
var iCW, iTW, iTL, iTR, iDiff;
var oContainer = null;
var oThumbs = null;
var oLeftArr = null;
var oRightArr = null;


function initScrollVars(){
    iCW = oContainer.offsetWidth;
    iTW = oThumbs.offsetWidth;
    iTL = oThumbs.offsetLeft;
    iDiff = iTW - iCW;
    iTR = -(iTL + iDiff);
}

function scrollInit(){
	if(oContainer = document.getElementById('gn_container')){
    if(oThumbs = document.getElementById('gn_thumbs')){
    if(oLeftArr = document.getElementById('gn_arr_left')){
    if(oRightArr = document.getElementById('gn_arr_right')){
        if(oContainer.attachEvent){
            oContainer.attachEvent("onmousewheel", mouseScrollThumbs);
        } else {
            oContainer.addEventListener("DOMMouseScroll", mouseScrollThumbs, true);
        }
        if(window.addEventListener){
            window.addEventListener('resize', resizeThumbs, false);
        } else {
            window.attachEvent('onresize', resizeThumbs);
        }
        checkScrollButtons();
        checkSelectedPosition();
        resizeThumbs();
    showImg(0, 1);
    }
    }
    }
    }
}

function checkSelectedPosition(){
    var a = photogalleryA;
	if(oThumbs){
        var i = 0;
        var num = 0;
        oSelThumb = null;
        for (i = 0; i < a.length; i++) {
            if(oThumb = document.getElementById('th' + a[i])) {
                if(oThumb.className == 'sel'){
                    oSelThumb = oThumb;
                    num = i;
                }
            }
        }
        iLeft = Math.round(iCW / 2 - (iTW / a.length) * (num + 1/2));
        iLeft = iLeft + iTW < iCW ? iCW - iTW : iLeft;
        iLeft = iLeft > 0 ? 0 : iLeft;
        oThumbs.style.left = iLeft + 'px';
    }
}

function checkScrollButtons(){
    initScrollVars();
    if(iDiff > 0){
        bRA = iTR < 0 ? true : false;
        bLA = iTL < 0 ? true : false;
    } else {
        bLA = false; bRA = false;
    }

    oLeftArr.style.visibility = bLA ? '' : 'hidden';
    oRightArr.style.visibility = bRA ? '' : 'hidden';
}

function resizeThumbs(){
    if(oThumbs){
        initScrollVars();
        if(iTL < 0){
            if(iDiff < 0) {
                oThumbs.style.left = '0px';
            } else if(iDiff > 0) {
                if(iTR >= 0){
                    oThumbs.style.left = -iDiff + 'px';
                }
            }
        } else {
            oThumbs.style.left = '0px';
        }
        checkScrollButtons();
    }
}

function scrollThumbs(sSS, sDirection){
    bSS = sSS == 'start' ? true : false;
    iDirection = sDirection == 'left' ? -1 : 1;
    _scroll(iDirection);
}

function mouseScrollThumbs(evt){
    if(!evt) evt = window.event;
    if(evt.wheelDelta) {
        delta = evt.wheelDelta / 120;
        if(window.opera) delta = -delta;
    } else if (evt.detail) {
        delta = -evt.detail / 3;
    }
    iDirection = delta > 0 ? -1 : 1;
    _scroll(iDirection, delta * 5);
    if (evt.preventDefault) evt.preventDefault();
    evt.returnValue = false;
    return false;
}

function _scroll(iDirection, iKoef){
    var iMultiplier = !isNaN(iKoef) ? Math.abs(iKoef) : 1;
    var iShift = iScrollKoef * iMultiplier * iDirection;

    initScrollVars();
    if(oThumbs && iCW > 0){
        if(iDiff > 0) {
            if(iTL > iShift) {
                oThumbs.style.left = '0px';
            } else { 
                if((iDiff + iTL) < iShift) {
                    oThumbs.style.left = -iDiff + 'px'; 
                } else {
                    oThumbs.style.left = (iTL - iShift) + 'px';
                    if(bSS){
                        setTimeout('_scroll(' + iDirection + ')', iTimeOut);
                    }
                }
            }
        }
    }
    checkScrollButtons();
}
