function get(el) {
  return document.getElementById(el);
}



$(document).ready(function(){
//	alert('dfdf');
	//explorer js

if(get('progWrap')) {
	$('#expData tr').each(function(){
		$(this).children(':first').addClass('left');
		$(this).children(':last').addClass('expButton');
	});

	
	$('#explorer').hide();
	$('#expData tr').css({cursor: "pointer"});
	$('#explorerTab').toggle(function(){
		$('#explorer').show('medium');
		$('#explorerTab').css({right: "5px"});
	}, function(){
		$('#explorer').hide('medium');
		$('#explorerTab').css({right: "0px"});
	});	
	
	$('#expData tr').hover(function(){
		$(this).addClass('darkBorder');
		$(this).find('img').attr({src: "/images/explorer/arrowOn.png"});
	
	}, function(){
	//	$(this).children().children('img').attr({src: "/images/explorer/arrowOff.png"});
		$(this).removeClass('darkBorder');
		$(this).find('img').attr({src: "/images/explorer/arrowOff.png"});
		
	});
}	
	
	//hover states for submits and stuff
	$("#topSearchSubmit").hover(
	  function () {
		$("#topSearchSubmit").attr({ src: "/images/global/arrows/mediumRightOn.gif"});
	  },
	  function () {
		$("#topSearchSubmit").attr({ src: "/images/global/arrows/mediumRightOff.gif"});
	  }
	);
	$("#s_submit").hover(
		  function () {
			$("#s_submit").attr({ src: "/images/search/updateOn.gif"});
		  },
		  function () {
			$("#s_submit").attr({ src: "/images/search/update.gif"});
		  }
	);
	$("#s_keywordSubmit").hover(
	  function () {
		$("#s_keywordSubmit").attr({ src: "/images/global/arrows/mediumRightOn.png"});
	  },
	  function () {
		$("#s_keywordSubmit").attr({ src: "/images/global/arrows/mediumRightOff.png"});
	  }
	);
	$("#topSearch").focus(function() {
		$(this).attr({value: "" });
	});
if(get('callBack')) {
	$('#callBack img').hover(function(){
		$(this).attr('src', '/images/global/requestCallback/requestOn.gif');
	}, function() {
		$(this).attr('src', '/images/global/requestCallback/request.gif');
		
	});
}

	
	// Left Nav below...
if(get('leftNav')){
	//This sets the sideToggle cursor to a hand (if js is switched on)
	$('.expander').css({cursor: "pointer"});
	//hides subnavs
	$('#leftNav ul').not(".onstate").hide();
	// this is the hover for the bottom span section in each of the bars in the leftnav
	$("#leftNav li a").not('.onstate').hover(
	  function () {
	    $(this).siblings().addClass("hover");
	 },
	  function () {
	    $(this).siblings().removeClass("hover");
	 }
	);
	//this is the hover for the little block bits (plus sign) on the left of the leftnav items
	$(".expander").hover(
	  function () {
	    $(this).addClass("hoverState");
		},
		  function () {
	    $(this).removeClass("hoverState");
		}
	);
	// expander function
	$(".expander").toggle(function(){
		$(this).siblings('ul').slideDown("fast");
		$(this).addClass("expanderMinus");
		},
		  function () {
		
		$(this).siblings('ul').slideUp("fast");
		$(this).removeClass("expanderMinus");
		
		}
	);
	
}

	// spanish lessons table toggle

if(get('withSpanish')) {	
	costs = new Object();
	
	$('#costVars select').each(function(){
		theName = $(this).attr('name');
		costs[$(this).attr('name')] = parseFloat($(this).attr('addon'));
		this.selectedIndex="0";
		$('#withSpanish .detailsDarker span').each(function(){
			var orig = parseFloat($(this).html());	
			//had to change the below line so the amount wasn't initially added - GVI wanted no to be default
			//var newPrice = orig + costs[theName];
			var newPrice = orig;
			$(this).html(newPrice);
		});
	});
	
	$("#costVars select").change(function(){
		var addon = costs[$(this).attr('name')];
		addon = parseFloat(addon);
		if($(this).children(':selected').attr('value') == "yes") {
			$('#withSpanish .detailsDarker').each(function(){
				var orig = parseFloat($(this).children('span').html());
				var newAmount = orig + addon;
				$(this).children('span').html(newAmount);
			});
		} else if($(this).children(':selected').attr('value') == "no") {
			$('#withSpanish .detailsDarker').each(function(){
				var orig = parseFloat($(this).children('span').html());
				var newAmount = orig - addon;
				$(this).children('span').html(newAmount);
			});
		}		
	});
}

$('.mainAnchor').hover(function(){
	$(this).children('img').attr("src", "/images/global/arrows/mediumRightOn.png");
}, function() {
	$(this).children('img').attr("src", "/images/global/arrows/mediumRightOff.png");
});
$('.mainAnchorSmall').hover(function(){
	$(this).children('img').attr("src", "/images/global/arrows/mediumRightOn.png");
}, function() {
	$(this).children('img').attr("src", "/images/global/arrows/mediumRightOff.png");
});
$('.mediaCenter').hover(function(){
	$(this).children('img').attr("src", "/images/global/arrows/mediumRightOn.png");
}, function() {
	$(this).children('img').attr("src", "/images/global/arrows/mediumRightOff.png");
});

if(get('mapWidget')) {
	$('#mapWidget').hover(function(){
		$(this).attr("src", "/images/jobs/searchOn.gif");
	}, function() {
		$(this).attr("src", "/images/jobs/searchOff.gif");
	});
}

if(get('faq')) {
	$('#faq .a').hide();
	$('#faq').children('div').addClass('qPointer');
	$('#faq .fHide').addClass('qPointer');
	$('#faq div').click(function(){
		if($(this).children(':last').css("display") == "none") {
			$(this).removeClass('qPointer').children(':last').slideDown("fast");
			$(this).siblings().children('.a').slideUp('fast');
		}
	});
	
	$('.fHide').click(function() {
		$(this).parent().slideUp('fast').parent().addClass('qPointer');
	});
	
	$('#faq div').hover(function(){
		if($(this).children(':last').css("display") == "none"){
			$(this).css("background-color", "#ebebeb");
		}
	}, function() {
		$(this).css("background-color", "#f5f5f5");
	});
}

if(get('mediaBoxLink')) {
	$('#mediaBoxLink').hover(function(){
		this.src = "/images/global/mediaCenterBox/viewOn.gif";
	}, function() {
		this.src = "/images/global/mediaCenterBox/view.gif";
	});
}

if(get('storyBoxLink')) {
	$('#storyBoxLink').hover(function(){
		this.src = "/images/global/mediaCenterBox/storiesOn.gif";
	}, function() {
		this.src = "/images/global/mediaCenterBox/stories.gif";
	});
}

if(get('courseStoryBoxLink')) {
	$('#courseStoryBoxLink').hover(function(){
		this.src = "/images/global/mediaCenterBox/StudentStoriesOn.gif";
	}, function() {
		this.src = "/images/global/mediaCenterBox/StudentStories.gif";
	});
}

if(get('trustBox')) {
	$('#trustBox').hover(function(){
		this.src = "/images/global/charitableTrust/trustBox.gif";
	}, function (){
		this.src = "/images/global/charitableTrust/trustBoxOff.gif";		
	});
}

if(get('parts')) {
	$('#parts').children().not(':first').hide();
	$('.centerTwoNav a').click(function(){
		$('.centerTwoNav a').removeClass('onstate');
		$(this).addClass('onstate');
		$('#parts').children().hide();
		$("#part"+this.name).show();
		return false;
	});
}

	$('#trustVisit').hover(function(){
		this.src = "/images/global/charitableTrustBox/visitOn.gif";
	}, function(){		
		this.src = "/images/global/charitableTrustBox/visit.gif";

	});
	
	$('#faceLink').hover(function(){
		this.src = "/images/global/facebook/clickHereOn.gif";
	}, function(){		
		this.src = "/images/global/facebook/clickHere.gif";

	});
	
if(get('contactWrapper')) {
	$('#contactWrapper div').not(':first').hide();
	$('.centerTwoNav li a').click(function(){
		$('.centerTwoNav li a').removeClass('onstate');
		$(this).addClass('onstate');
		$('#contactWrapper div').hide();
		$("#"+this.name+"Box").show();
		return false;
	});
}

if(get('requestList')) {
	function andy(arr) { // this gets passed an array of boolean values
		var flag = false;
		for (var i=0; i < arr.length; i++) {
			if(arr[i] == true) {
				flag = true; // if any of this array are true, flag is set to true
			}
		};
		return flag;
	}
	function stars(){
		for (var i=0; i < callback.length; i++) {
			callback[i].className = ""; // for each element (label) in the array callback (assigned below), remove its class. removes all classes
		};

		if(get('reqTrip').className == "onstate") { // if reqtrip is on
			for (var i=0; i < eb.length; i++) {
				eb[i].className = "star"; // add the class star to all the labels in eb
			};
		}

		if(get('reqCallback').className == "onstate") { // if reqCallback is on
			for (var i=0; i < callback.length; i++) {
				callback[i].className = "star"; // add the class start to all the labels in reCallback
			};
		}
		$('#requestList span').each(function(i){ // for all the big buttons
			question[i] = false;
			if(this.className == "onstate") { // if it has the class onstate
				$('#requestForm ul').append(eval(this.id)); // this.id is a variable, eval writes out its contained string (declared below)
				question[i]  = true;
			}
		});

		if(andy(question) == true) {
			$('#requestForm').removeClass('fade');
			$('#reqSelectSomething').hide('fast');
			
			$('#youSelected').show();
		} else {
			$('#youSelected').hide();
			$('#requestForm').addClass('fade');
			$('#reqSelectSomething').show('fast');
		}
		return false;
	}
	
	function toggleReq() {
		if($('#reqPost').attr('class') == 'onstate') { // if the post option is on
			$('.addy label').not('#addressTwoLab').addClass('star'); // add the class 'star' to the postal address fields
			$('.addy').show(); // show the postal address fields
		}	else {
			$('.addy label').not('#addressTwoLab').removeClass('star'); //or, remove the stars
			$('.addy').hide(); // hide the fields
		}
	}
	
	$('#reqSelectSomething').hide();
	
	var eb = new Array(get('tripLab'));
	var callback = new Array(get('telLab'), get('tripLab'));
	var callbackTrip = new Array(get('telLab'), get('tripLab'));
	var reqTrip = "<li>an E-Brochure via Email</li>";
	var reqPost = "<li>a Print Brochure via Post</li>";
	var reqPdf = "<li>a PDF Brochure to Download</li>";
	var reqNewsletter = "<li>a Newsletter email Subscription</li>";
	var reqCallback = "<li>a telephone Callback</li>";
	var question = new Array();
	var star = new Array();
	var reqError = "<p id=\"reqError\">Please fill in all fields marked<br /> with an <span class=\"star\">asterix</span></p>";
	var reqConfirm = "<p id=\"confirmError\">Please check your email address is typed<br /> correctly in both fields</p>";
	//var reqSending = "<p id=\"reqSending\">Your request has been received!</p>";
	
	if($('#reqPost').attr('class') == 'onstate') {
		$('.addy').show();
	}	else {
		$('.addy').hide();
	}
	$('#requestList span').click(function(){ // click the big buttons at the top
		$('#requestForm ul').html(""); // clear the list at the bottom of what your going to recieve
		$(this).toggleClass('onstate'); // give the clicked button the class onstate so it looks depressed
		var targetHidden = this.id+"Hidden"; // eg reqPostHidden
		$("#"+targetHidden).val(($("#"+targetHidden).val() == "true")?"false":"true"); // if the value of that hidden input was false, then true etc.
		toggleReq();
		stars();
		return false;
	});
	
	$('#reqSubmit').click(function(){	// on submit...
		$('#requestForm label').each(function(i) {  // each label
			star[i] = false; 
			if(this.className == "star") { // if it has the class star
				theVal = $(this).next().next().val(); // set theVal to its inputs value
				if(theVal == "" || theVal == "-- Please Select --") { // if this value is nothing or please select
					star[i] = true; // set star[i] to  true
				}
			} 
		});
		if($('#reqSelectSomething').is(':visible')) { // if please select is visible
			return false; // die
		} else if(andy(star) == true) { // if any of the star array are true
			$('#reqMessageWrap').html(reqError); // write out the 'please fill out all the...' message
			return false;
		} else if($('#emailAddress').val() != $('#confirmEmail').val()) { // if both email fields don't match
			$('#reqMessageWrap').html(reqConfirm); // print out a message saying they bloody well should
			return false;
			
		} else {
			//alert($('#reqTripHidden').val()+"  "+$('#reqPostHidden').val()+"  "+$('#reqPdfHidden').val()+"  "+$('#reqNewsletterHidden').val()+"  "+$('#reqCallbackHidden').val());
			document.forms[0].submit();
		}
		
	});
	
	$('#tripLabSelect').change(function(){
		if($('#reqCallback').attr("class") == "onstate" && $('#reqTrip').attr("class") != "onstate" ){
			$('#focusLab').removeClass('star');
			$('#regionLab').removeClass('star');
			
			if($(this).val() == "-- Please Select --") {
				$('#focusLab').addClass('star');
				$('#regionLab').addClass('star');
			}
		}
	});
	
	stars();
	toggleReq();
}

if(get('mediaHomepage')) {	
	
	$('#lifeTripWrap .fieldArrow').hover(function(){
		$(this).children().attr("src", "/images/field/arrowOn.gif");
	}, function() {
		$(this).children().attr("src", "/images/field/arrow.gif");
		
	});
	
	function makeHover(me, thePath, theClass) {
		var theName = document.createElement('img');
		theName.src = thePath;
		theName.className = theClass;
		$(me).append(theName);
	}

	$('#mediaHomepage img').hover(function(){
		this.src = "/images/field/mediaCenterHomepageOn.gif";
	}, function() {
		this.src = "/images/field/mediaCenterHomepage.gif";

	});

	$('#lifeTripWrap .photo li a').not('.fieldArrow').each(function(i) { // adds non video hover to elements
		makeHover(this, "/images/field/normHover.png", "normHover");

	});	

	$('#lifeTripWrap .vid li a').not('.fieldArrow').each(function(i) { // adds video hover to elements
		makeHover(this, "/images/field/vidHover.png", "vidHover");		
	});

	$('#lifeTripWrap .fieldHover').each(function(i) {
		makeHover(this, "/images/field/vidHover.png", "fieldInner");  				// adds video hover to fieldHover elements	
	});

	$('#lifeTripWrap ul li a').not('.fieldArrow').hover(function() { 
		$(this).children('img:last').show();
	}, function () {
		$(this).children('img:last').hide();

	});

	$('#lifeTripWrap .fieldHover').hover(function(){
		$(this).children('img:last').show();
	}, function () {
		$(this).children('img:last').hide();

	});
}


if(get('costVarTable')) {
	
	$('#costVarForm select').each(function() {
		this.selectedIndex="0"; // set each select to its first option
	});
	pagePrices = new Object();
	$('#costVarForm select').each(function(){	// for each select
		var cost = new Object(); //create a new object called cost
		cost.GBP = $(this).children(':last').attr('GBP'); // grab the first options value of its attribute GBP
		cost.EUR = $(this).children(':last').attr('EUR'); // grab the first options value of its attribute EUR
		cost.AUS = $(this).children(':last').attr('AUS'); // grab the first options value of its attribute AUS
		cost.US = $(this).children(':last').attr('US'); // grab the first options value of its attribute US
		pagePrices[$(this).attr('name')] = cost; // set the new cost object as a new property of the object pageprices
	});
	
	function formatCurrency(number) { // format the any number in a currency format
		var number = number+"";
		number = number.replace(/(.*)(\.[0-9]$)/g , "$1$20");
		number = number.replace(/^([0-9]{1})([0-9]{3})(\.[0-9]{2}$)/g , "$1,$2$3");
		number = number.replace(/^([0-9]{2})([0-9]{3})(\.[0-9]{2}$)/g , "$1,$2$3");
		number = number.replace(/^([0-9]{3})([0-9]{3})(\.[0-9]{2}$)/g , "$1,$2$3");
		number = number.replace(/^([0-9]{1})([0-9]{3})$/g , "$1,$2.00");
		number = number.replace(/^([0-9]{2})([0-9]{3})$/g , "$1,$2.00");
		number = number.replace(/^([0-9]{3})([0-9]{3})$/g , "$1,$2.00");
		return number;
	};
	
	$('#costVarTable td span').each(function(){ // for every span in costVarTable
		number = $(this).html(); // create and set the variable 'number' as its value
		$(this).html(formatCurrency(number)); // format that number as a currency
	});
//	for (i in pagePrices) { // for each property in pagePrices
//		function total(meme, country) {
//			var orig = $(meme).html();
//			orig = orig.replace(/,/g, "");
//			orig = parseFloat(orig);
//			var amount = parseFloat(pagePrices[i][country]);
//			var newPrice = amount + orig;
//		 	newPrice = Math.round(newPrice*100)/100;
		//	alert(newPrice);
//			$(meme).html(formatCurrency(newPrice));
//		}
//		$('#costVarTable .totalGBP').children('span').each(function(){
//			total(this, "GBP");
//		});
//		$('#costVarTable .totalEUR').children('span').each(function(){
//			total(this, "EUR");
//		});
//		$('#costVarTable .totalAUS').children('span').each(function(){
//			total(this, "AUS");
//		});
//		$('#costVarTable .totalUS').children('span').each(function(){
//			total(this, "US");
//		});
//	};

	$('#costVarForm select').change(function(){
		var me = $(this).attr('name');
		if($(this).children(':selected').attr('price') == "off") {
			// find the currency values of its object (eg. spanish lessons) and subtract them from the values in the correct columns in the table
			for(var prop in pagePrices[me]) { //for each currency (defined as prop) in the changed object (eg. spanish lessons)
				var amount = pagePrices[me][prop]; // amount  = the value of that currency(prop)
				amount = parseFloat(amount);
				$('#costVarTable .total'+prop).each(function(){ // for each cell that contains the correct currency
						var orig = $(this).children('span').html();//find the ammount in the cell
						orig = orig.replace(/,/g, "");
					//	alert(orig);
						orig = parseFloat(orig); // convert it to a proper number
						var newPrice = orig-amount; //subtract the value of that currency from it
						newPrice = Math.round(newPrice*100)/100; //round it
					    $(this).children('span').html(formatCurrency(newPrice)); //put it back in the cell
				});
			}
		} else if($(this).children(':selected').attr('price') == "on") {
			// find the currency values of its object (eg. spanish lessons) and add them to the values in the correct columns in the table
			for(var prop in pagePrices[me]) { //for each currency (defined as prop) in the changed object (eg. spanish lessons)
				var amount = pagePrices[me][prop]; // amount  = the value of that currency(prop)
				amount = parseFloat(amount);
				$('#costVarTable .total'+prop).each(function(){ // for each cell that contains the correct currency
						var orig = $(this).children('span').html();//find the ammount in the cell
						orig = orig.replace(/,/g, "");
						orig = parseFloat(orig); // convert it to a proper number
						var newPrice = orig+amount; //add prop from it
						newPrice = Math.round(newPrice*100)/100; //round it
					    $(this).children('span').html(formatCurrency(newPrice)); //put it back in the cell
				
				});
			}
			
		} else {
			alert('Admin: Please Check Price Variables in each select option');
		}
	});
	
}

if(get('textTabs')) {
	$('#wrapRight').hide();
	$('#tabWrap a').click(function(){
		$('#tabWrap a').removeClass('on');
		if(this.id == "textLeft") {
			$(this).addClass('on');
			$('#tabWrap').css("background-image", "url(/images/search/tabLeft.gif)");
			$('#wrapLeft').show();
			$('#wrapRight').hide();
			return false;
		} else if(this.id == "textRight"){
			$(this).addClass('on');
			$('#tabWrap').css("background-image", "url(/images/search/tabRight.gif)");
			$('#wrapLeft').hide();
			$('#wrapRight').show();
			return false;
		}
	});
	
	$('#textBoxSubmit').hover(function(){
		this.src = "/images/global/arrows/littlerArrowRightOn.gif";
	}, function() {
		this.src = "/images/global/arrows/littlerArrowRight.gif";
		
	});
}
	
});

