$(document).ready(function(){
	
	// Listen for all <a> tags to check for location and type
	/*
	$("a").unbind().bind('click',function(){
	 	isDispatcherLoaded();
	 });
	*/
	if (undefined != $(".fancybox").fancybox){
		$(".fancybox").fancybox();	
	}
	$("#tabs").tabs({
					"cache":true,
					"cookie":{ expires: 30 }
					});
	
	$("a.select_residential,a#select_residential").unbind().bind('click',function(){
		setDispatcherType('residential');		
		if(!isDispatcherLoaded()){
			selectLocationDialog();	
		}
		return false;
	 });
	
	$("a.select_business,a#select_business").unbind().bind('click',function(){
		setDispatcherType('business');		
		if(!isDispatcherLoaded()){
			selectLocationDialog();	
		}
		return false;
	 });
	
	$("a.select_location,area.select_location").unbind().bind('click',function(){
		selectLocationDialog();
		return false;
	 });

	$("a.select_navigation_spanish").unbind().bind('click',function(){
		var page = $(this).attr("href").substr($(this).attr("href").lastIndexOf("/") + 1);
		setDispatcherType('espanol');
		selectLocationDialog(page);
		return false;
	 });
	
	$("a.external").unbind().attr("target","_blank");
	
	$(".reset").bind("focus",function(){ $(this).val(''); });
  
	$(".js_required").removeClass("js_required");

	if(jQuery.browser.msie){
		/* Apply actions for IE */
		
		
		/* Apply actions for IE 6 */
		var msie_version = parseInt(jQuery.browser.version);
		if(msie_version == 6){
			
		}
	}
	
	$("#request").change(function(){
		$("dt.request-description").hide();
		$("dd.request-description").hide();
		
		$("dt.request-description").show();
		$("#" + $(this).val()).show();
	});
	if($("#request")){
		$("#request").trigger("change");
	}
	
	
	// Force the content area to be at a height so that the left navigation doesn't bleed into the footer
	sideNavHeight = $("#sidenav").height();
	contentAreaHeight = $("#content_area").height();
	if(sideNavHeight > contentAreaHeight){
		$("#content_area").css('height',sideNavHeight + "px")	
	}
});

function isDispatcherLoaded()
{
	$.ajax({
		   type: "POST",
		   url: "/includes/ajax.php",
		   data: "method=isDispatcherLoaded",
		   async: false,
		   cache: false,
		   success: function(html){
			   if(html == "true"){
					return true;
			   }else{
				   return false;
			   }
		   }
	});		
}

function setDispatcherType(type)
{
	$.ajax({
		   type: "POST",
		   cache: false,
		   url: "/includes/ajax.php",
		   data: "method=setDispatcherType&type=" + type,
		   async: false
	});		
}

function selectLocationDialog(page)
{
	// Page is optional
	var requestData = "";
	if(page != undefined){
		requestData = "method=selectLocationWithPage&page="+encodeURIComponent(page);
	}else{
		requestData = "method=selectLocation";
	}
	
	selectLocationHtml = $.ajax({
		   type: "POST",
		   async: false,
		   cache: false,
		   url: "/includes/ajax.php",
		   data: requestData
			   }).responseText;
	$.fancybox(selectLocationHtml ,
			   {
				'modal' : true ,
				'padding' : 25
			   });	
}
