//Global variable declarations     
var globalSubcontinentCode;
var globalCountryCode;
var globalStateCode;
var globalCityCode;
var globalCityName;
var globalExpandedDestinationCode='';
var globalSelectedCategory='';
var globalCalledFor ='';
var globalBackButtonFlag='';
var isGTFlag = 0;
var isCategory = 0;
var globalIsoCityCode='';
var tempCityCode;
var tempCityName;
var globalExlporeDestTab =0;
var globalTravelPodTab =0;
var globalCounter = 0;
var cityGuideFlag = 0;



//	Function used to load dropdown's for search city and search destination 
//	and load content for ground transport
function initDest(){
	createDestLocator();
	completerDest1 = new destinationAutoList("findDest", destCompleterPopupWrap, destCompleterPopdownWrap, "destpopup");
}



//	Function which inturn calls function to display the destination 
//  content	and ground transport information
function callShowDestinationAndGT(iataCode){
	exploreDestinations(iataCode);
	DWREngine._execute(dwrURL, 'groundTransportDwr', 'groundTransportInfoForIataCodeDwr', iataCode, loadGroundTransportInfo);
}


//	Function which inturn calls a function to display only the destination content
function callShowDestination(iataCode){
	 exploreDestinations(iataCode);
}


//	Function which inturn calls a function to display only the ground transport information
function callShowGroundTransportation(iataCode){
	DWREngine._execute(dwrURL, 'groundTransportDwr', 'groundTransportInfoForIataCodeDwr', iataCode, loadGroundTransportInfo);
}


//	CallBack function which inturn calls a function to display the ground transport information
function loadGroundTransportInfo(groundtansportObj){
	if(globalCalledFor == 'G'){
		exploreDestinationHideError();
		TravelpodHideError();
		exploreDestinationShowWaitMessage();
	}
	if(groundtansportObj != null){
		globalCityCode = groundtansportObj["cityCode"];
		globalCityName = groundtansportObj["cityName"];
		isGTFlag = 1;
		setTimeout('getGroundTransportContent(\''+globalCityCode+'\',\''+globalCityName+'\',\''+globalBackButtonFlag+'\',\''+contextPath+'\')',1000);
	}
	if(groundtansportObj == null && globalCalledFor == 'G'){
		exploreDestinationShowError();
	}
}


//	Function checks as to the presence of the GT link in the left panel and if present calls the 
//	getGroundTransport from groundTransport.js
function getGroundTransportContent(cityCode,cityName,globalBackButtonFlag,contextPath){

	var gtDivObj = document.getElementById("groundTransportLink");
	
	if(gtDivObj != null){
		if(gtDivObj.className != 'selected'){
			gtDivObj.className = 'selected';
	    }//End Of if
	}//End Of if
	
	getGroundTransport(cityCode,cityName,globalBackButtonFlag,contextPath);
}





//	Function responsible to get the categories on click of a city.	
function getCityContent(subcontinentCode,countryCode,stateCode,cityCode,cityname,isoCityCode){
		
	cityGuideFlag = 1;

	if (countryCode!=null && cityCode != null){									
		//trackCustomMetsHbx('c3', ""+globalCountryCode+"|"+cityCode);					
	}
	
	var Obj1= document.getElementById("tabdivs"); 
	
	document.getElementById('tabdivs').style.visibility='visible';
	document.getElementById('tabdivs').style.height='auto';
	Obj1.style.display= "";
	
	if (globalTravelPodTab==1 || globalTravelPodTab==1)
	{
		globalCatCode ='';
	}		
	globalExlporeDestTab =0;
	globalTravelPodTab =0;

	document.getElementById("tcontent1").style.display="block";
	document.getElementById("tcontent2").style.display="none";

	document.getElementById("scrtab1").parentNode.className="selected"
	document.getElementById("scrtab2").parentNode.className="";
	
	var cityCodeArray = new Array();
	cityCodeArray[0]=cityCode;

	globalCityCode = cityCode;
	globalCityName = cityname;
	globalIsoCityCode = isoCityCode;

	
	//get GroundTransportation status
	if(globalCalledFor != 'B'){
	   DWREngine._execute(dwrURL, 'groundTransportDwr', 'groundTransportInfoForDestCodeDwr', globalIsoCityCode, callBackIsGTInfo);
	}

	DWREngine._execute(dwrURL, 'GeocoderServiceDwr', 'getColumbusCodeForISOCode', globalIsoCityCode, calldisplayCityMap);
	getCategories(cityCode,cityname);


	// set the city name title bar and display
	document.getElementById("cityTitleName").innerHTML = cityname;
	document.getElementById("cityTitle").style.display="";
}

