/* ----------------- virtualEarth.js functions -------------------------- */
var map = null;
var mapToggled=false;
var activeMap=false;
/**
* Toggles the media ceneter and the virtual earth map.
**/
function toggleMap(){
	if(mapToggled==true){
		showMediaCenter(mediaCenterDiv);

	}
	else{
		showMap();
	}
}

function showMediaCenter(mediaCenterDiv){
	//map=null;
	hideEmailFriend();
	$('virtualEarthContainer').style.display='none';
	$('uploadedMap').style.display='none';
	$(mediaCenterDiv).style.display='';
	mapToggled=false;
	document.getElementById(mediaCenterDiv).innerHTML=proMedCenter_innerHTML;
}

function showUploadedMap(){
	
	if(!custom){
		//alert(mediaCenterDiv);
		$(mediaCenterDiv).style.display='none';
		$('virtualEarthContainer').style.display='none';
	}
	$('uploadedMap').style.display='';
}


function showEMap(custom){
	//logging onclick event
	write_event('41',siteID,'site');
	
	showVEMap(custom);
}


function showMap(){
	//logging onclick event
	write_event('19',siteID,'site');
	
	hideEmailFriend();
	//alert(BrowserDetect.browser);
	
	if(hasCustomMap){
		showUploadedMap();
	}else{
		if(hasEMap){
			showVEMap();
		}
	}	

}

function hideFlash(mediaCenterDiv) {
	//document.getElementById(mediaCenterDiv).innerHTML='&nbsp;'
}


function showMapPointMap(){
	trackConversion("pageview");
	mapToggled=true;
	//increase the size of the map div
	var controlWidth=(largeMediaCenter)?('962'):('721');
	$('virtualEarthContainer').style.overflow='auto';
	/*html='<div ><div style="float:left;">';
	
	if(hasCustomMap){
		html=html+'<img src="http://images.forrent.com/imgs/fr/siteFiles/design/profile/viemap_bttn.png" onclick="showUploadedMap();" style="; cursor: pointer; cursor: hand; z-index: 201;" id="viewMapButton"/></div>';
	}
	html=html+'<div style="float:right"><img src="http://images.forrent.com/imgs/fr/siteFiles/design/profile/close_bttn.png" onclick="toggleMap();" style="cursor: pointer; cursor: hand; z-index: 201;" id="closeMapButton"/></div><div>';

*/
	$('virtualEarthInsideDiv').style.position='';
	//$('virtualEarthInsideDiv').style.textAlign='center';

	$('virtualEarthInsideDiv').innerHTML='<div style="text-align:center;position:relative;z-index: 1;top:+25px;height:400px;"><iframe src="'+mapPointURL+'" frameborder="0" scrolling="no" width="480" height="400"></iframe></div>';
	//$html+'<div style="top:+25px; height: 25px; width: '+controlWidth+'px;background-image: url(http://images.forrent.com/imgs/fr/siteFiles/design/profile/cntctLink.png); "><div style="float: left;" class="tollFree">1-888-871-6267</div><div style="float: right;"><a class="mainLinkTextSm" href="">Learn More About This Property</a></div></div>';


	$(mediaCenterDiv).style.display='none';
	$('uploadedMap').style.display='none';
	$('virtualEarthContainer').style.display='';
}



