function $_(id) {
	return document.getElementById(id);
}
function addPics() {
	for(i in imgs) {
		$('#url_'+i).bind('click',
			new Function('return showBig(\''+imgs[i]['url']+'\','+imgs[i]['w']+','+imgs[i]['h']+');')
		);
	}
}
function showBig(url,w,h) {
		var wnd = window.open('','duze','width='+w+',height='+h+',toolbar=no,location=no,directories=no,status=no,menubar=no,resize=no,scrollbars=no');
		resize(w+30, h+30, wnd);
		
		wnd.document.clear();
		wnd.document.write (
		"<html><head><title>Noclegi4You</title></head><BODY bgcolor=#FFFFFF leftmargin=0 topmargin=0 marginwidth=0 marginheight=0 link=#000000 vlink=#000000 alink=#000000>\n"
		+"<table width='100%' height='100%' border='0' cellspacing='0' cellpadding='0' align='center'><tr><td align='center'>"
		+"<a href='javascript:window.close()'><img src='"+ url+"' width='"+w+"' height='"+h+"'></a>"
		+"</td></tr></table>\n"
		+"</body></html>"
	);
	wnd.document.close();
	wnd.focus();
	return false;
}
function resize(w,h,wnd) {
		 if (parseInt(navigator.appVersion)>3) {
		   if (navigator.appName=="Netscape") {
				wnd.innerWidth=w;
		   		wnd.innerHeight=h;
	   	   } else  {
	   	   		wnd.resizeTo(w+100,h+58);
	   	   }
	   	   }
}


function validateContactForm() {
	
	var name = $('#ifull_name').val();
	var error = false;
	if (jQuery.trim(name)=='') {
		$('#ifull_name').parent().children('span').remove()
		$('#ifull_name').parent().prepend('<span>Proszę podać imię i nazwisko</span>');
		error = true;
	} else {
		$('#ifull_name').parent().children('span').remove()
	}
	
	var name = $('#iemail').val();
	if (!isEmailValid(name)) {
		$('#iemail').parent().children('span').remove()
		$('#iemail').parent().prepend('<span>Proszę podać poprawny adres e-mail</span>');
		error = true;
	} else {
		$('#iemail').parent().children('span').remove()
	}
	
	var name = $('#iphone').val();
	if (!isPhoneValid(name)) {
		$('#iphone').parent().children('span').remove()
		$('#iphone').parent().prepend('<span>Proszę podać numer telefonu (9 cyfr)</span>');
		error = true;
	} else {
		$('#iphone').parent().children('span').remove()
	}
	
	var name = $('#icontent').val();
	if (jQuery.trim(name)=='') {
		$('#icontent').parent().children('span').remove()
		$('#icontent').parent().prepend('<span>Proszę podać treść zapytania</span>');
		error = true;
	} else {
		$('#icontent').parent().children('span').remove()
	}
	//Przepisz poprawnie kod z obrazka
	var name = $('#icode').val();
	if (jQuery.trim(name)=='') {
		$('#icode').parent().children('span').remove()
		$('#icode').parent().prepend('<span>Przepisz poprawnie kod z obrazka</span><br/>');
		error = true;
	} else {
		$('#icode').parent().children('span').remove()
	}
	return !error;
}

function isEmailValid(email) {
	return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(email);
}
function isPhoneValid(value) {
	return /^\d{9}$/i.test(value)
}

function changeAddForm() {
	if ($('#iFree').hasClass('selected')) {
		$('#iFree').removeClass('selected');
		$('#iPaid').addClass('selected');
		$('#izd5').show();
		$('#izd6').show();
		$('#izd7').show();
		$('#izd8').show();
		$('#iwwwPage').show();
		$('#iMapContent').show();
		$('#chbp').show();
		$('#fakdata').show();
		$('#bankcountid').show();
		$('#iObPaidType').val('PAID');
		$('#iBtnSend').val('Zapisz obiekt płatny'); 
		alert('Zmieniłeś typ dodawanego obiektu! Od teraz dodajesz obiekt korzystając z oferty płatnej');
		if (GBrowserIsCompatible()) {
    		initialize()
    	}
	} else {
		$('#iPaid').removeClass('selected');
		$('#iFree').addClass('selected');
		$('#izd5').hide();
		$('#izd6').hide();
		$('#izd7').hide();
		$('#izd8').hide();
		$('#iwwwPage').hide();
		$('#iMapContent').hide();
		$('#chbp').hide();
		$('#fakdata').hide();
		$('#bankcountid').hide();
		$('#iBtnSend').val('Zapisz obiekt bezpłatny');
		alert('Zmieniłeś typ dodawanego obiektu! Od teraz dodajesz obiekt korzystając z oferty darmowej');
		$('#iObPaidType').val('FREE');
	}
}


function initialize() {
  map = new google.maps.Map2(document.getElementById("imap"));
  if (typeof(postion) != 'undefined') {
    var point = new google.maps.LatLng(postion[1], postion[0])
    map.setCenter(point,postion[2]);
    if (arguments.length==0 && objhtml!='')
    	showPlaceMarker(postion[1], postion[0]);
    if (objhtml!='') {
    	map.openInfoWindow(point,objhtml);
    }
  }else {
    map.setCenter(new google.maps.LatLng(52, 19), 6);
  }
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  if (arguments.length==0) {
  	GEvent.addListener(map, "click", getAddress);
  	GEvent.addListener(map, "moveend", getPos);
  	GEvent.addListener(map, "zoomend", getPosZoom);
 	geocoder = new GClientGeocoder();
  } else if (arguments.length==1) {
 	GEvent.addListener(map, "moveend", getPos);
 	GEvent.addListener(map, "zoomend", getPosZoom);
  }
}

