/**************************************************************************/
/********************************************************************/
/**************************************************************************/
/********************************************************************/
/* 	start image scrolling functionality	on list page	*/
	function scroll_left(mls_num){
		if( img_arr_count[mls_num] > 0 ){
			img_arr_count[mls_num] = img_arr_count[mls_num]-1;
			$("#photo_num"+mls_num).html(img_arr_count[mls_num]+1);
			$("#image_"+mls_num).attr('src',img_arr[mls_num][img_arr_count[mls_num]]);			
		}
	}	
	function scroll_right(mls_num){
		if( img_arr_count[mls_num] < img_arr_max[mls_num] - 1 ){
			img_arr_count[mls_num] = img_arr_count[mls_num]+1;
			$("#photo_num"+mls_num).html(img_arr_count[mls_num]+1);
			$("#image_"+mls_num).attr('src',img_arr[mls_num][img_arr_count[mls_num]]);		
		}
		//alert(img_arr_count[mls_num]);
	}
/* 	end image scrolling functionality on list page*/
/**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/* 	start contact functionality on list page							  */
	function submit_contact(){
		error = false;
		$(".detail_error").css("display","none");
		if($("#detail_name").val()==""){
			$("#detail_name_error").css('display','block')
			error = true;
		}
		if($("#detail_phone").val()==""){
			$("#detail_phone_error").css('display','block')
			error = true;
		}
		if($("#detail_email").val()==""){
			$("#detail_email_error").css('display','block')
			error = true;
		}
		if($("#validation_code").val()==""){
			$("#detail_validation_error").css('display','block')
			error = true;
		}
		if(error){
			return false;
		}else{
			// make ajax call with form data
			//$("#contact_body").load("./content/properties/ajax/contact_logic.php",{"detail_name":$("#detail_name").val(),"detail_phone":$("#detail_phone").val(),"detail_email":$("#detail_email").val(),"detail_comments":$("#detail_comments").val()});
			return true;	
		}
	}
/* 	end contact functionality on list page			*/
/**************************************************************************/
/**************************************************************************/
/********************************************************************/
/* 	start contact functionality on list page		*/
	function submit_showings(){
		error = false;
		$(".error").css("display","none");
		if($("#fname").val()==""){
			$("#fname_error").css('display','block')
			error = true;
		}
		if($("#lname").val()==""){
			$("#lname_error").css('display','block')
			error = true;
		}
		if($("#phone").val()==""){
			$("#phone_error").css('display','block')
			error = true;
		}
		if($("#email").val()==""){
			$("#email_error").css('display','block')
			error = true;
		}
		if($("#validation_code").val()==""){
			$("#validation_error").css('display','block')
			error = true;
		}
		if(error){
			return false;
		}else{
			// make ajax call with form data
			//$("#showings_body").load("./content/properties/ajax/request_info_logic.php",{"fname":$("#fname").val(),"lname":$("#lname").val(),"phone":$("#phone").val(),"email":$("#email").val(),"comments":$("#comments").val(),"tosee":$("#tosee").val()});
			return true;	
		}
	}
/* 	end contact functionality on list page			*/
/**************************************************************************/
/********************************************************************/
/**************************************************************************/
/********************************************************************/
/*  start sort by functions - used in the dropdown box at the top of the property list page */
	function orderby_launch(){
		order = $("#orderby").val();
		href = window.document.location.href;
		redirect = href+"&orderby="+order;
		alert(href);
		alert(redirect);
		if(order == 'select'){
			return false;	
		}else{
			//return false;	
			window.location = redirect;		
		}
		/* 
		if($("#orderby option:selected").val()=='ltoh'){
			window.document.location.href = window.document.location.href+"&orderby=ltoh"		
		}
		if($("#orderby option:selected").val()=='htol'){
			window.document.location.href = window.document.location.href+"&orderby=htol"		
		}
		*/
	}
