jQuery.noConflict();

jQuery(document).ready(function($) {



	initInput = function(obj, str) {
		obj = $(obj);
		$(obj).attr("value", str);
		obj.addClass("disabled");
		obj.focus(function() {
			evtInputFocus(this,str);
		});
		obj.blur(function() {
			evtInputBlur(this,str);
		});
	}

	evtInputFocus = function(obj,str) {
		var txt = $(obj).attr("value");
		if (txt == "" || txt == str) {
			$(obj).attr("value", "");
		}
		$(obj).removeClass("disabled");
		$(obj).addClass("enabled");
	}

	evtInputBlur = function(obj,str) {
		var txt = $(obj).attr("value");
		if (txt == "" || txt == str) {
			$(obj).attr("value",str)
		}
		$(obj).removeClass("enabled");
		$(obj).addClass("disabled");
	}
	
	evtInputEnter = function(obj,key) {
		if (key.which == 13) {
			var submit = $(obj).parent("li").nextAll("li.submit").children("input");
			submit[0].click();
		}
	}	
	
	initEnterTrap = function() {
		var arrForms = $("fieldset input[type=text],input[type=password]")
		arrForms.each(function() {
			$(this).keypress(function(e) {
				evtInputEnter(this,e)
			});
		});
	}
	
	getParameterByName = function(name) {
      name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
      name = name.replace(" ", "-");
      var regexS = "[\\?&]"+name+"=([^&#]*)";
      var regex = new RegExp( regexS );
      var results = regex.exec( window.location.href );
      if( results == null )
        return "";
      else
        return results[1];
    }
    initPrepopulate = function() {
		var arrForms = $("div.contact-us-form input[type=text]")
		arrForms.each(function() {
			var name = $(this).parent("div").parent("div").attr("name");
			if (getParameterByName(name) != "") {
			    $(this).attr("value", unescape(getParameterByName(name)));
			}
		});
	}
	initPrepopulate();
	
	/* Contact Us */

	var strDefaultContactHelp = "Enter postcode"
	objDefaultContactHelp = $("div#contact-us fieldset ol li#contact-us-postcode input");
	initInput(objDefaultContactHelp, strDefaultContactHelp);

	/* Postcode Search */

	var strDefaultContactHelp = "Postcode (ie GL15 6HH)"
	objDefaultContactHelp = $("div#postcode-search li input[type=text]");
	initInput(objDefaultContactHelp, strDefaultContactHelp);

	$("div#postcode-search input").focus(function() {
		$("div#postcode-search input[type=image]").attr("src", "/images/btn_submit.gif");
	});
	$("div#postcode-search input").blur(function() {
		$("div#postcode-search input[type=image]").attr("src", "/images/btn_submit_search.gif");
	});
	
	/* Asset Download */

	objAsset = $("div#asset")
	objAssetForm = objAsset.find("div.form");
	objAssetFormFieldset = objAssetForm.find("fieldset");
	objAssetFormFieldset.addClass("fancy");

	var strDefaultAssetDownloadNameHelp = "Enter name";
	var objAssetFormName = objAssetForm.find("fieldset ol li#asset-download-form-name input");
	initInput(objAssetFormName, strDefaultAssetDownloadNameHelp);

	var strDefaultAssetDownloadEmailHelp = "Enter email";
	var objAssetFormEmail = objAssetForm.find("fieldset ol li#asset-download-form-email input");
	initInput(objAssetFormEmail, strDefaultAssetDownloadEmailHelp);

    var strDefaultAssetDownloadCompanyHelp = "Enter company";
	var objAssetFormCompany = objAssetForm.find("fieldset ol li#asset-download-form-company input");
	initInput(objAssetFormCompany, strDefaultAssetDownloadCompanyHelp);
	

	/* Open/Close */
	
	// Create/inject toggle objects

	if (!objAssetFormFieldset.hasClass("completed")) {
	
		objAssetForm.addClass("disabled");

		objAssetForm.before("<div class=\"introduction\"><a href=\"#\">Download now</a></div>")
		objAssetForm.prepend("<a href=\"#\">Close</a>");
		objAssetIntroduction = $("div#asset div.introduction")
	
		objAssetIntroduction.children("a").focus(function() {
			objAssetForm.removeClass("disabled");
			objAssetIntroduction.addClass("disabled");
			return false;
		});

		objAssetForm.children("a").focus(function() {
			objAssetForm.addClass("disabled");
			objAssetIntroduction.removeClass("disabled");
			return false;
		});
		
	}

	/* Supportive Employer List */
	
	var strDefaultKeywordHelp = "Keyword"
	objEmployerListKeyword = $("div#supportive-employer-list fieldset ol li#supportive-employer-listing-keywords input");
	
	var txt = $(objEmployerListKeyword).attr("value")
	if (txt == "Keyword") {
		(objEmployerListKeyword, strDefaultKeywordHelp);
	}
    
    initEnterTrap();

    /* Search focus */

    $("div#search input").focus(function() {
    	$("div#search input[type=image]").attr("src", "/images/btn_submit.gif");
    });
    $("div#search input").blur(function() {
    	$("div#search input[type=image]").attr("src", "/images/btn_submit_search.gif");
    });

	/* Section highlights */

	$("div#section-highlights").addClass("fancy");
	
	$("div#section-highlights > div.section-highlights-content.section1, div#section-highlights-navigation ul li.section1").addClass("selected");
	
	$("div#section-highlights-navigation ul li a").click(
		function () {
			$("div#section-highlights > div.section-highlights-content").removeClass("selected");
			$("div#section-highlights-navigation ul li").removeClass("selected");
			$(this).parent().addClass("selected");
			return false;
		}
	);
	$("div#section-highlights-navigation ul li.section1 a").click(
		function () {
			$("div#section-highlights > div.section-highlights-content.section1").addClass("selected");
		}
	);
	$("div#section-highlights-navigation ul li.section2 a").click(
		function () {
			$("div#section-highlights > div.section-highlights-content.section2").addClass("selected");
		}
	);
	$("div#section-highlights-navigation ul li.section3 a").click(
		function () {
			$("div#section-highlights > div.section-highlights-content.section3").addClass("selected");
		}
	);
	$("div#section-highlights-navigation ul li.section4 a").click(
		function () {
			$("div#section-highlights > div.section-highlights-content.section4").addClass("selected");
		}
	);
	
	/* Supportive employer application */
	
	initStepForm = function() {
		$("div.step-form fieldset").addClass("fancy");
		$("div.step-form ol li.step1").addClass("selected");
		
		$("div.step-form h2").click(
			function () {
				$("div.step-form ol li").removeClass("selected");
				$(this).parent().addClass("selected");
				return false;
			}
		);
		
		$("div.step-form ol li li.next").click(
			function () {
				$("div#supportive-employer-application ol li").removeClass("selected");
				$(this).parent().parent().next("li").addClass("selected")
				return false;
			}
		);
	}
	//initStepForm();


	evtPopOut = function(target) {
		if ($(target).hasClass("enabled")) {
			$(target).removeClass("enabled");
		} else {
			$(target).addClass("enabled");
		}
	}
	
	evtHighlightButton = function(target) {
		if ($(target).hasClass("enabled")) {
			$(target).removeClass("enabled");
		} else {
			$(target).addClass("enabled");
		}
	}	
	
	initSupportiveEmployerList = function() {
	
		$("div#supportive-employer-list").addClass("fancy");
		$("div#supportive-employer-list table tr > td:first-child > a:first-child").addClass("toggle");
		
		$("div#supportive-employer-list tr td a.org").toggle(
			function () {
				$(this).parent("td").parent("tr").addClass("selected");
			},
			function () {
				$(this).parent("td").parent("tr").removeClass("selected");
			}
		);

		var arrPopOutInits = $("div#supportive-employer-list table th a.information")

		arrPopOutInits.each(function() {
			var target = $(this).parents("th");
			$(this).mouseenter(function() {
				evtPopOut(target)
			});
			$(this).mouseleave(function() {
				evtPopOut(target)
			});
			$(this).focus(function() {
				evtPopOut(target)
			});
			$(this).blur(function() {
				evtPopOut(target)
			});
		});

		var arrHighlightButton = $("div.pagination .previous")

		arrHighlightButton.each(function() {
			var target = $(this).find("span");
			$(this).mouseenter(function() {
				evtHighlightButton(target)
			});
			$(this).mouseleave(function() {
				evtHighlightButton(target)
			});
			$(this).focus(function() {
				evtHighlightButton(target)
			});
			$(this).blur(function() {
				evtHighlightButton(target)
			});
		});
		
		var arrHighlightButton = $("div.pagination .next")

		arrHighlightButton.each(function() {
			var target = $(this).find("span");
			$(this).mouseenter(function() {
				evtHighlightButton(target)
			});
			$(this).mouseleave(function() {
				evtHighlightButton(target)
			});
			$(this).focus(function() {
				evtHighlightButton(target)
			});
			$(this).blur(function() {
				evtHighlightButton(target)
			});
		});

//		var arrHighlightButton = $("div.pagination ol li.previous")

//		arrHighlightButton.each(function() {
//			var target = $(this).find("a");
//			$(this).mouseenter(function() {
//				evtHighlightButton(target)
//			});
//			$(this).mouseleave(function() {
//				evtHighlightButton(target)
//			});
//			$(this).focus(function() {
//				evtHighlightButton(target)
//			});
//			$(this).blur(function() {
//				evtHighlightButton(target)
//			});
//		});		
//		
//		var arrHighlightButton = $("div.pagination ol li.next")
//		
//		arrHighlightButton.each(function() {
//			var target = $(this).find("a");
//			$(this).mouseenter(function() {
//				evtHighlightButton(target)
//			});
//			$(this).mouseleave(function() {
//				evtHighlightButton(target)
//			});
//			$(this).focus(function() {
//				evtHighlightButton(target)
//			});
//			$(this).blur(function() {
//				evtHighlightButton(target)
//			});
//		});		

	    evtHighlightButton = function(target) {
		    if ($(target).hasClass("enabled")) {
			    $(target).removeClass("enabled");
		    } else {
			    $(target).addClass("enabled");
		    }
	    }	

		
	}
	initSupportiveEmployerList()

	/* Section modules */
	
	$("div#modules").addClass("fancy");
	
	$("div.l1-landing div.fancy div.module").append("<div class='bg'></div>").find(".bg").css("opacity","0.9");
	
	$("div#modules div.module-collection div.module").hover(
		function () {
			$(this).addClass("selected")
		}, 
		function () {
			$(this).removeClass("selected")
		}
	);
	
		$("div#modules div.module-collection div.module a").focus(function () {
			$(this).parent().parent().parent().parent().addClass("selected")
		});
		$("div#modules div.module-collection div.module a").blur(function () {
			$(this).parent().parent().parent().parent().removeClass("selected")
		});
	
	/* Checklist */
	
	$("div#checklist").addClass("fancy");
	
	$("div#checklist li.choice span").click(
		function () {
			$(this).siblings().removeClass("selected")
			$(this).addClass("selected");
			$(this).find("input").attr("checked", true);
		}
	);
	
	$("div#checklist li.choice span input").focus(
		function () {
			$(this).parent().siblings().removeClass("selected")
			$(this).parent().addClass("selected");
		}
	);
	
	/* Checklist */
	
	$("div#profile-reservist, div#profile-employer").addClass("fancy");
	
	$("div#profile-reservist h2, div#profile-employer h2").toggle(
		function () {
			$(this).parent().addClass("selected");
		},
		function () {
			$(this).parent().removeClass("selected");
		}
	);
		
	
	/* Homepage sIFR links */
	
	/* MAGIC! */
	
	$("div#site-features").addClass("fancy");
	
	/* IE6 hacks */
	
	if ($.browser.msie && $.browser.version <= 7) $("div#primary-content > p:first-child, div#primary-content > div#related-content + p, div#primary-content > h2:first-child, div#primary-content > div#related-content + h2").css("margin-top","0");

	if (jQuery("div.content-carousel").length > 0)
	{
		var contentcarousel = new ContentCarousel();
		var _contentcarouselproperties = {
			itemwidth: 505,
			scroller: contentcarousel
		}
		contentcarousel.init(_contentcarouselproperties);
	}

	if (jQuery("div.image-carousel").length > 0)
	{
		var imagecarousel = new ImageCarousel();
		var _imagecarouselproperties = {
			itemwidth: 505,
			scroller: imagecarousel
		}
		imagecarousel.init(_imagecarouselproperties);
	}
	
});