/*
	Last published on : 01-Jul-10 17:58
	Location          : \commercial\destinationguide\cms\destinationguide_onlrb\static\js
	Filename          : dg-webtrends.js
*/
	
	function onLoadMetron(source,text) {
		//alert("load "+source);
	    metron.measureAndOverwriteVariable("z_application", "DG");
		metron.measureAndOverwriteVariable("z_country", countryLocale);
		metron.measureAndOverwriteVariable("z_language", langLocale);
		
		if(loc.match("index.htm")&& continent=="" && source=="body") { 
			metron.measureAndOverwriteVariable("ti", "Worldmap");
			metron.measureAndOverwriteVariable("dcsuri", "Worldmap.htm");
		}
		
		if(loc.match("index.htm")&& continent=="" && source=="top5cities") {
			metron.measureAndOverwriteVariable("ti", "Worldmap");
			metron.measureAndOverwriteVariable("dcsuri", "Worldmap.htm");
		    measureEvent("Clicked","link_worldtop5","World",text);
		}
		if(continent!="") { 
			metron.measureAndOverwriteVariable("dcsuri",getFromContinentToTopicFormatValues('/')+".htm");
			metron.measureAndOverwriteVariable("z_dg_continent", continent);
			
			if(source=="top5cities") measureEvent("Clicked","link_worldtop5",document.getElementById("pageAreaType").value,text);
			else if(source=="discover") measureEvent("Clicked","link_discover",getLoadCityTopic(),text);
			
			metron.measureAndOverwriteVariable ("ti",stringToLowerFirstCap(document.getElementById("pageAreaType").value));
		}
		if(subcontinent!="") metron.measureAndOverwriteVariable ("z_dg_subcontinent", subcontinent);
		if(country!="") metron.measureAndOverwriteVariable ("z_dg_country", country);
		if(city!="") { metron.measureAndOverwriteVariable ("z_dg_city", city); metron.measureAndOverwriteVariable ("ti",trimTopicPage());}
		if(topic!="") { 
				metron.measureAndOverwriteVariable ("z_dg_topic", topic);
				getLoadCityTopic();
				if(source == "dgmenu"){ 
					measureEvent("Clicked","menu_left",getLoadCityTopic(),stringToLowerFirstCap(ltrim(trimCity(city,text))) );
					metron.measureAndOverwriteVariable ("ti",trimTopicPage());
				}
		}
		metron.measuresCommit();	
	}
	
	/**
	* This function is used in tagging the city and topic pages
	*/
	function trimTopicPage(){
		var topicPage = "";
		topicPage = getLoadCityTopic().toString().indexOf("City - ") != -1 ? getLoadCityTopic().replace("City - ","") : "";
		return topicPage;
	}
	
	function measureEvent(event,eventType,eventPlace,eventValue){
		metron.measureAndOverwriteVariable ("z_dg_event", event);
		metron.measureAndOverwriteVariable ("z_dg_eventtype",eventType);
		metron.measureAndOverwriteVariable ("z_dg_eventplace",eventPlace);
		metron.measureAndOverwriteVariable ("z_dg_eventvalue",eventValue);
	}
	
	/**
	* Returns the formatted values after parsing from continent to topic page url
	*/
	function getFromContinentToTopicFormatValues(separator){
		var fValues; 
		fValues = continent;
		if(subcontinent != ""){
			if(subcontinent.toString() == "Not available") fValues;
			else fValues = fValues + separator + subcontinent;
		}else fValues;
		country != "" ? fValues = fValues + separator + country : fValues;
		city != "" ? fValues = fValues + separator + city : fValues;
		topic != "" ? fValues = fValues + separator + topic : fValues;
	
		return fValues;
	}
	
	function getCityName(){
		var cityPage = document.getElementById("cityPage").value;
		if(cityPage == "city") return city;
	}
	
	function getFromCityToTopic(){
		var fValues = ""; 
		var separator = "_";
		var cityPage = document.getElementById("cityPage").value;
		if(cityPage == "city"){
			fValues = city + separator + topic;
		}
		return fValues;
	}
	
	function getLoadCityTopic() {
		var topicCode = "";
		var cityPage = document.getElementById("cityPage").value;
		var infoPage = document.getElementById("mfInfoPage").value;
		var eventPlace = "";
		if(cityPage == "city"){
			topicCode = infoPage.substring(infoPage.lastIndexOf('|')+1,infoPage.length);
			pageTopicCode = topicCode.substring(0,topicCode.indexOf("page"));
			//alert("topic = "+topicCode.substring(0,topicCode.indexOf("page")));
			eventPlace = getCityEventPlace(topicCode.substring(0,topicCode.indexOf("page")))
		}
		//alert("load city topic = "+eventPlace);
		return eventPlace;
	}
	
	function getCityEventPlace(topicCode) {
		var eventPlace = "City - ";
		if(rtrim(topicCode) == "city")	eventPlace = eventPlace + "Overview";
		else if(rtrim(topicCode) == "event" || rtrim(topicCode) == "event-map") eventPlace = eventPlace + "Events";
		else if(rtrim(topicCode) == "ttd" || rtrim(topicCode) == "ttd-map") eventPlace = eventPlace + "Things to do";
		else if(rtrim(topicCode) == "shop" || rtrim(topicCode) == "shop-map") eventPlace = eventPlace + "Shopping";
		else if(rtrim(topicCode) == "rest" || rtrim(topicCode) == "rest-map") eventPlace = eventPlace + "Restaurants";
		else if(rtrim(topicCode) == "nl" || rtrim(topicCode) == "nl-map") eventPlace = eventPlace + "Nightlife";
		else if(rtrim(topicCode) == "practical") eventPlace = eventPlace + "Practical Information";
		else if(rtrim(topicCode) == "airport") eventPlace = eventPlace + "Airport";
		else if(rtrim(topicCode) == "travel") eventPlace = eventPlace + "Transportation";
		else if(rtrim(topicCode) == "weather") eventPlace = eventPlace + "Weather";
		else if(rtrim(topicCode) == "themedtour") eventPlace = eventPlace + "Themed Tours"; 
		else "unknown";
		return eventPlace;
	}
		
	function getPageTopicCode(){
		return pageTopicCode;
	}
	
	/**
	* Get the parameter trail from the flash and reports the values and events to webtrends
	*/
	function getSelectAreaType(eventtype){
		var areaTypeCode = "";
		var areaTypeName = "";
		
		var paramList = [];
		var isSubConAvail = true;
		var continent = "";
		var subcontinent ="";
		var country ="";
		var city = "";
		
		if(parameterTrail != ""){
			areaTypeCode = parameterTrail.substring(parameterTrail.lastIndexOf('_')-1,parameterTrail.length);
			areaTypeCode = areaTypeCode.substring(0,1);
		}
		
		paramList = parameterTrail.split('_');
		if(paramList.length > 4 && paramList[4] == 4){ // check after continent trail
			isSubConAvail = false;
		}
		if(isSubConAvail == false){
			switch (parseInt(areaTypeCode)) {
				case 1: break;
				case 2: continent = paramList[3]; select = "continent"; break;
				case 4: continent = paramList[3]; subcontinent = "Not available"; country = paramList[5]; select = "country"; break;
				case 5: continent = paramList[3]; subcontinent = "Not available"; country = paramList[5]; city = paramList[7];select = "city"; break;
				default:'unknown';
		    }
		}else{
			switch (parseInt(areaTypeCode)) {
				case 1: continent = "World"; break;
				case 2: continent = paramList[3]; select = "continent"; break;
				case 3: continent = paramList[3]; subcontinent = paramList[5]; select = "subcontinent"; break;
				case 4: continent = paramList[3]; subcontinent = paramList[5]; country = paramList[7]; select = "country"; break;
				case 5: continent = paramList[3]; subcontinent = paramList[5]; country = paramList[7]; city = paramList[9];select = "city"; break;
				default:'unknown';
			}
		}
		metron.measureAndOverwriteVariable("z_application", "DG");
		metron.measureAndOverwriteVariable("z_country", countryLocale);
		metron.measureAndOverwriteVariable("z_language", langLocale);
				
		if(continent!="") { 
			metron.measureAndOverwriteVariable("ti",stringToLowerFirstCap(select));
			metron.measureAndOverwriteVariable("dcsuri",getTitleValues(continent,subcontinent,country,city,'/')+".htm");
			
			if(eventtype == "searchBoxClick")	
				measureEvent("Clicked", "box_finddestination", document.getElementById("pageAreaType").value, getClickedText(continent,subcontinent,country,city));
			else measureEvent("Clicked", "navigation_select"+select, document.getElementById("pageAreaType").value, getClickedText(continent,subcontinent,country,city));	
			
		} 
		
		metron.measuresCommit();
		
	}
	
	/**
	* Called when the user moves the budget slider on the worldmap flash
	*/
	function measureBudgetSlider(amount){
		metron.measureVariablesAndCommit(
			{   "dcsuri" : getTitleValues(continent,subcontinent,country,city,'/')+".htm",
				"ti"  : "Worldmap", 
				"z_dg_event" : "Clicked",
				"z_dg_eventtype" : "slider_budget",
				"z_dg_eventplace" : document.getElementById("pageAreaType").value
			} );
	}
	function getClickedText(continent,subcontinent,country,city){
		var clicked = "";
		var title = getTitleValues(continent,subcontinent,country,city,"_");
		if(subcontinent != "" || subcontinent.toString() == "Not available") {
				clicked = title.substring(title.lastIndexOf('_')+1,title.length);
		} else clicked = title; 
		return clicked;
	}
	
	/**
	* Returns the formatted title values after parsing from continent to country url
	*/
	function getTitleValues(continent,subcontinent,country,city,separator){
		var fValues;
		fValues = continent;
		if(subcontinent != "") {
			if(subcontinent.toString() == "Not available") fValues; 
			else fValues = fValues + separator + subcontinent;
			
		} else fValues;
		country != "" ? fValues = fValues + separator + country : fValues;
		city != "" ? fValues = fValues + separator + city : fValues;
		return fValues;
	}
	
	function stringToLowerFirstCap(text_string){
		var new_string= text_string.substring(0,1).toUpperCase() + text_string.substring(1).toLowerCase();
		return new_string;
	}
	
	
	
	
	
	
	function stringToLowerFirstCap(text_string){
		var new_string= text_string.substring(0,1).toUpperCase() + text_string.substring(1).toLowerCase();
		return new_string;
	}
	function trimCity(city,topic){
	    var newtopic = "";
	 	if( topic.toString().indexOf(city.toString()) != -1 ){
			return topic.toString().replace(city.toString(),"");
		} else return topic;
	}	 
	
	/**
	* Invoke this function when email a friend and print a page links are clicked
	*/
	function emailFriendAndPrintPageOnClick(id) {
		var loc = ""+location.href;
		var dcsuri;
		var title;
		var eventType ;
		var eventPlace;
		var titleEmail = ""
		
		if(loc.match("index.htm")) eventPlace = document.getElementById("pageAreaType").value;
		else eventPlace = getLoadCityTopic();

		eventType = id == "mail_friend" ? "link_sendtofriend" : "link_printpage";
		dcsuri = getFromContinentToTopicFormatValues('/') != "" ? getFromContinentToTopicFormatValues('/')+".htm" : "Worldmap.htm";
		//title = getFromCityToTopic() != "" ? getFromCityToTopic()+"_"+eventType : "Worldmap_"+eventType;
		title = trimTopicPage() != "" ? trimTopicPage() : "Worldmap";
				
		metron.measureVariablesAndCommit(
			{ 	"dcsuri" : dcsuri,
				"ti"  : title, 
				"z_dg_event" : "Clicked",
				"z_dg_eventtype" : eventType,
				"z_dg_eventplace" : eventPlace
			} );

	}
	
	/**
	* Called when the onclick events occurred in flight, hotel and car tabs, links and submit buttons
	*/
	function crosssellingOnClick(id){
	  	var loc = ""+location.href;
		var dcsuri;
		var title;
		var eventType ;
		var eventPlace;
		var isFlight = false;
		var isHotel = false;
		var isCar = false;
		
		if(id == "booker-flights") eventType = "tab_flight"; 
		else if(id == "booker-hotel") eventType = "tab_hotel";
        else if(id == "booker-car") eventType = "tab_car";
        
        else if(id == "flightSearch") eventType = "link_ebt_search";
        else if(id == "hotelSearch") eventType = "link_hotel_search";
        else if(id == "carSearch") eventType = "link_car_search";
        
        else if(id == "flightSearchTrigger") eventType = "trigger_flight";
        else if(id == "hotelSearchTrigger") eventType = "trigger_hotel";
        else if(id == "carSearchTrigger") eventType = "trigger_car";
        
        else if(id == "5_14yrs") { eventType = "link_ebt_5-14years"; }
        else if(id == "Clickout - Flight") eventType = "link_ebt_search";
        else eventType = "unknown";
        
   		if(loc.match("index.htm")) eventPlace = document.getElementById("pageAreaType").value;
		else eventPlace = getLoadCityTopic();
	
		dcsuri = getFromContinentToTopicFormatValues('/') != "" ? getFromContinentToTopicFormatValues('/')+".htm" : "Worldmap.htm";
		//title = getFromCityToTopic() != "" ? getFromCityToTopic()+"_"+eventType : "Worldmap_"+eventType;
		title = trimTopicPage() != "" ? trimTopicPage() : "Worldmap";
				
		metron.measureVariablesAndCommit(
			{ 	"dcsuri" : dcsuri,
				"ti"  : title, 
				"z_dg_event" : "Clicked",
				"z_dg_eventtype" : eventType,
				"z_dg_eventplace" : eventPlace
			} );
	}
	
	/**
	* Called when the onclick events occurred in the city page internal
	*/
	function trackCityPageInternal(eventType,eventValue){
		//alert(eventValue);
		var dcsuri;
		var title;
		
		dcsuri = getFromContinentToTopicFormatValues('/')+".htm";
		//title = getFromContinentToTopicFormatValues('_')+"_"+eventType;
		title = trimTopicPage() != "" ? trimTopicPage() : "Worldmap";
		
		metron.measureVariablesAndCommit(
			{   "dcsuri" : dcsuri,
				"ti"  : title, 
				"z_dg_event" : "Clicked",
				"z_dg_eventtype" : eventType,
				"z_dg_eventvalue" : eventValue,
				"z_dg_eventplace" : getLoadCityTopic()
			} );
	}
	
	
	function trackChangeCountry(){
		//metron.webtrends.WT = {};
		var dcsuri;
		var title;
		var eventType;
		var eventPlace;
		var eventType = "link_change_country";
		
		if(loc.match("index.htm"))eventPlace = "Worldmap";
		else eventPlace = getLoadCityTopic();
			
		dcsuri = getFromContinentToTopicFormatValues('/') != "" ? getFromContinentToTopicFormatValues('/')+".htm" : "Worldmap.htm";
		//title = getFromCityToTopic() != "" ? getFromCityToTopic()+"_"+eventType : "Worldmap_"+eventType;
		title = trimTopicPage() != "" ? trimTopicPage(): "Worldmap";
		metron.measureVariablesAndCommit(
			{   "dcsuri" : dcsuri,
				"ti"  : title, 
				"z_dg_event" : "Clicked",
				"z_dg_eventtype" : eventType,
				"z_dg_eventplace" : eventPlace
			} );
	}
	
	function videoWebTrendsTracking(event,videoName,videoLengthPercent){
		//metron.webtrends.WT = {};
		var dcsuri;
		var title;
		var eventType;
		var eventPlace;
		
		videoLengthPercent = videoLengthPercent+escape('%');
					
		if(event == "PLAYING") eventType = "Start";
		else if(event == "click_to_open") eventType = "Start";
		else if(event == "BUFFERING") eventType = "Playing - "+videoLengthPercent;
		else if(event == "PAUSED" || event == "IDLE") eventType = "Paused - "+videoLengthPercent;
                else if(event == "STOP") eventType = "Stop";
                else if(event == "COMPLETED") eventType = "Completed - "+videoLengthPercent;
                else if(event == "CLOSED") eventType = "Close";
                else if(event == "RESIZE") eventType = "Full screen";
                else if(event == "PLAY") eventType = "Play - "+videoLengthPercent;
	        else if(event == "NORMALSIZE") eventType = "Normal screen";
                else eventType = "unknown";
        
		dcsuri = getFromContinentToTopicFormatValues('/')+".htm";
		title = trimTopicPage();
		eventPlace = getLoadCityTopic();
		videoName = getCityName();
		metron.measureVariablesAndCommit(
			{ 	"dcsuri" : dcsuri,
				"ti"  : title, 
				"z_dg_video_name" : videoName+" Video",
				//"z_dg_video_eventtype" : eventType,
				"z_dg_video_eventplace" : eventPlace,
				"z_dg_video_eventvalue" : eventType
			} );
		
	}
	
	
	
	
	
	