function calldisplayCityMap(data){
	displayCityMap(data["latitude"],data["longitude"],data["latposition"],data["longposition"],data["cityDisplayName"],data["adjustFlag"]);
}



//	Function to retrieve the Categories for the selected city ( checking the content if it is null)
function getCategories(placeCode,placeName) {
	tempCityCode = placeCode;
	tempCityName = placeName;

	DWREngine._execute(dwrURL, 'DestinationDWR', 'isContentnull', placeCode, placeName, callbackisContentnull);
}

function getCategoryDisplayable(placeCode,placeName,tempIsCategory) {
		if( tempIsCategory != 0)
		{
			getSubCategoriesString(placeCode,placeName);
		}
		else
		{
			getSubCategoriesString(placeCode,placeName);
		}
  }

 function ondemandExplodeDestination(){
		if (globalExlporeDestTab == 0)
		{
			globalExlporeDestTab =1;

			DWREngine._execute(dwrURL, 'DestinationDWR', 'getCategories', tempCityCode, tempCityName, loadCategories);
		}		
 }
   function ondemandTravelpodList(){
   
   
	   if (globalTravelPodTab == 0)
	   {
	   		globalTravelPodTab = 1;
			var travelBlogHeaderHtml = '';
			var travelBlogListHtml = '<ol class="arrList">';
			travelBlogListHtml += '</ol><ol class="arrList">';
			travelBlogListHtml += '<li class="" id ="TravelpodLink">'
			travelBlogListHtml += '</ol>';
			document.getElementById("travelBlogTree").innerHTML = travelBlogHeaderHtml + travelBlogListHtml;
			getTravelpodList(tempCityName,globalIsoCityCode);		
		}
 }
 
  
  function getSubCategoriesString(placeCode,placeName){
			document.getElementById("Categories").style.display='';
		var categoriesHeaderHtml = '<h3>categories  <br />'+placeName+'</h3><br/>';
		var categoriesListHtml = '<ol class="arrList">';
		var groundTransportListHtml = '<ol class="arrList">';

		
		var backButtonFlag = globalBackButtonFlag;
		queryStringFunction();
	
	 	categoriesListHtml += '</ol><ol class="arrList">';
		groundTransportListHtml += '</ol><ol class="">';
		
			groundTransportListHtml += '<li class="" id ="groundTransportLink">'

							   +'<ol id="groundTransportLink_ol"></ol>' 
							   +'</li>';
 	

		document.getElementById("categorydiv").innerHTML = ''; 
		document.getElementById("DestinationHeading").innerHTML = '';
		document.getElementById("DestinationBody").innerHTML = '';
		document.getElementById("groundTranspotationTree").innerHTML = groundTransportListHtml;
		
		
		if(globalCalledFor == 'B'){
			var gtDivObj = document.getElementById("groundTransportLink");
			if(gtDivObj != null){
				gtDivObj.className = 'selected';
				changeSelectedImageIcon('img_groundTransportLink');
		    }//End Of if
		}//End Of if
		getGroundTransportContent(globalIsoCityCode,placeName,backButtonFlag,contextPath);

	DWREngine._execute(dwrURL, 'DestinationDWR', 'getIataCodeForDestination', placeCode, plotCityCallBack);

}

//	Function for plot the city on the Map when iata code is typed in the find text box
function plotCityCallBack(iataCode){
	return;
}

//	Function responsible to change image icon from source to lowsrc and visa versa
function changeSelectedImageIcon(objectId){
	var imgObj = document.getElementById(objectId);
	if(imgObj != null){ 
		var ts=imgObj.lowsrc;
		imgObj.lowsrc=imgObj.src;
	    imgObj.src=ts;
    }
}

// Function to retrieve the Categories for the selected city
function callbackisContentnull(contentstr){
	var tisCategory;
	if(contentstr == "" || contentstr == null){
			tisCategory = 0;
			globalCatCode='';
	}else{
			tisCategory = 1;
			if (cityGuideFlag == 1 && !catcode)			{
				globalCatCode='cityguide';
			}
	}
	getCategoryDisplayable(tempCityCode,tempCityName,tisCategory);
}


