// for tomorrow:
// add menu timer for navigation menu

var menuTimer;
var tabIndex;
var browserIE6 = false;
var zoomLevel = 1.5;
var currentZoom = 1;
var galleryTimer;

function setMenuTimer()
{
   clearTimeout(menuTimer);
   menuTimer = setTimeout("hideNav()",500);
}

function displayBox()
{
   $(".favouritePopUp .favPopContent img").attr("src",imgSrc);
   $(".favouritePopUp .favPopContent img").attr("height",imgHeight);
   $(".favouritePopUp .favPopContent img").attr("width",imgWidth);
   // calculate height of the middle box
   imgWidth = imgWidth + 32; // allow for padding on image
   imgHeight = imgHeight + 32; // allow for padding on image
   midHeight = imgHeight + 90;
   midWidth = imgWidth + 60;
   // claculate dimensions of popup
   $(".favouritePopUp .favPopML").height(midHeight);
   $(".favouritePopUp .favPopMR").height(midHeight);
   $(".favouritePopUp .favPopContent").height(midHeight);
   $(".favouritePopUp .favPopContent").width(imgWidth);
   $(".favouritePopUp .favPopContent .infoBox").width(imgWidth);
   $(".favouritePopUp .imgPane").height(imgHeight);
   $(".favouritePopUp .imgPane").width(imgWidth);
   $(".favouritePopUp").width(midWidth + 3); // crappy little ie6 hack +3
   $(".favouritePopUp .favPopTop").width(midWidth);
   $(".favouritePopUp .favPopBot").width(midWidth);
   $(".favouritePopUp .favPopContent img").css("padding","16px");
   // calculate absolute position of the box
   boxWidth = $(".favouritePopUp").width();
   boxHeight = $(".favouritePopUp").height();
   pageWidth = 920;
   var dimensions = findScreenPos(); 
   leftPos = ((dimensions.screenWidth - boxWidth) / 2) + dimensions.scrollWidth;
   topPos = ((dimensions.screenHeight - boxHeight) / 2) + dimensions.scrollHeight;
   $(".favouritePopUp").css("left",leftPos);
   $(".favouritePopUp").css("top",topPos);
   //console.log(leftPos);
   
   $(".favouritePopUp").fadeIn("slow");
}

function getItemInfo(popupImg)
{
	var productDesc = $(popupImg).parent().parent().siblings(".productDesc").text();
	var productPrice = $(popupImg).parent().parent().siblings("h5").text();
	$(".infoBox h3").text(productDesc);
	$(".infoBox p").text(productPrice);
}

function addNavigation()
{
	// dirty hack until Tim can change the backend
	$(".navCareers").addClass("navPromotion");
	$(".navCareers").append('<ul class="pngbg"><li class="pngbg"><a href="" class="pngbg">TV Ad Viewer</a></li><li class="pngbg"><a href="" class="pngbg">Catalouge</a></li></ul>')
	

	$(".nav li > a").hover(function(){
		$(this).addClass("active");
	},
	function()
	{
		$(this).removeClass("active");
	});
   $(".nav li a").addClass("pngbg");
   $(".nav li ul").addClass("pngbg");
   $(".nav li ul li").addClass("pngbg");
   $(".nav > ul > li").css("marginLeft", "10px");
   if (browserIE6 === true)
   {
      $(".nav > ul > li").css("marginLeft", "10px");
   }

   $(".nav > ul > li").mouseover(function(){
      $(this).siblings("li").children("a").removeClass("selected");
      if ($(this).children("ul").length > 0)
      {
          $(this).children("a").addClass("selected");
      }
      $(this).siblings("li").children("ul").css("display","none");
      $(this).children("ul").css("display","block");
      clearTimeout(menuTimer);
      tabIndex = $(this).parent("ul").children("li").index(this);
      if (browserIE6 === false)
      {
         $(this).css("z-index","10");
      }
   });

   $(".nav li").mouseout(function(){
      setMenuTimer();
      // remove z-index on tab
      if (browserIE6 === false)
      {
         $(this).css("z-index",tabIndex);
      }
   });

   // crazy IE6 hack WOOT!!!!
   $(".nav > ul > li.active > a").addClass("active");
}

