var $J = jQuery.noConflict();
var activateSurvey;
jQuery(document).ready(function ($){

	if(typeof(Shadowbox) == "object") {

		var options = {
			resizeLgImages:     false,
			displayNav:         false,
			handleUnsupported:  'remove',
			keysClose:          ['c', 27], // c or esc
			autoplayMovies:     false
		};
	
		Shadowbox.init(options);
		//console.log("init shadowbox!");
	}
	
							 
	function showSurvey() {
		setTimeout(function() {
		Shadowbox.open({
			type:		'iframe',
			player:     'html',
			title:      'Welcome',
			content:    '/survey/default.aspx',
			height:     450,
			width:      700
		});
		}, 1000);
	}
	

	if (activateSurvey) {
	//	showSurvey();
	}
	
								 
	//var allLinks = $("A[@href^=http://]");
	
	if (typeof(trackingPrefix) != "string") {
		trackingPrefix = "";
	}
	else {
		trackingPrefix = "/" + trackingPrefix ;
	}	
	
	// find all links to external sites, and enable tracking
	var allLinks = $("A[@href^=http://]:not([@href^=http://www.gophila.com])");
	allLinks.each(											 
		function(i){
			var thisHref = $(this).attr("href");
			thisHref = thisHref.replace("http://", "");			// remove http
			if (thisHref.charAt(thisHref.length - 1) == "/")	// trim ending slash
				thisHref = thisHref.substr(0,thisHref.length - 1);
			//console.log(thisHref);
			$(this).click(function() { 
				trackUrl = trackingPrefix + '/outbound/' + thisHref;
				console.log("TRACKING ALL LINKS:  " + trackUrl);
				if (pageTracker) 
					pageTracker._trackPageview(trackUrl);
			})

 	});
	
	var allLinks = $("AREA[@href^=http://]:not([@href^=http://www.gophila.com])");
	
	
	allLinks.each(											 
		function(i){
			var thisHref = $(this).attr("href");
			thisHref = thisHref.replace("http://", "");			// remove http
			if (thisHref.charAt(thisHref.length - 1) == "/")	// trim ending slash
				thisHref = thisHref.substr(0,thisHref.length - 1);
			//console.log(thisHref);
			$(this).click(function() { 
				trackUrl = trackingPrefix + '/outbound/' + thisHref;
				console.log("TRACKING ALL LINKS:  " + trackUrl);
				if (pageTracker) 
					pageTracker._trackPageview(trackUrl);
			})

 	});	
	
	var pageHasTrip = $("SCRIPT[@src^=http://www.tripadvisor.com/WidgetEmbed-cdsdmo]");
									
	var parseTrip = false;
	if (pageHasTrip.length > 0) {
		console.log("trip advisor found on page");	
		parseTrip = true;
	}
	
	if (parseTrip) {
		var findTrip = setInterval(function() {
			var tripAdvisor = $(".widDMO");	// need to refer to an element rendered by the javascript
			//console.log("waiting for trip to load");
			if (tripAdvisor.length > 0) {
		
				var tripAdvisorLink = $("A[@href^=javascript:doPopup('http://www.tripadvisor.com/WidgetEmbed]");
				console.log(tripAdvisorLink);
				if (tripAdvisorLink.length > 0) {
					tripAdvisorLink.each(											 
						function(i){
							var thisHref = $(this).attr("href");
							thisHref = thisHref.replace("javascript:doPopup('http://", "");			// remove http
							//thisHref = thisHref.split("?")[0];					
							thisHref = thisHref.replace("&display=true', 'review');","");
							thisHref = thisHref.replace("&lang=en_US&partnerId=&format=html&num=1&","/");
							thisHref = thisHref.replace("WidgetEmbed-cdssinglereview?","");
							thisHref = thisHref.replace("www.tripadvisor.com","");
							pageTitle = $("TITLE");
							pageTitleText = pageTitle.text().split("::")[0];
							pageTitleText = $.trim(pageTitleText);
							thisHref = "TripAdvisorWidget/" + pageTitleText + thisHref;
					
							if (thisHref.charAt(thisHref.length - 1) == "/")	// trim ending slash
								thisHref = thisHref.substr(0,thisHref.length - 1);
							//console.log(thisHref);
							$(this).click(function() { 						
								if (pageTracker) {
									console.log("tracking " + thisHref);
									pageTracker._trackPageview('/outbound/' + thisHref);
								}
							})
					});		
				}
				console.log("fixed trip");
				clearInterval(findTrip);
			}
		},500);
		setTimeout(function() { clearInterval(findTrip); console.log("ending trip search")}, 10000); // clear after 10 seconds regardless
	}
														
	
	var allDownloads = $("A[@href^=/assets/dmt/downloads/]");
	allDownloads.each(											 
		function(i){
			var thisHref = $(this).attr("href");
			thisHref = thisHref.replace("/assets/dmt/downloads/", "");			// remove http
			//console.log(thisHref);
			$(this).click(function() { 
				console.log("tracking " + thisHref);
				if (pageTracker) 
					pageTracker._trackPageview('/downloads/' + thisHref);
			})

 	});
	
	var allPdf = $("a[href$='.pdf']:not([@href^=/assets/dmt/downloads/])");
	allPdf.each(											 
		function(i){
			var thisHref = $(this).attr("href");
			thisHref = thisHref.replace("http://www.gophila.com/", "");	
			//console.log(thisHref);
			$(this).click(function() { 
				console.log("tracking " + thisHref);
				if (pageTracker) 
					pageTracker._trackPageview('/downloads/' + thisHref);
			})

 	});	
	
	var allMp3 = $("a[href$='.mp3']:not([@href^=/assets/dmt/downloads/])");
	allMp3.each(											 
		function(i){
			var thisHref = $(this).attr("href");
			thisHref = thisHref.replace("http://www.gophila.com/", "");	
		//	console.log(thisHref);
			$(this).click(function() { 
				console.log("tracking " + thisHref);
				if (pageTracker) 
					pageTracker._trackPageview('/downloads/' + thisHref);
			})

 	});	
	
	if (setupAAA) setupAAA();
	
	
}
);