$(document).ready(function(){
	$("#loading").ajaxStart(function(){
		$(this).show();
	});
	$("#loading").ajaxStop(function(){
		$(this).hide();
	});
	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:     'turnLeft',
    delay:  -4000
});
	$("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 ); 
}
