﻿/*
	HealthPartners 2010 
	global js
*/

$(document).ready(function() {

		// Clear Search Box
	function checkSearchAutocomplete() {
		if ($("#search-form-container input#search-box").val().length != 0) {
			$("#search-form-container label").removeClass("light").hide();
		}
	}
	
		// Adds the class .has-js to <html> if javascript is enabled
	$("html").addClass("has-js");
		
		// Add the class disabled if input.button has attribute disabled.  	
	$("input.button:disabled").addClass("disabled");
	
 	 	// Adds the class .mac to <html> if OS is Mac OS X
 	if ($.client.os == "Mac") {
    		$("html").addClass("mac");
 	}
 		// Adds the class .win to <html> if OS is Windows
 	if ($.client.os == "Windows") {
    		$("html").addClass("win");
 	}

 		// Adds the class .moz to <html> if user-agent is mozilla
	if ($.browser.mozilla) {
    	$("html").addClass("moz");
 	}
 		// Adds the class .chrome to <html> if user-agent is chrome
	if ($.client.browser == "Chrome") {
    	$("html").addClass("chrome");
 	}
 	
 	 	// Adds the class .safari to <html> if user-agent is safari
	if ($.client.browser == "Safari") {
    	$("html").addClass("safari");
 	}
 	
 	  	// Adds the class .webkit to <html> if user-agent is webkit
 	if ($.browser.webkit) {
    	$("html").addClass("webkit");
 	}

 		 // Adds the class .ie9 to <html> if broswer is MSIE version 9
 	if ($.browser.msie && $.browser.version == 9 ) {
		$("html").addClass("ie9");
	}
	
 	 	// Adds the class .ie8 to <html> if broswer is MSIE version 8
 	if ($.browser.msie && $.browser.version == 8 ) {
		$("html").addClass("ie8");
	}
	
 		// Adds the class .ipad to <html> if OS is iPad 
 	if ($.client.os == "iPad") {
    		$("html").addClass("ipad");
 	}
 	
 		// Adds the class .iphone to <html> if OS is iPhone/iPod Touch 
 	if ($.client.os == "iPhone") {
    		$("html").addClass("iphone");
 	}
		// Open links in new blank window
 	$("a[rel*=external], a[rel*=nofollow]").live("click", function() {
  		window.open(this.href);
  		return false;
  	});
  	
  		// Open links in new blank window
 	$("a.button.print").live("click", function() {
		window.print();
  		return false;
  	});

		// iframe embed
	if ($("iframe.embed").length > 0) {
		$("iframe.embed").each(function() {
		
			var f = this;
			var a_names = new Array();
			var a_values = new Array();
		
			for( var i=0; i < f.attributes.length; i++ ) {
				a_names[i] = f.attributes[i].nodeName;
				a_values[i] = f.attributes[i].nodeValue;
			}
			
			if ($.browser.msie ) {
				var new_iframe_string = "<iframe frameBorder=\"0\" allowTransparency=\"true\" scrolling=\"no\""; // IE
			} else {
				var new_iframe_string = "<iframe"; // other broswers
			}
			
			for( var i=0; i < a_names.length; i++ ) {
		 			if (a_names[i] != "allowTransparency" && a_names[i] != "allowtransparency" && a_names[i] != "frameBorder" && a_names[i] != "frameborder" && a_names[i] != "scrolling" && a_values[i] != "") {
						new_iframe_string += " " + a_names[i] + "=\"" + a_values[i] + "\"";
					} 
			}
			
			new_iframe_string += " ></iframe>";	// Add custom parameters
			$(this).replaceWith(new_iframe_string);
		
		});
		
		// iframe auto height
		$("iframe.embed").load(function(e) {
			$(e.target).height(0);
			if (this.contentDocument) {
			   var document = this.contentDocument;
			} else if (this.contentWindow) {
			   var document = this.contentWindow.document;
			} else {
			   var document = this.document;
			}
			$(e.target).height($(document).height());
		});
	
				// iframe key survey
		if ($("iframe.key-survey").length > 0) {
			$("iframe.key-survey").each(function() {
			
				var f = this;
				var a_names = new Array();
				var a_values = new Array();
			
				for( var i=0; i < f.attributes.length; i++ ) {
					a_names[i] = f.attributes[i].nodeName;
					a_values[i] = f.attributes[i].nodeValue;
				}
				
				if ($.browser.msie ) {
					var new_iframe_string = "<iframe frameBorder=\"0\" allowTransparency=\"true\" scrolling=\"no\""; // IE
				} else {
					var new_iframe_string = "<iframe scrolling=\"no\""; // other broswers
				}
				
				for( var i=0; i < a_names.length; i++ ) {
			 			if (a_names[i] != "allowTransparency" && a_names[i] != "allow transparency" && a_names[i] != "frameBorder" && a_names[i] != "frame border" && a_names[i] != "scrolling" && a_values[i] != "") {
							new_iframe_string += " " + a_names[i] + "=\"" + a_values[i] + "\"";
						} 
				}
				
				new_iframe_string += " ></iframe>";	// Add custom parameters
				$(this).replaceWith(new_iframe_string);
			
			});
			
		}
		
	}

		// Highlight first input when .focus is present
	$("form input.focus, form select.focus, form textarea.focus").focus();
		
		// JS only speak text
	$(".required-information-label").addClass("voice-over");
	
		// Content Tabs
	if ($("div#content-tabs").length > 0) {
		$("div#content-tabs").tabs({ cookie: { expires: 1 } });
	}
	
			// Content Tabs
	if ($("div#content-accordion").length > 0) {
		$("div#content-accordion").tabs({ cookie: { expires: 1 } });
	}
	
		// Auto Ledger
	$ ("ul.ledger li:odd, table.ledger tr:odd, ul.ledger-hover li:odd, table.ledger-hover tr:odd").addClass("ledger-style");
	
		// Add class "main-aside-ellipsis" to h1 if class "aside" is present on the page
	if ($("div.aside").length > 0) {
		$("h1").addClass("main-aside-ellipsis");
	}
	
		// Site Search	
		// reset search

		// Only run if "search-form-container" ID is present on the page
	if ($("#search-form-container").length > 0) {
		checkSearchAutocomplete();
		window.setTimeout(function() {
    		checkSearchAutocomplete(true);
		}, 250);	
	}
	
	$("#search-form-container input#search-box").val() == "";
	$("#search-form-container .search-box-magnifying-glass").removeClass("active").removeClass("highlight");
		
	$("#search-form-container input#search-box").focus(function() {
		if($("#search-form-container input#search-box").val() == "")
		{
			$("#search-form-container input#search-box").caret(0,0);
			$("#search-form-container label").addClass("light");
		}
	});

	$("#search-form-container input#search-box").keydown(function() {
		if($("#search-form-container input#search-box").val() == "")
		{
			$("#search-form-container input#search-box").caret(0,0);
			$("#search-form-container label").removeClass("light").hide();
			$("#search-form-container .search-box-magnifying-glass").addClass("highlight");
		}
	});

	$("#search-form-container input#search-box").keyup(function() {
		if($("#search-form-container input#search-box").val() == "")
		{
			$("#search-form-container input#search-box").caret(0,0);
			$("#search-form-container label").addClass("light").fadeIn(150);
			$("#search-form-container .search-box-magnifying-glass").removeClass("highlight");
		}
	});
	
	$("#search-form-container input#search-box").blur(function() {
		if($("#search-form-container input#search-box").val() == "")
		{
			$("#search-form-container label").removeClass("light").show();
		
		}
	});
		
	$("#search-form-container .search-box-magnifying-glass").live("click", function() {
  		$("#search-form-container .search-box-magnifying-glass").removeClass("highlight").addClass("active");
  		$("form.search-form").submit();
  		return false;
  	}); 
	
	$("#search-form-container .search-box-magnifying-glass").live("click", function() {
		$("input#search-box").val("");
  		$("#search-form-container input#search-box").caret(0,0);
		$("#search-form-container label").addClass("light").fadeIn(150); 
		$("#search-form-container .search-box-magnifying-glass").removeClass("highlight");
  		return false;
  	}); 
		// Site Search - end

		// Toggle
	$("a.toggle").toggle(function() {
  		$(this).addClass("active");
  		return false;
	}, function() {
  		$(this).removeClass("active");
  		return false;
	});

		// Social Links
	var urlToShare = window.location.protocol + "//" + window.location.host + window.location.pathname; 
	var titleToShare = document.title;

	$("a#delicious-redirect").click(function() {
		window.open("http://delicious.com/save?url=" + urlToShare + "&title=" + titleToShare, "top");
		return false;
	}); 
	
	$("a#facebook-redirect").click(function() {
		window.open("http://facebook.com/sharer.php?u=" + urlToShare + "&title=" + titleToShare, "top");
		return false;
	});
	
	$("a#linkedin-redirect").click(function() {
		window.open("http://linkedin.com/shareArticle?mini=true&url=" + urlToShare + "&title=" + titleToShare, "top");
		return false;
	});
	
	$("a#digg-redirect").click(function() {
		window.open("http://digg.com/submit?phase=2&url=" + urlToShare + "&title=" + titleToShare, "top");
		return false;
	});
	
	$("a#twitter-redirect").click(function() {
		window.open("http://twitter.com/home?status=Check out this webpage: " + titleToShare + " " + urlToShare, "top");
		return false;
	});
	
	$("a#mailto-redirect").click(function() {
		location.replace("mailto:?subject=Check out this webpage: " + titleToShare + "&body=" + urlToShare, "top");
		return false;
	});
	
		// Social Links - end	

		// hide Safari Mobile address bar
	if ($.client.os == "iPhone") {
		addEventListener("load", function() {
			setTimeout(hideURLbar, 0);
		}, false);

		function hideURLbar() {
			window.scrollTo(0,1);
		}
	}
	
		// previous and next buttons - return
	$(".previous-button, .next-button").live("click", function() {
		return false;
	});

});

