// Bens Image Reflect
$.fn.extend({reflect:function(options){options=$.extend({height:23,opacity:0.3},options);return this.unreflect().each(function(){var img=this;if(/^img$/i.test(img.tagName)){function doReflect(){var imageWidth=img.width,imageHeight=img.height,reflection,reflectionHeight,wrapper,context,gradient;reflectionHeight=Math.floor((options.height>1)?Math.min(imageHeight,options.height):imageHeight*options.height);if($.browser.msie){reflection=$("<img />").attr("src",img.src).css({width:imageWidth,height:imageHeight,marginBottom:reflectionHeight-imageHeight,filter:"flipv progid:DXImageTransform.Microsoft.Alpha(opacity="+(options.opacity*100)+", style=1, finishOpacity=0, startx=0, starty=0, finishx=0, finishy="+(reflectionHeight/imageHeight*100)+")"})[0]}else{reflection=$("<canvas />")[0];if(!reflection.getContext)return;context=reflection.getContext("2d");try{$(reflection).attr({width:imageWidth,height:reflectionHeight});context.save();context.translate(0,imageHeight-1);context.scale(1,-1);context.drawImage(img,0,0,imageWidth,imageHeight);context.restore();context.globalCompositeOperation="destination-out";gradient=context.createLinearGradient(0,0,0,reflectionHeight);gradient.addColorStop(0,"rgba(255, 255, 255, "+(1-options.opacity)+")");gradient.addColorStop(1,"rgba(255, 255, 255, 1.0)");context.fillStyle=gradient;context.rect(0,0,imageWidth,reflectionHeight);context.fill()}catch(e){return}}$(reflection).css({display:"block",border:0});wrapper=$(/^a$/i.test(img.parentNode.tagName)?"<span />":"<div />").insertAfter(img).append([img,reflection])[0];wrapper.className=img.className;$.data(img,"reflected",wrapper.style.cssText=img.style.cssText);$(wrapper).css({width:imageWidth,height:imageHeight+reflectionHeight,overflow:"hidden"});img.style.cssText="display: block; border: 0px";img.className="reflected"}if(img.complete)doReflect();else $(img).load(doReflect)}})},unreflect:function(){return this.unbind("load").each(function(){var img=this,reflected=$.data(this,"reflected"),wrapper;if(reflected!==undefined){wrapper=img.parentNode;img.className=wrapper.className;img.style.cssText=reflected;$.removeData(img,"reflected");wrapper.parentNode.replaceChild(img,wrapper)}})}});

var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}



$(document).ready(function(){
	
	// Inline-popUp window
	$('#registerInterest').click(function(e) {
		centerPopup();
		loadPopup();
		e.preventDefault();
	});
					
	$("#popupContactClose, #backgroundPopup").click(function(){
		disablePopup();
	});

	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	
	loadCalendar('http://www.backstage-academy.co.uk/calendar');
	
	$('img.reflect').reflect();
		
	// Terms Popup
	$("a.termsPopup").fancybox({
		'width'			: 	500,
		'height'		:	800,
		'overlayShow'	:	true
	});
	
	
	//target blank fix
	$("a.external").attr("target","_blank");
	
	//nav fix
	$(".header .mainNav li, .footer .mainNav li").not(".header .mainNav li:last, .footer .mainNav li:last").each(function(){
		$(this).after("<li>|</li>");
	});
	
	//lightbox
	$("a.lightBox").lightBox();

	// According
	$('.groupList div.course, .hideshow .content_text').hide(); 
	$('.courseTitle:first, .hideshow h3:first').addClass('active').next().show(); 
	$('.courseTitle, .hideshow h3').click(function(){
		if($(this).next().is(':hidden')){ 
			$('.courseTitle, .hideshow h3').removeClass('active').next().slideUp(); 
			$(this).toggleClass('active').next().slideDown();
		}
		return false;
	});
	
	// Validate Available Date
	$("#placeBooking").click(function(e){ 	
		var found = 0;
		
		$(".validateAvailableDate input[type='radio']").each(function() {
			if ($(this).is(':checked')) found = 1;						
		});
		
		
		if (found == 0) {
			e.preventDefault();
			alert ("Please select a date for your booking");
		}
	});	
	
	// Copy Candidate Details to Billing Details
	$("#copyDetails").click(function() {
		if ($(this).attr("checked")) {
			$("#billing_forename").attr('value', $("#candidate_forename").attr("value"));
			$("#billing_surname").attr('value',  $("#candidate_surname").attr("value"));
			$("#billing_address1").attr('value', $("#candidate_address1").attr("value"));
			$("#billing_address2").attr('value', $("#candidate_address2").attr("value"));
			$("#billing_town").attr('value', 	 $("#candidate_town").attr("value"));
			$("#billing_county").attr('value', 	 $("#candidate_county").attr("value"));
			$("#billing_postcode").attr('value', $("#candidate_postcode").attr("value"));
			$("#billing_country").attr('value',  $("#candidate_country").attr("value"));
		} else {
			$("#billing_forename").attr('value', '');
			$("#billing_surname").attr('value', '');
			$("#billing_address1").attr('value', '');
			$("#billing_address2").attr('value', '');
			$("#billing_town").attr('value', '');
			$("#billing_county").attr('value', '');
			$("#billing_postcode").attr('value', '');
			$("#billing_country").attr('value', '');
		}
	});	
	
});


function loadCalendar (url) {
	$('#calendar').load(url, function () {
		// Get Links
		
		$('.calendar-prev, .calendar-next').click(function(e) {
			e.preventDefault();			
			var link = $(this).find('a').attr('href');			
			loadCalendar(link);
		});	
		
		$("a.iframe").fancybox({
				'width'			: 	350,
				'height'		:	200,
				'transitionIn'	:	'elastic',
				'transitionOut'	:	'elastic',
				'speedIn'		:	600, 
				'speedOut'		:	200, 
				'overlayShow'	:	true
		});
		
		$("td.selected div").hover(function() {
			$(this).parent().find('.hoverContent').show();		
		}, function() { 			
			$(this).parent().find('.hoverContent').hide(); 
		});
	});
}



function checkTerms () {
	if (!$('#terms').attr('checked')) {
		alert ('Please agree to our Terms and Conditions before proceeding with your booking');
		return false;
	}
	
	return true;
}
