// re-written js file for cabcorner
//uses jquery

$(function() {
  	$('body').show();
	//showhides for boroughbar on front page
	$('#boroughBar').change(function() {
		if( $('#boroughBar').val() == 'manhattan' ) {
			$('#manhattanSpots').removeClass('ghost');
			$('#brooklynSpots').addClass('ghost');
			$('#queensSpots').addClass('ghost');
		} else if( $('#boroughBar').val() == 'brooklyn' ) {
			$('#manhattanSpots').addClass('ghost');
			$('#queensSpots').addClass('ghost');
			$('#brooklynSpots').removeClass('ghost');
		} else if( $('#boroughBar').val() == 'queens' ) {
			$('#manhattanSpots').addClass('ghost');
			$('#queensSpots').removeClass('ghost');
			$('#brooklynSpots').addClass('ghost');
		} else {
			$('#manhattanSpots').addClass('ghost');
			$('#brooklynSpots').addClass('ghost');
			$('#queensSpots').addClass('ghost');
		}
	});

    //changes the map's center depending on what area is selected
	$('#brooklynSpots,#manhattanSpots,#queensSpots').change(function() {
		var coors = this.value;
		var coors = coors.split(',');
		var lat = coors[0];
		var long = coors[1];
		map.setCenter(new GLatLng(lat,long), 13)
	});
	
	//set up view hot spot button to point map to wherever it needs to go
	$('#viewHotSpot').css('cursor','pointer');
	$('#viewHotSpot').click(function() {

		if( !$('#manhattanSpots').hasClass('ghost') ) {
			var coors = $('#manhattanSpots').val();
		} else if( !$('#brooklynSpots').hasClass('ghost') ) {
			var coors = $('#brooklynSpots').val();
		}
		var coors = coors.split(',');
		var lat = coors[0];
		var long = coors[1];
		map.setCenter(new GLatLng(lat,long), 13)

	});


	//modal window calls
	$('a.modal').click(function() {
		if( $('#modal').hasClass('jqmHotSpot') ) {
			$('#modal').removeClass('jqmHotSpot');
			$('#modal').addClass('jqmNormal');	
		}
	});
	
	$('#modal').jqm({ajax: '@href', trigger: 'a.modal'});
	$('#modalMapBox').jqm();

	$('#today').click(function() {
		$('#dateFields').toggle();
		if( $('#today').is(':checked') ) {
			//change date fields to read today, disable them
			var date = new Date();
			//$('#month').attr('disabled','disabled');
			//$('#day').attr('disabled','disabled');
			$('#month').val(date.getMonth() + 1);
			$('#day').val(date.getDate());
			$('#dateFields').hide();
		}
	});
	
	$('#citySearch').click(function() {
		if( $('#citySearch').val() != 'NYC' ) {
			alert('Coming soon!');
		} 
	});
	
	$('#topHeader #message ul').mouseover(function() {
		$('#topHeader #message li').css('display','block');
	});

	$('#topHeader #message ul').mouseout(function() {
		$('#topHeader #message li').css('display','none');
		$('#topHeader #message li#nyc').css('display','list-item');
		$('#topHeader #message li#nyc').css('list-style-type','list-item');
	});

});

var modalOpen = false;
var dirn;
function walkingDirections(lat,coorLong,addr,div) {
		
	$('#modalMapBox').jqmShow();
	
	if( modalOpen ) {
		dirn.clear();
	} else {
		modalOpen = true;
		var directions = document.getElementById(div);
		dirn = new GDirections(modalMap,directions);
	}
	
	$('#modalDirections').html('');

	dirn.load("from: " + addr +  ",NY,USA to: (" + lat + "," + coorLong + ")", {
		travelMode: G_TRAVEL_MODE_WALKING
	});
	
	modalMap.checkResize();
	
}

