function showImg(thId){
    if (a.length == 1){
        oDiv = $('div' + a[0]);
        oDiv.style.display = '';
        oPreview = $('preview' + a[0]);
        oPreview.src = sources[0];
    }
    else{
        for(var i = 0; i < a.length; i++){
            if(oDiv = $('div' + a[i])){
                oThumb = $('th' + a[i]);
                oPreview = $('preview' + a[i]);
                if(a[i] == thId){
                    oDiv.style.display = '';
                    oThumb.className = 'currentPage';
                    oPreview.src = sources[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 = $('ph_thumb' + aIds[i])){
            if(aIds[i] == iThumbId){
                if(oPreview = $('ph_preview')){
                    oPreview.src = sNewSrc;
                }
                oThumb.className = 'currentPage';
            } 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 = $('gn_container')){
        if(oThumbs = $('gn_thumbs')){
            if(oLeftArr = $('gn_arr_left')){
                if(oRightArr = $('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);
                }
            }
        }
        

    }
    else{
        showImg(0);
    }
}

function checkSelectedPosition(){
    if(oThumbs){
        var i = 0;
        var num = 0;
        oSelThumb = null;
        for (i = 0; i < a.length; i++) {
            if(oThumb = $('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.opacity = bLA ? '' : '0.25';
    oRightArr.style.opacity = bRA ? '' : '0.25';
//oLeftArr.style.visibility = bLA ? '' : 'hidden';
//oLeftArr.style.cursor = bLA ? '' : 'none';
//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();
}

