var sponsoractive = false;
var tb_active = false;
var globalinputfield = 'blank';
var currentuser;
var ajaxpurpose = 'locsearch';

var dmoflag = false;
var comguidesearchterm;

var createscripts = false;
var videoscripts = false;
var eventscripts = false;
var calendarscripts = false;

var mapdiv;
var	mapcontrolsdiv;
var	mapcreatetoolsdiv;
var	mapdirectionsdiv;
var	mappanodiv;
var	mappanodetailsdiv;
var	highslidepanodiv;

//MAP GLOBALS
// GLOBAL SWITCHES FOR MAP FUNCTIONALTIY: 0 = Off; 1 = On;
var includemapcontrols = 1;
var includecreatecontrols = 1;
var includecustommarkers = 1;
var includedirections = 1;
var includepano = 1;
var traffictoggleState = 0;
var panotoggleState = 0;


function setmapglobals(){
		
	mapdiv = document.getElementById("map");
	var mapsetupflag = loadmap();
	
	if(mapsetupflag == 'success'){
		
		if(includecustommarkers == 1){
			addcustommarkers();
		}
		
		if(includemapcontrols == 1){
			mapcontrolsdiv = document.getElementById("mapcontrolsholder");
			setup_mapcontrols();
		}
		
		if(includecreatecontrols == 1){
			//mapcreatetoolsdiv = document.getElementById("createcontrols");
			setup_createcontrols();
		}
		
		if(includedirections == 1){
			mapdirectionsdiv = document.getElementById("directionscontent");
			setup_intdirections();
		}
		
		if(includepano == 1){
			mappanodiv = document.getElementById("pano");
			mappanodetailsdiv = document.getElementById("panodetails");
			highslidepanodiv = document.getElementById("panopanel");
			if(panotoggleState == 1){
				init_streetview();
			}
		}
	}
	
	sizemap();
}


//THE CHECH KEY FUNCTION SORTS OUT ALL KEY PRESSES EXCEPT THE ENTER KEY THEN TRIGGERS THE "processinput()" FUNCTION
//****NOTE: CHECK KEY FUNCTION REQUIRES THE JAVASCRIPT LINE "document.onkeypress = checkKey;" ON THE MASTER PAGE
//****NOTE: CHECK KEY FUNCTION REQUIRES A "processinput()" FUNCTION TO HANDLE THE ENTER KEY.
function checkKey(e)
{
	//alert(browsertype);
	var intKeyCode = 0;
	if (browsertype == "ie"){
		if (window.event.keyCode == 13){
			if(multilinetext==false){
				//alert('Enter key has been pressed');
				processinput();
				return false;
			}
		}
	}
	if (browsertype == "netscape"){
		//alert(e.which);
		intKeyCode = e.which;
		if (intKeyCode == 13){
			//alert('Enter key has been pressed');
			if(multilinetext==false){
				processinput();
				return false;
			}
		}
	}
	
}

function setupcreatefunctions(){
	manualcalsetup();
}

function detailsform(){
	highslidecontrol('destdetails');	
}

function manualcalsetup(){
	
	if(calendarscripts == false){
		addjspage('calendar');
		addjspage('calendar-en');
		addjspage('calendar-setup');
		calendarscripts = true;
		//setTimeout("setupcalendar()", 2500);
	}
}


function getlayout(whichl){
	
	switch(whichl){
		case 'cpcontrols':
			makeHttpRequest('cpajax/cplayouts.xml', setcpcontrols, true, 'cpglobalcontrols');
		break;
		
		case 'cpmidnavexplore':
			makeHttpRequest('cpajax/cplayouts.xml', buildexploremidnav, true, 'cpmidnavexplore');
		break;
		
		case 'cpmidnavgroup':
			makeHttpRequest('cpajax/cplayouts.xml', buildgroupmidnav, true, 'cpmidnavgroup');
		break;
		
		case 'exploremenu_us':
			makeHttpRequest('cpajax/cplayouts.xml', buildexploremenu, true, 'exploremenu_us');
		break;
		
		case 'exploremenu_nonus':
			makeHttpRequest('cpajax/cplayouts.xml', buildexploremenu, true, 'exploremenu_nonus');
		break;
		
		case 'createcthere':
			if(document.getElementById("HiddenFieldloggedonusername").value != 'nouser'){
				ajaxpurpose = 'createdest'
				makeHttpRequest('cpajax/cplayouts.xml', replaceajax, true, 'ctherelocatedest', 'createwiz');
			}else{
				alert("Gotta be logged in there kid...");	
			}
		break;
		
		case 'ctheremapclick':
			makeHttpRequest('cpajax/cplayouts.xml', replaceajax, true, 'ctherelocatedestmapclick', 'createwiz');
		break;
		
		case 'ctherelatlng':
			makeHttpRequest('cpajax/cplayouts.xml', replaceajax, true, 'ctherelocatedestlatlng', 'createwiz')
		break;
		
		case 'ctherebizsearch':
			makeHttpRequest('cpajax/cplayouts.xml', replaceajax, true, 'ctherelocatedestbiz', 'createwiz')
		break;
		
		case 'createctherebiz':
			makeHttpRequest('cpajax/cplayouts.xml', replacecreateajax, true, 'ctherelocatedestbiz');
		break;
		
		case 'newdestmenu':
			makeHttpRequest('cpajax/cplayouts.xml', replacecreateajax, true, 'ctherenewdestmenu', 'newdestmenu');
		break;
		
	}
	
}

