/* 
	demo skript, toimib ainult kahe pildiga ja alusta otsast peale
	yks pilt html'st, teised lisatakse pärast lehe laadimist html'i
	kõik selleks, et ei jääks pageload toppama 
 */
(function($){
  var imgs = {
    1:"images/data/frontpage-splash2.jpg"
  }; // see n2iteks ajaxiga kuskilt

  var count = 0;

  if($('#gallery_id').val() != 0){

    $.ajax({
      type: 'GET',
      url: 'json.php?controller=gallery&action=getimages',
      dataType: 'json',
      success: function(j) {
        imgs = j.images;

        all_images_html = "";
        for ( key in imgs ) {
          all_images_html += '<img id="splash_' + key + '" height="280" width="900" src="'+imgs[key]+'" alt="" class="hidden" />';
          count++;
        }

        $(".splash img").after(all_images_html);

        setTimeout(animate, 5000);

      },
      data: {
        gallery: $('#gallery_id').val()
      },
      async: true
    });

  }

 

  function animate() {
    var current = $("#current_splash_id").val();
    var next = 0;

    next = parseInt(current) + 1;

    if(next > count)
      next = 0;

    $("#current_splash_id").val(next);

    $("#splash_" + current).css("z-index", 1).fadeOut(2000);
    $("#splash_" + next).css("z-index", 0).removeClass("hidden").fadeIn(2000);
    setTimeout(animate, 7000);
  }

})(jQuery);

function PadDigits(n, totalDigits) 
    { 
        n = n.toString(); 
        var pd = ''; 
        if (totalDigits > n.length) 
        { 
            for (i=0; i < (totalDigits-n.length); i++) 
            { 
                pd += '0'; 
            } 
        } 
        return pd + n.toString(); 
    } 

$(function() {
  //$(".datepicker01").datepicker('option', {dateFormat: $(this).val("dd. mm. yyyy")});
  $(".datepicker").css('opacity', 0);
  $(".datepicker01").datepicker({
    dateformat: "dd.mm.yyyy",
    onSelect: function(dateText, inst) {
      if(inst.input.attr('name') == 'arrivaldate') {
        suffix = 1;
      } else if(inst.input.attr('name') == 'leavingdate') {
        suffix = 2;
      } else if(inst.input.attr('name') == 'seminararrivaldate') {
        suffix = 3;
      } else if(inst.input.attr('name') == 'seminarleavingdate') {
        suffix = 4;
      }
      
      $('select[name=day' + suffix +']').val(inst.currentDay)
      $('select[name=month' + suffix +']').val(PadDigits(inst.currentMonth + 1, 2))
      $('select[name=year' + suffix +']').val(inst.currentYear)
    }

  });
});

/*
Service menu
*/
(function($){
  $(".servicenav li.L1").mouseenter(function() {
    if (typeof this.orig_height === "undefined" ) {
      this.orig_height = $(".L2-popup", this).height();
    }
    $(".L2-popup", this).css("height", 1).removeClass("hidden").animate( {
      height: this.orig_height
    }, "fast" );
  }).mouseleave(function() {
    $(".L2-popup", this).css("height", 1).removeClass("hidden").animate( {
      height: 0
    }, "fast", function() {
      $(this).clearQueue();
    });
  });
})(jQuery);

/*
Broneeringuvormi lahi/kinni
*/
(function($){

	
  if ( $(".bronform").length ) {
    $(".bronform .sideelement").each(function() {
      if ($(this).parent().hasClass("fieldset-active")) {
        $(".sideelement ", $(this).parent()).css("height", $(this).parent().height()-20+"px");
      } else {
        $(".sideelement ", $(this).parent()).css("height", "30px");
      }
    });

    $(".fieldset .label").click(function() {
      disableSelection(this);
      $(this).parent().toggleClass("fieldset-active");
      $(".arrow", $(this).parent()).toggleClass("hidden");
      $(".content", $(this).parent()).toggleClass("hidden");
      if ($(this).parent().hasClass("fieldset-active")) {
        $(".sideelement", $(this).parent()).css("height", $(this).parent().height()-20+"px");
        if ( $.browser.msie && $.browser.version.substr(0,1)<7 ) {
          $(".bottom-corner", $(this).parent()).css("bottom", "-1px");
        }
      } else {
        $(".sideelement", $(this).parent()).css("height", "30px");
      }
			
    });
  }

})(jQuery);

