$('html').addClass('js');

$(function(){

  $('.form-t02 label').inFieldLabels({ fadeOpacity:0, fadeDuration:100 });
  
  
  
  
  //************************ Datepicker ***/
  $.datepicker.regional['hr'] = {
		closeText: 'Zatvori',
		prevText: '&#x3c;',
		nextText: '&#x3e;',
		currentText: 'Danas',
		monthNames: ['Siječanj','Veljača','Ožujak','Travanj','Svibanj','Lipani',
		'Srpanj','Kolovoz','Rujan','Listopad','Studeni','Prosinac'],
		monthNamesShort: ['Sij','Velj','Ožu','Tra','Svi','Lip',
		'Srp','Kol','Ruj','Lis','Stu','Pro'],
		dayNames: ['Nedjalja','Ponedjeljak','Utorak','Srijeda','Četvrtak','Petak','Subota'],
		dayNamesShort: ['Ned','Pon','Uto','Sri','Čet','Pet','Sub'],
		dayNamesMin: ['Ne','Po','Ut','Sr','Če','Pe','Su'],
		firstDay: 1,
		isRTL: false};
  
  $('.datepicker').datepicker($.extend({
    dateFormat: 'yy-mm-dd', 
    showOn: 'both', 
    buttonImage: phrasebook.URLROOT+'/images/btn-datepicker.png',
    buttonText: phrasebook.lbl_calendar,
    buttonImageOnly: true
  }, $.datepicker.regional[phrasebook.lang]));
  
  $('#form-reservations-steps .date').datepicker($.extend({
    dateFormat: 'dd.mm.yy', 
    showOn: 'both', 
    buttonImage: phrasebook.URLROOT+'/images/btn-datepicker.png',
    buttonText: phrasebook.lbl_calendar,
    buttonImageOnly: true,
    onSelect: function(dateText, inst){
        var day = new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay);
        day.setDate(day.getDate()+1);
        var nextInput = $(this).closest("div").next("div").find(".date");
        if(nextInput.val()==""){
            nextInput.val(addZero(day.getDate())+"."+addZero(day.getMonth()+1)+"."+day.getFullYear());
        }
    }
  }, $.datepicker.regional[phrasebook.lang]));
  
  
  function addZero(num){
      if(num<10){ num = "0"+num; }
      return num;
  }
  
});




