﻿var maxWidth = 520;
var maxHeight = 347;
var scroller = 0;
var offset = 0;
var stepSize = 106; // how many px
//var imgCount = 0;  // defined and updated dynamically when the gallery thumbnails loaded

function moveRight() {
	var rightBound = -1 * (imgCount - 4);
	if (scroller > rightBound) {
	    scroller --;
	}
	
    offset = stepSize * scroller;
   
	jQuery("#thumbnails").animate({left: offset},500);	
}

function moveLeft() {
	
    if (offset < 0) {
        scroller ++;
    } 
    offset = stepSize * scroller;    

	jQuery("#thumbnails").animate({left: offset},500);
}

function clickHandler(imgObj, imgURL) {
    var thumbW, thumbH;
    thumbW = imgObj.width;
    thumbH = imgObj.height;
    slideSwitch(imgURL, thumbW, thumbH);
}


function slideSwitch(imageURL, thumbW, thumbH) {
	var imageHolder = jQuery('#slideshow IMG.imageHolder');
	
	imageHolder.fadeOut(500, function(){imageHolder.attr({src: imageURL});});
	
	imageHolder.load(function(){
	                    var imgWidth, imgHeight, newWidth, newHeight;
	                    imgWidth = imageHolder.width();
	                    imgHeight = imageHolder.height();
	                    var ratioWH = thumbW / thumbH;
	                    
                        if (thumbW > thumbH) {
                            newWidth = maxWidth;
                            newHeight = parseInt(newWidth/ratioWH);
                        } else if (thumbW < thumbH) {
                            newHeight = maxHeight;
                            newWidth = parseInt(ratioWH * newHeight);
                        } else {
                            newWidth = maxHeight;
                            newHeight = maxHeight;
                        }

                        imageHolder.attr({height: newHeight, width: newWidth});
                        finalResize();
	                    imageHolder.fadeIn(500);
	                });
}

function finalResize() {
    var imageHolder = jQuery('#slideshow IMG.imageHolder');
    var imgWidth = imageHolder.width();
    var imgHeight = imageHolder.height();
    var ratioWH = imgWidth / imgHeight;
    
    if(imgWidth > maxWidth){
        imgWidth = maxWidth;
        imgHeight = parseInt(imgWidth/ratioWH);
    }
    
    if(imgHeight > maxHeight) {
        imgHeight = maxHeight;
        imgWidth = parseInt(ratioWH * imgHeight);
    }
    
    imageHolder.attr({height: imgHeight, width: imgWidth});
}

// Being called once to set the first image on the slide
function setDefaultImg() {
	var strDefaultImg = jQuery("#thumbnails img").attr("alt");
	var imageHolder = jQuery('#slideshow IMG.imageHolder');
	imageHolder.css("opacity", "0");
	imageHolder.attr({src: strDefaultImg});
	imageHolder.load(function(){
	                    var myW = imageHolder.width();
                        var myH = imageHolder.height();
                        
	                    var ratioWH = myW/myH;

                        var newWidth, newHeight;

                        if (myW > myH) {
                            newWidth = maxWidth;
                            newHeight = parseInt(newWidth/ratioWH);
                        } else if (myW < myH) {
                            newHeight = maxHeight;
                            newWidth = parseInt(ratioWH * newHeight);
                        } else {
                            newHeight = maxHeight;
                            newWidth = maxHeight;
                        }
                        imageHolder.attr({height: newHeight, width: newWidth});
                        finalResize();
                        imageHolder.css("opacity", "1");
	                });
}