// window.onerror = function() { return true; } scripts.js

var pinned = false;

$(document).ready(function()
{		
	/*Search Button Submit*/
	$('div #contactUsDropdown').keydown(function(e) {
		if (e.keyCode == 13){
			$('.btPartnerSr').click();
			return false;
		}
		else{return true;}
	});	

/*Top Nav Functionality*/
	
	/*Careers menus*/
	$('#zoneCareersHeader .TabControlTable a').mouseover(function(){Cufon.refresh();});
	
	/*Top nav cufon*/
	$('#topMenu li:not([class^="tier"])').attr('onmouseover', 'Cufon.refresh();').attr('onmouseout', 'Cufon.refresh();');
		
	/*Click to pin*/
	$('body').click(function() {
		hidePopup();
		if(pinned)
			pinned = false;
		Cufon.refresh();
	});
	
	/*Mouse hover enter actions*/
	$('#PrimaryNavigation span[class^="tier1CMSListMenuLink"]').parent().mouseenter(function(event){
		if(!pinned){
			$(this).children('ul').toggle(true);
			$(this).toggleClass('active', true);
			Cufon.refresh();
			event.stopPropagation();
		}
	});
	
	/*Mouse hover leave actions*/
	$('#PrimaryNavigation span[class^="tier1CMSListMenuLink"]').parent().mouseleave(function(event){
		if(!pinned){
			$(this).children('ul').toggle(false);
			$(this).toggleClass("active", false);
			Cufon.refresh();
			event.stopPropagation();
		}
	});
	
	/*Top nav click actions*/
	$('#PrimaryNavigation span[class^="tier1CMSListMenuLink"]').parent().click(function(event){
		
		var same = false;
		if(this.className.indexOf('active') != -1)
			same = true;
			
		/*Do this to check if we're toggling the same tab. Things execute twice, so this makes sure that it gets hidden*/
		if ($('#PrimaryNavigation li').hasClass('active')) {
			/*Removes the active class from everything except for the currently selected item*/
			$('#PrimaryNavigation li').not($(this)).removeClass('active')
			/*Selects all the second tier ULs except for the currently selected item, and hides them*/
			$('#PrimaryNavigation li ul.tier2CMSListMenuUL').not($(this).children("ul")).hide();
		}

		/*hide contact us pane if it's open*/
		if ($('#HeaderRight ul.utilityNav li.contactUs a.contactUsLink').hasClass('contactUsRoundTab')) {
			showHidePanel(this,'ctl00_Panel1');
		}
			
		if(pinned){
			/*Hide/show all items*/
			$(this).children("ul").toggle();
			$(this).toggleClass("active");
			Cufon.refresh();
			event.stopPropagation();
			if(same)
				pinned = false;
		}
		else
		{
			Cufon.refresh();
			event.stopPropagation();
			pinned = true;
		}
	});
	
	function hidePopup() {
		$('#PrimaryNavigation li').removeClass('active')
		$('#PrimaryNavigation li ul.tier2CMSListMenuUL').hide();
	}
	
	
	/* This code creates the 3 column setup for the Services dropdown menu*/
	var items = $('#topMenu li.Services .tier2CMSListMenuUL .topNavConsultingServices li');
	var colCount = 0;
	if( items.length % 3 == 0)
	{
		colCount = Math.floor(items.length / 3);
	}
	else if( items.length % 3 == 2)
	{
		colCount = Math.floor(items.length / 3 + 1);
	}
	else
	{
		colCount = Math.floor(items.length / 3);
	}
	
	/*get the consulting services items
	 *get the first X elements and wrap them in a new UL
	 *Select the UL we just created
	 *Add the first X before it's sibling UIL. Grab the next X (taking into account that you just added X before), and add them after*/
	$($('#topMenu li.Services .tier2CMSListMenuUL .topNavConsultingServices li')
		.slice(0,colCount))
		.wrapAll('<ul class="tier3CMSListMenuUL colFirst"></ul>')
		.parent()
		.insertBefore($('#topMenu li.Services .tier2CMSListMenuUL .topNavConsultingServices ul')
		.first());
	$($('#topMenu li.Services .tier2CMSListMenuUL .topNavConsultingServices li')
		.slice(colCount*2))
		.wrapAll('<ul class="tier3CMSListMenuUL colLast"></ul>')
		.parent()
		.insertAfter($('#topMenu li.Services .tier2CMSListMenuUL .topNavConsultingServices ul')[1]);
	
	//Set About MA columns
	setColumns("#topMenu li.AboutMossAdams");
	
	//Set Industries columns
	setColumns("#topMenu li.Industries");
		
	/*This sets the seperator height for all columns*/
	var max = 0;
	$('#topMenu li.Services .tier2CMSListMenuUL').css('display','block').css('visibility','hidden');
	$('li.Services .tier3CMSListMenuUL').each(function() {
		max = Math.max($(this).innerHeight(), max);
	}).css('min-height', max + 'px' );
	$('#topMenu li.Services .tier2CMSListMenuUL').css('display','none').css('visibility','visible');
	
	/*Start Left nav dropdown functionality*/
	$('li[class^="tier1CMSListMenuHighlightedLI"] img').addClass("active");
	
	$('li[class^="tier1CMSListMenu"] img').click(function(){
		$(this).siblings("ul").toggle();
		$(this).toggleClass("active");
		
		var leftnavheight;
		leftnavheight = $(".zoneLeft").height();
		$("#zoneMiddle .zoneCenter").css("min-height",leftnavheight);
	});
	/*End Left nav dropdown functionality*/
/*End Top Nav functionality*/

	/*Date Picker*/
    $(function()
    {
        $("input[id*=txtDateStart]").datepicker();
        $("input[id*=txtDateEnd]").datepicker({ minDate: '+1d' });
    });
	
	/*Left Nav resizing*/
	$(function()
	{
		var leftnavheight;
		leftnavheight = $(".zoneLeft").height();
		$("#zoneMiddle .zoneCenter").css("min-height",leftnavheight);
	});
	
	/*Clears search box*/
	$(function clearSearch() {
		var pageTitle = document.title;
		var searchText = "";
		var findPartner = "Enter a Name";
		
		searchText = "Search Moss Adams";
				
		// Sets the text box text
		$("#plc_lt_zoneTopMenu_SearchBox_txtWord").val(searchText);
		$("#plc_lt_contactcontrol_tbPartnerSr").val(findPartner);
		// Define what happens when the textbox comes under focus
		// Clear the box
		$("#plc_lt_zoneTopMenu_SearchBox_txtWord, #plc_lt_contactcontrol_tbPartnerSr").focus(function() {
			$(this).filter(function() {
				// We only want this to apply if there's not
				// something actually entered
				return $(this).val() == "" || $(this).val() == searchText || $(this).val() == findPartner;
			}).val("").css("color","#000000");
		});
		// Define what happens when the textbox loses focus
		// Add the default text
		$("#plc_lt_zoneTopMenu_SearchBox_txtWord").blur(function() {
			$(this).filter(function() {
					// We only want this to apply if there's not
					// something actually entered
					return $(this).val() == "";
			}).val(searchText).css("color","#999999");
		});
		$("#plc_lt_contactcontrol_tbPartnerSr").blur(function() {
			$(this).filter(function() {
					// We only want this to apply if there's not
					// something actually entered
					return $(this).val() == "";
			}).val(findPartner).css("color","#999999");
		});
	});
});


