var map = null;

var gmarkers = [];
var htmls = [];
var zooms = [];
var icons = [];
var hovericons = [];
var images = [];
var hoverimages = [];



var i=0;



function ShowHotelMap() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		var lat = parseFloat($("#map").attr("latitude"));
		var lng = parseFloat($("#map").attr("longtude"));
		var zoom = parseInt($("#map").attr("zoom"));

		map.setCenter(new GLatLng(lat, lng), zoom);
		map.setUIToDefault();

		showMarkers();
	}
}

function createHotelMarker(point, name, street, image, rating, id, price, icon, hovericon, city, link, online,zoom,imagepath,hoverimagepath,currency) {
	var marker = new GMarker(point, icon);
	//var marker = new GMarker(point);

	var innerhtml;
	innerhtml = "";
	innerhtml = innerhtml + '<div class="hotelbox" onmouseover=\'hoverMarker("' 
				+ id + '"); this.style.background="#eeeeee"; return false;\' onmouseout=\'unhoverMarker("' 
				+ id + '"); this.style.background="#ffffff"; return false;\'>';
	
	innerhtml = innerhtml + '<a href="#" onclick=\'showMarker("' + id + '");return false;\'>';	
	innerhtml = innerhtml + '<img class="hotelbox_marker" src="' + imagepath + '" /><span class="hotelbox_name">' + name + '</span>';
	if (rating != 0 && rating !='' && rating != '0' ) {
		for(i=0; i<rating;i++) {
			innerhtml = innerhtml + "<img class='hotelbox_star' src='/datastore/images/polish/starrating/star.png' />";
		}
		
	}
	
	innerhtml = innerhtml + "</a>";
	if (price != '') {
		innerhtml = innerhtml + '<span class="hotelbox_cena">Cena: <strong>' + price + ' ' + currency + '</strong></span>';
	}
	
	
	//innerhtml = innerhtml + "<br><a href="#" onclick="showMarker('" + id + "');return false;"><span style="font-size: 10px; font-family: Arial, sans-serif; font-weight: 500 ">Zobacz na mapie</span></a>";
	
	if(online == 1) {
		innerhtml = innerhtml + '<a class="hotelbox_link" href="' + link + '"><span>Rezerwacja Online</span></a>';
	} else {
		innerhtml = innerhtml + '<a class="hotelbox_link" href="' + link + '"><span>Rezerwacja za potwierdzeniem operatora</span></a>';
	}
	
	innerhtml = innerhtml + "</div>";


	tmphtml = "";
	
	tmphtml = tmphtml+ "<table><tr><td>";
	if (image != 0 && image !='' && image != '0' ) {
		tmphtml = tmphtml
			+ '<img width=70 height=60 src="' + image + '" />';
	} else {
		tmphtml = tmphtml + "&nbsp;";
	}
	tmphtml = tmphtml + "</td>";
	
	tmphtml = tmphtml + "<td>&nbsp;</td>"
	tmphtml = tmphtml + '<td><span style="font-size: 12px; font-family: Arial, sans-serif; font-weight: 900 ">' 
			+ name + '</span>';
	
	if (city != 0 && city !='' && city != '0' ) {
		tmphtml = tmphtml + "<br> " + city + ""
	}
	if (rating != 0 && rating !='' && rating != '0' ) {
		var ratingstars = '';
		for(i=0; i<rating;i++) {
			ratingstars = ratingstars + '*';
		}
		tmphtml = tmphtml + "<br>standard: " + ratingstars + "";
	}

	tmphtml = tmphtml + "<br>" + street + "";
	if (price != '')
		tmphtml = tmphtml + "<br><br>Cena: <b>" + price
				+ ' ' + currency + "</b>";
	if(online == 1) {
		tmphtml = tmphtml + '<br><br><a href="' + link
			+ '"><font size=2 face=arial>Rezerwacja Online</a>';
	} else {
		tmphtml = tmphtml + '<br><br><a href="' + link
			+ '"><font size=2 face=arial>Rezerwacja za potwierdzeniem operatora</a>';
	}

	tmphtml = tmphtml + "</td></table>";


	
	GEvent
			.addListener(
					marker,
					"mouseover",
					function() {

						html = "";
						
						html = html+ "<table><tr><td>";
						if (image != 0 && image !='' && image != '0' ) {
							html = html
								+ '<img width=70 height=60 src="' + image + '" />';
						} else {
							html = html + "&nbsp;";
						}
						html = html + "</td>";
						
						html = html + "<td>&nbsp;</td>"
						html = html + '<td><span style="font-size: 12px; font-family: Arial, sans-serif; font-weight: 900 ">' 
								+ name + '</span>';
						
						if (city != 0 && city !='' && city != '0' ) {
							html = html + "<br> " + city + ""
						}
						if (rating != 0 && rating !='' && rating != '0' ) {
							var ratingstars = '';
							for(i=0; i<rating;i++) {
								ratingstars = ratingstars + '*';
							}
							html = html + "<br>standard: " + ratingstars + "";
						}

						html = html + "<br>" + street + "";
						if (price != '')
							html = html + "<br><br>Cena: <b>" + price
									+ ' ' + currency +"</b>";
						if(online == 1) {
							html = html + '<br><br><a href="' + link
								+ '"><font size=2 face=arial>Rezerwacja Online</a>';
						} else {
							html = html + '<br><br><a href="' + link
								+ '"><font size=2 face=arial>Rezerwacja za potwierdzeniem operatora</a>';
						}

						html = html + "</td></table>";
						marker.openInfoWindowHtml(html);
					});

	gmarkers[id] = marker;
	htmls[id] = tmphtml;
	zooms[id] = zoom;
	icons[id] = icon;
	hovericons[id] = hovericon;
	images[id] = imagepath;
	hoverimages[id] = hoverimagepath;
	
	$("#lista_hotele").append(innerhtml);
	
	i++;
	return marker;
}