function hideNav()
{
   // hide navigation
   $(".nav > ul > li > ul").css("display","none");
   $(".nav > ul > li > a").removeClass("selected");
}

function closePopUps()
{
   $(".favouritePopUp").fadeOut("slow");
   if($.browser.msie)
   {
      $(".favouritePopUp .favPopContent img").css("padding","0px");
   }
   $(".sendForm").fadeOut("slow");
}

function addImagePopUp()
{
   // open popup
   $(".popup").click(function(){
	   // add event handling
	   var newPopup = $(".favouritePopUp").clone();
   	   $(".favouritePopUp").remove();
   	   newPopup.appendTo("body");
	   addZoomFeature();
	   // close popup
   	   $(".favPopContent .popClose").click(function()
   	   {
      	   closePopUps();
      	   return false;
   	   });
	   //Press Escape event! - note: not working on IE6, maybe won't include it
   	   $(document).keyup(function(e)
   	   {
      	   if(e.keyCode==27)
      	   {
          		closePopUps();
      	   }
   	   });

       imgSrc = $(this).attr("name");
       var popUpImg = new Image();
       popUpImg.onload = function()
       {
          imgHeight = popUpImg.height;
          imgWidth = popUpImg.width;
          displayBox();

       };
       popUpImg.src = imgSrc;
       getItemInfo(this);
   });
}

function addEmailPopUp() {
	$(".emailBut").click(function(){
   	        $("#aspnetForm > .sendForm").fadeIn("slow");
      		if (browserIE6 == true)
      		{
      		    displayTimer = setTimeout('$("#aspnetForm > .sendForm").addClass("pngbg")', 5);
			}
      		return false;
   	});
   	$("#aspnetForm > .sendForm .popClose").click(function() {
   	$("#aspnetForm > .sendForm").fadeOut("slow");
      		return false;
   	});

}

function addProductTabs()
{
   $(".productTabsNav li").click(function(){
      // get index of click function
      tabIndex = $(this).parent().children("li").index(this) + 1;
      $(this).siblings("li").removeClass("active");
      $(this).addClass("active");
      activeTab = ".productTabs > div:eq(" + tabIndex + ")";
      $(activeTab).siblings("div").css("display","none");
      $(activeTab).css("display","block");
      return false;
   });
}

function alignImages()
{
   // dirty hack for ie 6 - stupid browser !!!
   $(".imagePane").append("<span style='height:95px;display:inline-block;width:1px;color:#fff'>.</span>");
   $(".imagePane").prepend("<span style='height:95px;display:inline-block;width:1px;color:#fff'>.</span>");
}

function findScreenPos()
{
		var dimensions = {};
		if (typeof(window.innerHeight) == 'number')
   		{
   	  	    dimensions.screenHeight = window.innerHeight;
   	  	    dimensions.screenWidth = window.innerWidth;
   	  	    dimensions.scrollHeight = window.pageYOffset;
   	  	    dimensions.scrollWidth = window.pageXOffset;
  		}
   		else
   		{
   	  	 	if (document.documentElement && document.documentElement.clientHeight)
	  		{
 	   	 	    dimensions.screenHeight = document.documentElement.clientHeight;
 	   	 		dimensions.screenWidth = document.documentElement.clientWidth;
 	   	 		dimensions.scrollHeight = document.documentElement.scrollTop;
 	   	 		dimensions.scrollWidth = document.documentElement.scrollLeft;
	  		}
 	  		else
	  		{
 	   	  	 	if (document.body && document.body.clientHeight)
	  	  		{
 	   	   	 	   	dimensions.screenHeight = document.body.clientHeight;
 	   	 	 		dimensions.screenWidth = document.body.clientWidth;
 	   	 	 		dimensions.scrollHeight = document.body.scrollTop;
 	   	 	 		dimensions.scrollWidth = document.body.scrollLeft;
 	  	  		}
 	  		}
   		}
   		return dimensions;
}

