// JavaScript Document
function dm(user,domain) {
	locationstring = "mailto:" + user + "@" + domain;
	window.location = locationstring;
}

var featureIndex = 0;

$(document).ready(function() {
	// fancy zoom
	setupZoom();
	
	// ##############################################################################################################################
	// POSTER DETAIL PAGE 
	
	var $posterDetail		 = $("body#innerPosterPage");
	if ($posterDetail.exists()){
							 
		$("body#innerPosterPage .posterDetail .views a").click(function(){
				if ($(this).attr("rel") == "nozoom"){
				slidePosterView("imageViewer",$(this).html());
				$(".posterDetail .views a").removeClass("active");
				$(this).addClass("active");
				}
				return false;
		});
		
		// more than 2 non-zoom images present
		if ($(".imageViewer ul li").length > 2){
			
			// setup actions on next btn
			$(".quickPagination .next a").click(function(){
				var current = parseInt($("#imgIndex").val());
				var total = $(".imageViewer ul li").length - 1;

				next = current+1;
				if (next > total){ next = 1 };
	
				slidePosterView("imageViewer", next);
				current = next;
				$(".posterDetail .views a").removeClass("active");
				$("#view"+next).addClass("active");
				
				return false;
			});
			
			// setup actions on next btn
			$(".quickPagination .prev a").click(function(){
				var current = parseInt($("#imgIndex").val());
				var total = $(".imageViewer ul li").length - 1;

				prev = current-1;
				
				if (prev < 1){ prev = total };
	
				slidePosterView("imageViewer", prev);
				current = prev;
				$(".posterDetail .views a").removeClass("active");
				$("#view"+prev).addClass("active");
				
				return false;
			});
			
		}
		
	// ##############################################################################################################################
	// OTHER PAGES 
	
	} else {
		// make main images (click) advance
		// assuming there is more than one image
	
		
		if ($(".imageViewer ul li").length){
			
			// setup actions on next btn
			$(".quickPagination .next a").click(function(){
				var current = parseInt($("#imgIndex").val());
				var total = $(".imageViewer ul li").length - 2;

				next = current+1;

				if (next > total){ next = 0 };
				var seo = $(".imageThumbs ul li:eq("+next+") img").attr("alt");
				var title = $(".imageThumbs ul li:eq("+next+") a").attr("title");
				
				$(".quickPagination a").height($(".imageViewer").height()-15);
				
				slideGallery("imageViewer", next, seo, title, current);
				current = next;
				
				
				return false;
			});
			
			// setup actions on next btn
			$(".quickPagination .prev a").click(function(){
				var current = parseInt($("#imgIndex").val());
				var total = $(".imageViewer ul li").length - 2;

				prev = current-1;

				if (prev < 0){ prev = total };
				var seo = $(".imageThumbs ul li:eq("+prev+") img").attr("alt");
				var title = $(".imageThumbs ul li:eq("+prev+") a").attr("title");
				
				$(".quickPagination a").height($(".imageViewer").height()-15);
				
				slideGallery("imageViewer", prev, seo, title, current);
				current = prev;
				
				return false;
			});
					
		}
		/* if ($(".imageViewer ul").children().size() > 2){
			$(".imageViewer ul li img").click(function(){
				var $li = $(this).parents('li');
				var $ul = $(this).parents('ul');
				var num = $ul.children().index($li) + 1;
				var totalChildren = $ul.children().size() - 1;
				if (num >= totalChildren){
					num = 0;
				}
				var seo = $(".imageThumbs ul li:eq("+num+") img").attr("alt");
				var title = $(".imageThumbs ul li:eq("+num+") a").attr("title");
				
				slideGallery("imageViewer", num, seo, title);
																						 
				return false;
				
			});
		}*/
		
		// ---------------------------------------------------
		// make thumbs slide the top view
		$(".imageThumbs ul li a").click(function(){ 
			var $li = $(this).parents('li');
			var $ul = $(this).parents('ul');
			var num = $ul.children().index($li);
			var seo = $(this).children("img").attr("alt");
			var title = $(this).attr("title");
			
			slideGallery("imageViewer", num, seo, title, num);
																					 
			return false;
		});
		
		
		// ---------------------------------------------------
		// setup pagination buttons
		$(".thumbPagination ul li a").click(function(){ 
			var $li = $(this).parents('li');
			var $ul = $(this).parents('ul');
			var num = $ul.children().index($li);
			if ($("#postersPage").length > 0){
				slideThumbs("posterThumbs", num);		
			} else {
				slideThumbs("imageThumbs", num);
			}
			$(".thumbPagination ul li a").removeClass("active");
			$(this).addClass("active");
			
			
			
			return false;
		});
		
		// homepage?
		if ($("#featuredWorksNav").exists()){
			$("#featuredWorksNav ul li a").click(function(){
				var $li = $(this).parents('li');
				var $ul = $(this).parents('ul');
				var num = $ul.children().index($li);
				slideFeature(num);		
				clearInterval(featureTimer);
				return false;
			});
			
			// start a timer
			
			var featureTimer = setInterval("autoSlideFeature()",10000);
			
		}
			
		// ---------------------------------------------------
		// setup gallery hovers for desc
		$(".imageViewer ul li .desc").bind("mouseenter",
			function(){
				var targ =  $(this);
				var ydest = (targ.height() * -1);
			
				$(targ).animate({ 
					marginTop: ydest+"px"
				}, 300 );
				
				var pageHeight = $(".imageViewer").height();
				var newHeight = parseInt(pageHeight - targ.height());
				
				$(".quickPagination a").height(newHeight);
				
			}
			
		);
		// setup gallery hovers for desc
		$(".imageViewer ul li .desc .close a").click(
			function(){
				var targ =  $(this).parents('.desc');
				var ydest = -18;
			
				$(targ).animate({ 
					marginTop: ydest+"px"
				}, 300 );
				
				var newHeight = $(".imageViewer").height() - 15;
				
				$(".quickPagination a").height(newHeight);
				
			//	$(".quickPagination").show();
				
				return false;	
			}
		);
		/*$(".imageViewer ul li .desc").bind("mouseleave",
			function(){
				var targ =  $(this);
				var ydest = -18;
			
				$(targ).animate({ 
					marginTop: ydest+"px"
				}, 300 );
				
				$(".quickPagination").show();
			}
		);*/
		
		
	}
	
	// ##############################################################################################################################
	// ALL PAGES 

	// slider page
	// make image viewer super wide (# of images x mask width)
	$(".imageViewer ul").css("width",$(".imageViewer ul li").length * $(".imageViewer").width());
	
	
	var posterSort = $("#posterDD");
	if ($(posterSort).exists()){	
		$(posterSort).change(function(){
			var dest = $(this).val();
			if (dest != "#"){
				window.location = dest;
			}
		});
	}
	
	
	
	if ($("body#postersPage").length > 0) {
		
		$("body#postersPage .thumbPagination ul li a").click(function(){
			var seo = $(this).text();
			SWFAddress.setValue(seo);							 
		});
		
		
		if (SWFAddress.getValue() != "/"){
			
			var targ = SWFAddress.getValue().substr(1) - 1;
			slideThumbs("posterThumbs", targ);
			$(".thumbPagination ul li a").removeClass("active");
			$(".thumbPagination ul li:eq("+targ+") a").addClass("active");
			
		}
		
	} else if ($("body#storePage").length > 0) {
				
	} else {
		
		// see if we should load a section
		if (SWFAddress.getValue() != "/"){

			loadGallery(SWFAddress.getValue().substr(1));
		}
	}
	//*/

});