function showMarkers() {
	var markercount = 0;
	if (map != null) {
//		$("#below_overlay").hide(); $("#overlay_loading").show();
		$("#map_loading").show();
		gmarkers[i] = new Array();
		htmls[i] = new Array();
		zooms[i] = new Array();
		//map.clearMarkers();
		map.clearOverlays();
		$("#lista").html("<div id='lista_hotele'></div>");
		
		var result = "";
		$("#resultlist")
				.find(".box_oferta")
				.each(
						function(i) {
							if ($(this).attr("show") == "1") {
								var id = $(this).attr("id");
								var globalid = parseInt($(this).attr("globalid"));
								var rating = $(this).attr("stars");
								var price = $(this).attr("min");
								var lat = $(this).attr("latitude");
								var lng = $(this).attr("longtude");
								var zoom = $(this).attr("zoom");
								var name = $(this).find(".nazwa").find("a").text();
								var address = $(this).attr("address");
								var city = $(this).attr("cityname");
								var image = $(this).attr("imagelink");
								var link = $(this).attr("detailslink");
								var online = parseInt($(this).attr("online"));
								var currency =$(this).attr("currency");
								var point = new GLatLng(lat, lng);

								var icon = new GIcon();
								var imagepath = "/datastore/images/polish/mapgoogle/marker.php?image=" + (globalid);

								icon.image = imagepath;
								icon.shadow = "/datastore/images/polish/mapgoogle/mm_20_shadow.png";
								icon.iconSize = new GSize(30, 51);
								icon.shadowSize = new GSize(22, 20);
								icon.iconAnchor = new GPoint(15, 51);
								icon.infoWindowAnchor = new GPoint(15, 51);

								var hovericon = new GIcon();
								var hoverimagepath = "/datastore/images/polish/mapgoogle/marker_hover.php?image=" + (globalid);

								hovericon.image = hoverimagepath;
								hovericon.shadow = "/datastore/images/polish/mapgoogle/mm_20_shadow.png";
								hovericon.hovericonSize = new GSize(30, 51);
								hovericon.shadowSize = new GSize(22, 20);
								hovericon.hovericonAnchor = new GPoint(15, 51);
								hovericon.infoWindowAnchor = new GPoint(15, 51);

								
								if(lat != '') {
									marker = createHotelMarker(point, name,
										address, image, rating, id, price,
										icon, hovericon, city, link, online,
										zoom,imagepath,hoverimagepath,currency);
									map.addOverlay(marker);
									markercount++;
								}
							}
						});
		// $("#overlay_loading").hide(); $("#below_overlay").show();
	}
}


