$('html').addClass('js');

$(function(){

	// Lightbox
	$('a[rel=lightbox]').lightBox();
	
	// Accordion
	$('#faq.accordion dd, #links.accordion ul ').hide();
	
	$('#faq.accordion dt').click(function(){
		if ( $(this).next().is(':hidden') ) {
			$(this).next().slideDown(250).siblings('dd:visible').slideUp(250,function(){ $(this).prev().removeClass('active'); });
			$(this).addClass('active');
		}
		else {
			$(this).next().slideUp(250,function(){ $(this).prev().removeClass('active'); });
		}
	});
	
	$('#links.accordion > li strong').click(function(){
		if ( $(this).next().is(':hidden') ) {
			$(this).next().slideDown(250).parent().siblings().children('ul:visible').slideUp(250,function(){ $(this).prev().removeClass('active'); });
			$(this).addClass('active');
		}
		else {
			$(this).next().slideUp(250,function(){ $(this).prev().removeClass('active'); });
		}
	});

	// Show/hide value
	$("#queryUser_available").focus(function() {
			if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
			if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
	
	// Tooltip
	$('#query fieldset img').click(function(){ 
		$(this).next().toggle(); 
	});
	
	// Print link
	$('.link-print').click(function(){ 
		window.print(); 
	});
	
	// Send query
	$('.sendQuery').click(function(event){
	    var realEstateId = $(this).attr("rel");
      $.get("upit-forma.html", function(data){
          $("body").append(data);          
          var pos = $("#real-estate-single").position();
          var pos2 = $("#container").position();
          $("#queryPopup").css({ top: pos.top+pos2.top+200, left: pos.left+pos2.left+150 }).show();
          
          $(window).resize(function(){
              pos = $("#real-estate-single").position();
              pos2 = $("#container").position();
              $("#queryPopup").css({ top: pos.top+pos2.top+200, left: pos.left+pos2.left+150 });
          });          
          
          $("#closeQuary").click(function(){
              $("#queryPopup").remove();
          });
          
          $(document).bind('click', function(e){
              var $clicked = $(e.target);
              if (!($clicked.is('#queryPopup') || $clicked.parents().is('#queryPopup'))) {
                $("#queryPopup").remove();
                $(document).unbind('click');
              }
          });  
          var realEstateAction = $("#queryPopup form").attr("action");
          $("#submitQuary").click(function(){
              $("#queryPopup input").removeClass("error");
              switch ($("#queryPopup .radio:checked").val()){
                  case "email":
                    if( $("#quaryEmail").val()=="" ){ $("#quaryEmail").addClass("error"); }                   
                    break;
                  case "phone":
                    if( $("#quaryPhone").val()=="" ){ $("#quaryPhone").addClass("error"); }
                    break;              
              }
              if($("#queryPopup .error").length>0){ return false; }
              else {
                  $.get(realEstateAction, { realEstate_id:$("#realEstate_id").val(),contact_method:"email",quaryName:$("#quaryName").val(),quaryPhone:$("#quaryPhone").val(),quaryEmail:$("#quaryEmail").val(),quaryMessage:$("#quaryMessage").val() },function(data){
                    $("#queryPopup fieldset.left").remove();
                    $("#queryPopup fieldset label").remove();
                    $("#queryPopup fieldset textarea").remove();
                    $("#submitQuary").remove();
                    $("#queryPopup blockquote").after("<p class='note'>"+data+"</p>");
                	});
              }
              return false;
          });
      });
	});
	
	// Separated offer switcher
	$("#separated-offer ul li").click(function(){
      if ($(this).is(".active")){ return false; }
      else {
          $("#separated-offer ul li").removeClass("active");
          $(this).addClass("active");
          var order = $(this).prevAll().length;
          $("#separated-offer div").removeClass("on");
          $("#separated-offer div:eq("+order+")").addClass("on");
          return false;
      } 
  });
});