function createstepone(){
	makeHttpRequest('cpajax/cplayouts.xml', replaceajax, true, 'ctherelocatedest', 'createwiz');	
}


function openexploreintro(){
	try{
		closeactiveexpander();
	}catch(err){
		//NO ACTION
	}
	ajaxpurpose = 'cpexplore';
	//alert(ajaxpurpose);
	var htitle = '<img src="globalgraphics/general/cp_globe_icon_30h.png" /><i>Exploring with Cplanet</i>';
	highslideajax('cpajax/cpsnippets.html', 'exploringintro', 'exploreintroanchor', htitle, 550, 470);
}

function opencreateintro(){
	
	if(createscripts == false){
		var pageid = 'createcthere';
		var filename = 'http://www.cplanet.com/inc/createfunctions.js';
		var headTag = document.getElementsByTagName('head')[0]; 
		script = document.createElement('script'); 
		script.id = pageid; 
		script.type = 'text/javascript'; 
		script.src = filename; 
		headTag.appendChild(script);
		
		createscripts = true;
	}
	
	resetcreatepanels();
	var htitle = '<img src="globalgraphics/general/cp_globe_icon_30h.png" /><i>Creating with Cplanet</i>';
	highslideajax('cpajax/cpsnippets.html', 'creatingintro', 'createintroanchor', htitle, 650, 500);
	ajaxpurpose = 'cpexplore';
	setupcreatefunctions();
	
}

function restartcreate(){
	try{
		closeactiveexpander();
	}catch(err){
		//NO ACTION
	}
	
	resetcreatepanels();
	clearmap();
	setTimeout("opencreateintro()", 300);
}

function setcpcontrols(rethtml){
	//alert(rethtml);
	document.getElementById("cpcontrols").innerHTML = rethtml;
}

function sizemap(){

	getwinsize();
	document.getElementById("map").style.height = windowheight + "px";
	map.checkResize();
	
}

function setupmapclick(){
	closeactiveexpander();
	markthemap('click');
}

function senduserlocation(userlocation){
	geopurpose = 'locsearch';
	callgeocode(userlocation);	
}

function setexplorelocation(){
	//alert("Seting Explore Media");
	mashupsprepopulate();
	
	if(non_usaloc == false){
		getlayout('exploremenu_us');
	}else{
		getlayout('exploremenu_nonus');
	}
}

function buildexploremidnav(rethtml){
	
	document.getElementById("cpmidnavexplore").innerHTML = rethtml;
	
	document.getElementById("exploremidtitle").innerHTML = globallocation;
	
	document.getElementById("explorepaneltitle").innerHTML = '<div style="color:#FFFFFF; font-size:16px;"><span style="font-size:11px;">Exploring:<br /></span>'+globallocation+"</div>";
	
	highslidecontrol('explore');
	
	try{
		hsreflow();
	}catch(err){
		//NO ACTION
	}
	
	setTimeout("storefrontdelay()", 1500);
	
}

function storefrontdelay(){
	getdestinations('', '', 'prox_storefronts');	
}

function opentour(){
	
	activetourid = document.getElementById("HiddenFieldTourId").value;
	activetourname = document.getElementById("HiddenFieldTourName").value;
	getdestinations(activetourid, 'groupview' );
	getlayout('cpmidnavgroup');
	
}


function buildgroupmidnav(rethtml){
	try{
		document.getElementById("cpmidnavgroup").innerHTML = rethtml;
		document.getElementById("groupmidtitle").innerHTML = activetourname;
	}catch(err){
		//No Action	
	}
}