//	Call back function for loading categories 
function loadCategories(resultArray) {
	if(resultArray != null && resultArray[3].length>0){
		getCategoriesString(resultArray);
    }
}

//	Function to retrieve the content for the selected category
//	Function to retrieve the content for the selected category
function getCategoriesString(result){
		
		document.getElementById("Categories").style.display='';
		var categoriesHeaderHtml = '';
		var categoriesListHtml = '<div class="">';
		var groundTransportListHtml = '<ol class="arrList">';
		var travelBlogHeaderHtml = '<h3>categories  <br />'+result[1]+' | '+result[2]+'</h3><br/>';
		var travelBlogListHtml = '<ol class="arrList">';
		var backButtonFlag = globalBackButtonFlag;
		for(var i=0; i<result[3].length; i++){
			var categoryName = result[3][i].categoryName;
			var categoryTitle = result[3][i].categoryTitle;
			if (categoryName != "Getting Around" && categoryTitle != "Getting Around" ){
				categoriesListHtml += '<span class="" id="'+categoryName+'"></span>';
				categoriesListHtml += '<img id="img_'+categoryName+'" src="http://static.vayama.com/images/arr.sm.collapsed.gif" lowsrc="'+contextPathStatic+'/images/arr.sm.0B8CDD.r.gif"  style="visibility:hidden;" />&nbsp;<a href="javascript:getContentForCategory(\''+categoryName+'\',\''+categoryTitle+'\');" >'+categoryTitle+'</a>&nbsp;&nbsp;&nbsp;';
							   
			}
	 	}//End Of for
	 	categoriesListHtml += '</div>';
			groundTransportListHtml += '<li class="" id ="groundTransportLink1">'
							   +'<a href="javascript:getGroundTransportContent(\''+globalIsoCityCode+'\',\''+result[1]+'\',\''+backButtonFlag+'\',\''+contextPath+'\');" ></a>'
							   +'<ol id="groundTransportLink_ol"></ol>' 
							   +'</li>';
		travelBlogListHtml += '<li class="" id ="TravelpodLink1">'
 		travelBlogListHtml +='<img id="img_TravelpodLink" src="http://static.vayama.com/images/arr.sm.collapsed.gif" lowsrc="'+contextPathStatic+'/images/arr.sm.0B8CDD.r.gif" />&nbsp;&nbsp;<a href="javascript:getTravelpodList(\''+result[0]+'\',\''+result[1]+'\');">Travel Blogs</a></li>';
 		travelBlogListHtml += '</ol>';
		document.getElementById("categorydiv").innerHTML = categoriesHeaderHtml + categoriesListHtml;
		if(globalCalledFor == 'B'){
			var gtDivObj = document.getElementById("groundTransportLink");
			if(gtDivObj != null){
				gtDivObj.className = 'selected';
				changeSelectedImageIcon('img_groundTransportLink');
		    }//End Of if
		}//End Of if

	
	if(globalCatCode=='CityStatistics' || globalCatCode=='KeyAttractions' || globalCatCode=='AdditionalAttractions' || globalCatCode=='Restaurants' || globalCatCode=='Nightlife' || globalCatCode=='Excursions' || globalCatCode=='CityOverview' || globalCatCode=='Shopping'  || globalCatCode=='cityguide'){

			if(globalCatCode=='CityStatistics'){
				getContentForCategory('CityStatistics','City Statistics');
				}
			else if(globalCatCode=='KeyAttractions'){
				getContentForCategory('KeyAttractions','Key Attractions');
			}
			else if(globalCatCode=='AdditionalAttractions'){ 
				getContentForCategory('FurtherDistractions','Additional Attractions');
			}
			else if(globalCatCode=='Restaurants' || globalCatCode=='Nightlife' || globalCatCode=='Excursions' || globalCatCode=='Shopping' ){
				getContentForCategory(globalCatCode,globalCatCode);
			} else if (globalCatCode=='CityOverview' || globalCatCode=='cityguide')
			{
				getContentForCategory('CityOverview','City Overview');
			}
			 else if (globalCatCode=='Shopping')
			{
				getContentForCategory('Shopping','Shopping');
			}
	}  
	
	else if( globalCatCode=='GettingAround' || globalCatCode=='TravelBlog' || 	globalCatCode== null || globalCatCode=='' || globalCatCode== 'null'  ){
	getContentForCategory(result[3][0].categoryName,result[3][0].categoryTitle);
	}

	DWREngine._execute(dwrURL, 'DestinationDWR', 'getIataCodeForDestination', result[0], plotCityCallBack);
}



