
jQuery(function($) {
     $("img").pngfix();
});

var slideShowRunning = false;
var slideShowSpeed = 900;
var animating = false;
var videoFlashEmbedPrimer = "<object width='{WIDTH}' height='{HEIGHT}'><param name='movie' value='http://www.youtube.com/v/{VID}?fs=1&amp;hl=en_GB&autoplay=1&rel=0'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><embed src='http://www.youtube.com/v/{VID}?fs=1&amp;hl=en_GB&autoplay=1&rel=0'		type='application/x-shockwave-flash'		allowscriptaccess='always'		allowfullscreen='true'		width='{WIDTH}' height='{HEIGHT}'></embed></object>";

/*{FLASH_NAME}
 *{WIDTH}
 * {HEIGHT}
 * {BACKCOLOR}
 * */
var vanillaFlashEmbedPrimer = "<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' " +
		"codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0' " +
		"WIDTH='{WIDTH}' HEIGHT='{HEIGHT}' id='myMovieName'>" +
		"<PARAM NAME=movie VALUE='/sites/default/files/swf/{FLASH_NAME}.swf'><PARAM NAME=quality VALUE=high>" +
		
		"<param name='wmode' value='transparent'>"+
		"<EMBED wmode='transparent'  src='/sites/default/files/swf/{FLASH_NAME}.swf' " +
		"quality=high WIDTH='{WIDTH}' HEIGHT='{HEIGHT}' NAME='Flash movie' ALIGN='' " +
		"TYPE='application/x-shockwave-flash' PLUGINSPAGE='http://www.macromedia.com/go/getflashplayer'>" +
		"</EMBED></OBJECT>";