/* Set html for 2 columns */
function setColumns(topClass)
{
	items = $(topClass + ' .tier2CMSListMenuUL .tier2CMSListMenuLI,' + topClass + ' .tier2CMSListMenuHighlightedLI,' + topClass + ' .Careers');
	colCount = Math.floor(items.length / 2);

	if(items.length % 2 != 0)
	{
		colCount++;
	}
	
	//alert("items: " + items.length + " colCount: " + colCount);
	items.addClass("tier2_5CMSListMenuLI").removeClass("tier2CMSListMenuLI");
	
	/*wrap these in specific uls and ils*/
	$(topClass + ' .tier2CMSListMenuUL .tier2_5CMSListMenuLI').wrapAll('<ul class="tier2_5CMSListMenuUL colLast"></ul>');
	$(topClass + ' .tier2_5CMSListMenuUL').wrapAll('<li class="tier2CMSListMenuLI"></li>');
		
	$($(topClass + ' .tier2_5CMSListMenuLI').slice(0,colCount)).wrapAll('<ul class="tier2_5CMSListMenuUL colFirst"></ul>').parent().insertBefore($(topClass + ' .tier2CMSListMenuLI ul').first());
	
}



function showHidePanel(link, panelId) {
	panel = document.getElementById(panelId);
	if (panel.style.display == "block") {
		panel.style.display = "none";
		$('#HeaderRight ul.utilityNav li.contactUs a.contactUsLink').toggleClass('contactUsRoundTab');
	}
	else {
		panel.style.display = "block";
		$('#HeaderRight ul.utilityNav li.contactUs a.contactUsLink').toggleClass('contactUsRoundTab');
	}
}

