$(document).ready(function(){
	$("#loading").ajaxStart(function(){
		$(this).show();
	});
	$("#loading").ajaxStop(function(){
		$(this).hide();
	});
	
	$(".regionsLeft").bind("click", function(){
		$(".hiddenMenu").slideUp();
		$("#cities"+$(this).attr("name")).slideDown("slow");
	});
	
	var byDeveloper = 0;
	$("input[name=byDeveloper]").bind("blur", function(){
		$("#developersList").hide();
	});
	$("#contactDeveloper").bind("click", function(){
		if ($("#contactFormDeveloper").css("display") == "none"){
			$("#contactFormDeveloper").slideDown("slow");
		}else{
			$("#contactFormDeveloper").slideUp("slow");
		}
	});
     $("#bulgariaRadio").bind("click", function(){
          $("#countriesList").fadeOut("slow");
     });
     $("#overseas").bind("click", function(){
          var status = $("#countriesList").css("display");
          if(status == "none"){
               $("#countriesList").fadeIn("slow");
          }else{
               $("#countriesList").fadeOut("slow");
          }
     });
   $("#picsScrolling").cycle({
    fx:     'fade', 
    speed:  'fast', 
    timeout: 0, 
    next:   '#next2', 
    prev:   '#prev2' 
	});
	$("#picsScrolling1").cycle({
    fx:     'fade', 
    speed:  'fast', 
    timeout: 0, 
    next:   '#next21', 
    prev:   '#prev21' 
});
	$("#picsScrolling13a").cycle({
		 fx: "fade"
	});
	$("select[name=convertCurrency],select[name=convertCurrency2]").bind("change", function(){
		$("#priceCurrency1").empty();
		$("#priceCurrency1").append($("option[value="+$(this).val()+"]").text().substring(0,3));
		$(".currencyDetail").empty();
		$(".currencyDetail").append($("option[value="+$(this).val()+"]").text().substring(0,3));
		
		$.ajax({
			type: "GET",
			url: "/convertCurrency.php?price="+$("#priceTotal").text()+"&priceRate="+$(this).val(),
			success: function(data){
				$("#priceTotalDescr").empty();
				$("#priceTotalDescr").append(data);
				$("#priceTotalSign").empty();
				$("#priceTotalSign").append(data);
			}
		});
		$.ajax({
			type: "GET",
			url: "/convertCurrency.php?price="+$("#pricePerSqm").text()+"&priceRate="+$(this).val(),
			success: function(data){
				$("#pricePerSqmDescr").empty();
				$("#pricePerSqmDescr").append(data);
			}
		});
		$.ajax({
			type: "GET",
			url: "/convertCurrency.php?price="+$("#priceStartsFrom").text()+"&priceRate="+$(this).val(),
			success: function(data){
				$("#priceStartsFromDescr").empty();
				$("#priceStartsFromDescr").append(data);
				$("#priceStartsFromSign").empty();
				$("#priceStartsFromSign").append(data);
			}
		});
	});
	
	$("input[name=byDeveloper]").bind("keyup", function(e){
		$("#developersList").show();
		if (e.keyCode == 40){
			//$("#developer"+byDeveloper).setFocus(); 
			$(".developerName").each(function(currentElement){
				$(this).css("background-color", "#004b7f");
				$(this).css("color", "#5eb1ff");
				if (currentElement == byDeveloper){
					$("#developer"+currentElement).css("background-color", "#0086e2");
					$("#developer"+currentElement).css("color", "#FFFFFF");
					$("input[name=byDeveloper]").val($("#developer"+currentElement).attr("name"));
					$("#developer"+currentElement).bind("keyup", function(e2){
						alert (e2.keyCode);
					});
				}
			});
			//$("#developer"+byDeveloper).css("background-color", "#0086e2");
			//$("#developer"+byDeveloper).css("color", "#FFFFFF");
			//alert("#developer"+byDeveloper);
			byDeveloper++;
		}else{
			byDeveloper =  0;
			var stringValue = $(this).val();
			$.ajax({
				type: "GET",
				url: "/byDeveloper.php?string="+stringValue,
				success: function(data){
					$("#developersList").empty();
					$("#developersList").append(data);
				},
				error: function(p1,p2,p3){
					alert(p1);
					alert(p2);
					alert(p3);
				}
			});
		}
	});
	var byDevelopment = 0;
	$("input[name=byDevelopment]").bind("blur", function(){
		$("#developmentsList").hide();
	});
	$(".developmentName").bind("click", function(){
		alert($(this).attr("name"));
		$("input[name=byDevelopment]").val($(this).attr("name"));
	});
	
	$("input[name=byDevelopment]").bind("keyup", function(e){
		
		$("#developmentsList").show();
		if (e.keyCode == 40){
			//$("#developer"+byDeveloper).setFocus(); 
			$(".developmentName").each(function(currentElement){
				$(this).css("background-color", "#004b7f");
				$(this).css("color", "#5eb1ff");
				if (currentElement == byDevelopment){
					$("#development"+currentElement).css("background-color", "#0086e2");
					$("#development"+currentElement).css("color", "#FFFFFF");
					$("input[name=byDevelopment]").val($("#development"+currentElement).attr("name"));
				}
			});
			byDevelopment++;
		}else{
			byDevelopment =  0;
			var stringValue = $(this).val();
			$.ajax({
				type: "GET",
				url: "/byDevelopment.php?string="+stringValue,
				success: function(data){
					$("#developmentsList").empty();
					$("#developmentsList").append(data);
				},
				error: function(p1,p2,p3){
					alert(p1);
					alert(p2);
					alert(p3);
				}
			});
		}
	});
	
	$(".facilityPic").bind("mouseenter", function(){
		$("#facilityDisplay").empty();
		$("#facilityDisplay").append($(this).attr("alt"));
		$("#facilityDisplay").fadeIn("fast");
	});
	
	$(".facilityPic").bind("mouseleave", function(){
		$("#facilityDisplay").fadeOut("fast");
	});
	$("#sendToAFriend").bind("click", function(){
		if ($("#sendToAFriendFormContent").css("display") == "none"){
			$.ajax({
				type: "GET",
				url: "/sendToAFriend.php",
				success: function(data){
					$("#sendToAFriendFormContent").empty();
					$("#sendToAFriendFormContent").append(data);
				}
			});
			$("#sendToAFriendFormContent").slideDown("fast");
		}else{
			$("#sendToAFriendFormContent").slideUp("fast");
		}
	});
	
	
	$("#showImages").bind("click", function(){
		$("#picturesRest").slideDown("slow");
		$(this).hide();
		$("#hideImages").show();
	});
	$("#hideImages").bind("click", function(){
		$("#picturesRest").slideUp("slow");
		$(this).hide();
		$("#showImages").show();
	});
});