SWFAddress.onChange = function(){
	
	if (SWFAddress.getValue() != "/"){
		loadGallery(SWFAddress.getValue().substr(1));
	}
	
}

function loadGallery(seo){
	var $targ = $(".imageThumbs ul li");
	if ($targ.length > 0)
	{
		// make as if the thumb were clicked
		$(".imageThumbs ul li a img[alt="+seo+"]").click();
	}
}

function slideGallery(targ, dest, seo, title, current){
	var width = $("."+targ+"Mask").width();
	var xdest = (width * dest) * -1;
	
	
	$("."+targ+" ul").animate({ 
        marginLeft: xdest+"px"
      }, 300, "linear");
	
	// swfaddress
	SWFAddress.setValue(seo);
	SWFAddress.setTitle("Boss Construction - " + title);
	
	$("#imgIndex").val(dest);
	
	$(".imageViewer ul li").eq(current).find(".desc").animate({ 
					marginTop: "-18px"
				}, 100 );
	
					
	return false;
}
function slideThumbs(targ, dest){
	var height = $("."+targ+"Mask").height();
	

	if ($("#postersPage").length > 0){
		var extra = dest * 10;
		var ydest = ((height * dest) + extra) * -1;
	
		$("."+targ+"").animate({ 
			marginTop: ydest+"px"
		  }, 300 );
	} else {
		var extra = dest * 8;
		var ydest = ((height * dest) + extra) * -1;
	
		$("."+targ+" ul").animate({ 
			marginTop: ydest+"px"
		  }, 300 );
	}
	
}
function slideFeature(dest){
	var width = 215;//$("#featuredWorksMask img").width();
	var extra = 0;
	var newdest = ((width * dest) + extra) * -1;
	
	$("#featuredWorksMask ul").animate({ 
			marginLeft: newdest+"px"
		  }, 300 );
	
	$("#featuredWorksNav ul li a").removeClass("active");
	$("#featuredWorksNav ul li:eq("+dest+") a").addClass("active");
	
}
function autoSlideFeature(dest){
	featureIndex = featureIndex + 1;
	if (featureIndex > $("#featuredWorksNav ul li").children().size()-1){
		featureIndex = 0;
	}
	slideFeature(featureIndex);	
}

function slidePosterView(targ,dest){
	var width = $("."+targ+"Mask").width();
	var xdest = (width * (dest-1)) * -1;
	
	$("."+targ+" ul").animate({ 
        marginLeft: xdest+"px"
      }, 200, "swing");
	
	$("#imgIndex").val(dest);
	
	return false;
}



	
/* jQUERY */
/*	******************************
		PLUGIN - Utility Functions 
		Author: Jack Lukic - KNI (all plugins)
		Notes: Used to extend jQuery functionality and shorten code
	******************************	*/

jQuery.fn.extend({
	// test if el exists
	exists: function() {
		if(this.size() > 0) {
			return true;
		}
		else {
			return false;	
		}
	}
});