function hotSpotPopUp(spid) {
	$('#modal').load('/single-spot/', {
			'popUp': 'true',
			'spot': spid
		}
	);
	$('#modal').jqmShow();
	$('#modal').removeClass('jqmNormal');
	$('#modal').addClass('jqmHotSpot');	
	$('#modal').jqmHide();
	$('#modal').jqmShow();
}
/*function fareCalPopUp() {
	$('#modal').load('/search/farecal', {
			'popUp': 'true',
			'startAddr':document.getElementById('startAddr').value,
			'destAddr':document.getElementById('destAddr').value,
			'destBorough':document.getElementById('destBorough').value,
			'hh':document.getElementById('hh').value,
			'mn':document.getElementById('mn').value,
			'ampm':document.getElementById('ampm').value,
			'dd':document.getElementById('dd').value,
			'mm':document.getElementById('mm').value,
			'yy':document.getElementById('yy').value,
			'startChoice':'addr',
			'destChoice':'addr',
			'partySize':'1',
			'startBorough':document.getElementById('startBorough').value
			
		}
	);
	$('#modal').jqmShow();
	$('#modal').removeClass('jqmHotSpot');
	$('#modal').addClass('jqmNormal');
	$('#modal').jqmHide();
	$('#modal').jqmShow();
	return false;
	
}
*/
function fareCalPopUp() {
	var startAddr=document.getElementById('startAddr').value;
var destAddr=document.getElementById('destAddr').value;
var destBorough=document.getElementById('destBorough').value;
var hh=document.getElementById('hh').value;
var mn=document.getElementById('mn').value;
var ampm=document.getElementById('ampm').value;
var dd=document.getElementById('dd').value;
var mm=document.getElementById('mm').value;
var yy=document.getElementById('yy').value;
var startChoice='addr';
var destChoice='addr';
var partySize='1';
var startBorough=document.getElementById('startBorough').value;



		var url= '/search/farecal?startAddr='+startAddr+'&destAddr='+destAddr+'&destBorough='+destBorough+'&hh='+hh+'&mn='+mn+'&ampm='+ampm+'&dd='+dd+'&startChoice='+startChoice+'&destChoice='+destChoice+'&partySize='+partySize+'&startBorough='+startBorough;
		//openPopUp('Farecalculator',this,url,'createfarecal',750,450,'menubar=no,toolbar=no,location=no,status=no,resizable=no');
		PopupCenter(url,'Farecalculator',400,500)
		
return false;

}

function PopupCenter(pageURL, title,w,h) {
var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);
var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
} 


//checks the sent input element for the value text
//if values match, text is removed and font changed 
//from grey color to black
function ghostTextRemove(text, element) {
	if( element.value == text ) {
		element.value = '';
		element.style.color = '#000';
	}
}
function frmsubmit()
{
	document.search.submit();	
}
function openPopUp(winInst,currObj,baseURL,winName,width,height,features) {
	var left=parseInt(findPosX(currObj))
	var top=parseInt(findPosY(currObj))
	
	if (window.navigator.appName!="Opera") top+=parseInt(currObj.offsetHeight)
	else top+=(parseInt(currObj.offsetHeight)*2)+10

	
	
	features="width="+width+",height="+height+",top="+top+",left="+left+";"+features
	eval(winInst+'=window.open("'+baseURL+'","'+winName+'","'+features+'")')
}	
//checks the send input element, if value is nill
//fills in with text and changes color to grey
function ghostTextFill(text, element) {
	if( !element.value ) {
		element.value = text;
		element.style.color = '#666666';
	}
}function openPopUp(winInst,currObj,baseURL,winName,width,height,features) {
	var left=parseInt(findPosX(currObj))
	var top=parseInt(findPosY(currObj))
	
	if (window.navigator.appName!="Opera") top+=parseInt(currObj.offsetHeight)
	else top+=(parseInt(currObj.offsetHeight)*2)+10

	
	features="width="+width+",height="+height+",top="+top+",left="+left+";"+features
	eval(winInst+'=window.open("'+baseURL+'","'+winName+'","'+features+'")')
}
function findPosX(obj) {

	var curleft = 0;

	if (document.getElementById || document.all) {

		while (obj.offsetParent) {

			curleft += obj.offsetLeft

			obj = obj.offsetParent;

		}

	} else if (document.layers) {

		curleft += obj.x;

	}



	return curleft;

}



function findPosY(obj) {

	var curtop = 0;



	if (document.getElementById || document.all) {

		while (obj.offsetParent) {

			curtop += obj.offsetTop

			obj = obj.offsetParent;

		}

	} else if (document.layers) {

		curtop += obj.y;

	}



	return curtop;

}