/*  end sort by functions - used in the dropdown box at the top of the property list page */
/**************************************************************************/
/********************************************************************/
/**************************************************************************/
/********************************************************************/
/*  start detail functions */

	/* display the lightbox w/ detail information */
	function show_detail(mls_num,show_pg){
		$("<div id='faded' class='faded_overlay'></div>").appendTo('body');
		$("#faded").click(function(){
			hide_detail();
		});
		$("<div id='detail_content' class='detail_content'></div>").appendTo('body');
		$("#faded").css('display','block');
		$("#faded").css('height',getDocHeight());
		$("#detail_content").css('display','block');
		$("#detail_content").css('height',window.innerHeight-50);
		$("#detail_content").load('./content/properties/ajax/detail.php',{mls:mls_num,pg:show_pg},function(){																										   
			$("#details_slider").css('height',window.innerHeight-220);
		});
	}
	/* hide the lightbox w/ detail information */
	function hide_detail(){
		$("#faded").remove();
		$("#detail_content").remove();
	}
	/* figure out current document height */
	function getDocHeight() {
		var D = document;
		return Math.max(
			Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
			Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
			Math.max(D.body.clientHeight, D.documentElement.clientHeight)
		);
	}
	// submit request info form 
	function request_info(){
		$("#faded").css('display','block');
		$("#faded").css('height',getDocHeight());
		$("#detail_content").css('display','block');
		//alert($("#hidden_validation_code").val()+" - "+$("#validation_code").val());
		$("#details_content_area").load('./listing/content/sub/request_info.php',{fname:$("#fname").val(),lname:$("#lname").val(),phone:$("#phone").val(),email:$("#email").val(),comments:$("#comments").val(),validation_code:$("#validation_code").val(),hidden_validation_code:$("#hidden_validation_code").val(),Submit:'submit'},function(){});
	}
	//check the request info form data 
	function check_request_info(){
		var	$submit = true;
		if($("#fname").val()==""){
			$("#fname_error").css('display','block');
			$submit = false;
		}else{
			$("#fname_error").css('display','none');
		}
		if($("#lname").val()==""){
			$("#lname_error").css('display','block');
			$submit = false;
		}else{
			$("#lname_error").css('display','none');
		}
		if($("#phone").val()==""){
			$("#phone_error").css('display','block');
			$submit = false;
		}else{
			$("#phone_error").css('display','none');
		}
		if($("#email").val()==""){
			$("#email_error").css('display','block');
			$submit = false;
		}else{
			$("#email_error").css('display','none');
		}
		if($("#validation_code").val()==""){
			$("#validation_error").css('display','block');
			$submit = false;
		}else{
			$("#validation_error").css('display','none');
		}
		return $submit;
	}
/*  end detail functions */
/**************************************************************************/
/********************************************************************/
/**************************************************************************/
// BEGIN search box functionality
	//	clear input upon focus
	function input_focus(input){
		//alert(input);
		str = $("#"+input).attr('value');
		if(str=='City'  || str=='Zip Code' || str == 'MLS #' || str == 'Min' || str == 'Max' ){
			$("#"+input).val('');
		}
	}
	//restore the default value to an empty input upon blur
	function input_blur(input){
		str = $("#"+input).attr('value');
		id = $("#"+input).attr('id');
		if(str==''){
			if(id=='search_city'){
				$("#"+input).val('City');				
			}
			if(id=='search_zip'){
				$("#"+input).val('Zip Code');				
			}
			if(id=='search_mls'){
				$("#"+input).val('MLS #');				
			}
			if(id=='min'){
				$("#"+input).val('Min');				
			}
			if(id=='max'){
				$("#"+input).val('Max');				
			}
		}
	}
	//expand the search box downward
	var expand_search_open = false;
	function expand_search(){
		if(expand_search_open){	//close			
			$("#box2").animate({height:57},500);
			expand_search_open = false;
		}else{	//open			
			$("#box2").animate({height:140},500);
			expand_search_open = true;
		}
	}
	function clear_form(){
		$("#search_city").val("City");
		$("#search_mls").val("MLS #");
		$("#search_zip").val("Zip Code");
		$("#min").val("Min");
		$("#max").val("Max");
		$("#beds_input option:selected").attr("selected","");
		$("#bath_input option:selected").attr("selected","");
		$("#property_type option:selected").attr("selected","");
	}	
	
