var cookieHost = 'sixflags.com';
if(document.location.hostname.indexOf('sixflagsjobs.com')>-1) {cookieHost='sixflagsjobs.com';};
if(document.location.hostname.indexOf('sixflags.com.mx')>-1) {cookieHost='sixflags.com.mx';};
if(document.location.hostname.indexOf('laronde.com')>-1) {cookieHost='laronde.com';};


function extractParamFromUri(uri, paramName) {
	if (!uri) {return; } var uri = uri.split('#')[0]; var parts = uri.split('?'); if (parts.length == 1) {return;} var query = decodeURI(parts[1]); paramName += '='; var params = query.split('&'); for (var i = 0, param; param = params[i]; ++i) {if (param.indexOf(paramName) === 0) {return unescape(param.split('=')[1]);}}}

function trackEvent(eCategory, eAction, eLabel) {
	_gaq.push(['_trackEvent', eCategory, eAction, eLabel]);
}

function writeCookie(name,value,Minutes) {
	if (Minutes) {
		var date = new Date();
		date.setTime(date.getTime()+(Minutes*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+";domain="+cookieHost+";path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	writeCookie(name,"",-1);
}

function setSessionCookie(NameOfCookie, value) {
	writeCookie(NameOfCookie,value,0);
}

function deleteCookie (NameOfCookie) { 
	if (readCookie(NameOfCookie)) {
		document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}


// Tracking code for sixflags.com
// 
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-23632401-6']);  
_gaq.push(['_setDomainName', 'sixflags.com']);
_gaq.push(['_setLocalGifPath', '/__utm.gif']);
_gaq.push(['_setLocalRemoteServerMode']);
_gaq.push(['_addIgnoredOrganic', 'www.sixflags.com']);
_gaq.push(['_addIgnoredOrganic', 'sixflags.com']);
_gaq.push(['_addIgnoredOrganic', 'six flags.com']);
_gaq.push(['_addIgnoredOrganic', 'six-flags.com']);
_gaq.push(['_addIgnoredOrganic', 'laronde.com']);
_gaq.push(['_addIgnoredOrganic', 'www.six flags.com']);
_gaq.push(['_addIgnoredOrganic', 'six flags .com']);
_gaq.push(['_addIgnoredOrganic', 'www.laronde.com']);
_gaq.push(['_addIgnoredOrganic', 'http://www.sixflags.com']);
_gaq.push(['_addIgnoredOrganic', 'http://sixflags.com']);
_gaq.push(['_trackPageview']);  


(function() {    
	var ga = document.createElement('script'); 
	ga.type = 'text/javascript'; 
	ga.async = true;
	var dt = new Date();
	ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'content.sixflags.com/tracking/sfga2012.js';    
	var s = document.getElementsByTagName('script')[0]; 
	s.parentNode.insertBefore(ga, s);  
})();


//
//
// Make sure jQuery is loaded
//

var jQueryScriptOutputted = false;
function initJQuery() {
    if (typeof(jQuery) == 'undefined') {
        if (! jQueryScriptOutputted) {
            jQueryScriptOutputted = true;
            document.write("<scr" + "ipt type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js\"></scr" + "ipt>");
        }
        setTimeout("initJQuery()", 50);
    } else {
        $(function() {  
			jQuery(document).ready(function($) {
				var defaultOdds = 0;
				var secsUntilOpen = 5;
				function openInvite() {
					trackEvent('Research','Web Sat Survey Invite','Displayed');
					$.getScript('https:' == document.location.protocol ? 'https://' : 'http://' + 'content.sixflags.com/tracking/survey/surveyinvite.js');
				}
				function isPageOK() {
					var refloc = document.location.href.toLowerCase();
					var myHost = document.location.hostname.toLowerCase();
					if(myHost.indexOf('feedback.sixflags.com')>-1) {return false;}
					if(myHost.indexOf('frightfest.sixflags.com')>-1) {return false;}
					if(refloc.indexOf('172.30.3.11')>-1) {return false;}
					if(refloc.indexOf('/no-injection/')>-1) {return false;}
					if(refloc.indexOf('/iw-mount/')>-1) {return false;}
					return true;
				}
				if(readCookie("sfPageCounter") === null) {					// Set page counter if it is 0
					setSessionCookie("sfPageCounter",0);
					setSessionCookie("sfPageToShowSurvey", 1+Math.floor(Math.random()*15));
				}
				if(readCookie("sfPageCounter") >= 0) {						// Increment the page counter
					var counter = readCookie("sfPageCounter");
					counter = parseInt(counter) + 1;
					writeCookie("sfPageCounter",counter);
				}
				if(isPageOK()==true) {
					if(readCookie("sfGotSurvey") === null) {
						var currentPage = Number(readCookie("sfPageCounter"));
						var pageToShowSurvey = Number(readCookie("sfPageToShowSurvey"));
						if (currentPage >= pageToShowSurvey) {	
							writeCookie("sfGotSurvey",currentPage,60*24*30);
							setTimeout(function(){openInvite();}, secsUntilOpen*2000);
						}
					}
				}
				if(extractParamFromUri(document.URL,'t')=='test') {
					setTimeout(function(){openInvite();}, 5000);
				}
			});
        });
    }
}

   if (typeof(jQuery) != 'undefined') {
		$.expr[':'].regex = function(e, i, m) {
			var mP = m[3].split(','),
				l = /^(data|css):/,
				a = {
					method: mP[0].match(l) ? mP[0].split(':')[0] : 'attr',
					property: mP.shift().replace(l, '')
				},
				r = new RegExp(mP.join('').replace(/^\s+|\s+$/g, ''), 'ig');
			return r.test($(e)[a.method](a.property));
		};
		$('a:regex(href,"\\.(zip|mp\\d+|mpe*g|pdf|docx*|pptx*|xlsx*|jpe*g|png|gif|tiff*)")$').live('click', function(e) {
			_gaq.push(['_trackEvent', 'download', 'click', this.href.replace(/^.*\/\//, '')]);
		});
		$('a[href^="mailto"]').live('click', function(e) {
			_gaq.push(['_trackSocial', 'email', 'send', this.href.replace(/^mailto:/i, '')]);
		});
		$('a[href^="http"]:not([href*="//' + location.host + '"])').live('click', function(e) {
			var dest = this.href.match(/\/\/([^\/]+)/)[1];
			var intSites = 'www.sixflags.com|feedback.sixflags.com|sixflags.com|content.sixflags.com|sixflagsjobs.com|www.sixflagsjobs.com|hotels.sixflags.com|investors.sixflags.com';
			if(intSites.indexOf(dest,0)<0) {
				_gaq.push(['_trackEvent', 'Outbound Traffic', 'click', this.href.match(/\/\/([^\/]+)/)[1]]);
			}
		});
	
		if(location.host=='feedback.sixflags.com' || location.host=='frightfest.sixflags.com' ) {
			var _ga = _ga || {};
			var div = document.createElement('div');
			div.id = 'fb-root';
			if (document.body.firstChild) {
					document.body.insertBefore(div, document.body.firstChild);
			} else {
					document.body.appendChild(div);
			}
			(function() {
			   var e = document.createElement('script'); e.async = true;
				e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
				document.getElementById('fb-root').appendChild(e);
			}());
			window.fbAsyncInit = function() {
				FB.init({appId: '146917552078012', status: true, cookie: true, xfbml: true});
				_ga.trackFacebook();
			};
			_ga.buildTrackerName_ = function(opt_trackerName) {return opt_trackerName ? opt_trackerName + '.' : '';};
			_ga.trackFacebook = function(opt_pageUrl, opt_trackerName) {
				var trackerName = _ga.buildTrackerName_(opt_trackerName);
				try {
				   if (FB && FB.Event && FB.Event.subscribe) {
						FB.Event.subscribe('edge.create', function(targetUrl) {_gaq.push([trackerName + '_trackSocial', 'facebook', 'like', targetUrl, opt_pageUrl]);});
						FB.Event.subscribe('edge.remove', function(targetUrl) {_gaq.push([trackerName + '_trackSocial', 'facebook', 'unlike', targetUrl, opt_pageUrl]);});
						FB.Event.subscribe('message.send', function(targetUrl) {_gaq.push([trackerName + '_trackSocial', 'facebook', 'send', targetUrl, opt_pageUrl]);});
						FB.Event.subscribe('comment.create', function(targetUrl) {_gaq.push([trackerName + '_trackSocial', 'facebook', 'comment', targetUrl, opt_pageUrl]);});
						FB.Event.subscribe('comment.remove', function(targetUrl) {_gaq.push([trackerName + '_trackSocial', 'facebook', 'comment-remove', targetUrl, opt_pageUrl]);});
					}
				} catch (e) {}
			};
			var e = document.createElement('script'); e.type="text/javascript"; e.async = true; 
			e.src = 'http://platform.twitter.com/widgets.js';
			var opt_pageUrl = cleanURL(document.URL.toLowerCase());
			(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(e);
			$(e).load(function() {
				function clickEventToAnalytics(intent_event) {if (intent_event) {var label = intent_event.region;_gaq.push(['_trackSocial', 'twitter', intent_event.type, label, opt_pageUrl]);};}       
				function tweetIntentToAnalytics(intent_event) {if (intent_event) {var label = intent_event.data.tweet_id; _gaq.push(['_trackSocial', 'twitter', intent_event.type, label, opt_pageUrl]);};}       
				function favIntentToAnalytics(intent_event) {tweetIntentToAnalytics(intent_event);}       
				function retweetIntentToAnalytics(intent_event) {if (intent_event) {var label = intent_event.data.source_tweet_id;_gaq.push(['_trackSocial', 'twitter', intent_event.type, label, opt_pageUrl]);};}       
				function followIntentToAnalytics(intent_event) {if (intent_event) {var label = intent_event.data.user_id + " (" + intent_event.data.screen_name + ")";_gaq.push(['_trackSocial', 'twitter', intent_event.type, label, opt_pageUrl]);};}       
				twttr.events.bind('click',    clickEventToAnalytics);
				twttr.events.bind('tweet',    tweetIntentToAnalytics);
				twttr.events.bind('retweet',  retweetIntentToAnalytics);
				twttr.events.bind('favorite', favIntentToAnalytics);
				twttr.events.bind('follow',   followIntentToAnalytics);
			});
		};

   };




initJQuery();