function showMarker(id) {
	if (map != null) {
		var marker = gmarkers[id];
		marker.openInfoWindowHtml(htmls[id]);
	}
}

function hoverMarker(id) {
	if (map != null) {
		var marker = gmarkers[id];
		marker.setImage( hoverimages[id] );
	}
}

function unhoverMarker(id) {
	if (map != null) {
		var marker = gmarkers[id];
		//marker.setImage( icons[id] );
		marker.setImage( images[id] );
		
	}	
}

/**********************************************************************/
/***************** mapa na wynikach  przy obiekcie **************************/
/**********************************************************************/

function showGoogleMaps(lg, lt, zm, tx) {
	
	$("#special_layer").center();
	$("#special_layer").show('fast', function(){

			sHtml ="";
			sHtml = sHtml + '<div style="padding: 5px; background-color: #FFF; border: 3px solid #9DB0C3;text-align:right">';
			sHtml = sHtml + '<a style="cursor:pointer" onclick="hideGoogleMaps()"><b>X</b></a>';
			sHtml = sHtml + '<div id="gmap" style="width: 400px; height: 300px"></div>';
			sHtml = sHtml + '</div>';
			
			$("#special_layer").html(sHtml); 
			
			setTimeout( function() {var showmaps = new ShowMap( document.getElementById('gmap'), lg , lt  ,zm,  tx );}, 1000);

		
	});
	
}

function ShowMap( Elem, Latitude, Longtude, Zoom , Opis) {
    this.latitude  = Latitude;
    this.longtude  = Longtude;
    this.zoom   = Zoom;
    this.element= Elem;
    this.opis = Opis;
    this.show();
}
		    
ShowMap.prototype.show = function() {
    if (GBrowserIsCompatible()) {
    	    
    	baseIcon = new GIcon(G_DEFAULT_ICON);
        baseIcon.shadow = "/datastore/images/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);
        
         var letteredIcon = new GIcon(baseIcon);
         letteredIcon.image = "/datastore/images/h.png";
            
        markerOptions = {icon:letteredIcon};

	var map   = new GMap2( this.element );
	var point = new GLatLng( this.latitude, this.longtude );
	map.setCenter( point, this.zoom );
	map.setUIToDefault();
	var marker = new GMarker(point, markerOptions)
	map.addOverlay( marker );
	var opisik = this.opis;
	GEvent.addListener(marker, "click", function() {
	        marker.openInfoWindowHtml(opisik);
	});
   }	
}

function hideGoogleMaps() {
    $("#special_layer").hide();
}

//document.write('<div id="special_layer" style="z-index:1000;display:none;"></div>');
/**********************************************************************/


$(document).ready(function() {
	$("#resultmap").hide();
	
	$("#overlay_loading").hide(); 
	//$("#below_overlay").show();  
	
	$("#guzikwyniki a.switch").click(function() {
		$("#resultlist").show();
		$("#resultmap").hide();
		return false;
	});
	$("#guzikmapa a.switch").click(function() {
		$("#resultmap").show();
		$("#resultlist").hide();
		ShowHotelMap();
		return false;
	});

});
