$(document).ready(function() {

    $("#butLocation img").hover(
		function() {
		    this.src = this.src.replace("_lo-", "_hi-");
		},
		function() {
		    this.src = this.src.replace("_hi-", "_lo-");
		}
	);

});

var pGallery = {};

function initGallery() {

    pGallery.pZoomImage = document.getElementById("background");
    pGallery.jThumbsFramer = $('div.thumbsframer');
    pGallery.jThumbsWrapper = $('div.thumbswrapper');
    pGallery.pThumbWidth = 139;
    pGallery.pThumbSpace = 12;
    pGallery.pThumbCount = 0;

    var thumbs = pGallery.jThumbsWrapper.find('div.thumb');
    if (thumbs != null && thumbs.length > 0) {
        pGallery.pThumbCount = thumbs.length;
        var thumb = $(thumbs[0]);
        pGallery.pThumbWidth = parseInt(thumb.width());
        pGallery.pThumbSpace = parseInt(thumb.css('marginRight'));
    }

    pGallery.pThumbsPerPage = parseInt(Math.floor((1.0 * pGallery.jThumbsFramer.width() + pGallery.pThumbSpace) / (1.0 * (pGallery.pThumbWidth + pGallery.pThumbSpace))));

    pGallery.pPageWidth = pGallery.pThumbsPerPage*(pGallery.pThumbWidth + pGallery.pThumbSpace);
    pGallery.pPageIndex = 0;
    pGallery.pPageCount = parseInt(Math.ceil((1.0 * Math.max(pGallery.pThumbCount,1)) / (1.0 * pGallery.pThumbsPerPage)));

    pGallery.jThumbsWrapper.css({ width: (pGallery.pThumbCount * (pGallery.pThumbWidth + pGallery.pThumbSpace))+"px" });
    
//    alert("initGallery:"
//        + "\nthumbs: count=" + pGallery.pThumbCount + ", width=" + pGallery.pThumbWidth + ", space=" + pGallery.pThumbSpace + ", perPage=" + pGallery.pThumbsPerPage
//        + "\npage: count=" + pGallery.pPageCount + ", width=" + pGallery.pPageWidth
//    );
    
    var setupFrom = $('td.view div').find('a:first');
    if (setupFrom != null && setupFrom.length > 0) {
        setupFrom.trigger('click');
    }

    $('#slider IMG[title!=]').qtip({
        position: {
            corner: {
                target: 'topMiddle',
                tooltip: 'bottomMiddle'
            },
            adjust: {
                y: -4
            }
        },
        style: {
            width: 180,
            padding: 5,
            background: '#FFFFFF',
            color: 'black',
            textAlign: 'left',
            border: {
                width: 0,
                radius: 4,
                color: '#c0c0c0'
            },
            tip: {
                corner: 'bottomMiddle',
                color: '#c0c0c0',
                size: {
                    x: 20,
                    y: 10
                }
            }
        }

    });
}

function closeGallery(goTo, doBack) {
    if (doBack && window.history.length > 0) {
        window.history.back();
    } else {
        document.location = goTo;
    }
}

function showGalleryImage(ctrl, path) {

    pGallery.pZoomImage.src = path + "?t=glarge";
    ctrl.blur();
}

function pageGallery(ctrl, pageDelta) {
    pGallery.pPageIndex = Math.max(0, Math.min(pGallery.pPageCount - 1, pGallery.pPageIndex + pageDelta));
    var i = pGallery.pPageIndex * pGallery.pThumbsPerPage;
    var x = (i*pGallery.pThumbWidth+i*pGallery.pThumbSpace);
    //window.status = ("pageGallery: " + pGallery.pPageIndex + " -> " + x);
    pGallery.jThumbsWrapper.stop();
    pGallery.jThumbsWrapper.animate({ left: (-x)+"px" }, { duration: 500, easing: 'easeOutCirc' });
}