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'), '', '');
			if (newWindow) {
			    try {
			        if (newWindow.focus()) {
				        newWindow.focus();
				    }
			    }
			    catch(err) {		        
			        return false;		        
			    }	
			return false;
			}
		return true;
		}
	});
}

function initEventTracking() {	
	$(".track-event").click(function() {	
		if ($(this).hasClass("off-site")) {
			gaCategory = "Off-Site";
			gaAction = "Go to " + $(this).attr("href");
			gaLabel = "From " + window.location;
		}
		else if ($(this).hasClass("thickbox")) {
			gaCategory = "Overlay";
			gaAction = "Open " + $(this).attr("href");
			gaLabel = "From " + window.location;
		}
		if (location.hash == "#test") {
			alert(gaCategory + ", " + gaAction + ", " + gaLabel);	
			return false;		
		}
		else {			
			_gaq.push(['_trackEvent', gaCategory, gaAction, gaLabel]);			
		}	
	});	
}

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]);						
		}
	});
}

function addClasses() {
	$("a[href^='http://'],a[href^='https://'],a[target='_blank']").addClass("off-site");
	$("a[href$='.swf'],a[href$='.pdf'],a[href$='.jpg'],a[href$='.gif'],a[href$='.png'],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$='.pdf']").addClass("pdf");
	$("a[href$='.doc'],a[href$='.docx']").addClass("ms-word");
	$("a[href$='.xls'],a[href$='.xlsx']").addClass("ms-excel");		
	var currentDomain = window.location.host;
	currentDomain = currentDomain.toLowerCase();
	var currentDomainSelector = "a[href*='" + currentDomain + "']";	
	var additionalDomainSelectors = "";	
	currentDomainSelector = currentDomainSelector + additionalDomainSelectors;		
	$(currentDomainSelector).removeClass("off-site");
	$("a[rel^='#']").removeClass("off-site").removeClass("non-html");
	// For Google Analytics
	$("a.pdf").addClass("track-virtual-page");	
	$("a.off-site").addClass("track-event");
}

function clearMePrevious() {	
	var clearMePrevious = '';
	$.each($('.clearMeFocus'), function () {
	    $(this).attr('title', $(this).val());			
	});
	$('.clearMeFocus').focus(function () {
	    if ($(this).val() == $(this).attr('title')) {
	        clearMePrevious = $(this).val();
	        $(this).val('');
	    }		
	});
	$('.clearMeFocus').blur(function () {
	    if ($(this).val() == '') {
	        $(this).val(clearMePrevious);				
	    }
	});
}

function assignFonts() {
    Cufon.replace(".cufon-active", { fontFamily: "Avenir" });
}

function initSwitcher() {
	var intItems = 0;
	var intTemp;
	var intIndex = 0;
	var intError = 0;
	$("#image-slider ul.items").each(function() {
		intTemp = $(this).children("li").length;
		if (intItems != 0 && intTemp != intItems) {
			intError = 1;				
		}
		intItems = intTemp;		
	});
	if (intError != 1) {
		$("#image-slider-buttons").addClass("active");
		window.setTimeout(rotateThis, 10000);
	}	
	function rotateThis() {	   
		nextContent();
		window.setTimeout(rotateThis, 10000);	
	}	
	$("#image-slider-buttons p.right").click(function() {
		clearTimeout(rotateThis);
		nextContent();
	});
	$("#image-slider-buttons p.left").click(function() {
		clearTimeout(rotateThis);
		prevContent();
	});	
	function nextContent() {
		intIndex = $("#image-slider ul.items li.active").index();
		if (intIndex <= intItems) {				
			$("#image-slider ul.items li.active").removeClass("active").hide().next("li").fadeIn().addClass("active");					
		}
		else {
		   $("#image-slider ul.items li.active").removeClass("active").hide().siblings("li:first-child").fadeIn().addClass("active");					
		}		
	}
	function prevContent() {
		intIndex = $("#image-slider ul.items li.active").index();
		if (intIndex == 0) {			
			$("#image-slider ul.items li.active").removeClass("active").hide().siblings("li:last-child").fadeIn().addClass("active");		
					
		}
		else {
			$("#image-slider ul.items li.active").removeClass("active").hide().prev("li").fadeIn().addClass("active");			
		}
	}
}