function buildexploremenu(rethtml){
	
	document.getElementById("exploremenuholder").innerHTML = rethtml;
	
	try{
		hsreflow();
	}catch(err){
		//NO ACTION
	}
	
}


//BUSINESS VENUE FUNCTIONS
function scanvenues(sender, args){
	var currentnode = args.get_node();
	var vencatagory = currentnode.get_category();
	var venname = currentnode.get_text();
	var venvalue = currentnode.get_value();
	//Resets the page.
	markonmap=true;
	clearmap();
	
	setvenuemarker(vencatagory);
	activebizheader = venname;
	currentquery = venname;
	currentvenue = venvalue;
	searchyahoo('venue', 'initial');
}

function settextfields(nothing){
	try{
		document.getElementById('txtBusinessSearchLocationExplore').value = globallocation;
	}catch(err){
		 //alert('In Organization Mode');
	}
	
	try{
		document.getElementById('txtLocationSearchMap').value = globallocation;
	}catch(err){
		 //alert('In Organization Mode');
	}
}

function setthefocus(activeid){
    //alert('Setting globalenter to: '+activeid );
	document.getElementById(activeid).value = '';
	var newfocus = 	document.getElementById(activeid);
	newfocus.focus();
	globalinputfield = activeid;
}

function processinput(whichinput){
		
		if(globalinputfield == 'blank'){
			var activeinputfield = whichinput;
		}else{
			var activeinputfield = globalinputfield;	
		}
		
		//alert('activeinput: '+activeinputfield);
		
		var templocstring;
		var tempbizsearchterm;
		
      	switch (activeinputfield){
			case 'txtLocationSearchMain':
				geopurpose = 'comguide';
				templocstring = document.getElementById("txtLocationSearchMain").value;
				if(templocstring != ''){
					callgeocode(templocstring);
				}else{
					alert('Please enter a valid location search term');	
				}
			break;
			
			case 'txtLocationSearchAjax':
				processajax();
			break;
			
			case 'txtBusinessSearchAjax':
				ajaxpurpose = 'createdestbiz';
				processajax();
			break;
			
			case 'txtLocationSearchMap':
				geopurpose = 'comguide';
				templocstring = document.getElementById("txtLocationSearchMap").value;
				if(templocstring != ''){
					callgeocode(templocstring);
				}else{
					alert('Please enter a valid location search term');	
				}
			break;
			
			case 'txtLocationSearchGlobal':
				geopurpose = 'locsearch';
				templocstring = document.getElementById("txtLocationSearchGlobal").value;
				if(templocstring != ''){
					callgeocode(templocstring);
				}else{
					alert('Please enter a valid location search term');	
				}
			break;
			
			case 'txtBusinessSearchExplore':
				geopurpose = 'bizsearch';
				currentbizsearchterm = document.getElementById('txtBusinessSearchExplore').value;
				templocstring = document.getElementById("txtBusinessSearchLocationExplore").value;
				if((templocstring != '')&&(currentbizsearchterm != '')){
					callgeocode(templocstring);
				}else{
					alert('Please enter valid search terms');	
				}
			break;
			
			case 'dyn_bizsearch':
				geopurpose = 'bizsearch';
				currentbizsearchterm = document.getElementById('dyn_bizsearch').value;
				
				if(dmolocation){
					templocstring = dmolocation;
				}else{
					templocstring = globallocation;
				}
				
				if((templocstring != '')&&(currentbizsearchterm != '')){
					callgeocode(templocstring);
				}else{
					alert('Please enter valid search terms');	
				}
			break;
			
			case 'txtBusinessSearchBubble':
				geopurpose = 'bizsearch';
				currentbizsearchterm = document.getElementById('txtBusinessSearchBubble').value;
				templocstring = document.getElementById("txtBusinessSearchLocationBubble").value;
				if((templocstring != '')&&(currentbizsearchterm != '')){
					callgeocode(templocstring);
				}else{
					alert('Please enter valid search terms');	
				}
			break;
			
			case 'bubbleeverything':
				geopurpose = 'bizsearch';
				currentbizsearchterm = '*';
				templocstring = document.getElementById("txtBusinessSearchLocationBubble").value;
				if(templocstring != ''){
					callgeocode(templocstring);
				}else{
					alert('Please enter a location');	
				}
			break;
			
			case 'saddr':
				getdirections('to', globaldirectionpoint);
			break;
			
			case 'daddr':
				getdirections('from', globaldirectionpoint);
			break;
		}
		
		globalinputfield = 'blank';
	  
}

