/*deleteCache-description:hotel directory main js file*//****************************************************************************/
/*			Hotel directory JS, created in 2008-09	by Feri							 */
/****************************************************************************/

// global variables
var currentTab = -1;
var point;
var marker_count = 1;
var box;
var active_marker;
var bounds = 0;
var map;
var zoomOutDiv = 0;

$(document).ready(function(){ // on page loaded
	$("div.amenities").Tooltip({
		track: true,
		delay: 100
	});
	
	$("div.tools").Tooltip({
		track: true,
		delay: 100
	});
	
	$("div.star").Tooltip({
		track: true,
		delay: 100
	});
	
	change(0,null);
	
	if(document.getElementById("popup_mail"))
	document.onclick=close_mail;
	
	if ($('#l_All').length != 0) changeLetter('All');
	if ($('#tab_0_div').length != 0) changeTab(0);
	
	// destination map intit
	if(typeof(city_id) == 'undefined' && $('#tab_0_div').length == 0)
	{
	
		map = new GMap(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(centerLat, centerLng));
		map.setZoom(initialZoom);
		createMapIcon();
		
		var click_latlng = new GLatLng(0.0, 0.0);
		bounds = map.getBounds();
		
		// event listener for map
		GEvent.addListener(map, "click", function(marker,point) {
		if (!marker) {
			map.removeOverlay(box);
			add_box(point);
		}
		document.getElementById('help_popup').style.visibility='hidden';
		});
		
		GEvent.addListener(map, "zoomend", function() {
		bounds = map.getBounds();
		map.closeInfoWindow();
		map.removeOverlay(box);
		if (map.getZoom()>7) {
			marker_count = 1;
			bounds = map.getBounds();
			var params = "n=" +  bounds.getNorthEast().lat() + "&w=" + bounds.getSouthWest().lng()
						+ "&s=" + bounds.getSouthWest().lat() + "&e=" + bounds.getNorthEast().lng()
						+ "&idReferral=" + document.getElementById('idReferral').value + "&adid=" + document.getElementById('adid').value;
			makeRequestHotel(params);
		}
		else map.clearOverlays();
		});
		
		GEvent.addListener(map, "moveend", function() {
		bounds = map.getBounds();
		});
	}else if(typeof(city_id) != 'undefined') // city map init
	{
			map = new GMap2(document.getElementById("map"));
			bounds = new GLatLngBounds();
			control= new GSmallMapControl();
			map.addControl(control);
			map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng(0.0,0.0));
			show_markers();
	}
});


// other function
 
var position=0;
function change(pos, leng)
{
 if(document.getElementById('next'))
 {
	if(leng==null)
		{
		 var	im = document.getElementsByTagName('img');
				leng=0;
				for(i=0;i < im.length; i++)
					{
						if(im[i].id.indexOf('_holder') !=-1)
						leng++;
					}
		
		};
	position+=pos;
	if(position==(leng-1)) document.getElementById('next').style.visibility='hidden';
	else document.getElementById('next').style.visibility='visible';

	if(position==0) document.getElementById('prev').style.visibility='hidden';
	else document.getElementById('prev').style.visibility='visible';

	document.getElementById('img_container').src=document.getElementById(position+'_holder').src;
	
}
}

function preventEventToPropagate(ev)
  { 
	if(document.all)
	  {
			ev.returnValue = false;
			ev.cancelBubble = true;
	  }
	 else
	  {
			ev.preventDefault();
			ev.stopPropagation();
	  }
  }

function createRequestObject()
  {
	var ro;
	var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer")
	  {
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	  }else{
			ro = new XMLHttpRequest();
		   }
	return ro;
  }

function verify(id)
{
	var error="";
	if (document.getElementById('yourname').value.length==0) error+="Please enter your name!\n";
	if (document.getElementById('emails').value.length==0) error+="Please enter email addresses!\n";

	if(error!="") alert(error);
	else mail_hotel(id);
}

function close_mail()
{
 $("#popup_mail").css('visibility','hidden');
}

