﻿ //Print Error Handling
function printArticle() {
	if (window.print) {
		window.print();
	}else if (agt.indexOf("mac") != -1) {
		alert("Press 'Cmd+p' on your keyboard to print article.");
	}else {
		alert("Press 'Ctrl+p' on your keyboard to print article.")
	}
}


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

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") 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;
}

function sendForm(form,sender,recipient,subject,message){
	//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"><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()}).fadeIn();
	
	//Set up error handling
	$(form).ajaxError(function(event, request, settings){
		$(this).append("<strong>Error occured while requesting page " + settings.url + "</strong>");
	});

	//Send ajax request
	$.get("/forms/email_form.aspx", {
		sender:sender, 
		recipient:recipient, 
		subject:subject, 
		message:message
	}, function(data, textStatus){
		//Show confirmation on success
		$(form).find(".sending").hide();
		$(form).find("fieldset").hide();
		$(form).find(".confirmation").show();
	});
}

//-- End Form Functions

//-- Functions for flash video embeds
function showVideo(videoLink){
	if($("#flash_content").length > 0){
		
		$("#flash_content").empty();
		
		var parent = $(videoLink).parents(".media_message");
		var html = "<h6>" + parent.prev(".message_title").html() + "</h6>" + parent.find(".intro").html();
		$("#video_intro").html(html);
		
		var flashvars = {video: $(videoLink).attr("rel")};    
		var params = {wmode: "transparent", allowFullScreen: "true"};
		var attributes = {};
		swfobject.embedSWF("/scripts/flash/player.swf", "flash_content", "209", "185", "9.0.0", "expressInstall.swf", flashvars, params, attributes);  
	}else{
		jQuery.facebox({ ajax: $(videoLink).attr("href") });
	}
}

//jquery events - sgreenfield
$(document).ready(function(){
    //topnav
    $(".dropdown").hide();
	$(".nav_button").hover(function(){
        $("#"+$(this).attr("id")+" .dropdown").show();
	},function(){
        $("#"+$(this).attr("id")+" .dropdown").hide();
	});
    $(".nav_button").hover(function(){
        $("#"+$(this).attr("id")+" img").hide();
    },function(){
        $("#"+$(this).attr("id")+" img").show();
    });
    //end topnav
    
    //back to top
    $("#to_top").click(function(){
        $('html, body').animate({scrollTop:0}, 'fast'); 
        return false;
    });    
    //end back to top

    //facebox initialization
    $('.facebox, a[rel*=facebox]').facebox({opacity:.5});
    //end facebox initialization

    //offices map mouse behavior
    $(".offices_label").hover(function(){
        $("#"+$(this).attr("id")+" img").hide();
    },function(){
        $("#"+$(this).attr("id")+" img").show();
    });    
    //end offices map mouse behavior
        
    //media room
    $(".media_message").hide();
    
	//Handles flash embeds
	$(".play_btn a").click(function(){
        showVideo(this);
        return false;
    });   
	$(".play_btn:first a").click();
    
	$(".expand_it").click(function(e){
        if ($(e.target).is(".message_title"))
            $(e.target).next(".media_message").slideToggle(250);
    });
    //end media room
    
    //far left spotlight (position based on leftnav height)
        $(".left_spotlight").css({'top' : ($("#leftnav").height()+79)+'px'});
    //end far left spotlight
    
});
//end jquery events