/*
Image Gallery Box - created by S.Moye.
*/	

function imgBox()
{
	var imgY = 394;
	var imgX = 700; 
	var maxY = 600;
	var maxX = 700;
	var boxContents,gi,imgLnk;// = "";
	var gallery = ".imgB_Gallery";
	
	$('.imgBox').click(function(){				
		openBox(this.href);
		return false;
	});
	
	function openBox(fsrc)
	{	
		/*if(fsrc.indexOf("i=")>-1){
			gi = fsrc.substring(fsrc.indexOf("i="));
			gi = parseInt( gi.substring(2) ,10);
		}else{ gi = 1; }*/
		imgLnk = fsrc;
		
		boxContents = "<div class='imgB_lv1'>";
			boxContents += "<div class='imgB_lv2' style='width:"+imgX+"px;height:"+imgY+"px;left:-"+Math.round((imgX/2)+15)+"px;top:-"+Math.round(imgY/2 - 80)+"px;'>";
			boxContents += "<a href='#close' class='imgB_closeButton hide'><span>Close</span></a>";
				boxContents += "<div id='imgB_lv3' style='width:"+imgX+"px;height:"+imgY+"px;background:url() center no-repeat;'>";
				/*boxContents += "<a href='#' class='prevBtn'></a>";	
				boxContents += "<a href='#' class='nextBtn'></a>";*/
				boxContents += "<img src='/media/images/news/spacer.gif' alt='' />";
				boxContents += "</div>";
			boxContents += "<img src='/media/images/news/mediaAssetsLightShadow.png' alt='' class='bg' />";
			boxContents += "</div>";
		boxContents += "</div><div id='imgB_overlay'></div>";	
		
		$('body').append(boxContents);
		switchImg(fsrc);
		showContent();

		/*$('#imgB_lv3 a.prevBtn').click(function(){
			if(gi>0){gi--;}
			switchImg(gi);
			return false;
		});
		$('#imgB_lv3 a.nextBtn').click(function(){
			if(gi<$(gallery+" li img").length-1){gi++;}
			switchImg(gi);
			return false;
		});
		
		$('#imgB_lv3 a.prevBtn').bind('mouseenter',function(){
			if(gi>0){$(this).animate({opacity:'.5'},400);}	
		});
		$('#imgB_lv3 a.nextBtn').bind('mouseenter',function(){
			if(gi<$(gallery+" li img").length-1){$(this).animate({opacity:'.5'},400);}
		});
		$('#imgB_lv3 a.nextBtn, #imgB_lv3 a.prevBtn').bind('mouseleave',function(){
			$(this).animate({opacity:'0'},400);
		});*/
		
		$('.imgB_closeButton, #imgB_overlay').click(function(){
			$('.imgB_lv1').remove();
			$('#imgB_overlay').fadeOut(400, function(){
				$('#imgB_overlay').remove();
			});
			return false;				 
		});
	}
	
	/*function getFromString(val,str){
		if(fsrc.indexOf(val+"=")>-1){
			gi = fsrc.substring(fsrc.indexOf(val+"="));
			if(fsrc.indexOf(val+"=")>-1)
			gi = parseInt( gi.substring(val.length+1) ,10);
			
		}	
	}*/
	
	function showContent(){
		$("a:eq(0)")[0].focus(); 
		$('.imgB_lv1').fadeIn(600,function(){});
	}
	
	function switchImg(gi){
		//$('#imgB_lv3 a.nextBtn, #imgB_lv3 a.prevBtn').animate({opacity:'0'},400);
		$(".imgB_lv2 #imgB_lv3").css({background:'url(/media/images/ajax-loader.gif) center no-repeat'});
		imgLnk = gi;
		var imgPreloader = new Image();
		imgPreloader.src = gi;	
		
		imgPreloader.onload = function(){
			
			var finalW = imgPreloader.width;
			var finalH = imgPreloader.height;
			
			if(finalW > finalH){
				if(finalW > maxX){ 
					finalW = maxX;	
					finalH = (maxX/imgPreloader.width) * finalH;
				}
			}else{
				if(finalH > maxY){ 
					finalH = maxY;
					finalW = (maxY/imgPreloader.height) * finalW;
				}
			}
			
			var containerH = finalH + 68;
			var containerW = finalW + 64;
			
			//$(".imgB_lv2").animate({left:"-"+Math.round((containerW/2)+25)+"px",top:(Math.round(containerH/2 - 80)*-1)+"px",height: finalH+"px",width: finalW+"px"},400);
			$(".imgB_lv2").css({left:"-"+Math.round((containerW/2)+25)+"px",top:(Math.round(containerH/2 - 80)*-1)+"px",height: finalH+"px",width: finalW+"px"});
			
			//$(".imgB_lv2 .bg").animate({height: finalH+"px",width: finalW+"px"},400);
			$("#imgB_lv3").css({height:finalH+"px", width:finalW+"px"});
			//$("#imgB_lv3").animate({height:finalH+"px", width:finalW+"px"}, 200,function(){
			$(".imgB_lv2 #imgB_lv3 img")[0].src = imgLnk;	
			$(".imgB_lv2 .imgB_closeButton").css({"display":"block","height":"0px"});
			$(".imgB_lv2 .imgB_closeButton").animate({'height':'41px','top':'-'+Math.ceil(26+(2*((maxY-finalH)/140)) )+'px'},200);
			//});
			
		};
		
		
		//$('#imgB_lv3 a.prevBtn').css({'height':imgPreloader.height+"px"});
		//$('#imgB_lv3 a.nextBtn').css({'height':imgPreloader.height+"px"});
	}
}
