var debug = false;
var sc_send = true;
var Url = UrlCoder();
var oGet = getUrlVars();
var pathname = Url.decode(window.location.pathname);
var s = s_gi('mmediaChicagoWidgetDev');
    	

try { console.log('init console... done'); } catch(e) { console = { log: function() {} } }

//console.log(pathname);


// for now no https, converstion is 'book now'
// if ( window.location.protocol == "https:" ) { sc_send = false; }

// detect section
if ( pathname.indexOf("nexres/reservations/availability") > -1 ) {		
	
		s.pageName = "Hotel Property Deals View / "+str_replace("'","",$('.hotelname h1').html());
		s.eVar15 = s.prop15 = pathname;
		

} else if ( pathname.indexOf("nexres/reservations/reserve") > 1 ) {
		// purchase confirmation - conversion event
		s.pageName = "Hotel Reservation Confirmation / "+str_replace("'","",$('.hotel-table td:first').html());
		
	
} else if ( pathname.indexOf("nexres/reservations/resdata") > 1 ) {
	
		s.pageName = "Hotel Payment Information / "+str_replace("'","",$('.hotelname a').html());
		
		
} else if ( pathname.indexOf("nexres/reservations/review") > -1 ) {
	
		s.pageName = "Hotel Review Your Itinerary / "+str_replace("'","",$('.hotelname a').html());
		
		
} else if ( pathname.indexOf("nexres") > -1 && pathname.indexOf("nexres/activities") == -1  ) {
	
	   //hotel search
	if ( oGet['interstitial'] == 'done' ) {
		s.pageName = "Hotel Search Results";
		s.eVar3 = s.prop3 =  oGet['chk_in'];
		s.eVar4 = s.prop4 =  oGet['chk_out'];
		s.eVar7 = s.prop7 =  oGet['num_adults'];
		s.eVar8 = s.prop8 =  oGet['num_children'];
		s.eVar9 = s.prop9 =  oGet['num_rooms'];
	} else {
		// preliminary loading page, do not track
		sc_send = false;
	}
	
} else if ( pathname.indexOf("/hotel/") > -1 ) {	//Hotel Property View
	
	if ($('.bookNow').length > 0) {
		
		s.pageName = "Hotel Property Rates View / "+str_replace("'","",$('.hotelname h1').html());
		
		//$('.bookNow').click(function(){
			// until completion of report suite
			//var bookData =  getUrlVars(Url.decode($(this).attr('href'))) ;
			//console.log(bookData);			
		//});
		
	} else {
		
		s.pageName = "Hotel Property View / "+str_replace("'","",$('.hotelname h1').html());
	}
	s.eVar15 = s.prop15 = pathname;
	
} else if ( pathname.indexOf("flights") > -1 && pathname.indexOf("InitialSearch.do") == -1) {	
	
	//Flight search
	s.pageName = "Flight Search Results";
	
} else if ( pathname.indexOf('checkout/CheckoutReview.do') > -1 ) {
	
	s.pageName = "Flight Booking Confirmation";	
	sc_send = true;				
//	s.eVar3 = s.prop3 =  form.chk_in.value;
//	s.eVar4 = s.prop4 =  form.chk_out.value;
//	s.eVar5 = s.prop5 =  form.dateLeavingTime.value;
//	s.eVar6 = s.prop6 =  form.dateReturningTime.value;
//	s.eVar7 = s.prop7 =  form.num_adults.value;
//	s.eVar8 = s.prop8 =  form.num_children.selectedIndex;
//	s.eVar10 = s.prop10 =  form.leavingFrom.value

} else if ( pathname.indexOf("flights/InitialSearch.do") > -1 ) {
	
    s.pageName = "Flight Search Form";
    document.getElementById('submitButton').onclick=function(){
    	    	
    	var s = s_gi('mmediaChicagoWidgetDev');
    	s.linkTrackVars="pageName,eVar10,prop10,eVar3,prop3,eVar4,prop4,eVar5,prop5,eVar6,prop6,eVar7,prop7,eVar8,prop8";
                
    	s.pageName = "Flight Search Submit";
    	
		s.eVar10 = s.prop10 =  document.AirSearchForm.leavingFrom.value			
		s.eVar3 = s.prop3 =  document.AirSearchForm.leavingDateExact.value;
		s.eVar4 = s.prop4 =  document.AirSearchForm.returningDateExact.value;
		s.eVar5 = s.prop5 =  document.AirSearchForm.dateLeavingTime.value;
	
		s.eVar6 = s.prop6 =  document.AirSearchForm.dateReturningTime.value;
		s.eVar7 = s.prop7 =  document.AirSearchForm.adults.value;
		s.eVar8 = s.prop8 =  document.AirSearchForm.minorsAge0.selectedIndex;
		    	
		s.tl(false,'o',s.pageName);
		
		s.prop3=s.prop4=s.prop5=s.prop6=s.prop7=s.prop8=s.prop10 = "";
    	    	
    };
    
} else if ( pathname.indexOf("trips") > -1 ) {
	
	//AirHotel search
	s.pageName = "AirHotel Search Results";
	
} else if ( pathname.indexOf("trips/InitialSearch.do") > -1 ) {
	
	s.pageName = "AirHotel Search Form";
    document.getElementById('submitButton').onclick=function(){
    	    	
    	var s = s_gi('mmediaChicagoWidgetDev');
    	s.linkTrackVars="pageName,eVar10,prop10,eVar3,prop3,eVar4,prop4,eVar5,prop5,eVar6,prop6,eVar7,prop7,eVar8,prop8";
                
    	s.pageName = "AirHotel Search Submit";
    	
		s.eVar3 = s.prop3 =  document.AirSearchForm.leavingDateExact.value;
		s.eVar4 = s.prop4 =  document.AirSearchForm.returningDateExact.value;
		s.eVar5 = s.prop5 =  document.AirSearchForm.dateLeavingTime.value;
	
		s.eVar6 = s.prop6 =  document.AirSearchForm.dateReturningTime.value;
		s.eVar7 = s.prop7 =  document.AirSearchForm.adults.value;
		s.eVar8 = s.prop8 =  document.AirSearchForm.minorsAge0.selectedIndex;
		s.eVar10 = s.prop10 =  document.InitialSearchForm.userTypedFrom.value			
		
		    	
		s.tl(false,'o',s.pageName);
		
		s.prop3=s.prop4=s.prop5=s.prop6=s.prop7=s.prop8=s.prop10 = "";
    	    	
    };
    
} else if ( pathname.indexOf("car/carPmrDetails.do") > -1 ) {
	
	//Car search
	s.pageName = "Car Rental Details Page";
	s.eVar3 = s.prop3 =  oGet.pickupMonth+"/"+oGet.pickupDay+"/"+oGet.pickupYear;
	s.eVar4 = s.prop4 =  oGet.dropoffMonth+"/"+oGet.dropoffDay+"/"+oGet.dropoffYear;
	s.eVar5 = s.prop5 =  oGet.pickupTime;
	s.eVar6 = s.prop6 =  oGet.dropoffTime;
	s.eVar11 = s.prop11	=  oGet.itemLOBId+" / "+oGet.vendorCode;
	s.eVar12 = s.prop12 =  oGet.carType;
	
	$('.btn_prim').click( function(){
		
		var s = s_gi('mmediaChicagoWidgetDev');
    	s.linkTrackVars="pageName,eVar10,prop10,eVar3,prop3,eVar4,prop4,eVar5,prop5,eVar6,prop6,eVar7,prop7,eVar8,prop8";
                
    	s.pageName = "Car Rental Confirmation";
		s.eVar3 = s.prop3 =  oGet.pickupMonth+"/"+oGet.pickupDay+"/"+oGet.pickupYear;
		s.eVar4 = s.prop4 =  oGet.dropoffMonth+"/"+oGet.dropoffDay+"/"+oGet.dropoffYear;
		s.eVar5 = s.prop5 =  oGet.pickupTime;
		s.eVar6 = s.prop6 =  oGet.dropoffTime;
		s.eVar11 = s.prop11	=  oGet.itemLOBId+" / "+oGet.vendorCode;
		s.eVar12 = s.prop12 =  oGet.carType;
		    	
		s.tl(false,'o',s.pageName);
		
		s.prop3=s.prop4=s.prop5=s.prop6=s.prop11=s.prop12="";
		
	});
	
} else if ( pathname.indexOf("car") > -1 ) {
	
	//Car search
	s.pageName = "Car Rental Search Results";
	
} else if ( pathname.indexOf("activities/detail") > -1 ) {	
	
	//Activities search
	s.pageName = "Activity Detail Page / "+str_replace("'","",$('.dguidetitle').html().split('<br>')[0].trim());
	
	
} else if ( pathname.indexOf("activities") > -1 ) {	
	
	//Activities search
	s.pageName = "Activity Search Results";
	if (oGet.fromdate) {
		s.eVar3 = s.prop3 =  Url.decode(oGet.fromdate);
		s.eVar4 = s.prop4 =  Url.decode(oGet.todate);
	} else if (oGet.chk_in){
		s.eVar3 = s.prop3 =  Url.decode(oGet.chk_in);
		s.eVar4 = s.prop4 =  Url.decode(oGet.chk_out);	
	}
	var sActivities = "";
	sActivities += oGet.ACTTYPE_all == 1 ? "All / " : "";
	sActivities += oGet.ACTTYPE_attraction == 1 ? "Attractions / " : "";
	sActivities += oGet.ACTTYPE_dining == 1 ? "Dining / " : "";
	sActivities += oGet.ACTTYPE_outdoor == 1 ? "Outdoors / " : "";
	sActivities += oGet.ACTTYPE_shows == 1 ? "Shows / " : "";
	sActivities += oGet.ACTTYPE_tours == 1 ? "Tours / " : "";
	sActivities += oGet.ACTTYPE_transport == 1 ? "Transportation / " : "";
	
	s.eVar13 = s.prop13 = sActivities;
	
	
} else {
	
	//unknown	
	s.pageName = pathname;
}

