$(document).ready(function() {
    $("#mycarousel").jcarousel();
    $("#dalsieInfo").click(function(){
	    if(this.checked) {$("#text").val($("#text").val()+"Mám záujem o ďalšie informácie.");}
	    else {$("#text").val($("#text").val().replace("Mám záujem o ďalšie informácie.",""));}
    });
    $("#obhliadka").click(function(){
	    if(this.checked) {$("#text").val($("#text").val()+"Mám záujem o obhliadku nehnuteľnosti.");}
	    else {$("#text").val($("#text").val().replace("Mám záujem o obhliadku nehnuteľnosti.",""));}
    });
});

function zmenObce(okres_id) {

		var region_id = $('#select_region option:eq('+okres_id+')').attr("value");
		var region_clean_id = region_id.replace("-","");

		$("#select_obec").load("/getAjaxObec/"+region_clean_id);

}

//Vytvorí dialóg
function makeDialog(text)
{
	$('<div id="dialog"><p>'+text+'</p></div>').dialog({
		modal:true,
		resizable:false,
		draggable:false,
		height: 100,
		buttons:{
			"OK":function(){$(this).dialog("close");}
			}
	});
	return false;
}

function mapDialog(gpsX,gpsY,address)
{
	var canvas='map_canvas'+Math.random();
	$('<div id="dialog"><div id="'+canvas+'" style="width:500px;height:500px;"></div></div>').dialog({
		open:function(){drawMap(gpsX,gpsY,address,canvas);},
		modal:true,
		width:530,
		height:550,
		title:'Kde sa nehnuteľnosť nachádza?',
		resizable: false
	});
}

function drawMap(gpsX,gpsY,address,canvas) {
	gpsX=gpsX.replace(",",".");
	gpsY=gpsY.replace(",",".");
	var latlng = new google.maps.LatLng(gpsX,gpsY);
	var myOptions = {
		zoom: 14,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById(canvas),
	myOptions);
	if(gpsX!=0 && gpsY!=0)
	{
		var marker = new google.maps.Marker({
			position: latlng,
			map: map
		});
	}
	else
	{
		geocoder = new google.maps.Geocoder();
		geocoder.geocode( { 'address': address}, function(results, status) {
		  if (status == google.maps.GeocoderStatus.OK) {
			map.setCenter(results[0].geometry.location);
			var marker = new google.maps.Marker({
				map: map,
				position: results[0].geometry.location
			});
		  }
		});
	}
}

function ajaxWindow(url,title,width,height)
{
	$('<div class="dialog"></div>').dialog({
		modal:true,
		resizable:false,
		draggable:false,
		position:['center',50],
		width: width,
		height: height,
		title: title,
		open:function(){
			$(this).append('<div class="loading" style="display:block">Nahrávam ...</div>');
			$(this).load(url);
		}
	});
}

function postForm(where)
{
	$(".loading").show();
	$.ajax({
    	type: "POST",
    	url: $(where).attr("action"),
    	data: $(where).serialize(),
    	success: function(response) {
		try
		{
			//Ak je odpoveď cez JSON, tak formulár je v poriadku
			//a vykonám akciu podľa JSON poľa
			var respJSON=$.parseJSON(response);

			if(respJSON["action"]=="ajaxWindow")
				ajaxWindow(respJSON["url"],'');
			else if(respJSON["action"]=="redirect")
				window.location=respJSON["url"];
			else if(respJSON["action"]=="run")
				eval(respJSON["function"]);
			else if(respJSON["close"]) return;
			$(".dialog").dialog("close");
		}
		catch(e)
		{
			//Inak zobrazím odpoveď (teda znovu formulár)
			$(where).parent().html(response);
		}
    	},
    	error: function(url) {
    		alertDialog("Pri odosielaní formuláru nastala chyba. Skúste ešte raz");
    	}
    });
	return false;
}

function mamZaujemForm()
{
	if($("#name").val()=="") {alert("Zadajte meno");return false;}
	else if($("#posta").val()=="") {alert("Zadajte email");return false;}
	else if($("#text").val()=="") {alert("Zadajte text");return false;}
	return true;
}

function addLocality()
{
	var locality=$(".template:first").clone();
	$(locality).show();
	$(".locality").append(locality);
	$(".county").change(function(){
		$(this).next().next().next().load("/ciselnik-lokalit/district/"+$(this).val());
	});
	$(".district").change(function(){
		$(this).next().next().next().load("/ciselnik-lokalit/region/"+$(this).val());
	});
	return false;
}

function addCategory()
{
	var category=$(".categoryTemplate:first").clone();
	$(category).show();
	$(".category").append(category);
	return false;
}

function chcemKupitForm()
{
	var ret=true;
	$(".template:visible .county").each(function(){if($(this).val()==""){ret=false;alert("Zadajte kraj");}});
	$(".template:visible .district").each(function(){if($(this).val()==""){ret=false;alert("Zadajte okres");}});
	$(".template:visible .region").each(function(){if($(this).val()==""){ret=false;alert("Zadajte obec");}});
	if(!ret) return false;

	$(".categoryTemplate:visible .categorySel").each(function(){if($(this).val()==""){ret=false;alert("Zadajte kategóriu");}});
	if(!ret) return false;
	
	if($("#price").val()=="") {alert("Zadajte požadovanú cenu");return false;}
	else if($("#name").val()=="") {alert("Zadajte svoje meno");return false;}
	else if($("#posta").val()=="") {alert("Zadajte svoju emailovú adresu");return false;}
	else if($("#phone").val()=="") {alert("Zadajte svoje telefónne číslo");return false;}
	else if($("#address").val()=="") {alert("Zadajte svoju adresu");return false;}
	return true;
}

function chcemPredatForm()
{
	if($(".county:first").val()==""){alert("Zadajte kraj");return false;}
	else if($(".district:first").val()==""){alert("Zadajte okres");return false;}
	else if($(".region:first").val()==""){alert("Zadajte obec");return false;}
	else if($("#category").val()==""){alert("Zadajte kategóriu");}
	else if($("#price").val()=="") {alert("Zadajte požadovanú cenu");return false;}
	else if($("#name").val()=="") {alert("Zadajte svoje meno");return false;}
	else if($("#posta").val()=="") {alert("Zadajte svoju emailovú adresu");return false;}
	else if($("#phone").val()=="") {alert("Zadajte svoje telefónne číslo");return false;}
	else if($("#address").val()=="") {alert("Zadajte svoju adresu");return false;}
	return true;
}
