var bg = new Image(); 
bg.src = '/images/bg.jpg';

function buttonOver() {
    thewrap = document.getElementById('wrapper');
    buttons = thewrap.getElementsByTagName('input');
    for (i = 0; i < buttons.length; i++) {
        if(buttons[i].getAttribute('type') == 'image') {
            buttons[i].onmouseover = function() {
                this.src = this.src.substring(0, this.src.length - 4) + "2.jpg";
            }
            buttons[i].onmouseout = function() {
                if (this.src.substring( (this.src.length - 5) , (this.src.length) ) == "2.jpg" ) {
                    this.src = this.src.substring(0, this.src.length - 5) + ".jpg";
                }
            }
        }
    }
}

function imageOver() {
    thewrap = document.getElementById('wrapper');
    images = thewrap.getElementsByTagName('img');
    for (i = 0; i < images.length; i++) {
        if(images[i].className.substring(0, 5) == "hover") {
            images[i].onmouseover = function() {
                this.src = this.src.substring(0, this.src.length - 4) + "2.jpg";
            }
            images[i].onmouseout = function() {
                if (this.src.substring( (this.src.length - 5) , (this.src.length) ) == "2.jpg" ) {
                    this.src = this.src.substring(0, this.src.length - 5) + ".jpg";
                }
            }
        }
    }
}


function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function addUnloadEvent(func) {
    var oldonunload = window.onunload;
    if (typeof window.onunload != 'function') {
        window.onunload = func;
    } else {
        window.onunload = function() {
            oldonunload();
            func();
        }
    }
}

addLoadEvent(buttonOver);
addLoadEvent(imageOver);

var page = 0;
var pages = 0;
var totalCount = 0;
var perPage = 4;

function setupGallery(imgCount) {
    var totalWidth = 88 * imgCount;
    totalCount = imgCount;
    pages = imgCount / 4;
    pages = Math.ceil(pages);
    page = 1;
    document.getElementById('thumbSlider').style.height = '68px';
    document.getElementById('thumbSlider').style.width = totalWidth + 'px';
    document.getElementById('thumbSlider').style.position = 'absolute';
    document.getElementById('thumbWrapper').style.height = '68px';
    document.getElementById('thumbWrapper').style.overflow = 'hidden';
    document.getElementById('thumbLeft').style.display = 'inline';
    document.getElementById('thumbRight').style.display = 'inline';
    document.getElementById('xofx').style.display = 'block';
    enableDisable();
}

function setupSalesGallery(imgCount) {
    if (imgCount <5) {
        var imagesHigh = 1;
    } else if (imgCount <9) {
        var imagesHigh = 2;
    } else {
        var imagesHigh = 3;
    }

    var imagesAcross = ( 4 * (Math.ceil(imgCount / 12 ) ) );
    var totalWidth = 88 * imagesAcross;
    var totalHeight = 102 * imagesHigh;
    totalCount = imgCount;
    pages = Math.ceil(imgCount / 12);
    page = 1;
    perPage = 12;

    document.getElementById('thumbSlider').style.height = totalHeight + 'px';
    document.getElementById('thumbSlider').style.width = totalWidth + 'px';
    document.getElementById('thumbSlider').style.position = 'absolute';
    document.getElementById('thumbSlider').style.left = '0px';
    document.getElementById('thumbSlider').style.top = '0px';
    document.getElementById('thumbWrapper').style.height = totalHeight + 'px';
    document.getElementById('thumbWrapper').style.overflow = 'hidden';
    document.getElementById('thumbLeft').style.display = 'inline';
    document.getElementById('thumbRight').style.display = 'inline';
    document.getElementById('xofx').style.display = 'block';
    enableDisable();
}

function navRight() {
    new Effect.MoveBy('thumbSlider', 0, -352 , {duration: 1, queue: 'end',  transition: Effect.Transitions.sinoidal});
    page = page + 1;
    enableDisable();
}

function navLeft() {
    new Effect.MoveBy('thumbSlider', 0, 352 , {duration: 1, queue: 'end',  transition: Effect.Transitions.sinoidal});
    page = page - 1;
    enableDisable()
}

function enableDisable() {
    if(pages == 1) {
        document.getElementById('thumbRight').style.display = 'none';
        document.getElementById('thumbLeft').style.display = 'none';
        document.getElementById('thumbNavigation').style.display = 'none';
    } else if(page == pages) {
        document.getElementById('thumbRight').style.display = 'none';
        document.getElementById('thumbLeft').style.display = 'block';
    } else if(page == 1) {
        document.getElementById('thumbRight').style.display = 'block';
        document.getElementById('thumbLeft').style.display = 'none';
    } else {
        document.getElementById('thumbRight').style.display = 'block';
        document.getElementById('thumbLeft').style.display = 'block';
    }
    updateXofX();
}

function updateXofX() {
    pagel = ((page - 1) * perPage) + 1;

    if(page == pages) {
        pager = totalCount;
    } else {
        pager = page * perPage;
    }

    document.getElementById('xofx').innerHTML = pagel + " - " + pager + " of " + totalCount;
}

function imgSwap(newId, newCaption) {
    var mainImage = document.getElementById('mainImage');
    mainImage.src = '/images/galleryimages/img' + newId + '-lrg.jpg';
    if (typeof(newCaption) != "undefined") {
        document.getElementById('mainImageCaption').innerHTML = newCaption;
    }
}

