$(document).ready(function(){

	// zobrazeni detailu pro vyhledavani
    $("#seach_more").click(function(){
        $(this).hide();
        $("#seach_less").show();
		    $(".property").slideDown();
		    $.cookie("detail-search", "enable");
    });

    $("#seach_less").click(function(){
        $(this).hide();
        $("#seach_more").show();
		    $(".property").slideUp();
		    $.cookie("detail-search", "disenable");
    }); 

	if( $.cookie("detail-search") == "enable" ){
        $("#seach_more").hide();
        $("#seach_less").show();
		    $(".property").show();
	}

	//fotogalerie v detailu produktu
	var div = $('div.previews');
	var ul = $('ul#previews');
	var ulPadding = 15;
	var divWidth = div.width();
	div.css({
		overflow: 'hidden'
	});

	ul.css({
		width: '5000px'
	});

	var lastLi = ul.find('li:last-child');
	div.mousemove(function(e){
		var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
		var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
		div.scrollLeft(left);
	}); 

  $("#previews li").mouseover(function(){
      $(this).css({ opacity: '0.5' });
	});

	$("#previews li").click(function(){
		var anchor = $(this).children(":first");
		var src = anchor.attr("src");
		var title = anchor.attr("title");
		$("#main_image").attr("src", src.replace(/gallery(.*)-reality\./, 'gallery$1-detail.' ) );
		$("#main_image").attr("title", title);
		$("#main_image").parent().attr("href", src.replace(/gallery(.*)-reality\./, 'gallery$1.' ) );
		$("#image_title").html(title);	
  });

    $("#previews li").mouseout(function(){
        $(this).css({ opacity: '1.0' });
    });

    /* dialogove okno pro e-maily */
    $("#send_mail_box").dialog({
        autoOpen: false,
        bgiframe: true,
        resizable: false,
        modal: false,
        buttons: {
            'Odeslat': function(){
                subject = $('#subject').attr("value");
                email = $('#email').attr("value");
                text = $('#text').attr("value");        

                if (subject == '' || email == '' || text == '') {
                    jAlert("Vyplňte všechny povinné údaje!");
                    return false;
                }

                $(this).dialog('close');                
                broker_id = $('#broker_id').attr("value");
				var mes = $.get("slot.php?module=broker&slot=send_mail&broker_id=" + broker_id + "&subject=" + subject + "&text=" + text + "&mail=" + email,
                    function(mes) {    
        				if( mes == "true"){        
        					jAlert("Email byl odeslán makléří reality!");        
        				}        
        				else{        
        					jAlert("Email se nepodařilo odeslát makléří reality!");        
        				}
    				}
                );

            },

            'Zrušit': function(){
                $(this).dialog('close');
            }
        }

    });

    $('#send_message').click(function(){
        $("#send_mail_box").dialog('open');
    });

	/* prepinani modu seznamu realit */
    $("#list_type").change(function () {
          var id = $(this).val();   
          if (id == 2) {
		  	$('#object_list').addClass('noimage');
		  }
		  else {
		  	$('#object_list').removeClass('noimage');
		  }
		  $.cookie("mode", id);
    });

	if($.cookie("mode") == 2){
        $('#object_list').addClass('noimage');
		$("#list_type").val( '2' );
    }

	/* razeni seznamu */
    $('#sort_but').click(function(){
		if ($("#sort_content").is(":hidden") ) {
			$("#sort_content").slideDown();
			$(this).addClass("active");
		}

		else{
			$("#sort_content").slideUp();
			$(this).removeClass("active");
		}
    });  

  /* multiselecty */
  $('.myselect .multivalue').hide();
  $('.myselect .scroll').click(function(){
       var box = $(this).parent().find('.multivalue');  
       var simage = $(this).parent().find('.triangle');     
       if ( box.is(':visible')){
          box.slideUp();
          simage.addClass( 'down' );
          simage.removeClass( 'up' );
       }
       else{
          // zavrit i vsechny ostatnni
          $('.search li').css('zIndex', 1);
          $('.myselect .triangle').removeClass( 'up' );
          $('.myselect .triangle').addClass( 'down' );
          $('.myselect .multivalue').hide();
          // otevrit aktualni
          box.slideDown();
          box.parent().parent().css('zIndex', 1000);
          simage.addClass( 'up' );
          simage.removeClass( 'down' );

       } 
  });
  
  // prepocet 
  $('.myselect .checkbox').click(function(){
     var clist = $(this).parent().parent();
     var childs = clist.find('input[type="checkbox"][checked="true"]');
     clist.parent().find('.count').html('vybráno položek: <b>'+childs.length+'</b>');
  });
  $('.myselect .all .checkbox').click(function(){
     // hromadne zakliknuti / odkliknuti
     var val = $(this).attr('checked');
     var clist = $(this).parent().parent();
     var childs = clist.find('input[type="checkbox"]').attr('checked', val );  
     // prepocet
     var clist = $(this).parent().parent();
     var childs = clist.find('input[type="checkbox"][checked="true"]');
     clist.parent().find('.count').html('vybráno položek: <b>'+childs.length+'</b>');    
  });
  // zmena pri najeti mysi
  $('.myselect li').mouseover(function() {
      $('.myselect li').removeClass( 'active' );
      $(this).addClass( 'active' );
  });
  // pridano polozek z mapky
  $('#czrep area').click(function(){
      var locality_id = $(this).attr('id');
      $('#mv_locality').find('input[id="mv_'+locality_id+'"]').attr('checked', 'checked' ); 
     // prepocet
     var clist = $(this).parent().parent();
     var childs = clist.find('#mv_locality input[type="checkbox"][checked="true"]');
     clist.parent().find('#mv_locality .count').html('vybráno položek: <b>'+childs.length+'</b>');   
  });
  
  
	/* pocitani kliku na bannery */
	$('.countable').click(function(){
			var id = $(this).attr("id");
			var link = $(this).children(':first-child').attr("href");	
			$.get("slot.php?module=banner&slot=add_click&banner_id=" + id);	
			$(this).ajaxComplete(function() {
			  window.location.href = link;
			});
	});	

	/* muj ucet - aktivace/deaktivace poptavky */
	$('.demand_activate_button').change(function() {
      var active = "";
		  var id = $(this).attr("id");
          $("#"+id+" option:selected").each(function () {
                active = $(this).attr("value");
          });		  
		  if( active == "yes"){
		  	jAlert("Poptávka byla aktivována!");
		  }
		  else{ 
		  	jAlert("Poptávka byla deaktivována!");
		  }

		  id = id.substring(7);
		  $.get("slot.php?module=customer_demand&slot=change_activity&value=" + active+ "&id=" + id);
	});		

	/* muj ucet - smazani poptavky */
	$('.demand_delete_button').click(function(){
		  var id = $(this).attr("id");
		  id = id.substring(7);			  

		  jConfirm('Opravdu chcete tuto poptávku smazat?', 'JM reality - upozornění!', function(del) {
			  if(del){
			  	$("#demand_"+id).remove();
			  	$.get("slot.php?module=customer_demand&slot=delete&id=" + id);
			  }
		  });

	});		

	/* muj ucet - aktivace/deaktivace nabidky */
	$('.suggestion_activate_button').change(function() {
      var active = "";
		  var id = $(this).attr("id");	 
          $("#"+id+" option:selected").each(function () {
                active = $(this).attr("value");
          });		  

		  if( active == "yes"){
		  	jAlert("Nabídka byla aktivována!", "JM reality - upozornění!");
		  }
		  else{
		  	jAlert("Nabídka byla deaktivována!", "JM reality - upozornění!");
		  }

		  id = id.substring(7);	
		  $.get("slot.php?module=customer_suggestion&slot=change_activity&value=" + active+ "&id=" + id);

	});

	/* muj ucet - smazani nabidky */
	$('.suggestion_delete_button').click(function(){
		  var id = $(this).attr("id");
		  id = id.substring(7);			  

		  jConfirm('Opravdu chcete tuto nabídku smazat?', 'JM reality - upozornění!', function(del) {
			  if(del){
			  	$("#suggestion_"+id).remove();
			  	$.get("slot.php?module=customer_suggestion&slot=delete&id=" + id);
			  }
		  });
	});	
	
	/* detail reality - pridani reality do oblibenych */	
	var only_one_click = true;
	$('.add_object_to_favourite').click(function(){
		  if(only_one_click){
			  var id = $(this).attr("id");
			  id = id.substring(10);			  
			  $.get("slot.php?module=object&slot=add_to_favourite&id=" + id);
			  jAlert("Tato realita byla přidána do oblíbených!", "JM reality - upozornění!");
			  
			  $(this).removeClass( 'add_object_to_favourite' );
			  $(this).attr( 'title', '' );
			  var icon = $( $(this).find('img') ).attr("src");
			  var new_icon = icon.replace('status_add_favourite', 'status_favourite' );
			  $( $(this).find('img') ).attr("src", new_icon );

			  only_one_click = false;
		  }
	});	
	
	/* muj ucet - smazani z oblibenych */
	$('.favourite_delete_button').click(function(){
		  var id = $(this).attr("id");
		  id = id.substring(7);			  

		  jConfirm('Opravdu chcete tuto realitu odstranit z oblíbených?', 'JM reality - upozornění!', function(del) {
			  if(del){
			  	$("#favourite_"+id).remove();
			  	$.get("slot.php?module=object&slot=delete_from_favourite&id=" + id);
			  }
		  });

	});		
	
	/* aktivace uzivatele */
	$('#active_user_button').click(function(){
		  var id = $('#user_id').html();			  
		  var hash = $('#user_hash').html();
		  
		  var ok = $.get("slot.php?module=user&slot=activate_user&id=" + id + "&hash=" + hash, function(data){
			  if( data == '1'){
				  jAlert("Uživatel byl aktivován!", "JM reality - upozornění!");
				  $("#active_user_button").remove();			  
			  }
			  else{
				  jAlert("Uživatele se bohužel nepodařilo aktivovat!", "JM reality - upozornění!");
				  $("#active_user_button").remove();
			  }
		  });

	});
	
	/* info box k hlavni kategorii */
	$(".categories li").mouseover(function() {
    $(this).children().next().next().toggle();
  }).mouseout(function(){
    $(this).children().next().next().hide();
  });

});

if ($.browser.msie && $.browser.version.substr(0,1)<7) {
  if (location.href == "http://www.jm-reality.cz/") {
    alert("Používáte prohlížeč, který nepodporuje moderní technologie tvorby webových stránek. Neručíme za kvalitu zobrazení stránek. Pro správné zobrazení prosím aktualizujte svůj prohlížeč na vyšší verzi.");
  }
}

