$(document).ready(function() {

  function removeActiveClasses(element) {
    $(element).each(function() {
      var trida = $(this).attr("class").split(" ")[0];
      $(this).attr("class",trida);
    });
  }
  
  if($("#rotate").length) {
    $('#rotate').cycle({ 
      fx:     'fade',
      speed:   1000,
      timeout: 7000,
      next:   '#next',
      prev:   '#prev',
      pause:  1,
      before:  onBefore,
      after:   onAfter
    });
    $("#slidercontrols .total").text($("#rotate a").length);
  }
  $("#slidercontrols, #desc").hover(function() {
    $('#rotate').cycle('pause');
  }, function() {
    $('#rotate').cycle('resume');
  });
  function onBefore() {
    $("#rotate .desc").fadeOut(800);
    $("#slidercontrols #typakce").text($(this).find(".typakce").text());
    $("#slidercontrols .actual").text(parseInt($("#rotate a").index(this))+1);
  }
  function onAfter() { 
    $("#rotate .desc").fadeIn(1000);
  }

  if($("#mainmenu").length) {
    $("#mainmenu .submainmenu ul ul").addClass("subsubmainmenu").prev().css({"background-image":"url('img/arrow-submainmenu.png')","background-position":"180px 9px","background-repeat":"no-repeat"}).addClass("subcontroler");
    $("#mainmenu a[class^=m]").hover(function() {
      $("#mainmenu .submainmenu").removeClass("active");
      removeActiveClasses("#mainmenu a[class*=active]");
      var tridy = $(this).attr("class").split(" ");
      var trida = tridy[0];
      if($(this).next().attr("class")=="submainmenu") {
        $(this).addClass(trida+"active");
        var p = $(this).next();
        var position = $(this).offset();
        
        p.css({ "left": position.left ,"top": parseInt(position.top)+60 +"px" }).addClass("active").mouseenter(function() {
          $("#mainmenu a."+trida).addClass(trida+"active");
        }).mouseleave(function() {
          removeActiveClasses("#mainmenu a[class*=active]");
          $("#mainmenu .submainmenu").removeClass("active");
          $("#mainmenu .subsubmainmenu").hide();
        });
      }
    });
    $("#mainmenu .subsubmainmenu").prev().hover(function() {
      $("#mainmenu .subsubmainmenu").hide();
      $(this).next().show();
    });
    $("#mainmenu .subsubmainmenu").parent().mouseleave(function() {
      $("#mainmenu .subsubmainmenu").hide();
    });
    $("#mainmenu .subsubmainmenu").hover(function() {
      $(this).prev(".subcontroler").addClass("active");
    }, function() {
      $(this).prev(".subcontroler").removeClass("active");
    });
    $("#mainmenu .submainmenu a").not("#mainmenu a.subcontroler").not("#mainmenu .subsubmainmenu a").hover(function() {
      $(".subsubmainmenu").hide();
    });
    $("#mainmenu").mouseleave(function() {
      removeActiveClasses("#mainmenu a[class^=m]");
      $("#mainmenu .submainmenu").removeClass("active");
      $("#mainmenu .subsubmainmenu").hide();
    });
  }

  $("#tabcontrols a").click(function() {
    rel = $(this).attr("rel");
    $(this).parent().find("a").removeClass("active");
    $(this).addClass("active");
    $("#tabs div[id^=tab-]").hide();
    $("#tabs #"+rel).show();
  });
  
  $("#kreditky span").hover(function() {
    $(this).addClass($(this).attr("class")+"hover");
  }, function() {
    trida = $(this).attr("class").split(" ");
    $(this).removeClass(trida[1]);
  });
  
  function isThousands(position) {
    if (Math.floor(position/3)*3==position) return true;
    return false;
  };

  function formatMoney(theNumber) {
    var theThousands = " ";
    var theDecimal = ",";
    var theDecimalDigits = Math.round((theNumber*100)-(Math.floor(theNumber)*100));
    if(theDecimalDigits < 10) { theDecimalDigits = "0"+theDecimalDigits; };
    theDecimalDigits= ""+ (theDecimalDigits + "0").substring(0,2);
    theNumber = ""+Math.floor(theNumber);
    var theOutput = "";
    for(x=0; x<theNumber.length; x++) {
      theOutput += theNumber.substring(x,x+1);
      if (isThousands(theNumber.length-x-1) && (theNumber.length-x-1 !=0)) {
        theOutput += theThousands;
      };
    };
    if(theDecimalDigits!="00") { theOutput += theDecimal + theDecimalDigits; }
    return theOutput;
  };
  
  if($("#detail #aditional").length) {
    var zakladnicena = parseInt($("#detail #aditional .cena strong").text().replace(" ", "").replace(".", ""));
    $("#detail form #selectvalue").val($("#detail form option:selected").val());
    $("#detail form #selecttext").val($("#detail form option:selected").text());
    $("#detail form select").change(function() {
      $("#detail form #selectvalue").val($(this).val());
      $("#detail form #selecttext").val($(this).find("option:selected").text());
      $("#detail #aditional .cena strong").text(formatMoney(zakladnicena+parseInt($(this).val()))+" Kč");
    });
  }
  
  $(function() {
    $('a[rel=lightbox]').lightBox({
	imageLoading: 'img/lightbox/loading.gif',
	imageBtnClose: 'img/lightbox/close_grey.png',
	imageBtnPrev: 'img/lightbox/prev_grey.gif',
	imageBtnNext: 'img/lightbox/next_grey.gif',
	txtImage: 'Fotka',
	txtOf: 'z'
    });
  });
  
  $("#sound").jmp3({
    showfilename: "false",
    backcolor: "bb0000",
    forecolor: "ffffff",
    showdownload: "true",
    playerpath: "js/"
  });
  
});

