/**
 * @author: Chris Goodchild [chris.goodchild@designuk.com]
 * @copyright: 2009 Design UK
 * @description: BLP Scripts
 * @version: 1.0
 */
try {
	var $ = jQuery.noConflict();
}
catch(e) {
	alert('JQuery needs to be loaded first');
}

if (!BLP) {
	var BLP = {};
	var navRetractId = 0;
}

BLP.Navigation = {
	// Methods
	Retract: function() {
		clearTimeout(navRetractId)
		$("ul.second_level, ul.top_level > li > div.surround_why_blp").hide();
		$("ul.top_level > li > a").removeClass("selected");
	},
	Navigation: $(function () {
		// click event to toggle sub <ul> elements
		$("ul.top_level > li > a")
			.click(function(event) {
				event.preventDefault();
				if (!$(this).next().is(":visible")) {
					$("ul.second_level, ul.top_level > li > div.surround_why_blp").hide();
					$("ul.top_level > li > a").removeClass("selected");
					$(this).addClass("selected");
					if ($(this).next("ul").length > 0) {
						$(this).next("ul").slideDown("fast");
					} else {
						$(this).next().show();
					}
				} else {
					$(this).removeClass("selected");
					$(this).next().hide();
				}
			});
		$("ul.top_level").hover(
			function() {
				clearTimeout(navRetractId);
			},
			function() {
				navRetractId = setTimeout("BLP.Navigation.Retract()", 3000);
			}
		);
		$("ul.second_level > li").hover(
			function() {
				$(this).addClass("over");
				$(this).prev().attr("style", "background:#fff");
			},
			function() {
				$(this).removeClass("over");
				$(this).prev().removeAttr("style");
			}
		);
	})
}
	
	
	
	
BLP.LeftNav = {
	LeftNav: $(function() {
		// Hide all sub-level <ul> elements
		$("ul.left_nav li ul").css({"display" : "none"});
		$(".section_links ul li ul").css({"display" : "none"});
		
		// Setup a loop through all links under .section_links ul
		$(".section_links ul li a").each(function() {
			
			var linkRef = $(this).attr("href");
			
			// "pageLoc" is our url page ID that we're grabbing from the theme
			// now, take our link's href and compare it to this ID - if it doesn't match, nothing will happen
			if (linkRef.indexOf(pageLoc) != -1) { 
				// Add our classes and display and child <ul> elements
				$(this).parent().addClass("current");
				$(this).next("ul").css({"display" : "block"});
				$(this).parents("ul").css({"display" : "block"});
				
				if($(this).parent().children("ul").length > 0){
					$(this).parent().removeClass("current")
					$(this).parent().addClass("activeOpen");
				}
				
				$(this).parent().parents("li").addClass("open");
			}
		})		
		// Setup a loop through all links under ul.left_nav
		$("ul.left_nav li a").each(function() {
			
			var linkRef = $(this).attr("href");
			
			// "pageLoc" is our url page ID that we're grabbing from the theme
			// now, take our link's href and compare it to this ID - if it doesn't match, nothing will happen
			if (linkRef.indexOf(pageLoc) != -1) { 
				// Add our classes and display and child <ul> elements
				$(this).parent().addClass("active");	
				
				$(this).next("ul").css({"display" : "block"});	
				
				$(this).parents("ul").css({"display" : "block"});
				
				if($(this).parent().children("ul").length > 0){
					$(this).parent().removeClass("active")
					$(this).parent().addClass("activeOpen");
				}
				
				$(this).parent().parents("li").addClass("open");
			}
		})
	})
}


BLP.Tabs = {
	// Methods
	Tabs: $(function () {			  
		// elements to act upon:
		var $tabLinks = $("ul.tabs li a");
		
		// on load check for an "element" in the url, if it's there, run a highlight script for the right tab:
		var myLoc = window.location.href;
		if (myLoc.indexOf("element") != -1) {
			if(/element(\/|=)\d+/i.test(myLoc)) {	// if there is an element specified correctly in the url...
				$tabLinks.each(function(i) {		// for each tab link...
					elementString = /element(\/|=)\d+/i.exec(myLoc);	// firstly, grab 
					element = /\d+/.exec(elementString[0]);
					if($(this).attr("href").indexOf(element[0]) != -1) {	// check if the link matches the element
						$(this).parent().addClass("selected");
					} else {
						$(this).parent().removeClass("selected");
					}
				});
			}
		}		

		
		$(".loading").ajaxStart(function () {
			$(this).show();
			$("#tab-content").hide();				
		}).ajaxStop(function () {
			$(this).hide();
			$("#tab-content").show();
		});
		
		//$("ul.tabs li a").click(function () {
		$tabLinks.click(function () {
			
			if ($(this).attr("href").indexOf("index.cfm?") != -1) {
				// if it's a "traditional" link:
				var $loadElem = $(this).attr("href") + "&ajax=true";
			} else {
				// else it's an "sef" link:
				var $loadElem = $(this).attr("href") + "/ajax/true";
			}
			
			if (!$(this).parent().hasClass("selected")) {
				$("ul.tabs li").removeClass("selected");
				$(this).parent().addClass("selected");				
				$("#tab-content").load($loadElem, function() {
	/* This has been added due to deleted elements and as a result an error page being brought in which changes the style of a page */		
	/* Take the content of the ajax call element and look to see if the length of element style is greater than 0 */
						if($("#tab-content style").length > 0)	{		
    /*Delete the style element if it exists. */
						   $("#tab-content style").remove();	
						}
				});

			}
			return false;
		});
	})
}

/*	Clear the homepage Lawyer search box
	-------------------------------------------------- */

	$(function () {
		$(".lawyer_selector input, .lawyer_name input, #site_search input").each(function(i) {
			this.data = $(this).attr("value");		
			$(this).focus(function () {
				if ($(this).attr("value") == this.data) {
					$(this).attr("value", "");
				}
			});
			$(this).click(function () {
				if ($(this).attr("value") == this.data) {
					$(this).attr("value", "");
				}
			});
			$(this).blur(function () {
				if ($(this).attr("value") == "") {
					$(this).attr("value", this.data);
				}
			});	
																						  
		});
	});
	
/*	Homepage Lawyer search Hide/Show button (05.05.2010)
	-------------------------------------------------- */
	$(function () {
		$('.js-enabled .lawyer_selector h1').click(
			function () {
				if ($(this).parent().find("form").hasClass('form_on')){
					$(this).parent().find("form").removeClass('form_on')
					} else {
						$(this).parent().find("form").addClass('form_on');				
					}
			}
		);
	});