function processajax(){
	
	switch(ajaxpurpose){
		
		case 'cpexplore':
			geopurpose = 'comguide';
			templocstring = document.getElementById("txtLocationSearchAjax").value;
			if(templocstring != ''){
				callgeocode(templocstring);
				closeactiveexpander();
			}else{
				alert('Please enter a valid location search term');	
			}
		break;
		
		
		case 'buildstorefront':
			geopurpose = 'createcthere';
			templocstring = document.getElementById("txtLocationSearchAjax").value;
			//alert(templocstring);
			if(templocstring != ''){
				callgeocode(templocstring);
				closeactiveexpander();
			}else{
				alert('Please enter a valid location search term');	
			}
		break;
		
		case 'createdest':
			geopurpose = 'createcthere';
			templocstring = document.getElementById("txtLocationSearchAjax").value;
			//alert(templocstring);
			if(templocstring != ''){
				callgeocode(templocstring);
				closeactiveexpander();
			}else{
				alert('Please enter a valid location search term');	
			}
		break;
		
		case 'createdestbiz':
			geopurpose = 'createctherebiz';
			currentbizsearchterm = document.getElementById('txtBusinessSearchAjax').value;
			templocstring = document.getElementById("txtBusinessSearchAjaxLoc").value;
			if((templocstring != '')&&(currentbizsearchterm != '')){
				callgeocode(templocstring);
				//closeactiveexpander();
			}else{
				alert('Please enter valid search terms');	
			}
		break;
		
		case 'tripbuilder':
			geopurpose = 'tripbuilder';
			templocstring = document.getElementById("txtLocationSearchAjax").value;
			if(templocstring != ''){
				callgeocode(templocstring);
				opentb_controlpanel();
				setTimeout("setripvariables()", 500);
			}else{
				alert('Please enter a valid location search term');	
			}
		break;
		
	}
	
}

function cgsearch(term){
		
	setvenuemarker(term);
	comguidesearchterm = term;
	searchyahoo('search', 'initial', 'comguide');
	highslidecontrol('explore');

}

function onTabSelected(sender, args){
	//hsreflow();		
}

function setexploretabs(tabstate){
	
	var extabStrip = getexploretabstrip();
	
	var cpmenu = extabStrip.findTabByText("Menu");
	var cpvenues = extabStrip.findTabByText("Businesses");
	var cpresults = extabStrip.findTabByText("Results");
	
	
	switch (tabstate){
		
		case 'usaloc':
			cpmenu.enable();
			cpvenues.enable();
			cpresults.enable();
			cpmenu.select();
		break;
		
		case 'non_usaloc':
			cpmenu.enable();
			cpvenues.disable(); 
			cpresults.enable();
			cpmenu.select();
		break;
		
		case 'results':
			cpmenu.enable();
			cpvenues.enable();
			cpresults.enable();
			cpresults.select();
		break;
		
		case 'venue':
			cpmenu.enable();
			cpvenues.enable();
			cpresults.enable();
			cpvenues.select();
		break;
	
	}
	
}



function setvenuemarker(venue){
	
	markerimage = markerstandard;
	
	switch(venue) {
  	    	case 'Lodging':
			markericon = 'marker_lodging.png';
			break;
			
			case 'Restaurants':
			markericon = 'marker_restaurant.png';
			break;
			
			case 'Shopping':
			markericon='marker_shopping.png';
			break;
			
			case 'Bars and Clubs':
			markericon='marker_bar.png';
			break;
			
			case 'Beauty and Fitness':
			markericon='marker_beauty.png';
			break;
			
			case 'Home and Garden':
			markericon='marker_home.png';
			break;
			
			case 'Entertainment and Arts':
			markericon='marker_entertainment.png';
			break;
			
			case 'Recreation and Sports':
			markericon='marker_recreation.png';
			break;
			
			case 'Travel':
			markericon='marker_travel.png';
			break;
			
			case 'Health and Medicine':
			markericon='marker_health.png';
			break;
			
			case 'Services':
			markericon='marker_service.png';
			break;
			
			case 'Real Estate':
			markericon='marker_realestate.png';
			break;
			
			case 'ATMs':
			markericon='marker_atm.png';
			break;
			
			case 'PA':
			markericon='marker_parking.png';
			break;
			
			case 'Taxi Services':
			markericon='marker_taxi.png';
			break;
		
  		}
		
		markerimage = markerpath+markericon;
	
}
