//Master Javascript file
//version:   4.0
//author:    Paul Boag
//email:     paul.boag@headscape.co.uk
//website:   http://www.headscape.co.uk


// Loads up the various functions we are going to use
jQuery(document).ready(function(){
	csLinks("#csMenu a");
	tooltips();
	jsEnabled();
	popup('a[rel]');
	tagCorners();
	formCorners();
	newwindow();
	/*validateForm("#tfa_Name");
	validateForm("#tfa_Email");
	validateForm("#tfa_Organisation");
	validateForm("#tfa_Howcanwehelp");*/
	
	if(BrowserDetect.browser == "Explorer")
	{
		var strPath = String(window.location);
		var FaceboxIframeCSS = '<link rel="stylesheet" href="/facebox/iframe.css" type="text/css" />';
		
		if (strPath.indexOf("/audio_guides.asp") > 0)
			jQuery('head').append(FaceboxIframeCSS);
			
		if (strPath.indexOf("/gallery") > 0)
			jQuery('head').append(FaceboxIframeCSS);
			
		if (strPath.indexOf("/interesting_places") > 0)
			jQuery('head').append(FaceboxIframeCSS);
		
		if (strPath.indexOf("/section_videos") > 0)
			jQuery('head').append(FaceboxIframeCSS);
			
		if (strPath.indexOf("/visitors_comment") > 0)
			jQuery('head').append(FaceboxIframeCSS);
	}
});

// Adds a class to the body so I can style differently depending on whether Javascript is enabled.
function jsEnabled() {
	jQuery("body").addClass("jsenabled");	
}

// Swaps the case studies in and out
function csLinks(linkLoc) {
jQuery(linkLoc).click(function(){
	clickFire(this);
	return false;
});
};

function clickFire(h) {
	jQuery(document).trigger('close.facebox');
	var addr = (h.href + " #caseStudies > *");
	jQuery(".current").removeClass("current");
	jQuery("#caseStudies").wrapInner("<span id='caseInner'></span>");
	jQuery("#caseInner").fadeOut(300, function() {
		jQuery("#caseStudies").load(addr, function() {
			jQuery("#caseInner").hide();
			jQuery("#caseInner").fadeIn(300);
			tooltips();
			popup('#caseStudies a[rel]');
			tagCorners();
		});				
	});
	h.parentNode.className = "current";
};

//Displays the tooltips on the case study tags
function tooltips() {
jQuery("#caseStudies dt").toggle(
		function() {
		var descr = jQuery(this).next().text();
		jQuery("#caseStudies dt div").animate({ "bottom": "1em", "opacity": 0 }, 500,  function() {
		jQuery(this).remove();
		});
		jQuery(this).append("<div><span class='messageTR'><span class='messageTL'>" + descr + "</span></span><span class='messageBR'><span class='messageBL'><img src='/images/arrow.gif' /></span></span></div>");
		var theWidth = ((jQuery(this).width()/2) - (jQuery("#caseStudies dt div").width()/2));
		if ((jQuery("#caseStudies dt div").width()/2) > (jQuery("#caseStudies dt div").offset().left)) {
			jQuery(".messageBL img").addClass("arrowPos");	
		}
		else {
			jQuery("#caseStudies dt div").css({ left: theWidth + "px" });	
		}
		jQuery(this).children("div").animate({ "bottom": "2em", "opacity": 1 }, 500,  function() {		
		});
		},
		function() {
		jQuery("#caseStudies dt div").animate({ "bottom": "1em", "opacity": 0 }, 500,  function() {
		jQuery(this).remove();
		});
		}
	);


};

//Manages all of the popup windows using facebox
function popup(Elem) {
	jQuery(Elem).click(function(){
			var theUrl = this.href.split("#")
			var addr = (theUrl[0] + " #" + theUrl[1] + " > *");
			if (this.rel == "screenshot") {
				var c = jQuery("<div id='" + theUrl[1] + "'><div  style='text-align:center;'><img src='/facebox/loading.gif' /></div></div>");
			};
			if (this.rel == "allClients") {
				var c = jQuery("<div id='allClients'><div  style='text-align:center;'><img src='/facebox/loading.gif' /></div></div>");
			};
			if (this.rel == "cv") {
				var c = jQuery("<div class='cv'><div  style='text-align:center;'><img src='/facebox/loading.gif' /></div></div>");
			};
			if (this.rel == "clients") {
				var c = jQuery("<div id='clientList'><div  style='text-align:center;'><img src='/facebox/loading.gif' /></div></div>");
			};
			
			if (this.rel == "image") {
				var c = jQuery("<div class='facebox'><div  style='text-align:center;top:116px;'><img src='/facebox/loading.gif' /></div></div>");
				jQuery.facebox({
					image: this.href
				});
				
				return false;
			};
			
			if (this.rel == "facebox") {
				var c = jQuery("<div class='facebox'><div  style='text-align:center;top:116px;'><img src='/facebox/loading.gif' /></div></div>");
			};

			jQuery(c).load(addr, function(){
				jQuery.facebox(c);
				csLinks("#facebox li a");
			});
			return false;
	});
};


//Creates the curved corners on the tags
function tagCorners() {
	jQuery("#caseStudies dt").wrapInner('<span class="TR"><span class="BL"><span class="BR"></span></span></span>');
};

//Creates the curved corners on form elements
function formCorners() {
	jQuery("input, textarea").wrap('<span class="TL"><span class="TR"><span class="BL"><span class="BR"></span></span></span></span>');
};

//Opens links to email subscription in a new window 

function newwindow() {
	jQuery(".email a").click(function(){
			window.open (this.href, "mywindow","resizable=1,width=550,height=450"); 	
			return false;
	});
};

//Validates the contact us form

function validateForm(i) {
	jQuery(i + "L").append(" <em>(This is a required field)</em>");
	jQuery(i + "L em").hide();
	jQuery("#id2342994").submit(function() {
		jQuery(i + "L em").fadeOut();
		if (jQuery(i).val() == "") { 
		jQuery(i + "L em").fadeIn();
		return false; 
		}						   
	});
};

// SS - opens forms in iframe so they can be self contained.
function openForm(href, height){
	jQuery.facebox({
	iframe: href,
	rev: 'iframe|' + height
	});
}

function openFlashForm(href)
{
	jQuery.facebox({
	iframe: href,
	rev: 'iframe|800'
	});
}
