
//Print funcitonality
function printContent(){
	if( $('body').hasClass('e_modalEnabled') ){
		ele.print.elem( $('.jqmWindow:visible .mdlTarget') )
	}else{
		ele.print.elem( $('.e_contentMainWrapper') )
	}
}

function dEscrollTo(myTarget) {
	$('body').find('a[name*=' + myTarget +']').attr("id",myTarget);
	var targetOffset = $('#'+myTarget).offset().top;
	if ( targetOffset==0 ) { targetOffset = $target.parent().offset().top; }
	$('html, body').animate({scrollTop: targetOffset}, 700, 'swing', function(){  } );
}



$(document).ready(function(){

	$(".ele-MyServices-Summary .e_columns .e_row2").children('td').eq(0).html('<ul class="e_notes"><li><a href="index.cfm?event=base:myservices">All purchased templates</a></li></ul>');

	// Add autocomplete with JS as it's not valid HTML
	$("#searchstringForm").attr("autocomplete","off");
	
	/*****************************************************
	 * 
	 * Where: 			General javascript code and functionality
	 * 
	 *****************************************************/
	 
	
	//Login box
	var loginboxInputs = $('#ele-loginBox .e_username INPUT, #ele-loginBox .e_password INPUT');

	loginboxInputs.bind('focus', function(){
		$(this).addClass('e_hideLabel')
	});

	loginboxInputs.bind('blur', function(){
		if( $(this).val() == '' ){
			$(this).removeClass('e_hideLabel');
		}
	});
	
	//Chnge all the href="#" to href="javascript:void(0)"
	// $('a[href="#"], a[href=""]').attr('href', 'javascript:void(0)');
	
	if( $('body.e_body_myServicesTemplates').length ){		
		$(".e_catsToggle").bind("click",function(){
			var targetContent = $("#content_" + $(this).attr("rel"));
			var targetTitle = $("#title_" + $(this).attr("rel"));
			// var targetIDIsHidden = targetID.is(':hidden');		
			// $(".e_objectCats").slideUp("fast");
			// if (targetIDIsHidden) { targetID.slideDown("fast"); }
			targetContent.slideToggle("fast");			
			targetTitle.toggleClass('e_open');			
		});	
	}

	//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 .e_head .e_right').bind('mouseenter', function(){			
			//Add 'e_on' class and a mouseLeave event (to remove the 'e_on' class) to DIV.e_right
			$(this)
			.addClass('e_on')			
			//Add 'e_on' class and a mouseLeave event (to remove the 'e_on' class) to Root element of the box
			$(this).parents(".e_services")
			.addClass('e_on')
			.one('mouseleave', function(){
				$(this).removeClass('e_on')
				$(this).find('.e_head .e_right').removeClass('e_on');
			});
		});
	}

	// 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"); }
			}
		}
	});

	
	

	/*****************************************************
	 * 
	 * Where: 			Page dedicated functionality
	 * 
	 *****************************************************/
	
	// Smooth scrolling
	//Source: http://www.learningjquery.com/2007/09/animated-scrolling-with-jquery-12
	$('.e_categoriesList a[href*=#], .toc a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
		&& location.hostname == this.hostname) {
		  var $target = $(this.hash);
		  $target = $target.length && $target
		  || $('[name=' + this.hash.slice(1) +']');
		  if ($target.length) {
			var targetOffset = $target.offset().top;
			// fix for chrome 12 which can't find empty elements
			if ( targetOffset==0 ) { targetOffset = $target.parent().offset().top; }
			$('html, body')
			.animate({scrollTop: targetOffset}, 700, 'swing', function(){  } );
		   return false;
		  }
		}
	});


	//Product page - Show/hide Rapidocs description
	$('#e_page.ele-product').find('.e_productObjects .e_object .e_name').bind('click', function(){
		//Hide all already visible descriptions
		$(this).parent().siblings('.e_object').find('.e_desc:visible').hide();
		
		//Show the appropriate description
		$(this).siblings('.e_desc').toggle()
	})
	
	var searchTextDe = "Search documents and law guide";
	
	$("#searchstring").focus(function() { 
		if ( $(this).val() == searchTextDe) { $(this).val(""); }
		$(this).addClass('e_on');
	});
	$("#searchstring").blur(function() { 
		if ( $(this).val() == "") {	$(this).val(searchTextDe); }
		if ( $(this).val() == searchTextDe) { $(this).removeClass('e_on'); }		
	});

	// Drop-down menus
	$("#nav a.hasSubNav").mouseover(function() {
		if($.browser.msie && $.browser.version=="6.0"){ $('#nav UL LI .subNavWrapper').bgiframe(); }
		$(this).parent().find("div.subNavWrapper").show();
		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("div.subNavWrapper").hide();
		});
		}).hover(function() { 
			$(this).addClass("subhover");
		}, function(){
			$(this).removeClass("subhover");
	});

	/* message form */
	$(".ele-messageForm DIV.e_accountDetails UL LI.e_messageTextItem TEXTAREA").focus(function() { 
		if ( $(this).val().indexOf("Please enter your message here.") >= 0) { $(this).val(""); }
		$(this).addClass('e_on');
	});
	$(".ele-messageForm DIV.e_accountDetails UL LI.e_messageTextItem TEXTAREA").blur(function() { 
		if ( $(this).val() == "") { $(this).val("Please enter your message here."); }		
		if ( $(this).val().indexOf("Please enter your message here.") >= 0) { $(this).removeClass('e_on'); }
	});
	
	$(".ele-messageForm DIV.e_accountDetails UL LI.e_submit INPUT").bind("click",function(){
		var thisTextarea = $(".ele-messageForm DIV.e_accountDetails UL LI.e_messageTextItem TEXTAREA");
		if ( $(thisTextarea).val().indexOf("Please enter your message here.") >= 0) { $(thisTextarea).val(""); }
	});
	
	/* FAQ page Question / Answer open close functionality */
	$('.e_wrapper .e_faq_answers h3.e_title a').click(function() {
		var thisAnswer = $(this).parents("li").children("div.e_content");
		var thisAnswerIsHidden = thisAnswer.is(':hidden');		
		$("div.e_faq_answers div.e_content").slideUp("fast");
		if (thisAnswerIsHidden) { $(this).parents("li").children("div.e_content").slideDown("fast"); }
	});
	
	if( $('body.myServices').length ){		
		$('.e_list .e_documents .e_document .e_dragger').bind('mouseenter', function(){
			$(this).addClass('e_draggerOn')
			.one('mouseleave', function(){
				$(this).removeClass('e_draggerOn')
			})
		});
		
		
		$(".e_document .e_name").each(function() {
			// Show original document name if it is different from current name
			var currentDocName = $(this).find(".e_disabled");
			var originalDocName = $(this).find(".e_originalName");
			if ("("+currentDocName.text()+")" != originalDocName.text() ) {
				originalDocName.show();
			} 
		});
	
	
	}	
	
	$('#RegisterFormCustomerBusinessType').bind("change", function() {
		var businessTypeValue = $('#RegisterFormCustomerBusinessType').val();
		if ( businessTypeValue=="Other (please specify)" ) {
			$('#RegisterFormCustomerGeneric2').val("");
			$('#RegisterFormBusinessTypeOther').slideDown("fast");
		}
		else {
			$('#RegisterFormBusinessTypeOther').hide();
			$('#RegisterFormCustomerGeneric2').val(businessTypeValue);
		}
	});
	
	$('.e_customerLeadGenerator input').bind("change", function() {
		if ( $(this).val()==1 ) {
			$('#e_customerLeadGeneratorNameRow,#e_customerLeadGeneratorCodeRow').slideDown("fast");
		}
		else {
			$('#e_customerLeadGeneratorNameRow,#e_customerLeadGeneratorCodeRow').hide();
		}
	});
	
	$('.e_customerBankCustOnly input').bind("change", function() {
		if ( $(this).val()==1 ) {
			$('#e_customerBankCustOnlyNameRow,#e_customerBankCustOnlyCodeRow').slideDown("fast");
		}
		else {
			$('#e_customerBankCustOnlyNameRow,#e_customerBankCustOnlyCodeRow').hide();
		}
	});
	
	$('#e_pauseA').bind("click", function() {
		$('#e_pause').hide();
		$('#e_resume').show();
		$('#boxFeedbackSlides').cycle('pause'); 
	});
	
	$('#e_resumeA').bind("click", function() {
		$('#e_resume').hide();
		$('#e_pause').show();
		$('#boxFeedbackSlides').cycle('resume'); 
	});
	
	
});

function checkSearch() {
	var searchStr = document.forms['searchstringForm'].searchstring.value;
	if (searchStr == "" || searchStr == "Search documents and law guide") { alert('Please enter a search term before proceeding.');  return false; }
	else { return true; }
}


/*
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}
$.preloadImages("/la/images/head_drop_shade_middle_238x900_01.png", "/la/images/head_drop_shade_bottom_230x10_01.png", "/la/images/list-square-darkblue.gif");
*/