function mail_hotel(id)
{
	var http= new createRequestObject();
 	var numb = Math.random(10)+1; // for IE cache problem

 	posting= "id="+id;
	posting+= "&yourname="+document.getElementById('yourname').value;
	posting+= "&emails="+document.getElementById('emails').value;

	http.onreadystatechange=function()
				{
				 if(http.readyState==4)
				  {
					document.getElementById("popup_mail").innerHTML= http.responseText;
				  };
				}

	http.open('GET','/hotel-directory2/mail_db.php?'+posting+"&num="+numb,true);
	http.send(null);
}

function popup_mailer(id)
{
	
	var position = $('#mail').offset();
	var text="<table width=398 border=0 cellpadding=0 cellspacing=0><tr><td rowspan=3>"+img_left+"</td><td height=21>"+img_top+"</td><td rowspan=3>"+img_right+"</td></tr><tr><td height=193 bgcolor='#ffffff' valign='top'><table><tr><td><img src='http://www.travelgrove.com/images/categ_deals/close.gif' style='cursor:pointer' onclick='close_mail()'></td><td align='center'><span style='font:16px Helvetica, Arial, sans-serif;color:#ff6300;'><b>Mail this Hotel!</b></span></td><td align='right' width=57><img src='http://www.travelgrove.com/images/categ_deals/mail.gif'></td></tr><tr><td colspan=3><font style='color:#001943; font-weight:bold; font-size:10px'>Please fill out the infromation below, we will send the selected hotel to all people you enter.</font></td></tr><tr><td colspan='3'><table><tr><td><font style='color:#001943; font-weight:bold; font-size:10px'>Your Name:</font></td><td><input id='yourname'></td></tr><tr><td colspan=2><font style='color:#001943; font-weight:bold; font-size:10px'>Your friends mail address: (separate with comma)</font><br><input id='emails' style='width:252px;'></td></tr><tr><td colspan=2><img src='http://www.travelgrove.com/images/categ_deals/submit.gif' onclick='verify("+id+")' style='cursor:pointer'></td></tr></table></td></tr></table></td></tr><tr><td height=36>"+img_bottom+"</td></tr></table>";

	$("#popup_mail").html(text);
	$("#popup_mail").css('left', position.left-380);
	$("#popup_mail").css('top', position.top);
	$("#popup_mail").css('visibility', 'visible');
}

function tiny_submit()
{
	var error="";
	if(document.getElementById('city_t').value.length==0)
	error+= "Please fill out City filed!\n";

	var check_in = new Date();
	month = parseInt($('#ckin_month_t').val())-1;
	day = parseInt($('#ckin_day_t').val());
	year= parseInt($('#ckin_year_t').val());
	check_in.setFullYear(year, month, day);

	var check_out = new Date();
	month = parseInt($('#ckout_month_t').val())-1;
	day = parseInt($('#ckout_day_t').val());
	year= parseInt($('#ckout_year_t').val());
	check_out.setFullYear(year, month, day);

	if(check_out <= check_in) error+="Check Out date must be greater than Check In date!\n";
	if(error!="")
	alert(error);else
	document.getElementById('city_t').form.submit();
}

