var starRating = {
    create: function(selector) {
        $(selector).each(function() {
            var $list = $('<div></div>');
            // loop over every radio button in each container
            $(this)
                .find('input:radio')
                .each(function(i) {
//                     var rating = $(this).parent().text().trim(); //proc tam je ten trim??
                    var rating = $(this).parent().text();
                    var $item = $('<a href="#"></a>')
//                         .attr('title', rating / 2)
                        .addClass(i % 2 == 1 ? 'rating-right' : '')
                        .attr('rel', rating);
                    if (rating != '')
                    {
                        starRating.addHandlers($item);
                    } else {
                        $item.click(function () { return false });
                    }
                    $list.append($item);
                    if ($(this).is(':checked')) {
                        $item.prevAll().andSelf().addClass('rating');
                    }
                });
            // Hide the original radio buttons
            $(this).append($list).find('input:radio').parent().hide();
//             $(this).append($list);
        });

    },
    addHandlers: function(item) {
        $(item).click(function(e) {
            // Handle Star click
            var $star = $(this);
            var $allLinks = $(this).parent();
            // Set the radio button value
            $allLinks
                .parent()
                .find('input:radio[value=' + $star.attr('rel') + ']')
                .attr('checked', 'checked');
            // Set the ratings
            $allLinks.children().removeClass('rating');
            $star.prevAll().andSelf().addClass('rating');
            // prevent default link click
            e.preventDefault();
        })
        .hover(function() {
            $(this).prevAll().andSelf().addClass('rating-over');
        },function() {
            $(this).siblings().andSelf().removeClass('rating-over');
        });
    },
    removeHandlers: function(item){
        $(item).find('*').unbind('click').unbind('mouseover');
    }
}

$(document).ready(function(){
    starRating.create('.stars');
    starRating.removeHandlers('.bezeventu');

    $('form#hodnoceni').submit(function(){
        var iAutor = $(':input[name=commentAuthor]');
        var iText = $(':input[name=commentText]');
        var iKapca = $(':input[name=kapca]');
        var success = true;
        var errorMessages = [];

        if( iText.val() == "" ){
            success = false;
            errorMessages.push(iText.attr('rel'));
        }

        if( iAutor.val() == "" ){
            success = false;
            errorMessages.push(iAutor.attr('rel'));
        }

        if( iKapca.val() != "t18342" && iKapca.val() != "T18342" ){
            success = false;
            errorMessages.push(iKapca.attr('rel'));
        }

        if( success == false ){
            alert( errorMessages.join("\n") );
            return false;
        }

    });

	$('#forgottenPassword.hide').hide();
	$('a.sendPassword').click(function() {
		$('#forgottenPassword').slideDown();
	});

});

function SkryjZobraz(id)
    {
       var prvek;
     
       if (prvek = document.getElementById(id)){
          
           if (prvek.style.display=="none"){ 	
    	     prvek.style.display="block";
    	     }
       else {     
          prvek.style.display="none";
          } 
      }   
	 }

// "javascript bez javascriptu" - otevirani odkazu v novem okne - v html pro odkaz pouzijeme class="popup"

function aktualizujOdkazy() {
  if(!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if(links[i].className.match("noveokno")) {
      links[i].onclick = function () {
        return !window.open(this.href);
      }
    }
  }
}
window.onload=aktualizujOdkazy;

// natahovaci sirka webu  
function resizeFunction(){
         
    var body = $("body").width();
    //var window = $(window).width();// je zde jen pro debug rozliseni 1024
//    alert (body);
    var hp_banner = $(".bannerProVituAVercu").width();
    obsahAuto = body - 235;
      
    if ($("body").width() > 1250) {
    
      $(".obsah").css("width", 1020); // 1250 - 220 (sirka menu)
      $("body").css("overflow-x", "hidden");
      
    }
    
    else if ($("body").width() < 994) {
    
      $(".obsah").css("width", 758); // 994 - 220 - 16 (rezerva) (sirka menu)
      $("body").css("width", 994);
      $("#head").css("width", 994);
      $("body").css("overflow-x", "auto");
    
    }
    
    else if ($("body").width() > 994 && $("body").width() < 1250){
    
      $(".obsah").css("width", obsahAuto);
    
    }
    
    var obsah = $(".obsah").width();
    productWidth = ((obsah / 2) - 30);
    
    $(".product").css("width", productWidth);
    
    // posouvani banneru na uvodni strane
    
    var divObsah = $(".obsah").width();
    var posunBanneru = (divObsah / 2) - ( hp_banner / 2);
    var posunBanneruPresne = Math.floor(posunBanneru)
    $(".bannerProVituAVercu").css("margin-left", posunBanneruPresne+"px");

    $(".bannerProVituAVercu, .bannerProVituAVercu *").mouseover(function () {
      $('#changing').cycle('pause');  
    });
    
    $(".bannerProVituAVercu, .bannerProVituAVercu *").mouseout(function () {
      $('#changing').cycle('resume');  
    });
    
    // uprava pri rozliseni mensim nez je 1024px
    
    if (body < 1100 && body > 994){
      $("div.similar, div.detail").addClass("wide");
    }
    //alert (body);
    
  
}