function adjustSearches() {
	$("#search input[type=text]").val("Search");
	$("#search input[type=text]").addClass("clearMeFocus");
}

/* VIDEO OVERLAYS - DISPLAYED IN JQUERY TOOLS OVERLAY */
function initVideoOverlays() {   	
	var intCount = 1;
	var strEmbed;
	var strEmbedId;
	var strHref;
	var aryHrefSplit;
	var strVideoId;
	var strElementId;
	var strRelId;
	var intVideoWidth;
	var intVideoHeight;
	var strOverlay;
	var strOverlayLink;
	var intOverlayHeight;
	$.each($("div.video"), function() {
		intVideoWidth = parseFloat($(this).find("span.width").text());
		intVideoHeight = parseFloat($(this).find("span.height").text());
		intOverlayHeight = intVideoHeight + 5;
		strHref = $(this).children("a").attr("href");
		aryHrefSplit = strHref.split("/");
		aryHrefSplit.reverse();
		strVideoId = aryHrefSplit[0];
		strEmbed = "<iframe width='" + intVideoWidth + "' height='" + intVideoHeight + "' src='http://www.youtube.com/embed/" + strVideoId + "' frameborder='0' allowfullscreen></iframe>";
		strElementId = "video-" + intCount;
		strRelId = "#" + strElementId;
		$(this).children("a").removeClass("off-site").attr("rel",strRelId);		
		strOverlay = "<div class='video-overlay simple-overlay' id='" + strElementId + "' style='width: " + intVideoWidth + "px'>" + strEmbed + "</div>";
		$(this).append(strOverlay);
		intCount++;
	});	
}

/* FLASH MAP - DISPLAYED IN JQUERY TOOLS OVERLAY */
function initMap() {   
	if ($("#toledo-map").length == 1) {
		$("#toledo-map").append("<div id='toledo-map-flash'></div>");
		var FO = { movie:"/App_Themes/ToledoRegion/swf/toledo-map.swf", width:"850", height:"695",
		majorversion:"8", build:"0", loop:"true", menu:"false", quality:"best", wmode:"transparent" };
		UFO.create(FO, "toledo-map-flash");
	}
}

/* INITIALIZE JQUERY TOOLS OVERLAY */
function initOverlay() {   
   $(".simple-overlay").appendTo("body");	
	$("a[rel^='#']").overlay({
		fixed: false, 
		mask: { color: "#000000" },
		closeOnClick: true
	});	
}

