var productQuantity = 4;
var linkActive = true;
var advancedFiltr = false;
$(document).ready(function(){

    $("#phraze").focus(function() {
        if ($(this).attr("value") == "Hledaný výraz ...") {
            $(this).attr("value","");
        };
    });

    $("#phraze").blur(function() {
        if($(this).attr("value") == "") {
            $(this).attr("value","Hledaný výraz ...");
        };
    });
    
    $(".quantityValue").focus(function() {
       productQuantity = $(this).attr('value');
    });
    
    $(".quantityValue").blur(function() {
       $('#link'+$(this).attr('id')).attr('href',$('#link'+$(this).attr('id')).attr('href').replace('quantity='+productQuantity,'quantity='+$(this).attr('value')));
    });
    
    $("div#content div#view_type.catalog div.item div.bottom-box input[type=text]").click(function(){
       linkActive = false;
    });
    
    $("div#content div#view_type.catalog div.item").click(function(){
       if(linkActive){ 
         var link = $(this).children(".right-box").children("h3").children("a").attr('href');
         $(window.location).attr('href', link);
       }
       else{
        linkActive = true;
       }
    });
   
  var starsNumber = 0;
  $('#id_rate').after('<div id="stars"><div class="star"></div><div class="star"></div><div class="star"></div><div class="star"></div><div class="star"></div></div>');
  $('#id_rate').css('display','none');
  $('#stars .star').hover(function(){
    switch($(this).parent().children().index(this)){
    case 0:
          $(this).parent().attr('class','one');
          break;
    case 1:
          $(this).parent().attr('class','two');
          break;
    case 2:
          $(this).parent().attr('class','three');
          break;
    case 3:
          $(this).parent().attr('class','four');
          break;
    case 4:
          $(this).parent().attr('class','five');
          break;
    }
  },
  function(){
    switch(starsNumber){
    case 1:
          $(this).parent().attr('class','one');
          break;
    case 2:
          $(this).parent().attr('class','two');
          break;
    case 3:
          $(this).parent().attr('class','three');
          break;
    case 4:
          $(this).parent().attr('class','four');
          break;
    case 5:
          $(this).parent().attr('class','five');
          break;
    default:
          $(this).parent().removeAttr('class');
          break;      
    }
  });
  $('#stars .star').mousedown(function(){
    starsNumber=$(this).parent().children().index(this)+1;
    var options = $('#id_rate').children();
    $(options[0]).attr('value',starsNumber);
  });
  
  $('div.advanced').hide();
  $('.show-advanced').click(function(){  
    if(advancedFiltr){
      $('div.advanced').hide(); 
      $('.show-advanced').text('Zobrazit pokročilé vyhledávání');     
    }else{
      $('div.advanced').show();
      $('.show-advanced').text('Skrýt pokročilé vyhledávání');
    }
    advancedFiltr = !advancedFiltr;
  });
  
  $('table.products a').hover(function(){
    $(this).parent().children('.plus').addClass('hover');
  },
  function(){
    $(this).parent().children('.plus').removeClass('hover');
  });
  
  $('table.product_gallery .main a').hover(function(){
    $(this).parent().children('.plus').addClass('hover');
  },
  function(){
    $(this).parent().children('.plus').removeClass('hover');
  });
  
  $('.catalog-style label').click(function(){
  });
  
  $('#compare-page img').hover(function(){
    $(this).parent().parent().children('.plus').addClass('hover');
  },
  function(){
    $(this).parent().parent().children('.plus').removeClass('hover');
  });
  
  $('table.list .image').each(function(){
    $(this).attr('style','position: absolute; width: '+$(this).parent().width()+'px; height: '+$(this).parent().height()+'px;');
    $(this).hover(function(){
      $(this).children('.product-detail-image').css('display','block');
      var borderWidth = 197;
      var borderHeight = 196;
      var imgWidth = $(this).children('.product-detail-image').children('img').width();
      var imgHeight = $(this).children('.product-detail-image').children('img').height();
      var marginTop = Math.floor((borderHeight - imgHeight)/2);
      var marginLeft = Math.floor((borderWidth - imgWidth)/2); 
      
      $(this).children('.product-detail-image').children('img').css('margin',marginTop+'px '+(marginLeft-5)+'px '+marginTop+'px '+(marginLeft+5)+'px');
    },
    function(){
      $(this).children('.product-detail-image').css('display','none');
    });
  });
  
  $('.btn-nex').attr('value','');
});