function addZoomFeature()
{
   $(".zoomIn").click(function(){
   		$(".zoomOut").css("visibility","visible");
   		if (currentZoom > 1)
		{
			  return false;
		}
		else
		{
		      zoomImage = $(this).parent().parent().parent().siblings(".imgPane").children("img");
		      zoomHeight = zoomImage.attr("height");
		      zoomWidth = zoomImage.attr("width");
		      imgHeight = zoomHeight * zoomLevel;
		      imgWidth = zoomWidth * zoomLevel;
		      displayBox();
		      currentZoom ++;
		      if (currentZoom > 1)
		      {
			  	  $(".zoomIn").css("visibility","hidden");
			  }
		      return false;
  	  	}
   });

   $(".zoomOut").click(function(){
   		$(".zoomIn").css("visibility","visible");
 	 	if (currentZoom < 1)
		{
			  return false;
		}
		else
		{
		      zoomImage = $(this).parent().parent().parent().siblings(".imgPane").children("img");
		      zoomHeight = zoomImage.attr("height");
		      zoomWidth = zoomImage.attr("width");
		      imgHeight = zoomHeight / zoomLevel;
		      imgWidth = zoomWidth / zoomLevel;
		      displayBox();
		      currentZoom --;
		      if (currentZoom < 1)
		      {
			  	  $(".zoomOut").css("visibility","hidden");
			  }
		      return false;
  	  	}
   });
}

function addDesc()
{
   $(".hoverGallery img").mouseover(function(){
   	  clearTimeout(galleryTimer);
      $(this).siblings(".itemDesc").css("display","block");
      $(this).parent().parent().siblings().children("a").children(".itemDesc").css("display","none");
   });
   $(".hoverGallery img").mouseout(function(){
   	  galleryTimer = setTimeout("hideDesc()",200);
   });
   $(".itemDesc").mouseover(function(){
   	  clearTimeout(galleryTimer);
   });
   $(".itemDesc").mouseout(function(){
   	  galleryTimer = setTimeout("hideDesc()",200);
   });
}

function hideDesc()
{
	$(".itemDesc").css("display","none");
}

function addExpando()
{
	$(".expandMe").toggle(function()
	{
		$(this).parent().next().slideDown("slow");
	},
	function()
	{
		$(this).parent().next().slideUp("slow");
	});
}

function addTransparency()
{
	$(".colourItem img").addClass("pngbg");
	$(".productGallery img").addClass("pngbg");
	$(".collectionsImage a img").addClass("pngbg");
}

function addFlashVideo() {
    $(".videoLink").click(function() {
        showFlash($(this).attr("name"), $(this).text());
        return false;
    });

    var ul = $("#videoLinkList").children();

    if (ul.length > 0)
    {
        showFlash(ul.children("a:eq(0)").attr("name"), ul.children("a:eq(0)").text());
    }
}

// add to plush......
function showFlash(url, title)
{
    var so = new SWFObject("/Resources/Plush/Flash/plush_video_player.swf", "flashMovie", "512", "440", "9", "#FFFFFF");
    so.addVariable("id", "flashMovie");
    so.addParam("scale", "noscale");
    so.addVariable("title", title);
    so.addVariable("videoURL", url);
    so.write("videoPlayer");   
}

function dirtyHacks()
{
	// I feel bad doing this :(  lol!!! sorry to anyone who has to maintain this site
   $(".masthead .searchBut").attr("value","");  
   $(".collectionsNav").next(".collectionsPageGallery").children(".collectionsImage").css("height","126px");
   $(".collectionsNav").next(".collectionsPageGallery").children(".collectionsImage").find(".itemDesc").removeClass("large");
   $(".collectionsHomeList").find(".itemDesc").removeClass("large");
   
	$(".boutique .collectionsImage:last").css("margin-right","0px");
	if ($(".extras").length > 0)
	{
		$(".pageContent").css("background","#ffffff");
	}
}

$(document).ready(function()
{
   if($.browser.msie)
      {
         if($.browser.version == "6.0")
         {
         browserIE6 = true;
         addTransparency();
         $(".productTabs .favourites").prepend("<div style='height:20px'></div>");
         }
      }
   addExpando();
   addNavigation();
   addImagePopUp();
   addEmailPopUp();
   addProductTabs();
   alignImages();
   addDesc();
   addFlashVideo();
   // make pop up window draggable
   //$(".favouritePopUp").draggable();
   dirtyHacks();
});