function initCufon() {
	Cufon.replace('#cms-nav-container > ul > li > a', { fontFamily: 'Avenir-Heavy', textTransform: 'uppercase', textShadow: '1px 1px #FFF, -1px -1px #ddd' });
   Cufon.replace('#cms-nav-container > ul > li > span', { fontFamily: 'Avenir-Heavy', textTransform: 'uppercase', textShadow: '1px 1px #FFF, -1px -1px #ddd' });
   Cufon.replace('#cph-blogs > h3', { fontFamily: 'Avenir-Medium', textTransform: 'uppercase', textShadow: '0px 1px #17344e, 1px 1px #71889E' });
   Cufon.replace('#cph-news > h3', { fontFamily: 'Avenir-Medium', textTransform: 'uppercase', textShadow: '0px 1px #17344e, 1px 1px #71889E' });
   Cufon.replace('#utility-nav > a', { fontFamily: 'Avenir-Medium' });
   Cufon.replace('#resources-wrapper h3', { fontFamily: 'Avenir-Heavy', textTransform: 'uppercase', textShadow: '0px 1px #ddd' });
   Cufon.replace('#articles-wrapper h3', { fontFamily: 'Avenir-Heavy', textTransform: 'uppercase', textShadow: '0px 1px #ddd' });
   Cufon.replace('#center-content-body h1', { fontFamily: 'Avenir-Medium', textShadow: '1px 0px #ddd' });
   Cufon.replace('#center-content-body h2:not(div.video h2)', { fontFamily: 'Avenir-Medium', textShadow: '1px 0px #ddd' });
   Cufon.replace('#center-content-body h3', { fontFamily: 'Avenir-Medium', textShadow: '1px 0px #ddd' });
   Cufon.replace('#center-content-body th', { fontFamily: 'Avenir-Heavy', textTransform: 'uppercase', textShadow: '1px 1px #17344e' });
   Cufon.replace('#county-map', { fontFamily: 'Avenir-Heavy', textShadow: '0px 1px #2C4F6E' })
   Cufon.replace('#footer-newsletter-copy', { fontFamily: 'Avenir-Heavy', textTransform: 'uppercase', textShadow: '1px 1px #fff, -1px -1px #ddd' });
   Cufon.replace('#ctl00_uc_cphToledoMainContent_uc_bodyEditor_BizFormControl1_Bizform1 input[type=submit]', { fontFamily: 'Avenir-Heavy', textTransform: 'uppercase', textShadow: '0px 1px #2C4F6E' });
   Cufon.replace('.title-block', { fontFamily: 'Avenir-Heavy', textTransform:'uppercase', textShadow: '0px 1px #efefef' });
   Cufon.replace('div#sidenav-inner-wrapper ul#uc_sidenavMenu .CMSListMenuHighlightedLI', { fontFamily: 'Avenir-Heavy', textTransform: 'uppercase', textShadow: '0px -1px #2C4F6E' });
   Cufon.replace('div#sidenav-inner-wrapper ul#uc_sidenavMenu .CMSListMenuHighlightedLInav-last', { fontFamily: 'Avenir-Heavy', textTransform: 'uppercase', textShadow: '0px -1px #2C4F6E' });
   Cufon.replace('#ctl00_uc_newsletterSubmit', { fontFamily: 'Avenir-Heavy', textTransform: 'uppercase', textShadow: '0px 1px #2C4F6E' });
   Cufon.replace('#footer-newsletter-copy', { fontFamily: 'Avenir-Heavy', textTransform: 'uppercase', textShadow: '1px 1px #fff, -1px -1px #ddd' });
   Cufon.replace('#news-nav-button-row', { fontFamily: 'Avenir-Heavy', textTransform: 'uppercase', textShadow: '0px 1px #17344e, 1px 1px #71889E' });
   Cufon.replace('#blog-nav-button-row', { fontFamily: 'Avenir-Heavy', textTransform: 'uppercase', textShadow: '0px 1px #17344e, 1px 1px #71889E' });
   Cufon.replace('#copyright', { fontFamily: 'Avenir-Medium', textShadow: '0px 1px #71889E' });
	$('.FormButton').each(function() {	
		$(this).hide().after('<span class="FormButton"></span>').next('span.FormButton').text($(this).val()).click(function () {
			$(this).prev('input.FormButton').click();
		});		
	});
	Cufon.replace('.FormButton', { fontFamily: 'Avenir-Heavy', textTransform: 'uppercase', textShadow: '0px 1px #2C4F6E' });
}

$(function () {     
	 $(".req-js").removeClass("req-js");
    $(".non-js").css("display","none");
    addClasses();
	 initVideoOverlays();		
    getNewWindowLinks();	
	 assignFonts();
    adjustSearches();
    clearMePrevious();
	 initCufon();
	 initSwitcher();
	 initMap();
	 initOverlay();
	 initVirtualPageTracking();
	 initEventTracking(); 
});