// autosuggestion
 function goTo(loc) {
	document.location.href = loc;
	
 }


 function encode(textToEncode) {
	textToEncode = textToEncode.replace(/&/g, "and");
	textToEncode = textToEncode.replace(/ \/ /g, ";");
	textToEncode = textToEncode.replace(/'/g, ",");
	//textToEncode = escape(textToEncode);
	textToEncode = encodeURIComponent(textToEncode);
	textToEncode = textToEncode.replace(/%20/g, "%2B");
	return textToEncode;
 }

function changeTab(tab)
	{
		if(tab == 5)
		{
				$(".datepicker_calendar").datepicker('destroy');
				$(".datepicker_calendar").datepicker('destroy');
		};
		$("#tab_"+currentTab+"_div").attr('className', 'tab_unselected');
		$("#tab_"+currentTab+"_left").attr('className', 'tab_left');
		$("#tab_"+currentTab+"_middle").attr('className', 'tab_middle title_menu');
		$("#tab_"+currentTab+"_right").attr('className', 'tab_right');
		$("#tab_"+currentTab+"_content").html($("#tab_container").html());
		$("#tab_"+tab+"_div").attr('className', 'tab_selected');
		$("#tab_"+tab+"_left").attr('className', 'tab_left_hl');
		$("#tab_"+tab+"_middle").attr('className', 'tab_middle_hl title_menu');
		$("#tab_"+tab+"_right").attr('className', 'tab_right_hl');
		currentTab = tab;
		
		$("#tab_container").html($("#tab_"+tab+"_content").html());
		$("#tab_"+tab+"_content").empty();
		if(tab == 5) {createDatePicker();}
		if(tab == 4 && longitude != 0)
		{
			map = new GMap2(document.getElementById("map"));
			control= new GSmallMapControl();
			map.addControl(control);
			map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng(latitude,longitude));
			var point = new GLatLng(latitude, longitude);
			var marker = createMarker(point, address);
			map.addOverlay(marker);
			map.setZoom(15);
		}
	}
	
// map


 function add_box(latlng) {
	click_latlng = latlng;
	var point = map.fromLatLngToDivPixel(latlng);
	var nePoint = new GPoint(point.x + 20, point.y - 20);
	var swPoint = new GPoint(point.x - 20, point.y + 20);
	var ne = map.fromDivPixelToLatLng(nePoint);
	var sw = map.fromDivPixelToLatLng(swPoint);
	var nw = new GLatLng(ne.lat(), sw.lng());
	var se = new GLatLng(sw.lat(), ne.lng());

	box = new GPolyline([ne, se, sw, nw, ne], "#FF6300", 2, .80);
	map.addOverlay(box);

	var _params = "center_x=" + latlng.lng() + "&center_y=" + latlng.lat() + "&n=" +  nw.lat() + "&w=" + nw.lng() + "&s=" + se.lat() + "&e=" + se.lng() + "&zoom=" + map.getZoom();
	show_bubble(latlng, _params);
	map.recenterOrPanToLatLng(latlng);
 }


 function show_bubble(latlng, _params) {
	_params2 = _params;
	var _html = "<div id='map_inner_div' style='width: 200px; height: 160px;'><table cellpadding='0' cellspacing='0'><tr><td align='center' valign='middle' width='200' height='160'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='/community/images/global/waiting.gif'></td></tr></table></div>";
	map.openInfoWindowHtml(latlng, _html);
	window.setTimeout("makeRequest('get_map_cities.php', _params2, document.getElementById('map_inner_div'))", 300);
 }


 function zoomIn(_lat, _lng) {
	if (_lat=='0' && _lng=='0') {
		map.setCenter(click_latlng, map.getZoom()+2);
	}
	else {
		_center = new GLatLng(_lat, _lng);
		map.setCenter(_center, 8);
	}
 }


 function zoomOut(_lat, _lng) {
	_center = new GLatLng(_lat, _lng);
	map.setCenter(_center, 2);
 }


 function makeRequest(_url, _params, _div) {
	_url = "/hotel-directory2/" + _url;
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
            }
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	
	http_request.onreadystatechange = function (  ) {
		if (http_request.readyState == 4) {
			if (_div.id=="map_inner_div") {
				_div.innerHTML = http_request.responseText;
			}
		}
	}

	http_request.open("GET", _url + "?" + _params + "&rand=" + Math.random(), true);
	http_request.send(null);
 }


 function show_help() {
	var position = $('#mapHelpIcon').position();
	$('#help_popup').css('left',position.left+7);
	$('#help_popup').css('top',position.top-22)
	$('#help_popup').css('visibility','visible');
 }


 function close_help() {
	if (document.getElementById('help_popup')) document.getElementById('help_popup').style.visibility = "hidden";
 }


 function createMapIcon() {
	var mapDivs = document.getElementById('map').getElementsByTagName('div');
	for (i=0; i<mapDivs.length; i++)
		if (mapDivs[i].title=="Zoom Out") { zoomOutDiv = mapDivs[i]; break; }
	var position = $(zoomOutDiv).offset();
	$('#mapHelpIcon').css('left',position.left);
	$('#mapHelpIcon').css('top',position.top+20);
	$('#mapHelpIcon').css('visibility','visible');

 }


 function makeRequestHotel(params) {
	var request = GXmlHttp.create();
	request.open("GET", "/hotel-directory2/get_map_hotels.php?" + params, true);

	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			var xmlDoc = request.responseXML;
			if (!xmlDoc) return;
			var markers = xmlDoc.documentElement.getElementsByTagName("marker");

			for (var i = 0; i < markers.length; i++) {
			    var lat = parseFloat(markers[i].getAttribute("lat"));
				var lng = parseFloat(markers[i].getAttribute("lng"));
					 point = new GLatLng(lat,lng);
				var html = markers[i].getAttribute("html");

				var marker = createMarker(point,html);
				map.addOverlay(marker);
				if (marker_count>=20) return;
			}
		}
	}
	request.send(null);
 }


 function createMarker(point, html) {
	var hotelIcon = new GIcon();
	hotelIcon.image="/hotel-directory2/images/hotelicons/hot" + marker_count + ".gif";
	hotelIcon.shadow = "/hotel-directory2/images/hotelicons/shadow2.png";
	hotelIcon.iconSize = new GSize(17, 19);
	hotelIcon.shadowSize = new GSize(23, 19)
	hotelIcon.iconAnchor = new GPoint(31, 34);
	hotelIcon.infoWindowAnchor = new GPoint(9, 9);
	hotelIcon.transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
	var marker = new GMarker(point,hotelIcon);
	
    GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	
	});

	marker_count++;
	return marker;
 }


