// JavaScript Document


function valPX(pixels){
	// removes px from string
	return pixels.substr(0,pixels.length-2);
}


function in_array(what, where){
	// Modified from http://snipplr.com/view/4584/inarray/ to provide -1 on false and array key # otherwise
  var a=-1;
  for(var i=0;i<where.length;i++){
    if(what == where[i]){
     a=i;
     break;
	   }
  }
  return a;
}





  function buildZMax() {
	// Get maz z-index as per site http://commonmanrants.blogspot.com/2008/11/fancybox-jquery-and-z-index.html
	// Modified to return value - Mark White CMD 2009
  var zmax = 0 ;

		$('*').each(function() {
      var cur = parseInt($(this).css('zIndex'));
      zmax = cur > zmax ? $(this).css('zIndex') : zmax;
    });
		
		return zmax;
  }



$(function(){
	// Initial onload calling of activity;


	var t; 	
	// Client Area
	$("#clientLoginButton").click(function(){
																			 
			var clientAreaPos=valPX($("#clientLogin").css("top"));
			//alert(clientAreaPos);
      if (clientAreaPos<0) {
				clearTimeout(t); 
        $("#clientLogin").animate({"top":"0px"},1500);
				$("#clientLoginButton").animate({"width" : "55px"},1500).text("Hide");
      } else {
				clearTimeout(t); 
        $("#clientLogin").animate({"top":"-300px"},1500);
				$("#clientLoginButton").animate({"width" : "98px"},1500);

				t = setTimeout(function() {$("#clientLoginButton").text("Client Login")}, 1500);
      }

	});

	// Initial Screen Positioning
	var pageWidthCurrent=$(document).width();
	$("#Menu").html($("#Menu .container").contents()); // Put menu buttons in for interactivity (takes them out of container)
	var newpos=((pageWidthCurrent-980)/2)+"px";

	$("#PortfolioButton").css({"left" : newpos}); // Move Buttons to correct placement;
	
	$(".Button").click(function(){return false;});
	
	
	// Portfolio functions
	
	$("#previews ul li a").click(function(){


		// Setup Variables
		var thisPortfolioID=$(this).attr("id");
		var urlAddr="/portfolio/getPortfolio/"+thisPortfolioID.substr(6,thisPortfolioID.length);
		var zmax=buildZMax();
		var currentTopPos=($(document).scrollTop());
		var bodyHeight=$(document).height();
		var bodyWidth=$(document).width();
		var windowHeight=$(window).height();
		var windowWidth=$(window).width();
		var PortfolioBodyWidth=868;
		var PortfolioBodyHeight=643;
		var imageWidth=128;
		var imageHeight=15;
		var centreScreenWidth=((windowWidth-PortfolioBodyWidth)/2)+"px";
		var centreScreenHeight=(((windowHeight-PortfolioBodyHeight)/2)+currentTopPos)+"px";		
		var centreImageX=((windowWidth-imageWidth)/2)+"px";
		var centreImageY=(((windowHeight-imageHeight)/2)+currentTopPos)+"px";				
//		alert(windowHeight+ " / 2  - "+ PortfolioBodyHeight+" = "+centreScreenHeight);
		var blackspace='<div id="blackspace"></div>';
		var imageLoad='<img id="imagePreLoader" src="/images/ajax-loader.gif" />';
		var blackspaceCss= {background : "#000000",
							"opacity" : 0.5, 
							width : bodyWidth+"px", 
							height: bodyHeight+"px", 
							"position" : "absolute", 
							"top" : "0px", 
							"left" : "0px", 
							"z-index" : zmax
		};
		var imageCss= {		"position" : "absolute", 
							"top" : centreImageY, 
							"left" : centreImageX, 
							"z-index" : zmax+1
		};		
		var portfolioBody='<div id="portfolioBody"></div>';
		var portfolioBodyCss={width : PortfolioBodyWidth+"px", 
													height: PortfolioBodyHeight+"px", 
													"opacity" : 0, 
													"z-index" : zmax+2, 
													"position" : "absolute", 
													"top" : centreScreenHeight, 
													"left" : centreScreenWidth,
													width : PortfolioBodyWidth+"px",
													height: PortfolioBodyHeight+"px"
													};

					
		// create
		
		$("#header").before(blackspace+imageLoad); // Create blackspace
		$("#blackspace").css(blackspaceCss); // set the css for blackspace
		$("#imagePreLoader").css(imageCss);// Set Image CSS
		$("#blackspace").fadeTo("slow",0.75); // Slowly show blackspace
		$("#header").before(portfolioBody); // create the portfolio item
		$("#portfolioBody").css(portfolioBodyCss); // put the kiabosh on the content
		$("#blackspace").css({"margin": "auto"}); // Move image icon?
		$.ajax({url: urlAddr,
					  success: function(html){
							
				  	  $("#portfolioBody").append(html);
//						alert(html);						
					  },
						complete: function(){
//							var centreScreenWidth=((windowWidth/2)-$("#portfolioBody").width())+"px";
//							var centreScreenHeight=((windowHeight/2)-$("#portfolioBody").height())+"px";
//							$("#portfolioBody").css({"position": "absolute", "top" : centreScreenWidth, "left" : centreScreenHeight});
							$("#portfolioBody").fadeTo("slow",1);
							// Add close even to click
							$("#pannelClose").click(function(){
								var x;			
								clearTimeout(x);
								$("#imagePreLoader").remove();
								$("#portfolioBody").fadeTo("slow",0);
								$("#blackspace").fadeTo("slow",0);
								x = setTimeout(function() {$("#portfolioBody").remove();$("#blackspace").remove();}, 500);
							});
//							alert("I've Loaded the data");

						}
	
		});

		$("#blackspace").click(function(){
			// Kill Blackspace
			var x;			
			clearTimeout(x); 		
			$("#imagePreLoader").remove();
			$("#portfolioBody").fadeTo("slow",0);
			$("#blackspace").fadeTo("slow",0);
			x = setTimeout(function() {$("#portfolioBody").remove();$("#blackspace").remove();}, 500);
		});

	return false;		
	});
	
	
	
	// a test

// This does stuff on resizze of  browser window
/*
      function doSomething() {
//        alert("I'm done resizing for the moment");
      };
         
      var resizeTimer = null;
      $(window).bind('resize', function() {
        if (resizeTimer) clearTimeout(resizeTimer);
        resizeTimer = setTimeout(doSomething, 100);
  	  });
*/
});