$(document).ready(function(){
	
	// Customer Login Clears	
	$("#username").focus(function(){if($(this).attr("value") == "Username"){$(this).attr("value","")}});
	$("#password").focus(function(){if($(this).attr("value") == "Password"){$(this).attr("value","")}});	
	
	//COLORBOX EVENT LISTENERs
	if (('a[rel^="videobox"]').length > 0){
		$('a[rel^="videobox"]').click(function(){
			var videoHeight = parseInt($(this).attr('rel').split('|')[2])+30;
			var videoWidth =parseInt($(this).attr('rel').split('|')[1])+1;
			$(this).colorbox({iframe:true, innerWidth:videoWidth, innerHeight:videoHeight, onClosed:function(){ slideManualOverride = false; } });
			slideManualOverride = true;
		});
	}
	
	if (('a[rel^="slideshow"]').length > 0){
		$('a[rel^="slideshow"]').click(function(){
			var videoHeight = parseInt($(this).attr('rel').split('|')[2])+20;
			var videoWidth =parseInt($(this).attr('rel').split('|')[1])+1;
			$(this).colorbox({iframe:true, innerWidth:videoWidth, innerHeight:videoHeight, onOpen:function() {colorBoxDark();}, onComplete:function(){videoCheck();},  onClosed:function(){ slideManualOverride = false;autoSlide(); }});
			slideManualOverride = true;
			
			
		});
	}
	
	
	
	$('a.mailinglist').click(function(){
		var width = 600;
		var height = 380;
		$(this).colorbox({iframe:true, innerWidth:width, innerHeight:height, onClosed:function(){ slideManualOverride = false;autoSlide(); }});
		slideManualOverride = true;
	});
	
	if($("#edit-submitted-toemail").length>0){
		
		
		$("#edit-submitted-toemail").val($.query.get("e"));
		$("#edit-submitted-viewpointname").val($.query.get("n"));
		
	}
	
	$('a.pdfversion').click(function(){
		var width = 600;
		var height = 420;
		$(this).colorbox({iframe:true, innerWidth:width, innerHeight:height, onClosed:function(){ slideManualOverride = false;autoSlide(); }});
		slideManualOverride = true;
	});
	$('.cta-block a').click(function(){
		var width = 700;
		var height = 580;
		$(this).colorbox({iframe:true, innerWidth:width, innerHeight:height, onClosed:function(){ slideManualOverride = false;autoSlide(); }});
		
		slideManualOverride = true;
    });
    $('.column-a a#emailcv').click(function () {
        var width = 700;
        var height = 580;
        $(this).colorbox({ iframe: true, innerWidth: width, innerHeight: height, onClosed: function () { slideManualOverride = false; autoSlide(); } });

        slideManualOverride = true;
    });
	$('.teammember.cb').click(function(){
		var width = 800;
		var height = 450;
		$(this).colorbox({iframe:true, innerWidth:width, innerHeight:height, onClosed:function(){ slideManualOverride = false;autoSlide(); }});
		slideManualOverride = true;
	});
	if ($('a[rel^="colorbox_map"]').length > 0){
		$('a[rel^="colorbox_map"]').click(function(){
			var videoHeight = 500;
			var videoWidth = 800;
			$(this).colorbox({iframe:true, innerWidth:videoWidth, innerHeight:videoHeight, onClosed:function(){ slideManualOverride = false;autoSlide(); }});
		});
	}
	
	$("#siteSearch").submit(function(){
		window.location.href = 'http://www.conrad.co.uk/search/node/'+$("#searchform").val();
		return false;
	});
	
	if($(".case-study-type-selector input").length > 0){
	$(".case-study-type-selector input").click(function(){
		$("#frmCaseStudySelector").submit();
	});
	}
	if ($("#frmSortViewpoints select").length > 0){
		$("#frmSortViewpoints select").change(function(){
			$("#frmSortViewpoints").submit();
		});
	}
	
	if($("#slideshow").length > 0)
	{
		$("#slideshow .thumbs img").each(function(index){
			var imageName = $(this).attr("src").replace("_thumb","_full");
			jQuery.preLoadImages(imageName,imageName);
		});
		       
	}
	if ($("#emailcv").length > 0)
	{
		$.cookie("emailcv",$("#emailcv").attr('rel'),{expires: 7,path: '/'});
	}
	
	if ($("#getintouch").length > 0)
	{
		$.cookie("emailcv","matt@netizen.co.uk",{expires: 7,path: '/'});
	}
	
	if ($("#edit-submitted-email-to").length>0)
	{
		
		$("#edit-submitted-email-to").val($.cookie("emailcv"));
		$("#edit-submitted-job").val(document.referrer);
	}
	
	
	
	//slideshow	
	if($("#slideshow").length > 0){
		
		//$("#slideshow .details").css("margin-top",$("#height").val()-90);
		
		
		$("#slideshow .details p").html("Above: " + $("#slideshow .thumbs img:first").attr("title"));
		$("#slideshow .close").click(function() {parent.$.colorbox.close();});
	
	$("#slideshow .thumbs img").click(function(){
		// Additional code to populate the image title
		$("#slideshow .details p").html("Above: " + $(this).attr("title"));
		
		if (slideShowRunning == false){
			//VIDEO HANDLING
			var alt = $(this).attr("alt");
			
			if(alt.indexOf("FLASH|")>-1 || alt.indexOf("VIDEO|")>-1)
			{			
				embedVideoOrFlash($(this),$("#slideshow"));
				
			}
			else
			{
				slideShowRunning = true;
				//IMAGE HANDLING
				var newSrc = $(this).attr("src").replace("_thumb","_full");
				if ($("#slideshow img.backbuffer").css("display") == "none")
				{
					
					$("#slideshow img.backbuffer").attr("src",newSrc);
					if ($("#slideshow .flashholder").html() != "")
					{
						$("#slideshow .flashholder").fadeOut(slideShowSpeed);
						$("#slideshow .flashholder").html("");
					}
					else
					{
						$("#slideshow img.main-image").fadeOut(slideShowSpeed);
					}
					$("#slideshow img.backbuffer").fadeIn(slideShowSpeed,function(){slideShowRunning = false;});
				}
				else
				{
					$("#slideshow img.main-image").attr("src",newSrc);
					if ($("#slideshow .flashholder").html() != "")
					{
						$("#slideshow .flashholder").fadeOut(slideShowSpeed);
						$("#slideshow .flashholder").html("");
					}
					else
					{
						$("#slideshow img.backbuffer").fadeOut(slideShowSpeed);
					}
					$("#slideshow img.main-image").fadeIn(slideShowSpeed,function(){slideShowRunning = false;});
				}
			}
		}
	});
	}
	if ($("#webform-client-form-15").length > 0)
	{
		$("#webform-client-form-15").validate();
	}
	if ($("#webform-client-form-101").length > 0)
	{
		$("#webform-client-form-101").validate();
	}
	if ($("#webform-client-form-125").length > 0)
	{
		$("#webform-client-form-125").validate();
	}
	
	if (navigator.appVersion.indexOf("MSIE")==-1 && $(".couponsSmall .coupon:not(.active)"))
	{
	
		// Image faders
		$(".couponsLarge .coupon").mouseenter(function(){
			couponOver($(this).find("img"),'110px','108px',5,5);
		});
		$(".couponsLarge  .coupon").mouseleave(function(){
			couponOut($(this).find("img"),'95px','95px',5);
		});
		
		$(".couponsSmall .coupon").mouseenter(function(){
			var couponImage = $(this).find("img");		
			couponOver(couponImage,'58px','58px',2,2);
		});
		
		$(".couponsSmall .coupon").mouseleave(function(){
			var couponImage = $(this).find("img");		
			couponOut(couponImage,'51px','51px',2);
		});
		
		$(".couponsWide .coupon").mouseenter(function(){
			couponOver($(this).find("img.imagefield-field_coupon_image"),'110px','108px',5,6);
		});
		
		$(".couponsWide .coupon").mouseleave(function(){
			couponOut($(this).find("img.imagefield-field_coupon_image"),'95px','95px',5);
		});
	}

	if ($(".couponsSmall .coupon").hasClass('active')){
		$(".couponsSmall .active").mouseleave(function(){
			$(".couponsSmall .active .image").css("opacity", "0.7")
			});
	}

	if (Modernizr.touch){
  	
  		//$(".couponsLargeMk2 .coupon a").attr("rel",$(this).attr('href'));
  		
  		$(".couponsLargeMk2 .coupon a").each(function(index){
  			$(this).attr("rel",$(this).attr("href"));
  		});
  		
  		$(".couponsLargeMk2 .coupon a").attr("href","javascript:;");
  		
  		$(".couponsLargeMk2 .coupon").click(function(){
  			if ($(this).find(".couponinner").hasClass("on"))
  			{
  				
  				window.location.href = $(this).find("a").attr("rel");
  			}
  			$(".couponsLargeMk2 .couponinner.on").animate({bottom:'0'},{queue:false, duration:200,easing:'swing',complete: function(){$(this).find(".inner").fadeOut(400);$(this).removeClass("on");}});
  			if (!$(this).find("p").hasClass("appened"))
  			{
	  			$(this).find("p").append("<span>Touch to read more...</span>");
	  			$(this).find("p").addClass("appened");
  			}
  			$(this).find(".inner").fadeIn(400);
  			
  			$(this).find(".couponinner").animate({bottom:'-100px'},{queue:false, duration:200,easing:'swing',complete: function(){$(this).addClass("on");}});
			
  		});
  		
  
	} else {

		$(".couponsLargeMk2 .coupon").click(
			function(){
				window.location.href = $(this).find("a").attr("href");
			}
		);
	
		$(".couponsLargeMk2 .coupon").mouseover(function(event){
			
			$(this).find("img").animate({opacity:0.7,queue:false},200);
			$(this).find("img").clearQueue();
			$(this).find(".inner").stop(true,true);
			$(this).find(".inner").fadeIn(400);
			$(this).find(".couponinner").animate({bottom:'-100px'},{queue:false, duration:200,easing:'swing',complete: function(){
			}});
			$(this).clearQueue();				
		});
		$(".couponsLargeMk2 .coupon").mouseleave(function(event){
			$(this).find("img").animate({opacity:1,queue:false},200);
			$(this).find("img").clearQueue();
			$(this).find(".inner").stop(true,true);
			$(this).find(".inner").fadeOut(400,function(){});
			$(this).find(".couponinner").animate({bottom:'0'},{queue:false, duration:200,easing:'swing',complete: function(){
			}});
			$(this).clearQueue();				
		});
	  
	}
	
	//fade in and out the social icon links when they are hovered over
	$('.logos li').mouseover(function() {
		$(this).find("img").animate({opacity: 1}, 200, function() {
		// Animation complete.
		});
	});
				
	$(".logos li").mouseleave(function(){
		$(this).find("img").animate({opacity: 0.4}, 200, function() {
		// Animation complete.
		});
	});
	
	
});