//	Function responsible for getting the content for the selected Category in selected city
function getContentForCategory(categoryName,categoryTitle) {
	var catestr = categoryTitle.toLowerCase();
	var selectedDivObj = document.getElementById(globalSelectedCategory);
	if(selectedDivObj!=null){
		document.getElementById(globalSelectedCategory).className='';
		changeSelectedImageIcon('img_'+globalSelectedCategory);
	}
	globalSelectedCategory = categoryName;
	var catDivObj = document.getElementById(categoryName)
	if (catDivObj.className !='selected'){
		catDivObj.className='selected';
		changeSelectedImageIcon('img_'+categoryName);
	}
	DWREngine._execute(dwrURL, 'DestinationDWR', 'contentForDestinationCategory', globalCityCode, globalCityName, categoryName,categoryTitle, loadContent);
}

//	Call back method for getConentForCategory
function loadContent(result) {
	exploreDestinationHideError();
	TravelpodHideError();

	var destinationContentDivObj = document.getElementById("DestinationContent");
	destinationContentDivObj.style.display='';
  	if(result != null && result.length > 0){
    	getContentString(result);
   	}
}

//	load the content for the  selected Category in selected city in the appropriate div
function getContentString(result){
	var destinationContentHeadDivObj = document.getElementById("DestinationHeading");
	var destinationContentBodyDivObj = document.getElementById("DestinationBody");
	var url='www.columbusguides.com';
	var copyright = '<br><font style="font-size:9px">From <a href="javascript:openWindowURL(\''+url+'\');">www.columbusguides.com</a> copyright © Columbus Travel Publishing Ltd 2007</font>';
	destinationContentBodyDivObj.innerHTML='';
	destinationContentBodyDivObj.innerHTML='';
	if(result[2].length > 0){
		var headingHTML='<h2>'+result[0]+' | '+result[1]+'</h2>';
		var bodyHTML=result[2]+'<br />&nbsp;';
		destinationContentHeadDivObj.innerHTML = headingHTML
		destinationContentBodyDivObj.innerHTML = bodyHTML+copyright;
	}
}

//	 CallBack Function used to set the GT status flag to 1 or 0  depending on the gtListObj
function callBackIsGTInfo(gtListObj){
	if(gtListObj != null){
		isGTFlag = 1; 
	}else{
		isGTFlag = 0;
	}
	return isGTFlag;
}



//	Call back method for exploreDestinations
function getCity(result) {
	var destinationCode='';
	if(result != null && result["destinationCode"] != null && result["destinationCode"] != '' ){
    	destinationCode = result["destinationCode"];
    	
    	getDestinationCity(destinationCode);
    }else{
       	exploreDestinationShowError();
		closeEDpopups();
    }
}

//	Function to get the destination city
function getDestinationCity(destinationCode){
	DWREngine._execute(dwrURL, 'DestinationDWR', 'getDestinationCity', destinationCode, getDestinationCallback);
}

//	Callback Function  selected destination city
function getDestinationCallback(cityResult){
	if(cityResult!=null){
		var subcontinentCode = cityResult["subcontinentcode"];
		var countryCode = cityResult["countrycode"];
		var stateCode = cityResult["statecode"];
		var cityCode = cityResult["citycode"];
		var cityName = cityResult["cityname"];
		var isoCityCode = cityResult["isocitycode"];
		
		if(stateCode == null || stateCode.length == 0){
			stateCode = 'xxx';
		}
		
		getCityContent(subcontinentCode,countryCode,stateCode,cityCode,cityName,isoCityCode);
	}
}


//	Function used to Show the Error Massage block and hide the search massage block
function exploreDestinationShowError(){
	var errorObj = document.getElementById('errorBlock');
	if(errorObj != null){
		errorObj.style.display='';
		if(globalCalledFor != null && globalCalledFor != ''){
			document.getElementById('errorBlockBackButton').style.display='';
		}
	}
}


