$(window).load(function() {

  $("a.fancybox").fancybox({
    'zoomSpeedIn': 300,
    'zoomSpeedOut': 300,
    'overlayShow' :true
  });
  
  $("a.ifancybox").fancybox({
    'zoomSpeedIn': 300,
    'zoomSpeedOut': 300,
    'overlayShow' :true,
    'type' : 'iframe'
  });

  $('#slider-in').coinslider({ 
    width: 941, // width of slider panel
    height: 317, // height of slider panel
    spw: 7, // squares per width
    sph: 5, // squares per height
    delay: 3000, // delay between images in ms
    sDelay: 30, // delay beetwen squares in ms
    opacity: 0.7, // opacity of title and navigation
    titleSpeed: 500, // speed of title appereance in ms
    navigation: false, // prev next and buttons
    links: false, // show images as links
    hoverPause: false // pause on hover
  });

  $('#slider-desc').cycle({});
  
  var useCufon = true;
  if($.browser.msie) {
    if(parseInt($.browser.version.substr(0,1)) < 7) {
      useCufon = false;
    }
  }
	
  if(useCufon) {
    // Cufon replace
    Cufon.now(); 
    Cufon.replace("#head #mainMenu li, #head #submenu li, #foot .header .h, .boxPhotogallery .photo-menu li a", { fontFamily: "NudistaMedium", hover: true });
    Cufon.replace("#foot .content .title, #content .boxHpMenu .menu ul li, #content .boxHpNews .item .title, #content .boxHpNews .item .date, #content .boxText .in .big, #content .boxHpMenu .in .reservation p, #content .team h2, #content .boxText h1, #content .statice h1, #content .statice h2,", { fontFamily: "NudistaMedium" });
    Cufon.replace("#content .cs-title, #content .box .h, #content .text h3, #content .text h4, #content .text h5, #content .statice h3, #content .statice h4, #content .statice h5,", { fontFamily: "NudistaMedium", textShadow: "#000 1px 1px 1px" });    
    Cufon.replace("#head #submenu .title, #content .boxHpMenu .h, #foot .foot .fb", { fontFamily: "NudistaLight" });
  }
  
  // mailto funkce
  $("a[href='mailto:']").each(function() { $(this).attr("href", "mailto:"+ $(this).text()); });
  
  // external links
  $("a.external").attr("target", "_blank");
  
  // image mask
  $("a img").each(function() {
    $(this).after('<span></span>'); 
  });
      
  // jquery masonry
  $("#content .boxPhotogallery .items").masonry({
    columnWidth: 10,
    itemSelector: 'li.masonry',
    animate: true,
    animationOptions: {
      duration: 300,
      easing: 'linear',
      queue: false
    }
  });
  
  // hp news
  var width = 0;
  $(".boxHpNews .in .items .item").each(function() { width = width + $(this).outerWidth(); });
  $(".boxHpNews .in .items .scroll").width(width);
  $(".boxHpNews").serialScroll({
		target: ".items",
		items: ".item",
		prev: ".paging .prev a",
		next: ".paging .next a",
		axis: "x",
		duration: 700,
		force: true,		
		cycle: false,
		step: 1,
		onBefore: function(e, elem, $pane, $items, pos) {
		  var box = $(".boxHpNews");
		  var count = $(box).find(".items .item").length;
		  if(pos >= count-3) {
		    $(box).find(".paging .next").fadeOut();
      } else {
        $(box).find(".paging .next").show();
      }
      
      if(pos <= 0) {
        $(box).find(".paging .prev").fadeOut();
      } else {
        $(box).find(".paging .prev").show();
      }
    }
	});
	
	// main menu
	$("#mainMenu ul li.submenu a").click(function() {
    setSubmenu($(this), true);
    return false;
  });
  
  if ($("#mainMenu ul li.submenu.active").length) {
    setSubmenu($("#mainMenu ul li.submenu.active > a"));
  }
  
  //$("#head").hover(function() {}, function() { $("#submenu").slideToggle("normal", function() { $(this).addClass("hidden"); }); });
  
  // display course form
  $(".displayCourseForm a").click(function() {
    var form = $(".boxCourseForm");
    if($(form).hasClass("hidden")) {
      $(".boxCourseForm").slideToggle("normal", function() { 
        $(this).removeClass("hidden");
        var pos = $(form).position();
        $("html, body").animate({scrollTop: pos.top + 'px'}, 500); 
      });
    } else {
      var pos = $(form).position();
      $("html, body").animate({scrollTop: pos.top + 'px'}, 500);
    }
    return false;
  });
  
  $("#subscribeToNewsletterForm").submit(function(){
    $.post(
      BreezyCMS_URL + 'ajax/subscribe_newsletter/',
      $("#subscribeToNewsletterForm").serialize(),
      function (payload) {
        if (payload.state == 'ok') {
          $("#subscribe_newsletter_email").val("");
          $.fancybox(
            "<div class=\"success-message\"><p>" + payload.message + "</p></div>",
            {
              "width": 350
            }
          );
        }
        else {
          $("#subscribe_newsletter_message").text(payload.message);
        }
      },
      'json'
    );
    
    return false;
  });
  
});

function setSubmenu($menuItem, slide) {
  var submenu = $("#submenu ul");
  $(submenu).empty();
  $menuItem.parents("li").find("ul li a").each(function() {
    $(submenu).append('<li class="'+ $(this).parent().attr('class') +'"><a href="'+ $(this).attr('href') +'">'+ $(this).text() +'</a></li>');
  });
  $(submenu).find("li:last").addClass("last");
  $("#submenu .title").text($menuItem.text());
  
  if($("#submenu").hasClass("hidden")) {
    if (slide)
      $("#submenu").slideToggle("normal", function() { $(this).removeClass("hidden"); });
    else
      $("#submenu").removeClass("hidden");
  }
  
  Cufon.replace("#head #submenu .title");
  Cufon.replace("#head #submenu li a", { fontFamily: "NudistaMedium", hover: true });
}

// nastavi swip predanemu elementu
function setSwip ($element, message) {
  $element.focus(function(){
    swip($element, message, "");
  });
  
  $element.blur(function(){
    swip($element, "", message);
  });
}

    // Vymazani hodnoty value u inputu
function swip ($element, swipFrom, swipTo) {
  if ($element.val() == swipFrom) $element.val(swipTo);
}