//************************ Image gallery ***/
$(function(){

  var slide = $('#img-gallery li').outerWidth(true);
  var slideContainer = $('#img-gallery ul');
  
  $('#img-gallery ul').css({'width':slide*$('#img-gallery li').length});
  
  $('#img-gallery li:first-child').addClass('active first-visible');
  $('#img-gallery li:nth-child(6)').addClass('last-visible');
  $('#img-gallery li a').click(function(){
    $(this).parent().addClass('active').siblings().removeClass('active');
    updateHolder();
    return false;
  });
  
  $('#img-gallery').append('<div class="controls"><a class="prev">'+phrasebook.lbl_prev+'</a><a class="next">'+phrasebook.lbl_next+'</a></div>');
  $('#img-gallery .controls a').click(function(){ sliderControls( $(this).attr('class') ) });
  
  function updateHolder() {
    var imgLink = $('#img-gallery .active a');
    var imgBig = $('#content-head.img-holder img');
    $('#content-head.img-holder').addClass('loading');
    if ( imgBig.attr('src') !== imgLink ) {  
      imgBig.animate({ opacity: 0 }, function(){
        var img = new Image();
        $(img).load(function(){   
          $(this).css({opacity: 0});
          $('#content-head.img-holder').removeClass('loading').html(this);
          $(this).animate({opacity: 1});
        }).error(function(){}).attr({src: imgLink.attr('href'), alt: imgLink.children().attr('alt') });
      });
    }
  }
  function sliderControls(sliderControl){
    if (sliderControl === 'next') {
      $('#img-gallery .active').removeClass('active').next().addClass('active');
      if ( $('#img-gallery .active').prev().hasClass('last-visible') ) {
        $('#img-gallery .last-visible').removeClass('last-visible').next().addClass('last-visible');
        $('#img-gallery .first-visible').removeClass('first-visible').next().addClass('first-visible');
        slideContainer.animate({ marginLeft: -(slide-parseInt(slideContainer.css('margin-left'))) });
      } else if ( $('#img-gallery .active').prevAll().length === 0 ) {
        $('#img-gallery li:first-child').addClass('active first-visible').siblings().removeClass('active first-visible');
        $('#img-gallery li:nth-child(6)').addClass('last-visible').siblings().removeClass('last-visible');
        slideContainer.animate({ marginLeft: 0 });
      }
      updateHolder();
    } else if (sliderControl === 'prev') {
      $('#img-gallery .active').removeClass('active').prev().addClass('active');
      if ( $('#img-gallery .active').next().hasClass('first-visible') ) {
        $('#img-gallery .first-visible').removeClass('first-visible').prev().addClass('first-visible');
        $('#img-gallery .last-visible').removeClass('last-visible').prev().addClass('last-visible');
        slideContainer.animate({ marginLeft: parseInt(slideContainer.css('margin-left'))+slide });
      } else if ( $('#img-gallery .active').nextAll().length === 0 ) {
        $('#img-gallery li:last-child').addClass('active last-visible').siblings().removeClass('active last-visible');
        $('#img-gallery li:eq('+($('#img-gallery li').prevAll().length-(6-1))+')').addClass('first-visible').siblings().removeClass('first-visible');
        if ($('#img-gallery li').length >= 6){
          slideContainer.animate({ marginLeft: -(slide*($('#img-gallery li:last-child').prevAll().length-(6-1))) });
        }
      }
      updateHolder();
    }
  }
  
});




//************************ Slideshow 01 ***/
$(function(){
  var slide = $('#slideshow-t01 .inner');
  var slideWidth = slide.outerWidth(true);
  var slideContainer = $('#slideshow-t01 .holder');
  $('#slideshow-t01 .title em:first').addClass("active");
  slideContainer.css({'width':slideWidth*slide.length}); 
  $('#slideshow-t01 > ul li').click(function(){ slideshowControl($(this)); clearInterval(slideInterval); });    $('#slideshow-t01 .order_voucher').click(function(){ clearInterval(slideInterval); });    var slideInterval = setInterval(function(){ slideshowControl($('#slideshow-t01 > ul .active').next()) },4350);    function slideshowControl(slide) {    var slideIndex = slide.prevAll().length;    if ( slideIndex === 0 ) { slide = $('#slideshow-t01 > ul li:first'); }    $('#slideshow-t01 .title em:eq('+slideIndex+')').addClass('active').siblings('em').removeClass('active');    slide.addClass('active').siblings().removeClass('active');    slideContainer.animate({'margin-left':-(slideWidth*slideIndex)},580);  }
});




//************************ Slideshow 02 ***/
$(function(){

  $('#slideshow-t02 img').each(function(i){ $(this).attr('rel',''+parseInt(i+1)); });
  
  $('#slideshow-t02 .info').append('<div class="controls"><a class="pause">Play/pause</a><a class="prev">'+phrasebook.lbl_prev+'</a><a class="next">'+phrasebook.lbl_next+'</a></div>');
  
  $('#slideshow-t02 .prev').after('<span class="amount"><span/>/'+$('#slideshow-t02 img').length+'</span>');
  $('#slideshow-t02 .amount span').html($("#slideshow-t02 img.active").attr('rel'));
  
  if($("#slideshow-t02 img.active").attr("alt")!=""){ $("#slideshow-t02 .info em").text($("#slideshow-t02 img.active").attr("alt")); }
  
  var slideImgInterval = setInterval("slideImg('next')", 5000 );
  
  $("#slideshow-t02 .pause").toggle(function(){
      clearInterval(slideImgInterval);
      $(this).removeClass('pause').addClass("play");
  },function(){
      slideImgInterval = setInterval("slideImg('next')", 5000 );
      $(this).removeClass('play').addClass("pause");
  });
  
  $("#slideshow-t02 .prev, #slideshow-t02 .next").click(function(){
      if(!$("#slideshow-t02 .play").is(".pause")){
          $("#slideshow-t02 .play").click();
      }
      switch($(this).attr("class")){
          case "prev":
              slideImg("prev");
              break;
          case "next":
              slideImg("next");
              break;
      }
  });
  
});