function couponOver(o,a,b,c,d)
{
	
	o.clearQueue();
	
	imgTop = o.position().top;
	imgLeft = o.position().left;
	o.closest(".coupon").css("height",o.closest(".coupon").height());
	o.css("position","absolute");
	o.css("left","0");
	o.css("top","0");
	o.css("border",c + "px solid #CCC");
	o.animate({opacity:"1",width:a,height:a,left:'-'+d+'px',top:'-'+d+'px',opacity:0.7},100);
}
function couponOut(o,a,b,c)
{
	o.clearQueue();
	
	o.css("border",c+"px solid #E4E2E3");
	o.animate({opacity:"1",width:a,height:a,left:'0',top:'0',opacity:1},100);
}

function videoCheck(){
	$("#colorbox iframe").load(function(){
		if($(this).contents().find(".thumbsinner img:first").attr("alt") != ""){
			var currentImage = $(this).contents().find(".thumbsinner img:first");
			embedVideoOrFlash($(currentImage),$(this).contents().find("#slideshow"));
		}		
	});
}

function embedVideoOrFlash(currentImg,slideShow)
{
	var newSrc = currentImg.attr("src").replace("_thumb","_full");
	var alt = currentImg.attr("alt");
	var flashEmbed = "";
	var leftOffset = 0;
	var topOffset = 0;
	if (alt.indexOf("FLASH|")>-1)
	{
		var flashName = alt.split('|')[1];
		var width = alt.split('|')[2];
		var height = alt.split('|')[3];
		if (width == null) {width = 700;}
		if (height == null) {height = 500;}
		
		flashEmbed = vanillaFlashEmbedPrimer;
		flashEmbed = flashEmbed.replace(/{WIDTH}/g,width);
		flashEmbed = flashEmbed.replace(/{HEIGHT}/g,height);
		flashEmbed = flashEmbed.replace(/{FLASH_NAME}/g,flashName);
	
	}
	if (alt.indexOf("VIDEO|")>-1){
		flashEmbed = videoFlashEmbedPrimer;
		
		var width = alt.split('|')[2];
		var height = alt.split('|')[3];
		if (width == null) {width = 700;}
		if (height == null) {height = 500;}
		
		flashEmbed = flashEmbed.replace(/{WIDTH}/g,width);
		flashEmbed = flashEmbed.replace(/{HEIGHT}/g,height);	
		flashEmbed = flashEmbed.replace(/{VID}/g,alt.split('|')[1]);
		
		
		
		
	}
	
	if (width != 700)
	{
		leftOffset = 700 - width;
		leftOffset = leftOffset /2;
	}
	if (height != 500)
	{
		topOffset = 500 - height;
		topOffset = topOffset / 2;
		topOffset = topOffset - 20;
	}
	
	if (slideShow.find("img.backbuffer").css("display") == "none")
	{
		slideShow.find("img.main-image").fadeOut(slideShowSpeed);
		slideShow.find("img.backbuffer").hide();
	}
	else
	{
		slideShow.find("img.backbuffer").fadeOut(slideShowSpeed);
		slideShow.find("img.main-image").hide();
	}
	
	slideShow.find(".flashholder").html(flashEmbed);
	slideShow.find(".flashholder").css("padding-left",leftOffset);
	slideShow.find(".flashholder").css("padding-top",topOffset);
	
	slideShow.find(".flashholder").show();
}

