$(document).ready(function(){

	//Event Delegationg
	$(document).click(function(e){
		var target = $(e.target);		
	})
	
	//Toggle display for Product description
	$('.products .product .info A').bind('click', function(){
		var pid = $(this).parents('.product').attr('pid');
		$('.products .desc[pid='+ pid +']').toggle()
	});
	
	//Toggle display for iContent boxes
	$('.icontent .item .head .button').bind('click', function(){
		$(this).parents('.item').children('.text').toggle()
	});
	
	//Change all the href="#" to href="javascript:void(0)"
	$('a[href="#"], a[href=""]').attr('href', 'javascript:void(0)');
	
	if($('#ele-loginBox').length>0 || $('#hj_search').length>0){
		var a=[{elem:$('#ele-loginBox').find('#username'),defaultmsg:'Username'},{elem:$('#ele-loginBox').find('#password'),defaultmsg:'Password'}],msg,elem;
		for(var i=0;i<a.length;i++){
			(function(elem,msg,type){
				var ilabel=$('<input class="input-inline-label" />');
				ilabel.val(msg).hide().insertAfter(elem);
				if(elem.val()==''){
					elem.hide();
					ilabel.show();
				}
				ilabel.bind('focus',function(){
					ilabel.hide();
					elem.show().focus();
				});
				elem.bind('blur',function(){
					if(elem.val()==''){
						elem.hide();
						ilabel.show();
					}
				});
			})(a[i].elem,a[i].defaultmsg,a[i].elem.attr('type'));
		}
	}
	
	$("#searchstring").focus(function() { 
		if ( $(this).val() == "Search our website") 
		$(this).val(""); 
	});
	$("#searchstring").blur(function() { 
		if ( $(this).val() == "") 
		$(this).val("Search our website"); 
	}); 

	
	/*
	for (var i = 0; i < $(".validationErrors").length; i++) {
		var valtext = $(".validationErrors:eq("+i+")").text()
		if (valtext != "") { $(".validationErrors:eq("+i+")").css("display", "block"); }
	}
	*/
	
	/* Browser detection - adds body class */
	var a=navigator.userAgent.toLowerCase();$.browser.chrome=/chrome/.test(navigator.userAgent.toLowerCase());if($.browser.msie){$('body').addClass('browserIE');$('body').addClass('browserIE'+$.browser.version.substring(0,1))}if($.browser.chrome){$('body').addClass('browserChrome');a=a.substring(a.indexOf('chrome/')+7);a=a.substring(0,1);$('body').addClass('browserChrome'+a);$.browser.safari=false}if($.browser.safari){$('body').addClass('browserSafari');a=a.substring(a.indexOf('version/')+8);a=a.substring(0,1);$('body').addClass('browserSafari'+a)}if($.browser.mozilla){if(navigator.userAgent.toLowerCase().indexOf('firefox')!=-1){$('body').addClass('browserFirefox');a=a.substring(a.indexOf('firefox/')+8);a=a.substring(0,1);$('body').addClass('browserFirefox'+a)}else{$('body').addClass('browserMozilla')}}if($.browser.opera){$('body').addClass('browserOpera')}

	// Check for L3 LawGuide items with one child and re-direct href to single child href
	$("#lawguide a.level3").each(function() {
		// Only change visible links with only one child
		if ( $(this).parent('LI').children('UL').children('LI').size() == 1 && $(this).parent('LI').parent('UL').is(':visible')) {
			$(this).addClass("singlechild");
			var newLocation = $(this).parent('LI').children('UL').children('LI').children('A').attr('href');
			$(this).attr('href', newLocation);
			$(this).parent('LI').children('UL').hide();
			if ( $(this).hasClass("open") ) { $(this).addClass("selected"); }			
		}	  
	});
	// Open and close L3 LawGuide submenus if not a single child submenu
	$("a.level3","#lawguide").bind("click",function(){
		// Don't change click action if href has been re-written for single child items
		if ( !$(this).hasClass("singlechild") ) {
			var lgClicked = $("#lawguide-"+this.id.split("-")[1]+"-children");			
			if (lgClicked.is(':hidden')) { 
				lgClicked.slideDown("fast");
				$(this).addClass("clicked");
				if ( $(this).hasClass("open") ) { $(this).removeClass("unclicked"); }
			}
			else {
				lgClicked.slideUp("fast");
				$(this).removeClass("clicked");
				if ( $(this).hasClass("open") ) { $(this).addClass("unclicked"); }
			}
		}
	})
	
	//Services box Open/close functionality
	if( $('.e_sidebar .e_services').length ){
		
		//Add '.selected' class to selected L1 LI (gets subsiteId from the URL)
		var url = window.location.toString()
		//must allow format of subsite= and node=A(followed by subsiteid)
		var subsiteIndex = url.lastIndexOf('subsite=') + 8;
		if(subsiteIndex==7)
			var subsiteIndex = url.lastIndexOf('node=A') + 6;
		var subsiteId = url.substr(subsiteIndex, 5);
		// $('#collection').find('LI#li-' + subsiteId).addClass('selected')
		
		
		$('.e_sidebar .e_box.e_services.e_closed').bind('mouseenter', function(){
			
			//Add 'e_on' class and a mouseLeave event (to remove the 'e_on' class) to DIV.e_right
			$(this).find('.e_head .e_right')
			.addClass('e_on')
			
			//Add 'e_on' class and a mouseLeave event (to remove the 'e_on' class) to Root element of the box

			$(this)
			.addClass('e_on')
			.one('mouseleave', function(){
				$(this).removeClass('e_on')
				$(this).find('.e_head .e_right').removeClass('e_on');
			})

		})
	}
	
	
	/*****************************************************
	 * 
	 * Where: 			View page "basketFirmRule2.cfm"
	 * What: 			Validation
	 * Created:			KA 13 May 2008
	 * Last changed: 	KA 13 May 2008
	 * 
	 * Comments:
	 * 
	 *****************************************************/
	
	$('.ele-basketFirmRule2 .e_nav FORM').bind('submit', function(){
		var acceptTerms = $(this).find('.e_acceptTerms');
		
		if( acceptTerms.find('.e_acceptCheckbox').is(':checked') ){
			return true
		}else{
			alert("You need to accept the terms & conditions");
			acceptTerms.addClass('e_validationError')
			return false;
			
		}
	})
	
	/*****************************************************
	 * 
	 * Where: 			Product page (product.cfm)
	 * What: 			Show/hide Rapidocs description
	 * Created:			KA 29 May 2008
	 * Last changed: 	KA 29 May 2008
	 * 
	 * Comments:
	 * 
	 *****************************************************/
	$('.ele-product').find('.e_productObjects .e_object .e_name').bind('click', function(){
		
		$(this).siblings('.e_desc').toggle()
	})
	
	
})

function checkSearch() {
	var searchStr = document.forms['searchstringForm'].searchstring.value;
	if (searchStr == "" || searchStr == "Search our website") { alert('Please enter a search term before proceeding.');  return false; }
	else { return true; }
}


function openThisWin(thisLink,winName) { 
	thisNewWin = window.open(thisLink,winName,"width=800,height=525,menubar=no,toolbar=no,location=no,directories=no,scrollbars=yes,status=yes,resizable=yes");
	thisNewWin.focus();
	return false;
}