var flashVersion = "0" ;
var pageTracker ;

var popOpen = {
	init:function() {
		//alert("idiot") ; //IE6 won't make a popup unless this is uncommented.
		$("a.footer,a.page404").click( function() {
				var spec="height=600,width=600,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,titlebar=no,toolbar=no";
				var destination = $(this).attr('href');
				var footerWindow=window.open(destination,"footerWin",spec);
				return false ;
		});
	}
}

// show link' url in parent window
// add swf address for flash
var popLink = { 
	init:function(){
		$(".parent_link").click( function() {
			var flashVersion = getFlashVersion().split(',').shift() ;
			if(flashVersion >= 9 ) {
				// make swf address work
				var destination = $(this).attr('href');
				var page = destination.substring(context_root.length) ;
				destination = context_root + "/#" + page ;
				if (page != "") {
					if (window.opener !=null) {
						window.opener.location.href = destination;
						//window.opener.SWFAddress.setValue(page) ;
						// would like to tell just the flash to reload
						// can't spend any more time on this right now
						window.opener.location.reload();
						window.close(); 
					} else {
						// Hack for IE which for some reason refuses to open the sitemap in a popup window
						window.location.href = destination;
					}
				}
				
			} else if (window.opener != null) {
				window.opener.location.href = $(this).attr('href');
			}
			
			return false; 
		});
	}
}

var clickListeners = {
	init:function() {
		$("#cold_vs_flu_download").click( function(event) {
				trackItem('/homepage/RecognizingFlu/DownloadColdvsFlu'); 
				fireDoubleclick('flumi349');
		});
		
		$("#tracker_cdc_link").click( function(event) {
				trackItem('/homepage/WhereIsFluTodayFluTracker/CDC');
		});
		
		$("#flumist_landing_link").click( function(event) {
				trackItem('/homepage/YourVaccinationOptions/LearnMoreAboutVaxOptions'); 
				fireDoubleclick('flumi793');
		});
		
		$("#big_download_book").click( function(event) {
				trackItem('/homepage/BigShareDownloads/FluAndYou-Download'); 
				fireDoubleclick('flumi263');
		});
		
		$("#big_download_pack").click( function(event) {
				trackItem('/homepage/BigShareDownloads/DownloadFreePack-Download'); 
				fireDoubleclick('flumi881');
		});
		
		$("#big_download_guide").click( function(event) {
				trackItem('/homepage/BigShareDownloads/DownloadFreeGuide-Download'); 
				fireDoubleclick('flumi754');
		});
		
		$("#big_register_link").click( function(event) {
				trackItem('/homepage/BigShareDownloads/Register'); 
				fireDoubleclick('flumi108');
		});
		
		$("#full_article_download_1").click( function(event) {
				trackItem('/homepage/WhySharingIsGood/BrowseArticle1');
				fireDoubleclick('flumi612');
		});
		
		$("#full_article_download_2").click( function(event) {
				trackItem('/homepage/WhySharingIsGood/BrowseArticle2'); 
				fireDoubleclick('flumi612');
		});
		
		$("#full_article_download_3").click( function(event) {
				trackItem('/homepage/WhySharingIsGood/BrowseArticle3'); 
				fireDoubleclick('flumi612');
		});
		
		$("#prevent_flumist_landing_link").click( function(event) {
				trackItem('/homepage/PreventingtheFluHowNotShare/LearnVaccineOptions'); 
				fireDoubleclick('flumi986');
		});
		
		$("#prevent_tips_download").click( function(event) {
				trackItem('/homepage/PreventingtheFluHowNotShare/DownloadPreventionTips'); 
				fireDoubleclick('flumi985');
		});
		
		$("#nav_cdc_link").click( function(event) {
				trackItem('/homepage/WhatIsFlu/CDC');
		});
		
	}
}
	
$(document).ready( 
	function(){
		flashVersion = getFlashVersion().split(',').shift() ;
		popOpen.init();
		popLink.init();
		
		clickListeners.init() ;
		//swfAddress.init();
		// set Google Analytics account number
		try {
			pageTracker = _gat._getTracker("UA-9872689-2");
		} catch(err) {}
		
		/* only do Google Analytics tracking if flash is absent or disabled */
		if( flashVersion < 9 ) {
			// track page load in GA
			trackItem(googlePageName);
			appendDoubleclick() ;
		}
		
	}
);

function getFlashVersion() { 
	// ie 
	try { 
		try { 
			// avoid fp6 minor version lookup issues 
			// see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/ 
			var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6'); 
			try { axo.AllowScriptAccess = 'always'; 
			} catch(e) { return '6,0,0'; } 
		} catch(e) {
		} 
		return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1]; 
		// other browsers 
	} catch(e) { 
		try {
		  if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){ 
			return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1]; 
		  } 
		} catch(e) {
		} 
	} 
	return '0,0,0'; 
}

function openFlashPage() {
	flashVersion = getFlashVersion().split(',').shift() ;
	if(flashVersion >= 9 ) {
		// make swf address work
		var destination = location.pathname;
		var page = destination.substring(context_root.length) ;
		if (! page.match("/#/") && page != "" && page != "/") {
			// don't go to the flash if the page is unknown
			
			if (pageIsInList(page)) {
				destination = context_root + "/#" + page ;
				window.location.href = destination;
			}
		}
				
	} 
}

/* For Flash + Google Analytics */
function trackItem(str){
	//console.log("tracking: " + str) ;
	if (str != null && str != "") {
		try{
			pageTracker._trackPageview(str);
		}catch(err){
			//console.log("error tracking pageview");
		};
	}
}

/* For non-flash DoubleClick tags on pageload */
function appendDoubleclick() {
	/* only if no flash */
	if(flashVersion < 9 && doubleclickCat != null && doubleclickCat != "") {
		var axel = Math.random() + ""; 
		var a = axel * 10000000000000; 

		$(document.body).append('<iframe id="doubleclick_ifr" src="http://fls.doubleclick.net/activityi;src=2378231;type=flumi985;cat=' + doubleclickCat + ';ord=' + a + '?" width="1" height="1" frameborder="0"></iframe>'); 
	}
}

/* for firing onclick events */
function fireDoubleclick(str) {
	var axel = Math.random() + ""; 
	var a = axel * 10000000000000; 
	
	var url = "http://fls.doubleclick.net/activityi;src=2378231;type=flumi985;cat=" + str + ";ord=" + a + "?" ;
	$("#doubleclick_ifr").attr('src', url);
}

/* checks to see if the page we're trying to reach is in the list of possible pages */
function pageIsInList(str) {
	var pageList = ["influenza-virus", "stomach-flu", "how-the-flu-spreads", "flu-contagious", "flu-vaccine", 
			"avoiding-the-flu", "pandemic-flu", "flu-vaccination", "flu-symptoms", "preventing-flu", "flu-tracker",
			"kids-sharing", "kids-and-sharing", "kids-sharing-toys", "flu-vaccine-medimmune", "flu-vaccine-registration" ] ;
	
	for(page in pageList) {
		if (str.match(pageList[page]) != null) {
			return true ;
		}
		
	}
	
	return false ;
}