// END search box functionality
/**************************************************************************/
/**************************************************************************/
// BEGIN map functionality

	function map_refresh(e){
		//map.LatLongToPixel(new VELatLong(latitude, longitude), zoomLevel, alert(map.));
		pixel = new VEPixel(0,0);
		corner = map.PixelToLatLong(pixel);
		
		pixel2 = new VEPixel(400,350);
		corner2 = map.PixelToLatLong(pixel2);
		
		center = map.GetCenter();
		zoom = map.GetZoomLevel();
//alert(corner);
		//alert('/content/properties/ajax/map_refresh.php?tl='+corner.Latitude+'&bl='+corner2.Latitude+'&ll='+corner.Longitude+'&rl='+corner2.Longitude);
		//top_lat = corner[0];
		//bottom_lat = corner[1];
		//alert("---"+corner[0]+"---");
		//left_lon = ;
		//right_lon = ;
		alert(corner.Longitude);
		//$("#new_pins").load('/content/properties/ajax/map_refresh.php',{tl:corner.Latitude,bl:corner2.Latitude,ll:corner.Longitude,rl:corner2.Longitude });	
		$("#new_pins").load('/content/properties/ajax/map_refresh.php');	
		
	}
	/* bing map */
	function GetMap(clat,clon,zoomLevel){
		map = new VEMap('myMap');
		map.SetDashboardSize(VEDashboardSize.Normal);
		map.LoadMap(new VELatLong(clat,clon));
		map.SetZoomLevel(zoomLevel);
		map.ShowDashboard();
		map.AttachEvent("onmouseup", map_refresh);
	}     
	function AddPushpin(p){
		var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(p['lat'],p['lon']));
		shape.SetCustomIcon(icon);
		shape.SetTitle('<p>'+p['num']+' '+p['str']+'</p>');
		//shape.SetDescription('<p><a href="http://www.google.com" target="_blank" style="color:#111;">'+p['price']+'</a></p>');
		shape.SetDescription('<div class="bubble_side"><img src="http://www.kemteck3.com/photos/'+p['listing_num']+'.jpg" border=0 width="100" /></div><div class="bubble_side">'+p['price']+'<br><a href="javascript:void(0)" onclick = "show_detail('+p['listing_num']+',\'details\');" style="color:#111;">View Details</a></div>');
		//shape.SetDescription('<p><img src=\'http://www.kemteck3.com/images/'+p['thumb']+'\' /></p>');
		map.AddShape(shape);
	}

	/* google map */
    function gmap(center_lat,center_lon) {
		
		properties_gmap = new GMap2(document.getElementById("myMap"));
		var mapTypeControl = new GMapTypeControl();
		var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(5,5));		
		properties_gmap.addControl(mapTypeControl, topRight);
		GEvent.addListener(properties_gmap, "dblclick", function() {
			  //map.removeControl(mapTypeControl);
		});
		properties_gmap.addControl(new GSmallMapControl());
		properties_gmap.setCenter(new GLatLng(center_lat,center_lon), 10);
    	properties_gmap.setZoom(12);
		/* 
		var baseIcon = new GIcon();
		baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
		
		bubble = "<div class='google_map_head'>test</div><div class='google_map_body'>lorum ipsum. lorum ipsum. lorum ipsum. lorum ipsum. lorum ipsum. lorum ipsum. lorum ipsum. lorum ipsum. lorum ipsum. lorum ipsum. </div>";
		
		var letteredIcon = new GIcon(baseIcon);
		letteredIcon.image = "http://www.google.com/mapfiles/marker.png";
		
		var point = new GLatLng(center_lat,center_lon);
		marker = new GMarker(point);
		
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(bubble);
		});		
		properties_gmap.addOverlay(marker);		
		*/
	}
	function properties_add_pin(lat,lon,bubble){
		//bubble = "<h3>test</h3>";
		var baseIcon = new GIcon();
		baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
		
		var letteredIcon = new GIcon(baseIcon);
		letteredIcon.image = "http://www.google.com/mapfiles/marker.png";
		
		var point = new GLatLng(lat,lon);
		marker = new GMarker(point);
		
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(bubble);
		});		
		properties_gmap.addOverlay(marker);		
	}
	function call(i){
             marker.openInfoWindowHtml(i);
	}

// END map functionality
/**************************************************************************/