// city map

function show_markers()
		{

			map.clearOverlays();
			var request = GXmlHttp.create();
			request.open("GET","/hotel-directory2/map_in_city_db.php?city_id="+city_id+"&rand="+parseFloat(Math.random(10)+1), true);

			request.onreadystatechange = function() {
					if (request.readyState == 4) 
					{	
						var xmlDoc = request.responseXML;
						var markers = xmlDoc.documentElement.getElementsByTagName("marker");
						
						for (var i = 1; i < markers.length; i++)
						{
						   var lat = parseFloat(markers[i].getAttribute("lat"));
							var lng = parseFloat(markers[i].getAttribute("lng"));
							 
							if(lat!=null && lat!=0)
								{	
							 		var point = new GLatLng(lat,lng);
							 		var html = markers[i].getAttribute("html");
									bounds.extend(point);
									var marker = createMarker(point,html);
									map.addOverlay(marker); 
								}
						}

					    var lat = parseFloat(markers[0].getAttribute("lat"));
						 var lng = parseFloat(markers[0].getAttribute("lng"));
						 
				 		map.setZoom(map.getBoundsZoomLevel(bounds));
      							var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2;
      							var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2;
      							map.setCenter(new GLatLng(clat,clng), map.getZoom());
					}
			}
			 request.send(null);
		}

function changeLetter(letter)
{
	$('#l_All').css('text-decoration', 'underline').css('color','#000099').css('font-weight', 'normal');
	letters  = "ABCDEFGHIJKLMNOPQRSTUVWXY";
	for (i = 0; i <= letters.length; i++)
	$('#l_'+letters[i]).css('text-decoration', 'underline').css('color','#000099').css('font-weight', 'normal');

	$('#l_'+letter).css('text-decoration', 'none').css('color','#ff6300').css('font-weight', 'bold');
	if(letter == "All")
		$('#city_container').html($('#hidden_container').html());
	else
		$('#city_container').html($('#letter_'+letter).html());
		
	if(typeof(zoomOutDiv) != 'undefined' && zoomOutDiv != 0 && $('#mapHelpIcon').css('visibility') == 'visible')
	{
		var position = $(zoomOutDiv).offset();
		$('#mapHelpIcon').css('left',position.left);
		$('#mapHelpIcon').css('top',position.top+20);
	}
};