// Used by Code to generate the URL to the bizform. 03/29/11
function AskASpecialist(target)
{
    target = document.getElementById(target);

    if (target.selectedIndex >= 0)
    {
		$('#spnNoSelection').hide();
		
        window.location = '/Ask-a-Specialist/?Industry=' + encodeURIComponent(target.options[target.options.selectedIndex].value);
    }
    else
    {
        $('#spnNoSelection').show();
    }
}

function ClearInputs(){
	$(':input','#formContainer')
	.not(':button, :submit, :reset, :hidden')
	.val('')
	.removeAttr('checked')
	.removeAttr('selected');
}
	/* Used for autosetting tab widths
	$(function() {
		var containerWidth = $('.TabControlTable').width();
		var numItems = $('.TabControlTable td').length/3;
		var maxContentWidth = containerWidth / numItems;
		
		//sort
		var tabs = $('.TabControl, .TabControlSelected');
		tabs.sort(function(a,b){
			return $(b).find('a').width() - $(a).find('a').width() ;
		});
		
		//stuff
		for(var i=0; i<tabs.length; i++){
			var idealContentWidth = containerWidth / numItems;
			var itemWidth = $(tabs[i]).find('a').width();
			if(itemWidth > idealContentWidth){
				containerWidth -= itemWidth;
				numItems--;
			}
		}
		
		maxContentWidth = containerWidth / numItems;
		//set padding
		$('.TabControl, .TabControlSelected').each(function(index){
			var itemWidth = $(this).find('a').width();
			perfectPadding = ((maxContentWidth - itemWidth) / 2) - 1;
			$(this).css('padding-left',perfectPadding).css('padding-right',perfectPadding);
		});
	});*/
	
// loads the html
// function LoadHTML(url, callback)
// {
    // $.ajaxSetup
    // ({
        // cache: false
    // });

    // $.ajax
    // ({
        // type: "GET",
        // url: url,
        // dataType: "html",
        // contentType: "text/html",
        // success: eval(callback),
        // error: function(xhr, ajaxOptions, thrownError)
        // {
            // alert(xhr.status);
            // alert(thrownError);
        // }  
    // });
// }
	
// function FillIndustry(target)
// {
	// /* Load the query and return the industry*/
	// hu = window.location.search.substring(1);
	// gy = hu.split("?");
	// if(gy.length > 0)
	// {
		// ft = gy[1].split("=");
		// if (ft.length > 1) 
		// {
			// return ft[1];
		// }
	// }
// }

//Used for contact us Find a Partner
//function SearchPartners()
//{
//   $('#divPartners').html("<img src='/CMSTemplates/MossAdams/images/loading.gif' alt='Loading' />");
//    $('#divPartners').load('/CMSTemplates/MossAdams/Handlers/GetPartners.ashx?Keywords=' + $('#txtFindAPartner').val().replace(/ /g, ''), function(data){});
//}

// var timer;
// var downloadPath;

// function InvokeDownload(aliasPath)
// {
    // downloadPath = aliasPath;
    
    // timer = setInterval("ExecuteDownload()", 1000);
// }


// function ExecuteDownload()
// {
    // window.location = downloadPath + '?Download=true'

    // if (timer != null)
    // {
        // clearTimeout(timer);
    // }
// }

// function DefaultMessage(sender, msg, action)
// {
    // if (action == 0 && sender.value == msg)
    // {
        // sender.value = '';
    // }

    // if (action == 1 && sender.value.length == 0)
    // {
        // sender.value = msg;
    // }
// }


// function SetDefaultCheckList(sender, targetContainer)
// {
    // targetContainer = document.getElementById(targetContainer);

    // var cboxes = targetContainer.getElementsByTagName('input');

    // for (var i = 0; i < cboxes.length; i++)
    // {
        // if (cboxes[i].type == 'checkbox' && sender.checked && i > 0)
        // {
            // cboxes[i].checked = false;
        // }
    // }
// }