function setSizeSel()
{
  var $sizeSel = $("#size-select");
  var selected = $sizeSel.find("option:selected").first();
  
  if (!selected)
  {
    selected = $sizeSel.find("option").first();
  }
  
  $("#size-label").text( selected.text() );
  if ($("#size-table tbody tr:visible").size() > 0)
  {
    $("#size-table tbody tr:visible").fadeOut('fast', function() { $("." + selected.val()).fadeIn('fast'); } );
  } else {
    $("." + selected.val()).fadeIn('fast');
  }
}

function translateCategory( cat )
{
    switch(cat)
    {
        case 'product':
            return 'Produkty';
        case 'manufacturer':
            return 'Značky';
        case 'category':
            return 'Kategorie';
        case 'more':
            return 'Zobrazit další výsledky&hellip;';
    }
    
    return '';
}

$(document).ready(function(){
   
  $("#size-select").change( setSizeSel );
  setSizeSel();
  /** zaskrnuti prvni barvy pytle **/
	
	$("input[name='antaresbarva']:first").attr('checked', 'checked');
  
  // uvodni strana parfemu

      $("#parfemy-hp-nejprodavanejsi").hide();
      $("#parfemy-hp-vsechny").show();
       

    
    // resize detail banner - parfumes advertisement
    
    var sirkaBanneru = $(".detail_banner a img").width();
    if (sirkaBanneru > 971){
      $(".detail_banner a img").css("width", "971px");
    }
  
    resizeFunction();
    

/**
 *  Resize obrázků v rozcestníku kategorie - THE NEW CROSSWAY
 */


var images = $('#crossway img');


$.each(images, function(i, img){
    $(img).ready(function(){
        var width = $(img).width();
    	var height = $(img).height();
        if ( width > 130){
            $(img).css( 'width', "130px" );
        }
        else {

            if ( height > 130){
              $(img).css( 'height', "130px" );
            }
            else {
        	  $(img).css( 'height', "auto" );
            }

            $(img).css( 'width', "auto" );

        }
    });
});


/**
 *  Klikatelny banner s pomoci jedineho odkazu
 */ 
 
  var bannerExists = $('.bannerProVituAVercu').height();
  if(bannerExists > 0){
     
      $('.bannerProVituAVercu, .bannerProVituAVercu > *').click(function () {
        var url = $("a.bannerodkaz").attr("href");
        if (url) {
          window.location = url;
        }
      });
      
      
    /**
     *  Changing the background on the home page
     */
     
      $("#changing").css("display", "block");
      $("#changing").css("visibility", "visible");
       
      $("#changing").cycle({
          fx:    'turnUp',
          timeout: 6000
      });

  } else {
    $("#changing").hide();
  }
   
/**
 *  Klikatelny rozcestnik parf. a hodinky
 */ 

  var $rozcestnik = $('.multiple .box');
  if($rozcestnik.length){
     
    $rozcestnik.each(function() {
        var $box = $(this);
        $box.click(function () {
            var url = $box.find('h2').children('a').attr("href");
            if (url) {
                console.log(url);
              window.location = url;
            }
        });
    });
     
//        var url = $rozcestnik.find('h2').children('a').attr("href");
//        if (url) {
//            console.log(url);
//          window.location = url;
//        }
//        else {
//            console.log(url);
//        }
  }
   
  /**
   *  Odeslani formulare ONCHANGE
   */     
   
  $("#znackyForm, #sortByForm").change(function(){
    this.submit();
  });
  
  $("#rozmery tr").hover(function(){
      $(this).addClass("hover");
  }, function(){
    $(this).removeClass("hover");
  });

  var product2Width = $(".product2").width();
  if (product2Width)
  {
    $(".product-img").css("width", product2Width+"px");
  }

  /**
   *  POCET PRODUKTU NA RADEK - PRODUKT2
   */
   
   var obsah = $(".obsah").width();
   
  if (obsah <= 1020 && obsah >= 940){
      var product2 = ((obsah/4)-10);
//      alert (product2);
      $(".product2").css("width", product2+"px");
      $(".product2").css("margin-right", "5px");
  }
  if (obsah < 940 && obsah >= 758){
      var product2 = ((obsah/3)-10);
//      alert (product2);
      $(".product2").css("width", product2+"px");
      $(".product2").css("margin-right", "5px");
  } 

});


$(window).resize(function(){

    resizeFunction();
  
});
 
 
  