function colorBoxDark()
{
	
	$("#cboxMiddleLeft").css("background","#000");
	$("#cboxMiddleLeft").css("filter","");
	$("#cboxMiddleRight").css("background",'#000');
	$("#cboxMiddleRight").css("filter",'');
	$("#cboxTopLeft").css("background",'#000');
	$("#cboxTopLeft").css("filter",'');
	$("#cboxTopCenter").css("background",'#000');
	$("#cboxTopCenter").css("filter",'0');
	$("#cboxTopRight").css("background",'#000');
	$("#cboxTopRight").css("filter",'');
	$("#cboxBottomLeft").css("background",'#000');
	$("#cboxBottomLeft").css("filter",'');
	$("#cboxBottomCenter").css("background",'#000');
	$("#cboxBottomCenter").css("filter",'');
	$("#cboxBottomRight").css("background",'#000');
	$("#cboxBottomRight").css("filter",'');
	$("#cboxContent").css("background","#000");
	$("#cboxClose").css("background","url(/sites/all/themes/conrad/assets/img/close.png)");
	$("#cboxClose").css("height","21px");
	$("#cboxClose").css("width","21px");
	$("#cboxClose").css("bottom","8px");
	$("#cboxClose").css("right","8px");
	$("#cboxCurrent").css("visibility","hidden");
	$("#cboxNext").css("background","none");
	$("#cboxPrevious").css("background","none");
	$("#cboxSlideshow").css("background","none");
}

function showSlideShowImage(img)
{

}