function slideImg(direction){
  if($("#slideshow-t02 img").is(":animated")){ return false; }
  switch(direction){
      case "prev":
          if($("#slideshow-t02 img.active").prev().is("img")){
              $("#slideshow-t02 img.active").fadeOut(function(){
                  $(this).removeClass("active");
              });
              $("#slideshow-t02 img.active").prev("img").addClass("active").show();
              $('#slideshow-t02 .amount span').html($("#slideshow-t02 img.active").attr('rel'));
              if($("#slideshow-t02 img.active").prev("img").attr("alt")!=""){ $("#slideshow-t02 .info em").text($("#slideshow-t02 img.active").prev("img").attr("alt")); }
              else { $("#slideshow-t02 .info em").text( $("#slideshow-t02 .info em").attr("rel") ); }
          } else {
              $("#slideshow-t02 img:last").fadeIn(function(){
                  $("#slideshow-t02 img.active").removeClass("active").hide();
                  $(this).addClass("active");
                  $('#slideshow-t02 .amount span').html($(this).attr('rel'));
                  if($(this).attr("alt")!=""){ $("#slideshow-t02 .info em").text($(this).attr("alt")); }
                  else { $("#slideshow-t02 .info em").text( $("#slideshow-t02 .info em").attr("rel") ); }
              });
          }

          break;
      case "next":
          if($("#slideshow-t02 img.active").next().is("img")){
              $("#slideshow-t02 img.active").next("img").fadeIn(function(){
                  $("#slideshow-t02 img.active").removeClass("active").hide();
                  $(this).addClass("active");
                  $('#slideshow-t02 .amount span').html($(this).attr('rel'));
                  if($(this).attr("alt")!=""){ $("#slideshow-t02 .info em").text($(this).attr("alt")); }
                  else { $("#slideshow-t02 .info em").text( $("#slideshow-t02 .info em").attr("rel") ); }
              });
          } else {
              $("#slideshow-t02 img.active").fadeOut(function(){
                  $(this).removeClass("active");
              });
              $("#slideshow-t02 img:first").addClass("active").show();
              $('#slideshow-t02 .amount span').html($("#slideshow-t02 img:first").attr('rel'));
              if($("#slideshow-t02 img:first").attr("alt")!=""){ $("#slideshow-t02 .info em").text($("#slideshow-t02 img:first").attr("alt")); }
              else { $("#slideshow-t02 .info em").text( $("#slideshow-t02 .info em").attr("rel") ); }
          } 
          break;
  }
}


//************************ Voucher popup ***/
$(function(){
    $("#popup .close").live("click",function(){ $("#popup").remove(); });

    $(".order_voucher").click(function(){
        var offset = $(this).offset();
        $.get($(this).attr("rel"),function(data){
            $("#popup").remove();
            $("body").append('<div id="popup">'+data+'</div>');
            var left = offset.left + 65;
            var top = offset.top - $("#popup").height() +15;            
            $("#popup").css({left: left, top: top}).show();
            $("#popup form").submit(function(){
               submitForm($(this));
               return false;
            });
        });
    });
});

function submitForm(this_form){
    var data = this_form.serializeArray();                                      
    $.post(this_form.attr("action"), data, function(data){                       
         $("#popup").html(data);
         $("#popup form").submit(function(){
             submitForm($(this));
             return false;
         });
    },"html");
    
}