function getPos() {
	var pos = map.getCenter(); 
	$('#ilongitude').val(pos.lng());
    $('#ilatitude').val(pos.lat());
    $('#izoom').val(map.getZoom());
}
function getPosZoom(oldz,newz) {
  	$('#izoom').val(newz);
}

function getAddress(overlay, latlng) {
   /*if (latlng != null) {
    	address = latlng;
    	geocoder.getLocations(latlng, showAddress);
   }*/
   if (latlng) {   
   		map.clearOverlays();
   		if (objhtml!='') {
   			var myHtml = objhtml;
   		} else {
   			var myHtml = "Pozycja: " + map.fromLatLngToDivPixel(latlng) + ", zbliżenie: " + map.getZoom();
        }
    	marker = new GMarker(latlng,{draggable: true});
    	GEvent.addListener(marker, "dragend", function(place) {
  		if (place) {
    		getAddress(null,place)
      	}
  		});
        map.addOverlay(marker);
        marker.openInfoWindow(myHtml);
        $('#ilongitude').val(latlng.lng());
    	$('#ilatitude').val(latlng.lat());
    	$('#izoom').val(map.getZoom());
   }
}

function showAddress(response) {
  map.clearOverlays();
  if (!response || response.Status.code != 200) {
    alert("Status Code:" + response.Status.code);
  } else {
    place = response.Placemark[0];
    showPlaceMarker(place.Point.coordinates[1],place.Point.coordinates[0],place);
    $('#ilongitude').val(place.Point.coordinates[0]);
    $('#ilatitude').val(place.Point.coordinates[1]);
    $('#izoom').val(map.getZoom());
  }
}

function showPlaceMarker(cor1,cor0,place) {
    point = new GLatLng(cor1,cor0);
    marker = new GMarker(point,{draggable: true});
    
    GEvent.addListener(marker, "dragend", function(place) {
  		if (place) {
      		geocoder.getLocations(this.getLatLng(), showAddress);
      	}
  	});
  	
    map.addOverlay(marker);
    
    if (place) {
    	info = 'Prawdopodobny adres: <br />' + place.address;
    	marker.openInfoWindowHtml(info);
    }
    
}




function initializePublic() {
  map = new google.maps.Map2(document.getElementById("map"));
  map.addControl(new GLargeMapControl());
  if (typeof(postion) != 'undefined') {
    map.setCenter(new google.maps.LatLng(postion[1], postion[0]), 15);
    showPlaceMarkerPublic(postion[1], postion[0]);
  }else {
    map.setCenter(new google.maps.LatLng(52, 19), 6);
  }

}

function showPlaceMarkerPublic(cor1,cor0,place) {
  point = new GLatLng(cor1,cor0);
  marker = new GMarker(point);
  map.addOverlay(marker);
  var info = hotelLogo+'<br/><b>'+ hotelName +'</b><br />' + hotelAddress;
  if (place)
    info += '<br /><b>Address:</b>' + place.address;
  marker.openInfoWindowHtml(info);
}
function showMap(longitude,latitude,zoom,html) {
	map = new google.maps.Map2(document.getElementById("gmap"));
	map.setCenter(new google.maps.LatLng(latitude,longitude), zoom);
	map.addControl(new GLargeMapControl());
  	map.addControl(new GMapTypeControl());
  	var point = new GLatLng(latitude,longitude);
  	map.addOverlay(new GMarker(point));
  	if (html!='')
  		map.openInfoWindow(point,html);
}

function showPrintMap(longitude,latitude,zoom) {
	map = new google.maps.Map2(document.getElementById("gmap"));
	map.setCenter(new google.maps.LatLng(latitude,longitude), zoom);
  	var point = new GLatLng(latitude,longitude);
  	map.addOverlay(new GMarker(point));
}

function showClearMap(longitude,latitude,zoom) {
	map = new google.maps.Map2(document.getElementById("gmap"));
	map.setCenter(new google.maps.LatLng(latitude,longitude), zoom);
	map.addControl(new GLargeMapControl());
  	map.addControl(new GMapTypeControl());
}

function loadObjects(id) {
	var cstr = ''
	if (typeof(city)!='undefined') {
		cstr = '&cid='+city;
	} 
	
	$.getJSON("/maps/obj?id="+id+cstr,function(data) {showObjects(data)});
}

function loadRelics() {
	var cstr = ''
	if (typeof(city)!='undefined') {
		cstr = '?cid='+city;
	}
	$.getJSON("/maps/relic"+cstr,function(data) {showObjects(data)});
}


function loadPrice(pid) {
	var cstr = ''
	if (typeof(city)!='undefined') {
		cstr = '&cid='+city;
	}
	$.getJSON("/maps/price?id="+pid+cstr,function(data) {showObjects(data)});
}

function showObjects(data) {
	for(var i = 0;i<data.length;i++) {
		var p = data[i];
		var point = new GLatLng(p['lat'],p['lng']);
		var m = createMarker(point,p['html']);
		map.addOverlay(m);
	}
}


function createMarker(point, html) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, 'click', function() {
    marker.openInfoWindowHtml(html);
  });
  return marker;
}


function changePickers() {
	$('.tlist li').removeClass('active');
	$('.clist li').removeClass('active');
	$(this).parent().addClass('active');
	if (this.id=='relics') {
		loadRelics();
	} else {
		var typeId = parseInt(this.id.replace('ty_',''));
		loadObjects(typeId);
	}
	map.clearOverlays();
	
	return false;
	
}

function printWindow(id) {
	windowprops = "width=750,scrollbars=yes";
	preview = window.open('/index/print/id/'+id, "", windowprops);
	return false;
}