$(window).load(function() {
  if($("#photoslider").length) {
    var cnt = $("#photoslider a").length;
    var arrval = 0;
    var refwidth = $("#photoref").width();
    var speed = 10;
    $("#photoslider a:last").css("margin-right","0");
    
    for(var i=0; i<cnt; i++) {
      blok = $("#photoslider a").eq(i);
      blok.find("img").attr("width", blok.width());
      arrval += (blok.width() + parseInt(blok.css("margin-right")));
    }
    $("#photoslider").width(arrval).css("margin-left","0");
    var arrvalcor = arrval-refwidth;
    
    if(arrval>=refwidth) {
      $("#photowrapper #left").hover(function(){
        if(parseInt($("#photoslider").css("margin-left"))<0) {
          $("#photoslider").animate({marginLeft: '0'}, arrvalcor-parseInt($("#photoslider").css("margin-left"))*speed, "linear");
        }
      },function(){ $("#photoslider").stop(); });

      $("#photowrapper #right").hover(function(){
        if(parseInt($("#photoslider").css("margin-left"))<arrvalcor) {
          $("#photoslider").animate({marginLeft: '-'+arrvalcor+'px'}, Math.abs((arrvalcor+parseInt($("#photoslider").css("margin-left")))*speed), "linear");
        }
      },function(){ $("#photoslider").stop(); });

    }
    $("#photoslider a:first").addClass("active").find("img").css("opacity","0.6");
    $("#photoslider a").each(function() {
      $(this).attr("rel2",$(this).attr("href"));
      $(this).attr("href","javascript:void(0)");
    });
    $("#photoslider a").hover(function() {
      if(!$(this).hasClass("active")) { $(this).find("img").css("opacity","0.6"); }
    }, function() {
      if(!$(this).hasClass("active")) { $(this).find("img").css("opacity","1"); }
    }).click(function() {
      $("#photoslider a").removeClass("active").find("img").css("opacity","1");
      $(this).addClass("active").find("img").css("opacity","0.6");
      $("#bigphoto").attr("href",$(this).attr("rel2"));
      $("#bigphoto img").attr("src",$(this).attr("rel"));
    });
  }
});