function deleteImage(imgObj) {
	answer = confirm("За да потвърдите изтриването натиснете OK!");
	if(answer) {
		$.ajax({
			type: "GET",
			url: "/deleteImage.php?file_path=" + $(imgObj).attr("src"),
			success: function(data) {
				$($(imgObj).parent()).remove();
				$(imgObj).remove();			
			}
		});
	}	
}
function onBefore() { 
    $('#output').html( this.src); 
} 
function onAfter() { 
    $('#output').html( this.src) 
        .append( this.alt ); 
}



$(document).ready(function(){
		$(".link_blue_bigRegion").bind("click", function(){
			$("div[name='hideLeftMenuUL']").css({"display":"none", "visibility":"hidden"});
			$("#viewR" + $(this).attr("name")).css({"display":"block", "visibility":"visible"});
			//document.getElementById("viewR" + $(this).attr("name")).focus();
			//alert("viewR" + $(this).attr("name"));
		});
		$("#region_id").bind("change", function(){
			obj = this;
			getCity = jQuery("#city_id").val();
			if(getCity == undefined) getCity = 0;
			jQuery.ajax({
				type: "GET",
				url: "getItems.php?field=" + jQuery(obj).attr("name") + "&amp;value=" + jQuery(obj).val() + "&amp;getCity=" + getCity,
				dataType: "json",
				success: function(data){
					jsonData = data;
					options = "<option value=\"\">{/literal}{#select_any#} {#search_area#}{literal}</option>";
					jQuery.each(jsonData, function(i){
						options += "<option value=\"" + jsonData[i].id + "\">" + jsonData[i].name + "</option>";
					});
					jQuery("#city_id").html(options);
				}
			});
		});
});

