// CHECK FOR NEW WINDOW LINKS
function getNewWindowLinks() {
	$("a.non-html, a.off-site").click(function(e) {
		var event;
		if (!e) {
			 event = window.event;
		}	
		else { 
			event = e;
		}
		if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {			
			return true;
		}
		else {				
			var newWindow = window.open(this.getAttribute('href'), '', 'width=780, height=500, scrollbars=yes, resizable=yes, toolbar=yes, location=yes, directories=no, menubar=yes, copyhistory=no');
			if (newWindow) {
			    try {
			        if (newWindow.focus()) {
				        newWindow.focus();
				    }
			    }
			    catch(err) {		        
			        return false;		        
			    }	
			return false;
			}
		return true;
		}
	});
	$("a.timeline").click(function(e) {
		var event;
		if (!e) {
			 event = window.event;
		}	
		else { 
			event = e;
		}
		if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {			
			return true;
		}
		else {				
			var newWindow = window.open(this.getAttribute('href'), '', 'width=570, height=860, scrollbars=yes, resizable=yes, toolbar=yes, location=yes, directories=no, menubar=yes, copyhistory=no');
			if (newWindow) {
			    try {
			        if (newWindow.focus()) {
				        newWindow.focus();
				    }
			    }
			    catch(err) {		        
			        return false;		        
			    }	
			return false;
			}
		return true;
		}
	});
}

// LOOK FOR CLOAKED LINKS AND MAKE THEM CLICKABLE
// Hopefully this will help hide e-mail addresses from spam spiders
function createMailtoLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {			
		var email; // E-mail address
		var mailto; // The mailto hyperlink
		var cloak; // The mailto span element
		var spans = document.getElementsByTagName('span'); // Array of span elements
		for (var i = 0; i < spans.length; i++) {
			cloak = spans[i];
			// Find all span elements with a class name of "cloak"
			if (/\bcloak\b/.test(cloak.className)) {				
				email = cloak.innerHTML;
				cloak.innerHTML = "";
				mailto = document.createElement('a');															
				mailto.href = 'mailto:' + email;
				mailto.innerHTML = email;
				cloak.appendChild(mailto);			
			}
		}	
	}
}

// ADD CLASSES v3.0
// Requires jQuery
function addClasses() {
	// Add classes to different types of links to control behavior
	$("a[href^=http://],a[href^=https://],a[target=_blank]").addClass("off-site");
	$("a[href$=.pdf],a[href$=.jpg],a[href$=.gif],a[href$=.png],a[href$=.sflb],a[href$=doc],a[href$=docx],a[href$=xls],a[href$=xlsx],a[href$=txt],a[href$=avi],a[href$=wma],a[href$=mov]").addClass("non-html");
	//$("a[href$=.jpg],a[href$=.gif],a[href$=.png]").addClass("image");
	//$("a[href$=.avi],a[href$=.wma],a[href$=.mov]").addClass("video");
	$("a[href$=.pdf]").addClass("pdf");
	//$("a[href$=.doc],a[href$=.docx]").addClass("ms-word");
	//$("a[href$=.xls],a[href$=.xlsx]").addClass("ms-excel");
	$("a[href$=txt]").addClass("text");	
	// Add event tracking triggers
	//$("a.off-site,a.non-html").addClass("track-event");
	$("a.pdf").addClass("track-virtual-page");
	// Build selector for current host check
	var currentDomainSelector = "a[href*=" + window.location.host + "], a[href*=phx.corporate-ir.net], a[href*=ir.worthingtonindustries.com], a[href*=dev.worthingtonindustries.com], a[href*=staging.worthingtonindustries.com], a[href*=worthingtonindustries.staging.fahlgreninteractive.com], a[href*=worthingtonindustries.com]";
	// Exclude certain links from updated behavior
	$(currentDomainSelector).removeClass("off-site");
	//$("a[rel~=lightbox],a[class~=lightbox]").removeClass("non-html");
	//$("a[rel~=lightbox],a[class~=lightbox]").removeClass("off-site");
	if (window.location.host == "phx.corporate-ir.net" || window.location.host == "ir.worthingtonindustries.com") {
		$("body").addClass("ir");
	}	
}

// GOOGLE ANALYTICS VIRTUAL PAGE TRACKING v1.1
// Requires jQuery
// This method creates pseudo-pages in the Google Analytics dashboard
function initVirtualPageTracking() {
	$("a.track-virtual-page").click(function() {
		var strHref = $(this).attr("href");
		var aryHrefSplit = strHref.split("/");
		aryHrefSplit.reverse();
		var strFileName = aryHrefSplit[0];
		var strPath = "/downloads/" + strFileName;
		if (location.hash == "#test") {
			alert(strPath);
		}
		else {			
			_gaq.push(['_trackPageview', strPath]);
		}
	});
}

// MAIN NAVIGATION
function initNav() {
	var intItemWidth;
	var strDropPadding;
	$.each($("ul#nav-primary ul"), function() {
		strDropPadding = $(this).css("padding-right");	
		intDropPadding = parseFloat(strDropPadding) * 2;	
		intItemWidth = $(this).parent().width() - intDropPadding;
		$(this).width(intItemWidth);
	});		
	$("ul#nav-primary > li").hoverIntent( showDrop, hideDrop );
	function showDrop() {
		$(this).find("ul").slideDown("fast");
	}
	function hideDrop() {
		$(this).find("ul").slideUp("fast");
	}
}

// SEARCH FORM
function initSearch() {
	$("#keyword-search").focus(function() {
		$(this).val("");
	});
	/* REM due to Opera issues
	$("#keyword-search").blur(function() {
		$(this).val("Search");
	});
	*/
}

// DESIGN ENHANCEMENTS
function adjustPage() {
	$("div#features a").append(" &raquo;");
	// IR adjustment
	if ($("body").hasClass("ir")) {	
		$("div#content").prepend("<h2 class='ir'><img src='http://www.worthingtonindustries.com/images/hdr/stabilize-optimize-grow.gif' alt='Stabilize, optimize, grow' /></h2>");		
		if ($("div.primary").length == 1 && $("div.primary h1").length > 0) {
			$("div#content").prepend("<h1 class='ir'></h1>");
			var strHeader = $("div.primary h1:first-child").text();
			$("div.primary h1:first-child").remove();
			$("h1.ir").text(strHeader);
		}
	}
}

// CUFON
function initCufon() {
	Cufon.replace("div#feature-main, ul#nav-redundant > li > a");	
}

// NEWSLETTER ANIMATION
function initNewsletters() {      
   $("div.newsletter").hover( active, inactive );
	function active() {
		$(this).animate({
            top: "-80px"
         }, 500, function() {         
      });
	}
	function inactive() {
		$(this).animate({
            top: "0px"
         }, 300, function() {         
      });
	}
}

// ON-LOAD EVENTS 
// Requires jQuery
$(function() {   
	addClasses();	
	getNewWindowLinks();
	createMailtoLinks();	
	initNav();	
	initSearch();
	initCufon();
	initNewsletters();
	adjustPage();	
	initVirtualPageTracking();
});

// GOOGLE ANALYTICS
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1393095-4']);
_gaq.push(['_setDomainName', '.worthingtonindustries.com']);
_gaq.push(['_trackPageview']);

(function() {
 	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
 	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
 	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