//	Function used to Hide the Error Massage block 
function exploreDestinationHideError(){
	var errorObj = document.getElementById('errorBlock');
	if(errorObj != null){
		errorObj.style.display='none';
		if(globalCalledFor != null && globalCalledFor != ''){
			document.getElementById('errorBlockBackButton').style.display='none';
		}
	}
}

//	Function to expand the tree based on the cityCode selected on the 
//	click of the search submit button
function searchDestination(){	
	closeEDpopups();
	exploreDestinationHideError();

	var destinationCode = document.getElementById("findDestCodeHd").value;	
	if(destinationCode != null && destinationCode != ''){
		document.getElementById("findDestCodeHd").value ='';
		document.getElementById("findDestNameHd").value ='';

		getDestinationCity(destinationCode);
	}else{
		exploreDestinationShowError();
		closeEDpopups();
	}//End Of if..else
}


//	Function to close the destination category content
function closeDestination(){
	var destinationContentDivObj = document.getElementById("DestinationContent");
   	destinationContentDivObj.style.display='none';
	var catDivObj = document.getElementById(globalSelectedCategory);
	if (catDivObj!=null && catDivObj.className =='selected'){
		catDivObj.className='';
		changeSelectedImageIcon('img_'+globalSelectedCategory);
		globalSelectedCategory='';
	}
}

//	Function to close the destination categories
function closeCategories(){
	var categoriesDivObj = document.getElementById("Categories");
   	categoriesDivObj.style.display='none';
}

//	Function to close the destination categories,groundTransport,destination content 
//	and reset the selection of city
function closeEDpopups(){
	closeDestination();
	closeGroundTransport();
	closeCategories(); 
	closeTravelPod();
}



function getCityCodrforCity(cityname){
	DWREngine._execute(dwrURL, 'DestinationDWR', 'getCityCodeForCity', cityname, loadCityCode);	
}

function loadCityCode(citycode){
	getDestinationCity(citycode);
}

function exploreTransportCity(){
	var destinationCode = document.getElementById("findDestCodeHd").value;	
	if(destinationCode != "" && destinationCode != null)
		DWREngine._execute(dwrURL, 'DestinationDWR', 'getDestinationCity', destinationCode, getCityNameForTrans);
	else
		ddrivetip("<font color= 'red' >Please enter destination</font>", 170, 15, findPosY(document.getElementById("findDest")) - 17, findPosX(document.getElementById("findDest")) + 100, findPosY(document.getElementById("findDest")) - 22, findPosX(document.getElementById("findDest")) + 122);
}


function getCityNameForTrans(result){
	var cityname = result.cityname ;
	var countryname = result.countryname ;
	cityname  = cityname.toLowerCase();
	
	for (;cityname.indexOf(' ') != -1;)
		cityname = cityname.replace(" ","_");
	countryname  = countryname.toLowerCase();
	
	for (;countryname.indexOf(' ') != -1;)
		countryname = countryname.replace(" ","_");
	
	var url2Open = contextPath + "/"+ countryname + "/" + cityname + "/airport-transportation?s=" + sessionId;	

	window.open(url2Open, "_self"); 
}


function exploreCityGuide(){
	var destinationCode = document.getElementById("findDestCodeHd").value;
	
	if(destinationCode != "" && destinationCode != null)
		DWREngine._execute(dwrURL, 'DestinationDWR', 'getDestinationCity', destinationCode, getCityNameForCityGuide);
	else
		ddrivetip("<font color= 'red' >Please enter destination</font>", 170, 15, findPosY(document.getElementById("findDest")) - 17, findPosX(document.getElementById("findDest")) + 100, findPosY(document.getElementById("findDest")) - 22, findPosX(document.getElementById("findDest")) + 122);
}


function getCityNameForCityGuide(result){
	var cityname = result.cityname ;
	var countryname = result.countryname ;
	cityname  = cityname.toLowerCase();
	for (;cityname.indexOf(' ') != -1;)
		cityname = cityname.replace(" ","_");
	countryname  = countryname.toLowerCase();
	for (;countryname.indexOf(' ') != -1;)
		countryname = countryname.replace(" ","_");
	var url2Open = contextPath + "/"+ countryname + "/" + cityname + "/city-guide?s=" + sessionId;	
//	var url2Open = contextPathS + "/jsp/destination/exploreDestinations.jsp?city="+cityname+"&type=cityguide";
	window.open(url2Open, "_self"); 
}