function disableSelection(target){
  if (typeof target.onselectstart!="undefined") //IE route
    target.onselectstart=function(){
      return false
    }
  else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
    target.style.MozUserSelect="none"
  else //All other route (ie: Opera)
    target.onmousedown=function(){
      return false
    }
  target.style.cursor = "default"
}

function cbox_sharewithfriend() { 
  $.fn.colorbox({
    inline:true,
    href:".cbox_sharewithfriend",
    overlayClose:false,
    close:"",
    opacity:0.7,
    transition:"elastic",
    innerWidth: "260px"
  });
} 

$(document).ready(function() { 
  //cbox_sharewithfriend()
  var rotate = 1;

  function showNextOffer(){
    var current = parseInt($('#activeoffer').val());
    var next = current + 1;

    if($('#offer_' + next).length != 0){
      $('#offer_' + next).show();
      $('#offer_' + current).hide();
      $('#activeoffer').val(next);
    } else {
      $('#offer_0').show();
      $('#offer_' + current).hide();
      $('#activeoffer').val(0);
    }

    if(rotate == 1)
      t = setTimeout(showNextOffer, 4000);

  }

  var t = setTimeout(showNextOffer, 4000);

  $('a[id=offer_next]').click(function() {

    rotate = 0;
    clearTimeout(t);
    showNextOffer();

  });

  $('a[id=offer_previous]').click(function() {

    clearTimeout(t);
    var current = parseInt($('#activeoffer').val());
    var last = parseInt($('#lastoffer').val());
    var prev = current - 1;

    if(current != 0){
      $('#offer_' + prev).show();
      $('#offer_' + current).hide();
      $('#activeoffer').val(prev);
    } else {
      $('#offer_' + last).show();
      $('#offer_' + current).hide();
      $('#activeoffer').val(last);
    }

  });
  
  /*var def_rotate = 1;
  
  function def_showNextOffer(){
    var current = parseInt($('#def_activeoffer').val());
    var next = current + 1;
  
    if($('#def_offer_' + next).length != 0){
      $('#def_offer_' + next).show();
      $('#def_offer_' + current).hide();
      $('#def_activeoffer').val(next);
    } else {
      $('#def_offer_1').show();
      $('#def_offer_' + current).hide();
      $('#def_activeoffer').val(1);
    }

    if(def_rotate == 1)
      t = setTimeout(def_showNextOffer, 4000);

  }

  var def_t = setTimeout(def_showNextOffer, 4000);

  $('a[id=def_offer_next]').click(function() {

    def_rotate = 0;
    clearTimeout(def_t);
    def_showNextOffer();

  });

  $('a[id=def_offer_previous]').click(function() {

    clearTimeout(t);
    var current = parseInt($('#def_activeoffer').val());
    var last = parseInt($('#def_lastoffer').val());
    var prev = current - 1;

    if(current > 1){
      $('#def_offer_' + prev).show();
      $('#def_offer_' + current).hide();
      $('#def_activeoffer').val(prev);
    } else {
      $('#def_offer_' + last).show();
      $('#def_offer_' + current).hide();
      $('#def_activeoffer').val(last);
    }

  });*/
});

$('#sendtofriend').click(function() {

  var receiverEmail = $('#receiver_email').val();
  var senderEmail = $('#sender_email').val();
  var message = $('#message').val();
  var sendcontent = $('#sendcontent').val();

  if(receiverEmail == ''|| senderEmail == '' || message == '') {
    $('#error').show();
    $("#cboxLoadedContent").css("overflow", "hidden");
    $.fn.colorbox.resize();

    return false;
  } else
    $('#error').hide();

  $.getJSON('json.php?controller=sendtofriend&action=send',
  {
    receiver_email: receiverEmail,
    sender_email: senderEmail,
    //sendcontent: sendcontent,
    message: message
  }, function(j) {
    $.fn.colorbox.close();
  });
  return false;
});

