/*
Art Gallery Box - created by S.Moye.

This code is explicitly open source. 
Use and abuse at your leisure, just don't blame me for your own mistakes if you do use it.
*/	

function showBox()
{
	var imgY = 450;
	var imgX = 700; 
	var boxContents,gi,imgLnk;
	var gallery = ".imgB_Gallery";
	
	$('.imgBox').click(function(){		
		openBox(this);
		return false;
	});
	
	function openBox(f)
	{	
		var fsrc = f.href;
		if(fsrc.indexOf("i=")>-1){
			gi = fsrc.substring(fsrc.indexOf("i="));
			gi = parseInt( gi.substring(2) ,10);
		}else{ gi = 1; }		
		//Check containing span class for file type: pictures, animations, games
		var AT = $(f).children('span')[0].className;	
		
		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 += "<div id='imgB_lv3' style='width:"+imgX+"px;height:"+imgY+"px;'>";
					boxContents += "<div class='lv3feature' style='width:"+imgX+"px;height:"+imgY+"px;background:url() center no-repeat;'></div>";	
					boxContents += "<div class='caption'>";
						boxContents += "<span class='text'></span>";
						boxContents += "<a href='#' class='prevBtn'></a>";	
						boxContents += "<a href='#' class='nextBtn'></a>";
					boxContents += "</div>";
				boxContents += "</div>";
				boxContents += "<a href='#touch' class='interactiveTab'>iTab</a>";
				boxContents += "<a href='#close' class='imgB_closeButton'>Close</a>";
		boxContents += "</div><div id='imgB_overlay'>&nbsp;</div>";	
		
		$('body').append(boxContents);
		switchImg(gi,AT);
		showContent(AT);

		$('#imgB_lv3 a.prevBtn').click(function(){
			if(gi>0){gi--;}
			var AT = $(gallery+" li:eq("+gi+") span:eq(0)")[0].className;
			switchImg(gi,AT);
			return false;
		});
		$('#imgB_lv3 a.nextBtn').click(function(){
			if(gi<$(gallery+" li").length-1){gi++;}
			var AT = $(gallery+" li:eq("+gi+") span:eq(0)")[0].className;
			switchImg(gi,AT);
			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").length-1){$(this).animate({opacity:'.5'},400);}
		});
		$('#imgB_lv3 a.nextBtn, #imgB_lv3 a.prevBtn').bind('mouseleave',function(){
			if(gi>0){
			$('#imgB_lv3 a.prevBtn').animate({opacity:'1'},400);
			}
			if(gi<$(gallery+" li").length-1){
				$('#imgB_lv3 a.nextBtn').animate({opacity:'1'},400);
			}	
		});*/
		
		
		$('.imgB_closeButton, #imgB_overlay').click(function(){
			$('.imgB_lv1').remove();
			$('#imgB_overlay').fadeOut(400, function(){
				$('#imgB_overlay').remove();
			});
			return false;				 
		});
	}
	
	function showContent(AT){
		$("a:eq(0)")[0].focus(); 
		$('.imgB_lv1').fadeIn(600,function(){
			$(".imgB_closeButton").css({'right':'0px','display':'block'});
			$(".imgB_closeButton").animate({'right':'-84px'},300);	
			switch (AT){
				case 'games':
					$(".interactiveTab").css({'right':'0px','display':'block'});
					$(".interactiveTab").animate({'right':'-96px'},300);	
					break;
				default: break;
			}
		});
	}
	
	function switchImg(gi,AT){
		var hgt = 400;
		var wdt = 550;
		imgLnk = $(gallery+" li a.imgBox")[gi].href;
		var imgPreloader = new Image();
		imgPreloader.src = imgLnk;
		
		$("#imgB_lv3 .caption .text").html("");
		var textContents =$(gallery+" li:eq("+gi+") .innerText")[0].innerHTML;
		$("#imgB_lv3 .caption .text").html(textContents);
		
		$('#imgB_lv3 a.nextBtn, #imgB_lv3 a.prevBtn').animate({opacity:'0'},400);
		$(".imgB_lv2 .lv3feature").css({background:'url('+sDir+'/images/ajax-loader.gif) center no-repeat'});
		$(".imgB_lv2 .lv3feature object").remove();
		
		switch (AT){
			case 'animations':
				addFlash(400,550,imgLnk);
				break;
			case 'games':
				addFlash(400,550,imgLnk);
				break;
			case 'video':
				addFlash(400,550,imgLnk);
				break;
			case 'music':
				addFlash(400,550,imgLnk);
				break;
			default: //pictures
				hgt = imgPreloader.height|| 400;
				wdt = imgPreloader.width || 550;
				$(".imgB_lv2 #imgB_lv3 .lv3feature").css({background:'url('+imgLnk+') center center no-repeat'});
				imgPreloader.onload = function(){
					hgt = imgPreloader.height;
					wdt = imgPreloader.width;
					
					reSize(hgt,wdt);
				}
				
				break;
		}
		
		reSize(hgt,wdt);
		
		if(gi>0){
			$('#imgB_lv3 a.prevBtn').animate({opacity:'1'},400);
		}if(gi<$(gallery+" li").length-1){
			$('#imgB_lv3 a.nextBtn').animate({opacity:'1'},400);
		}

	}
	
	function addFlash(hgt,wdt,imgLnk){
			var swfCts = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+wdt+'" height="'+hgt+'" id="main" align="middle">';
			swfCts += '<param name="allowScriptAccess" value="sameDomain" />';
			swfCts += '<param name="movie" value="'+sDir+'/flash/flashPlayer.swf?swf='+imgLnk+'" />';
			swfCts += '<param name="quality" value="high" />';
			//swfCts += '<param name="bgcolor" value="#000000" />';
			swfCts += '<param name="wmode" value="opaque" />';
			swfCts += '<embed src="'+sDir+'/flash/flashPlayer.swf?swf='+imgLnk+'"wmode="opaque" quality="high" width="'+wdt+'" height="'+hgt+'" name="main" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
			$(".lv3feature").css({height:hgt+"px", width:wdt+"px"});
			$(".imgB_lv2 #imgB_lv3 .lv3feature").append(swfCts);
	}
	
	function reSize(hgt,wdt){
		if(wdt<500){	wdt=500;	}
		//$(".imgB_lv2").animate({left:"-"+Math.round((wdt/2)+15)+"px",top:(Math.round((hgt+66)/2 - 80)*-1)+"px",height:(hgt+66)+"px",width: wdt+"px"},400);
		$(".imgB_lv2").css({left:"-"+Math.round((wdt/2)+15)+"px",top:(Math.round((hgt+66)/2 - 80)*-1)+"px",height:(hgt+66)+"px",width: wdt+"px"});
		$(".lv3feature").css({height:hgt+"px", width:wdt+"px"});
		//$(".lv3feature").animate({height:hgt+"px", width:wdt+"px"}, 400 );
		
		//$(".imgB_lv2 #imgB_lv3 .caption").animate({width:(wdt-200)+"px"}, 400 );
		//$("#imgB_lv3").animate({height:(hgt+66)+"px", width:wdt+"px"}, 400 );
		$(".imgB_lv2 #imgB_lv3 .caption").css({width:(wdt-200)+"px"});
		$("#imgB_lv3").css({height:(hgt+66)+"px", width:wdt+"px"});
	}
}
