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

$(function(){

	// Accordion
	$('#faq.accordion dd').not(":first").hide();
	$('#links.accordion div').not(":first").hide();
	$('#faq.accordion dt:first, #links.accordion strong:first').addClass('active');
	
	$('#side-accordion dd').not(":first").hide();
	
	$('#side-accordion dt').click(function(){
	  if ( $(this).is('.active') ) { return false; }
		if ( $(this).next().is(':hidden') ) {
		  $('#side-accordion .scroll').css("overflow","hidden");
			$(this).next().slideDown(250).siblings('dd:visible').slideUp(250,function(){ 
          $(this).prev().removeClass('active');
          $('#side-accordion .scroll').css("overflow","auto");
      });
			$(this).addClass('active');
		}
		else {
			$(this).next().slideUp(250,function(){ $(this).prev().removeClass('active'); });
		}
	});
	
	$('#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('div: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;
		}
	});
	
	// Print link
	/* $('.real-estate-single-controls > ul').prepend(''); */
	$('.link-print').click(function(){ window.print(); });
	
	// Add to favorites link
	function addToBookmarks(url,title) {
	if (window.external && window.external.AddFavorite) 
		window.external.AddFavorite(url,title);
	else if (window.sidebar && window.sidebar.addPanel)
		window.sidebar.addPanel(title,url,'');
	}
	/* $('#footer #extras').append('<li><a href="#" title="Stavite stranicu u favorite">Stavi stranicu u favorite</a></li>'); */
	$('#footer #extras li:last-child').click(function(){ addToBookmarks(window.document.location, window.document.title); });
	
	// Lightbox
	$('a[rel=lightbox]').lightBox();
	
	// Send query
	$('.sendQuery').click(function(event){
	    var realEstateId = $(this).attr("rel");
      $.get(phrasebook.URL_ROOT+"/index.php?page=queryPopup&realEstate_id="+realEstateId, 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;
          });
      });
	});
	
	// Tooltip
	$('#query fieldset img').click(function(){ $(this).next().toggle(); });
	$('.curr-button').click(function(){ $(this).next(".info-popup").toggle(); });
	
	// Png Fix
	$('.sold').pngFix();
	
	var checkie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
	var checkie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

	if (jQuery.browser.msie && (checkie55 || checkie6)) {
	   $("#content-supp-additional li h3").css("background","none").css("filter","progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+phrasebook.URL_ROOT+"/images/bg-real-estate-sale-01.png', sizingMethod='crop')");
     $("#content-main #picture-slideshow span").css("background","none").css("filter","progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+phrasebook.URL_ROOT+"/images/bg-picture-slideshow.png', sizingMethod='crop')");	
	}
	
	

});

