// JavaScript Document

//BEGIN:  used for locations map scroll arrows (Scott G)
$(document).ready(function(){
    function openFacebox(url){
	    jQuery.facebox({ajax:url});
    }
    var id = "0";
    $(".scrollarrow").mouseup(function(){ 
        id = $(this).attr("id");
        if (id.toString() == "leftarrow"){
            $("#themap").animate({marginLeft: "0px"}, 585);
            $("#leftarrow").fadeOut(585);
            $("#rightarrow").fadeIn(585);                        
        }
        if (id.toString() == "rightarrow"){
            $("#themap").animate({marginLeft: "-595px"}, 585);
            $("#leftarrow").fadeIn(585);
            $("#rightarrow").fadeOut(585);            
        }
    });
	
	//Set up all structures google search boxes
	$("form.structuresSearch").submit(function(){
		try{
			loadStructuresResults($(this).find("input:first").val(),$(this).attr("action"));
		}catch(exception){
			alert(exception)
		}
		return false;
	});
})

function loadStructuresResults(searchTerms,searchForm)
{
	var results = $("#structuresSearch");
	if(results.length == 0){ //Lazy Instantiation
		$("body").append("<div id='structuresSearch' style='width:0'><div id='hideStructuresSearch' /><iframe id='searchResults' /><div/>");
		results = $("#structuresSearch");
		results.find("#hideStructuresSearch").click(hideStructuresResults);
	}
	results.animate({width:"340px",opacity:1},"fast");
	results.find("#searchResults").attr("src",searchForm + "?searchTerms=" + searchTerms);
}
function hideStructuresResults()
{
	$("#structuresSearch").animate({width:"0",opacity:0},"fast");
}

function showIRDisclaimer()
{
	if($.cookie("showDisclaimer") == null){
	
		$.facebox({ ajax: '/popup.aspx?id=458'});
		var oldFooter = $('#faceboxFooter').html();
		$('#faceboxFooter').html('<a class="button disagree" title="Disagree"><span></span>Decline</a><a class="button agree" title="Agree"><span></span>Confirm</a><div style="clear:both;"></div>');
		$('#facebox_overlay').unbind('click')
		.attr('title','');
		
		$('#faceboxFooter .agree').click(function(){
			$(document).trigger('close.facebox');
			$(".dhtml").show();
			$('#faceboxFooter').html(oldFooter);
			$.cookie("showDisclaimer", "false");
		});
			
		$('#faceboxFooter .disagree').click(function(){
			window.location = "http://cms.dday3.com/page.aspx?id=459";
			$('#faceboxFooter').html(oldFooter);
			//history.go(-1);
		});
		
	}else{
		$(".dhtml").show();
	}
}

//--------------------------- Begin Form Functions --||

function sendForm(form,sender,recipient,subject,message,token,hash){
	//Check that all elements are present
	if($(form).find(".sending").length == 0)
		$(form).append('<div class="sending"></div>');
	if($(form).find(".confirmation").length == 0)
		$(form).append('<div class="confirmation" style="display:none;"><p>Thank you.  Your information has been successfully submitted.</p><p>Please feel free to continue browsing our site.</p></div>');
	
	
	//Display Sending Animation
	$(form).find(".sending").css({width:$(form).width(), height:$(form).height()}).fadeTo("fast",".9");
	
	//Set up error handling
	$(form).ajaxError(function(event, request, settings){
		$(this).append("<strong class='error'>Error occured while requesting page " + settings.url + "</strong>");
	});
	
	//Send ajax request
	$.post("/forms/email_form.aspx", {
		sender:sender, 
		recipient:recipient,
		subject:subject, 
		message:message,
		token:token,
		hash:hash
	}, function(data, textStatus){
		//Remove form and sending status
		$(form).find(".sending").hide();
		$(form).find("fieldset").hide();
		
		//Search for an 'Success' status in data
		if(data.match(/Success/)){
			//Show confirmation on success
			$(form).find(".confirmation").show();
		}else{
			//Show errors
			$(form).append(data);
		}
	});
}

function compileForm(form){	
	//Only include each field if filled
	var message = "";
	
	$(form).find(":input").each(function(){
		if($(this).val() == "") return;
		
		//Eliminate null options
		var type = $(this).attr("type");
		if(type == "submit" || type == "reset" || type == "button" || type == "hidden") return;
		if(type == "radio" && !$(this).is(":checked")) return;
		if(type == "checkbox" && !$(this).is(":checked")) return;
		if($(this).is("select") && $(this).find("option:selected").hasClass("legend")) return;
		
		//Find the label
		var name = $(this).attr("name");
		var id = (type == "radio") ? $(form).find(":input[name='"+name+"']:first").attr("id") : $(this).attr("id");
		var label = $(form).find("label[for='"+id+"']:first");
		
		message += label.text() +" : " + $(this).val() +"\n";
	});
	
	return message;
}

//-- End Form Functions

