$(function() { 
    $("a[rel]").overlay({
                onBeforeLoad: function() { 
                var wrap = this.getContent().find("div.wrap"); 
                    if (wrap.is(":empty")) { 
                        wrap.load(this.getTrigger().attr("href")); 
                    }  
             	}, 
            	expose: {
            	closeOnClick: false,
            	color: '#a9a9a9'
            	},
                onLoad: function() {  
                $('#galImg').hide();
					reloadSubNav(0);
					reloadNav(0); 
            	},
    			closeOnClick:false
    });                           
});

function resizeImage(){
	var selector = '#galImg img'; 	//Sets the syntax for finding the images.
	
	$(selector).each(function(){
 		var maxWidth = 460; // Max width for the image
        var maxHeight = 360;    // Max height for the image
        var ratio = 0;  // Used for aspect ratio
        var width = $(this).width();    // Current image width
        var height = $(this).height();  // Current image height

        // Check if the current width is larger than the max
        if(width > maxWidth){
            ratio = maxWidth / width;   // get ratio for scaling image
            $(this).css("width", maxWidth); // Set new width
            $(this).css("height", height * ratio);  // Scale height based on ratio
            height = height * ratio;    // Reset height to match scaled image
            width = maxWidth;
        }
 
        // Check if current height is larger than max
        if(height > maxHeight){
            ratio = maxHeight / height; // get ratio for scaling image
            $(this).css("height", maxHeight);   // Set new height
            $(this).css("width", width * ratio);    // Scale width based on ratio
            width = width * ratio;    // Reset width to match scaled image
			$(this).css("margin-left", (maxWidth - width)/2);  //ADDED TO CENTER THE LARGE IMAGE
        }

	});
	
}

function resizeNavImages(){
	var selector = '#scrollBar img'; 	//Sets the syntax for finding the images
	
	$(selector).each(function(){
 		var maxWidth = 90; // Max width for the image
        var maxHeight = 90;    // Max height for the image
        var ratio = 0;  // Used for aspect ratio
        var width = $(this).width();    // Current image width
        var height = $(this).height();  // Current image height
		
 
        // Check if the current width is larger than the max
        if(width > maxWidth){
            ratio = maxWidth / width;   // get ratio for scaling image
            $(this).css("width", maxWidth); // Set new width
            $(this).css("height", height * ratio);  // Scale height based on ratio
            height = height * ratio;    // Reset height to match scaled image
            width = maxWidth;
			
        }
 
        // Check if current height is larger than max
        if(height > maxHeight){
            ratio = maxHeight / height; // get ratio for scaling image
            $(this).css("height", maxHeight);   // Set new height
            $(this).css("width", width * ratio);    // Scale width based on ratio
            width = width * ratio;    // Reset width to match scaled image
        }

	});
}

function swapImage(imgSrc){	
if($.browser.msie){
	$('#galImg').hide();
}
	$('#galImg').slideUp("normal",function(){
	var formatName = imgSrc.substring(imgSrc.lastIndexOf("/")+1).replace(/%20/g," ");
		document.getElementById('galImg').innerHTML = " <img id=mainImg src="+imgSrc+" /><span id=imgcomment>"+formatName.substring(0,formatName.lastIndexOf("."))+"</span>";
		$('img#mainImg').load(function(){$('#galImg').slideDown("normal");resizeImage();
			document.getElementById('imgcomment').style.left = $('#mainImg').css('margin-left');//document.getElementById('mainImg').offsetLeft;
			document.getElementById('imgcomment').style.textAlign = 'center';
			document.getElementById('imgcomment').style.width = (document.getElementById('mainImg').style.width);
			document.getElementById('imgcomment').style.top = $('#mainImg').css('height');//parseInt(document.getElementById('mainImg').style.height.replace("px",""));
		
		});
	});

}

function displayDebug(message){
	document.getElementById('debug').innerHTML = message;
}

function reloadNav(navId){
var navHTML = '<ul>';
var x;

for (x in jsFldAr){
	navHTML += '<li><a href="javascript:reloadNav('+x+');reloadSubNav('+x+');"'; 
	if(navId == x){
		navHTML += ' id=active';
	}
	navHTML += '>'+jsFldAr[x].substring(jsFldAr[x].lastIndexOf("/")+1)+'</a></li>';
}
navHTML +='<li><span id=debug></span></li></ul>';
document.getElementById('navLink').innerHTML = navHTML;
}