console.log(s.pageName);

s.prop1 = s.pageName;

if (sc_send) {
	var s_code=s.t();if(s_code)document.write(s_code)
}

s.prop1=s.prop2=s.prop3=s.prop4=s.prop5=s.prop6=s.prop7=s.prop8=s.prop9=s.prop11=s.prop13=s.prop14="";


function getUrlVars(url)
{
    var vars = [], hash;
    if (url) {
    	var hashes = url.slice(url.indexOf('?') + 1).split('&');
    	
    } else {
    	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    	
    }
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    return vars;
}


function str_replace (search, replace, subject, count) {
    // Replaces all occurrences of search in haystack with replace  
    // 
    // version: 908.406
    // discuss at: http://phpjs.org/functions/str_replace
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}

/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Justas | http://www.webtoolkit.info/ */
function UrlCoder() 
{
	return {

 	// public method for URL encoding
 	encode : function (string) {
 		 return escape(this._utf8_encode(string));
 	},

 	// public method for URL decoding
	 decode : function (string) {
 	 	return this._utf8_decode(unescape(string));
 	},

 	// private method for UTF-8 encoding
 	_utf8_encode : function (string) {
  		string = string.replace(/\r\n/g,"\n");
 	 	var utftext = "";

  		for (var n = 0; n < string.length; n++) {
   			var c = string.charCodeAt(n);
   			if (c < 128) {
    				utftext += String.fromCharCode(c);
 			} else if((c > 127) && (c < 2048)) {
  				utftext += String.fromCharCode((c >> 6) | 192);
  				utftext += String.fromCharCode((c & 63) | 128);
 			} else {
  				utftext += String.fromCharCode((c >> 12) | 224);
  				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
 	 			utftext += String.fromCharCode((c & 63) | 128);
 			}
 	}

		return utftext;
	},

 	// private method for UTF-8 decoding
 	_utf8_decode : function (utftext) {
 		 var string = "";
 		 var i = 0;
 		 var c = c1 = c2 = 0;

  		while ( i < utftext.length ) {
  			 c = utftext.charCodeAt(i);
   			if (c < 128) {
    				string += String.fromCharCode(c);
    				i++;
  			 } else if((c > 191) && (c < 224)) {
 				   c2 = utftext.charCodeAt(i+1);
    				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
    				i += 2;
  			 } else {
 				   c2 = utftext.charCodeAt(i+1);
    				c3 = utftext.charCodeAt(i+2);
    				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
   				 i += 3;
 			  }
		  }
		return string;
	 }
}
}