﻿function skBox(){
	var boxopen = false;
	$('.photoGalleries #myThumbs').addClass('jstyle');
	var index = 0;

	$('.photoGalleries #myThumbs .link-item a').click(function(){
		//Mix skBox logic with createGroups to only do the gallery view for smaller groups of images
		var Item = ".link-item";
		if($(this).parent()[0].className.match("pg")){
			var ni = $(this).parent()[0].className.indexOf("pg");
			Item = "."+$(this).parent()[0].className.substring(ni);
			if(Item.match(" ")){
				Item =Item.substring(0,Item.indexOf(" "));
			}
		}
		
		if(boxopen == false){
			boxopen = true;
			var title = $(this).children("img")[0].alt;
			var src = this.href;
			var dn = " style='display:none'";
			
			//open Box window and Overlay immediately
			$('body').append("<div id='skB_overlay' "+dn+"></div><div class='skB_window'></div>");	
			
			$('#skB_overlay').fadeIn(300,function(){
				$('#myThumbs select').fadeOut(100);
				//PhotoGallery Contents
				$('body .skB_window').append("<div id='phtgWindow'></div>");
					$('#phtgWindow').append("<div class='closeBox' "+dn+"><a href='#'>Close</a></div>");
					$('#phtgWindow').append("<h1 "+dn+">Transitions Championship</h1>");
					$('#phtgWindow').append("<div class='contImg'><img "+dn+" src="+src+" title="+title+" alt="+title+" /></div>");
					$('#phtgWindow').append("<div class='slider' "+dn+"><ul></ul></div>");
					var gs= ".photoGalleries #myThumbs "+Item;
					 for(var i=0;i<$(gs).length;i++){
						 if($(gs+' a')[i].href == src)
						 { index = i;}
						 
						$('#phtgWindow .slider ul').append("<li><a href='"+$(gs+' a')[i].href+"' style='background-image:url("+$(gs+' img')[i].src+")' class='pbimgId_"+i+"'>"+$(gs+' img')[i].alt+" "+i+"</a></li>");
					 }
					$('#phtgWindow').append("<div class='sliderControls' "+dn+"><a href='#' class='prev'>prev</a><a href='#' class='next'>prev</a></div>");
				
				runPhotoGallery(index);
			});
		}
		return false;
	});
	
	function skB_showBox(){
		$('.skB_window, #phtgWindow h1').fadeIn(200,function(){
				//FadeIn Chain
				$('#phtgWindow img').fadeIn(350,function(){
					$('#phtgWindow .slider').fadeIn(350,function(){
						$('#phtgWindow .sliderControls').fadeIn(150,function(){
							$('#phtgWindow .closeBox').fadeIn(150,function(){
								//ensure all elements are visible.										   
								$('#skB_overlay').css({'background':'#000 none'});				
							});	
						});								
					});									
				});									
				
		});
		//Close Box
		$('.closeBox, #skB_overlay').click(function(){
			$('.skB_window').fadeOut(400, function(){
				$('.skB_window').remove();		
				$('#skB_overlay ').remove();
				$('#myThumbs select').fadeIn(100);
				boxopen = false;
			});	
			return false;
		});
	}
	
	function runPhotoGallery(inum)
	{
		var slideamount = 102 + 10; // width of element + left and right margins and padding
		var slideContainer = "#phtgWindow .slider ul";
		var index = 0;
		if(inum){index = inum-2;}
		var numShown = 5;
		var total = $(slideContainer+" li").length - numShown;
		
		skB_showBox();
		Slide();
		
		$('#phtgWindow .slider a').click(function(){
			index = this.className.substr(8);
			index -= 2;
			
			var lnk = this.href;
			$("#phtgWindow img").fadeOut(150,function(){
				imgPreloader = new Image();
				imgPreloader.src = lnk;
				imgPreloader.onload = function(){
					$("#phtgWindow img")[0].src=imgPreloader.src; 
					$("#phtgWindow img").fadeIn(600);
				}
			});
			Slide();
			return false;								  
		 });
		
		$(".sliderControls .prev").click(function(){ index-=5; Slide();return false; });
		$(".sliderControls .next").click(function(){ index+=5; Slide(); return false; });
		
		function Slide(){
			if(index<=0){	index = 0;	}
			if(index>total){  index = total;	}
			var one = slideamount*index;
			$(slideContainer).animate( { left:"-"+one+"px" },{ queue:false, duration:200 } );
		}
	}
}