function reloadDefaultImg(navId){
	swapImage(jsImgAr[navId][0]);
}

function reloadSubNav(navId){
//show the preloader
document.getElementById('bottomNavMessage').innerHTML = '<img src=/global/images/overlay/loading.gif height=50px width=50px /><div id=message> Loading Image Thumbnails...</div></div>';
$('#bottomNavMessage').show();

$('#bottomNavWrapper').hide();
if($.browser.msie){
	$('#galImg').hide();
}
	$('#galImg').slideUp("normal",function(){
	var formatName = jsImgAr[navId][0].substring(jsImgAr[navId][0].lastIndexOf("/")+1).replace(/%20/g," ");
		document.getElementById('galImg').innerHTML = " <img id=mainImg name=mainImg src=http://www.deweys.com/"+jsImgAr[navId][0].replace('/httpdocs','')+" /><span id=imgcomment>"+formatName.substring(0,formatName.lastIndexOf("."))+"</span>";
		$('img#mainImg').load(function(){$('#galImg').slideDown("normal");
		resizeImage();
		//set the comment to the correct position
		document.getElementById('imgcomment').style.left = $('#mainImg').css('margin-left');//document.getElementById('mainImg').offsetLeft;
		document.getElementById('imgcomment').style.textAlign = 'center';
		document.getElementById('imgcomment').style.width = (document.getElementById('mainImg').style.width);
		document.getElementById('imgcomment').style.top = $('#mainImg').css('height');//parseInt(document.getElementById('mainImg').style.height.replace("px",""));
		//alert($('#mainImg').css('height'));
		
var subNavHTML = '<div id="scrollBar" >';
var i;
var y = 0;
for (x in jsImgAr[navId]){
	subNavHTML += '<div id='+x+' class=thumbnail >';
	subNavHTML += '<img id=subnavImg class='+x+' onClick=swapImage("http://www.deweys.com/'+jsImgAr[navId][x].replace('/httpdocs','')+'") src=http://www.deweys.com/'+jsImgAr[navId][x].replace('/httpdocs','')+' /></div>'
	i=x;
	y += 1;
	if(y == jsImgAr[navId].length){
	//	alert('last image ' + y);
		
			subNavHTML += '</div>';
			document.getElementById('scroller').innerHTML = subNavHTML;
			$('img#subnavImg').load(function(){resizeNavImages();});
		//	for(z in jsImgAr[navId]){
		//		$('img.'+z+'').load(function(){resizeNavImages();});
			//	if(z == jsImgAr[navId].length){
			//		$('img.'+i+'').load(function(){$('#bottomNavWrapper').show();resizeNavImages();$('#bottomNavMessage').hide();});
			//	}
		//	}
			//when the last image loads show the wrapper
			$('img.'+i+'').load(function(){$('#bottomNavWrapper').show();resizeNavImages();$('#bottomNavMessage').hide();});
	}
}

/*
		subNavHTML += '</div>';
		document.getElementById('scroller').innerHTML = subNavHTML;
		$('img#subnavImg').load(function(){resizeNavImages();});
		//when the last image loads show the wrapper
		$('img.'+i+'').load(function(){$('#bottomNavWrapper').show();resizeNavImages();$('#bottomNavMessage').hide();});
	
	*/
		});
	});
}
/*
function preloader(navId){
var i =0;
for (x in jsImgAr[navId]){
		imageObj = new Image();
	//for(i=0;i<jsImgAr[navId].length;i++){
		imageObj.src = jsImgAr[navId][x];
	//}
		$(imageObj).load(function(){myImages.push('loaded ');});
	
	//if(myImages.length >= jsImgAr[navId].length){
	//alert('all images have loaded!');
	//}
}
}
*/

///^^^^^^^^^^^ SCROLLER STUFF ^^^^^^^^^^^^^^^^^^^

$(document).ready(function(){
	//set intro fade and alpha here
	$('.scroller img').fadeTo("slow",0.6);
	//When an <a> is hovered it animates and returns back
	$('.scroller img').hover(function(){
		$(this).fadeTo('slow',1.0);
		},function(){
		$(this).fadeTo("slow",0.6);
	});

});


function scrollNext(){
$('#scrollBar').animate({marginLeft:"-=100"},200);
 	//$("ul#scroller").animate({marginLeft:"+=100"},200);
  };
  
function scrollPrev(){
    $("#scrollBar").animate({marginLeft:"+=100"},200);
  };