function showVEMap(){
	try{
		trackConversion("pageview");
	}catch(e){

	}
	
	$('virtualEarthInsideDiv').style.position='relative';
	if(!custom){

		$(mediaCenterDiv).style.display='none';
		$('uploadedMap').style.display='none';
		$('virtualEarthContainer').style.display='';
	}
	mapToggled=true;
	if(map){

		return false;
	}


	map = new VEMap('virtualEarthInsideDiv');

	//If a customer indentification token string was not received,
	//the JavaScript variable bingMapsIdToken will be 'NO_TOKEN_RETRIEVED'.
	//If this is the case, do not attempt to set the client token.
	if(bingMapsIdToken != 'NO_TOKEN_RETRIEVED') {
		//Set the Bing Maps Service Customer Identification Token
		map.SetClientToken(bingMapsIdToken);
	}

	map.AttachEvent('ontokenexpire', MyHandleTokenExpire);
	map.AttachEvent('ontokenerror', MyHandleTokenError);

	
	map.LoadMap(new VELatLong(latitude, longitude), ((pushPin)?(17):(14)),'r' ,false);

	//hack to fix firefox dashboard
	$('MSVE_navAction_FlatlandMapMode').innerHTML = "2D";
	$('MSVE_navAction_View3DMapMode').innerHTML = "3D";
	$('MSVE_navAction_RoadMapStyle').innerHTML = "Road";
	$('MSVE_navAction_AerialMapStyle').innerHTML = "Aerial";
	$('MSVE_navAction_showLabels').innerHTML = "Labels";
	$('MSVE_navAction_ObliqueMapView').innerHTML = "Bird's Eye";
	
	//attach our event handler

	map.AttachEvent('onchangemapstyle', mapStyleChangedEvent);
	map.AttachEvent("oninitmode", mapModeChangedEvent);

	

	if(pushPin){
		//create the pushpin for the site
		var myVEShapePins = new VEShape(VEShapeType.Pushpin, new VELatLong(latitude, longitude));

		//Set the title for the pushpin
		myVEShapePins.SetTitle(siteName);

		myVEShapePins.SetDescription(shortDescription);

		myVEShapePins.SetPhotoURL(mainPhoto);

		//determine the browser version:
		var arVersion = navigator.appVersion.split("MSIE")
		var version = parseFloat(arVersion[1])

		if ((version < 7) )
		{
			//show .gif for ie 6 and lower
			myVEShapePins.SetCustomIcon('http://images.forrent.com/imgs/fr/siteFiles/design/mc/mark.gif');
		}else{
			//show a nice png for IE 7 and REAL browsers
			myVEShapePins.SetCustomIcon('http://images.forrent.com/imgs/fr/siteFiles/design/mc/mark.png');

		}
		//Add the Pin to the map
		map.AddShape(myVEShapePins);
	}


	if(custom){
		//$('virtualEarthInsideDiv_dashboard').style.display="none";
	}
	else{

		//disable mouse wheel
		map.AttachEvent("onmousewheel", mouseWheel);

		


		//$('virtualEarthInsideDiv_dashboard').style.zIndex='100';
		addControls();
	}

	//log our default views
	write_event('67',siteID,'site');
	write_event('70',siteID,'site');

}

function MyHandleTokenExpire(error) {
	var errorMessage = error;
   // insert code here to handle token expiration
   	//console.log("MSVirtualEarth token has expired.");
}

function MyHandleTokenError(error) {
	var errorMessage = error;
   // insert code here to handle token errors
   	//console.log("Invalid MSVirtualEarth token.");
}


function mouseWheel(e){
	return true;
}




var temp;
function mapStyleChangedEvent(e)
{
	temp=e;
	var events = "mapstyle: " + e.mapStyle;
	//remove center control
	var div;
	div=document.getElementById("centerControlWrapper");
	
	if(div != null && typeof(div.parentNode) != 'undefined') {
		div.parentNode.removeChild(div);
	}
	
	switch(e.mapStyle){
		case 'o':
		write_event('66',siteID,'site');
		break;
		case 'r':
		addControls();
		write_event('67',siteID,'site');
		break;
		case 'a':
		addControls();
		write_event('68',siteID,'site');
		
		break;
		case 'h':
		addControls();
		write_event('69',siteID,'site');
		break;

	}


}

var dashboardLeft=null;
function mapModeChangedEvent(e){
	switch(map.GetMapMode()){
		case 1: //2d view
		//move the dashboard back to the left
		$('virtualEarthInsideDiv_dashboard').style.left=dashboardLeft;
		$("centerMapControl").style.display='';
		write_event('70',siteID,'site');
		break;
		case 2: //3d view
		//move the dashboard over so it doesnt block the 3d controls
		dashboardLeft=$('virtualEarthInsideDiv_dashboard').style.left;
		$("centerMapControl").style.display='none';

		if(largeMediaCenter){
			$('virtualEarthInsideDiv_dashboard').style.left='850px';
		}else{
			$('virtualEarthInsideDiv_dashboard').style.left='600px';
		}
		
		write_event('71',siteID,'site');
		break;
	}
}

function showEmptyMap(latitude,longitude){
	map = new VEMap('virtualEarthInsideDiv');
	map.LoadMap();
}
function addPin(latitude,longitude){
	var myVEShapePins = new VEShape(VEShapeType.Pushpin, new VELatLong(latitude, longitude));

	//Set the title for the pushpin
	//myVEShapePins.SetTitle(siteName);

	//myVEShapePins.SetDescription(shortDescription);

	//myVEShapePins.SetPhotoURL(mainPhoto);
	//myVEShapePins.SetMoreInfoURL(moreInfoLink);



	//Add the Pin to the map
	map.AddShape(myVEShapePins);
}

