// Please feel free to integrate this with another js library if one is created at a later time
$(document).ready(function() {

	//Share Link
	
   /* $(".share_socials input").val(document.location.href);
	$(".share_socials input").click(function(){
		$(this).select();	
	})
	$(".url a").click(function(){
		$(".share_socials").slideToggle("fast");	
	})*/
	
	






	//Adjust the size of the product container on load
	$(window).load( function() {		//Fix for webkit browsers which load css and js in parallel
		browser = navigator.userAgent.toLowerCase()
		internet_explorer = /msie/.test(browser);

		var content_wrapper_height = $(".content_wrapper").height();
		var content_container_height = $(".content_container").eq(0).height();
		//Interferes with the products page under an agent login 	
		var content_container_padded_height = content_container_height + 50;
		$(".content_wrapper").css({"height": content_container_padded_height});
	});
	
	
//---------------------------------------------------------------------------------------------------------------------------------
//	Products Page Content Slider
//---------------------------------------------------------------------------------------------------------------------------------
	
	//This function works based on element indexes and relies on the fact that each product link corresponds
	//to a product container which contains relevant product information. The product containers can vary
	//in height and the event listener will automatically adjust the product wrapper 
	
	$(".content_list a").click( function() {
		var index = $(".content_list a").index(this);
		var target_container_offset = $(".content_container").eq(index).position();
		var target_container_offset_top = target_container_offset.top;
		var target_container_height = $(".content_container").eq(index).height();
		var target_container_padded_height = target_container_height + 50;
		$(".content_wrapper").animate({"height": target_container_padded_height}, "fast", function () {
			$(".content_list a").removeClass("selected");
			$(".content_list a").eq(index).addClass("selected");
			$(".content_slider").animate({							 
				"top" : "-"+target_container_offset_top+"px"
			}, "normal");
		});
	});	
	
//---------------------------------------------------------------------------------------------------------------------------------
//	Home Page Content Slider
//---------------------------------------------------------------------------------------------------------------------------------
	
	//This function works based on element indexes and relies on the fact that each slide link corresponds
	//to a slide image. It will toggle through slides every 5 seconds until a specific link is clicked
	//upon which time the timer is canceled and the user specified slide is displayed until a different
	//link is clicked
	
	//Show the first image in sequence at page load all other images are set to none
	$(".home_slider_wrap img").eq(0).css({"display": "block"});

	//Create 5second interval for slide change
	home_slide_timer = setInterval (home_slide_cycle, 5000); 

	//Initilialise Slide Count
	current_slide = 0;
	function home_slide_cycle(){
		total_slides = $(".home_slider_wrap img").size()
		total_slides_index = total_slides - 1;
		
		if (current_slide < total_slides_index){
			$(".home_slider_wrap img").eq(current_slide).fadeOut(1000);
			$(".home_slider_sections span").removeClass("selected");
			$(".home_slider_sections span").eq(current_slide +1).addClass("selected");
			$(".home_slider_wrap img").eq(current_slide + 1).fadeIn(1000);
			current_slide ++
		}else{
			$(".home_slider_wrap img").eq(current_slide).fadeOut(1000);
			$(".home_slider_sections span").removeClass("selected");
			$(".home_slider_sections span").eq(0).addClass("selected");
			$(".home_slider_wrap img").eq(0).fadeIn(1000);
			current_slide = 0;
		}
	}
	
	
	$(".home_slider_sections span").click( function() {
		clearInterval(home_slide_timer);													
		var index = $(".home_slider_sections span").index(this);		
		var display_property = $(".home_slider_wrap img").eq(index).css("display");
		
		if (display_property == "none"){
			$(".home_slider_wrap img").fadeOut(1000);
			$(".home_slider_sections span").removeClass("selected");
			$(".home_slider_sections span").eq(index).addClass("selected");
			$(".home_slider_wrap img").eq(index).fadeIn(1000);
		}else{												

		}
	});	


//---------------------------------------------------------------------------------------------------------------------------------
//	Login Lightbox 
//---------------------------------------------------------------------------------------------------------------------------------

	$("#login_link").click( function() {
		
		//Run the getCenterOffsets function to determine where to place the login dialog
		getCenterOffsets(385, 330);
		
		//Build overlay [see stylesheet for aditional parameters]
		overlay_html = "<div id='overlay'></div>";
		$("body").append(overlay_html);
		
		
		
		//Build login dialog [see stylesheet for additional parameters]
		login_html  = "<div id='login_inner' style='top:"+window_height_center+"px; left: "+window_width_center+"px;'>"
		login_html += "<div id='login_close'></div>"
		login_html += "<label>Username:</label>"
		login_html += "<input type='text' name='username' />"
		login_html += "<label>Password:</label>"
		login_html += "<input type='password' name='password' />"
		login_html += "<a href='javascript:void(0);'></a>"
		login_html += "</div>";
		$("body").prepend(login_html)
		
		if (internet_explorer == true){
			//IE has borked alpha transparency filters hence need to reset it everytime opacity is changed
			document.getElementById("login_inner").style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=login_background.png,sizingMethod='scale')";
			$("#overlay").animate({opacity : 0.7});
			$("#login_inner").addClass("iesux");
			
		}else{
			$("#overlay").animate({opacity : 0.7});
			$("#login_inner").animate({opacity : 1.0});
		}
		
	
		
		//Bind a click event to the overlay so that it can be removed when user clicks away from the login screen
		$("#overlay, .login_close").bind("click", function() {
			if (internet_explorer == true){
				$("#overlay, #login_inner").remove();
			}else{
			$("#overlay, .login_inner").animate({opacity : 0}, function(){
				$("#overlay, #login_inner").remove();})
			}
		});
	});//end of $("#login_link").click( function()


   $(window).resize( function() {
	   getCenterOffsets(385, 330);
	   $("#login_inner").css({"top": ""+window_height_center+"px", "left": window_width_center+"px"});
							  });
   $(window).scroll( function() {
	   getCenterOffsets(385, 330);
	   window_height_center += document_position;
	   $("#login_inner").css({"top": ""+window_height_center+"px", "left": window_width_center+"px"});
							  });
   
   	
   function getCenterOffsets(target_height, target_width) {
	   window_height = $(window).height();
	   window_width = $(window).width();
	   document_position = $(window).scrollTop();

	   window_height_center = (window_height - target_height) / 2;
	   window_width_center = (window_width - target_width) / 2;
   };
   
   function closeVidgrid(){
   	alert('Should close now');
   }
   
   
//---------------------------------------------------------------------------------------------------------------------------------
//	Crew and product guide fancyboxes
//---------------------------------------------------------------------------------------------------------------------------------
   
   $(".crew_picture_wrap a").fancybox({
		'overlayOpacity' : 0.7,
		'zoomSpeedIn' : 600,
		'frameWidth' : 930,
		'frameHeight' : 370,
		'zoomSpeedOut' : 500
		 });
	
	$("li.locationMap a, .col_03 a").fancybox({
		'overlayOpacity' : 0.7,
		'zoomSpeedIn' : 600,
		'frameWidth' : 480,
		'frameHeight' : 360		
	})	
	
	
	 $(".products_picture_frame a").fancybox({
		'overlayOpacity' : 0.7,
		'zoomSpeedIn' : 600,
		'frameWidth' : 930,
		'frameHeight' : 370,
		'zoomSpeedOut' : 500
		 });


	 $("a.services_iframe").fancybox({
		'overlayOpacity' : 0.7,
		'zoomSpeedIn' : 600,
		'frameWidth' : 930,
		'frameHeight' : 370,
		'zoomSpeedOut' : 500
		 });
		 

	$("a.viewStills").fancybox({
		'overlayOpacity' : 0.7,
		'zoomSpeedIn' : 600,
		'frameWidth' : 930,
		'frameHeight' : 450,
		'zoomSpeedOut' : 500
		 });	 
		 
		 
	$("a.viewGraph").fancybox({
		'overlayOpacity' : 0.7,
		'zoomSpeedIn' : 600,
		'frameWidth' : 650,
		'frameHeight' : 550,
		'zoomSpeedOut' : 500
		 });	 
		 

});
	
//##################################################################################################################################
//##  Footer Unfuckerer
//##################################################################################################################################
	$(window).load( function() {		//Fix for webkit browsers which load css and js in parallel
							 
		var document_height = $(window).height();
		var header_height = $("#header_wrap").height();		
		var footer_height = $("#footer_wrap").height();
		var remaining_space = document_height - (header_height + footer_height) -1 //Footer has 1px border;
		$(".two_col_cont").css({"min-height": remaining_space});
	});

	



