// OPEN A PAGE IN A NEW WINDOW
// Create the new window
function openInNewWindow(e) {
	var event;
	if (!e) event = window.event;
	else event = e;
	// Abort if a modifier key is pressed
	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');
		var newWindow = window.open(this.getAttribute('href'));
		if (newWindow) {
		    // For IE 8
			try {
		        if (newWindow.focus()) {
			        newWindow.focus();
			    }
		    }
		    catch(err) {		        
		        return false;		        
		    }	
		return false;
		}
	return true;
	}
}

// CALL THIS FUNCTION TO INITIATE FUNCTION THAT OPENS CERTAIN LINKS IN NEW WINDOWS
function getNewWindowLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {			
		// Find all links
		var link;
		var links = document.getElementsByTagName('a');
		for (var i = 0; i < links.length; i++) {
			link = links[i];
			// Find all links with a class name of "non-html" - Use for PDF documents and the like
			if (/\bnon\-html\b/.test(link.className)) {				
				link.onclick = openInNewWindow;
			}
			// Find all links with a class name of "off-site" 
			else if (/\boff\-site\b/.test(link.className)) {				
				link.onclick = openInNewWindow;
			}			
		}	
	}
}

// ADD CLASSES
// 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$=.doc],a[href$=.jpg],a[href$=.gif],a[href$=.png],a[href$=.sflb]").addClass("track-page non-html");
	$("a[href$=pdf],a[href*=PDFs]").addClass("pdf");
	// Build selector for current host check
	var currentDomainSelector = "a[href*=" + window.location.host + "]";
	// Exclude certain links from updated behavior
	$(currentDomainSelector).removeClass("off-site");
           $("a[href^=http://blog.ohiomeansbusiness.com]").removeClass("off-site"); // Exception added 9/16/10 GML
	$("a[rel~=lightbox]").removeClass("non-html");
}

// ACCORDIAN
function initAccordian() {
	$("ul.accordian").addClass("active");
	$.each($("ul.accordian h4"), function() {		
		if ($(this).children("a").length > 0) {
			$(this).addClass("has-link");
		}
	});
	$("ul.accordian h4").not(".has-link").click(function() {			
		if ($(this).next("div.content").css("display") == "none") {
			$("ul.accordian div.content").slideUp("normal");
			$("ul.accordian h4").removeClass("selected");
			$(this).next("div.content").slideDown();
			$(this).addClass("selected");
		}                        			
	});
	// Event tracking
	$("ul.accordian h4").not(".has-link").addClass("track-event site-selection-toolkit");
	$("ul.accordian a").addClass("track-event site-selection-toolkit");
}	

// GOOGLE ANALYTICS GENERIC EVENT TRACKING
// Requires jQuery
function initEventTracking() {	
	$(".track-event").click(function() {
		// 1. GET DATA FOR GA CATEGORY
		var aryCategories = new Array("video","pdf","lightbox","off-site","site-selection-toolkit");
		var strCategory = "";
		var strCount = 0;
		for (strCount = 0; strCount <= 4; strCount++) {
			if ($(this).hasClass(aryCategories[strCount])) {
				if (strCount > 0) {
					strCategory = strCategory + " ";
				}
				strCategory = strCategory + aryCategories[strCount];
			}
		}
		if (strCategory == "") {
			strCategory = this.nodeName.toLowerCase();
		}
		var gaCategory = strCategory;	
		// 2. GET DATA FOR GA ACTION
		var strText;
		// Check to see if text is present
		if ($(this).text() != "") {
			strText = $(this).text();
		}
		// No text - see if this is an image
		else {			
			if ($(this).find("img").length > 0) {
				strText = $(this).find("img").attr("alt");
				if (strText == "") {
					strText = $(this).find("img").attr("src");
				}
			}
			if (strText == "") {
				strText = "No text";
			}
		}
		var gaAction = strText.replace(/["']{1}/gi,"");		
		// 3. GET DATA FOR GA LABEL
		var gaLabel = window.location;		
		// 4. TEST FUNCTION OR SEND DATA TO GOOGLE
		if (location.hash == "#test") {
			alert(gaCategory + ", " + gaAction + ", " + gaLabel);			
		}
		else {
			// INSERT GOOGLE TRACKING CODE HERE
			// Differs based on which version of GA is being used
			pageTracker._trackEvent(gaCategory, gaAction, gaLabel);
			// _gaq.push(['_trackEvent', gaCategory, gaAction, gaLabel]);		 
		}	
	});	
}

// URCHIN/GOOGLE ANALYTICS GENERIC EVENT TRACKING
// Requires jQuery
// This method creates pseudo-pages in the GA dashboard
function initPageTracking() {	
	$("a.track-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 {
			pageTracker._trackPageview(strPath); 
		}
	});
}

// ON-LOAD EVENTS
// Requires jQuery
$(document).ready(function() {
    addClasses();
    initAccordian();
    initPageTracking();
    initEventTracking();
    getNewWindowLinks();    
});