function addControls(){
	//make the new element to insert into the map
	var wrapper=document.createElement("div");
	
	var newElement=document.createElement("div");
	newElement.onclick=function(){centerMap();};
	//newElement.className='MSVE_Dashboard MSVE_Dashboard_V6';
	//create some text to go in the node
	wrapper.style.position="relative";
	wrapper.style.top='3px';
	wrapper.style.paddingLeft="1px";
	wrapper.style.paddingBottom="1px";
	

	//newElement.style.left='3px';
	
	newElement.style.cursor="pointer";
	newElement.style.cursor="hand";
	newElement.id="centerMapControl";
	newElement.style.width="26px";
	newElement.style.height="23px";
	newElement.title="Center map on apartment community";
	newElement.onmouseover=function(){$("centerControlWrapper").style.opacity=.90;this.style.border="solid 1px #aacbee"};
	newElement.onmouseout=function(){$("centerControlWrapper").style.opacity=.70;this.style.border=""};
	
	
	newElement.style.background= "transparent url(http://images.forrent.com/imgs/fr/siteFiles/design/centermapicon.png)";
	
	wrapper.style.opacity="0.7";
	wrapper.id="centerControlWrapper";
	
	wrapper.style.background="#235087";
	wrapper.appendChild(newElement);
	$('MSVE_TinyZoomBar').appendChild(wrapper);
	
}



function centerMap(){

	map.SetCenterAndZoom(new VELatLong(latitude, longitude), 17);
	write_event('72',siteID,'site');
}


function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/-/g, "\-");
	var oRegExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}


function setVideoSize(size){
	createCookie('videoSize',size,10);

}



/*Run this on all controlls to enable 3d support*/
function addShim(el)
{
	var shim = document.createElement("iframe");
	shim.id = "Shim"+el.id;
	shim.frameBorder = "0";
	shim.style.position = "absolute";
	shim.style.zIndex = "1";
	shim.style.top  = el.offsetTop;
	shim.style.left = el.offsetLeft;
	shim.width  = el.offsetWidth;
	shim.height = el.offsetHeight;
	el.shimElement = shim;
	el.parentNode.insertBefore(shim, el);
}

/* ----------------- guestCard.js functions -------------------------- */
function setupGuestCard(siteID)
{
	createCookie('doSubmit','yes',1);
	//alert("setting up guest card submission!");
	var ajax = new Ajax.Request('/ajax/guestCardSecurity.ajax.php?siteID='+siteID,{asynchronous:false,method: 'get'});
}

/* ----------------- emailFriend.js functions -------------------------- */
function showEmailFriend(){
	//Write Event 38 Ticket#1199
	write_event('38', siteID, 'site');
	new Ajax.Updater("emailFriend","/ajax/emailFriend.ajax.php?form=1"+"&siteID="+siteID,{asynchronous:true, evalScripts:true});
	$(mediaCenterDiv).style.display='none';
	$(mediaCenterDiv).innerHTML='';
	$("emailFriend").style.display="block";
}

function closeEmailFriend(){
	hideEmailFriend();
	$(mediaCenterDiv).innerHTML=proMedCenter_innerHTML;
	$(mediaCenterDiv).style.display='';
	
	return false;
}

function hideEmailFriend(){
	clearValidator();
	if($("emailFriend").style.display!="none"){
		write_event('82',siteID,'site');
		$("emailFriend").style.display="none";
	}
	
}

function submitEmailFriendForm(){
	
	if(validateSubmit()){
	if($F('friendEmail1')=="" && $F('friendEmail2')=="" && $F('friendEmail3')=="" && !$('sendCopyToSender').checked){
		alert("Please enter an email address.");
		return false;
	}
	
	var request="/ajax/emailFriend.ajax.php"+formToString("emailFriendForm");
	//var request="/sendemailfriend.php"+formToString("emailFriendForm");
	
	
	//alert(request);

	new Ajax.Updater("emailFriend",request,{asynchronous:true, evalScripts:true});
	}
	return false;
}

/**
Reads data from a form and builds a query string from it.
**/
function formToString(formID){
	var x;
	var buffer='';
	for(x=0;x<$(formID).elements.length;x++){
		if($(formID).elements[x].name!=''){
			if(x==0){
				buffer=buffer+"?";
			}else{
				buffer=buffer+"&";
			}
			if($(formID).elements[x].type=="checkbox"){
				if($(formID).elements[x].checked){
					buffer=buffer+$(formID).elements[x].name+"="+escape($(formID).elements[x].value);
				}
			}
			else{
				buffer=buffer+$(formID).elements[x].name+"="+escape($(formID).elements[x].value);
			}
		}
	}
	return buffer;
}
