$(document).ready(function() {
  
  // homepage_banner scroll
  if($('#homepage_banner .banner').size() > 1) {
    $('#homepage_banner').cycle({
      fx:       'scrollVert',
      speed:    800,
      timeout:  16000,
      pause:    1,
      pager:  '#nav',
      startingSlide: Math.floor(Math.random()*3)
    });
  }

  //banner scroll
  if($('.right_banner .part, .content_banner .part').size() > 1) {
    $('.right_banner .part, .content_banner .part').hide();
    $('.right_banner, .content_banner').cycle({
      fx:       'scrollVert',
      speed:    800,
      timeout:  8000,
      pause:    1,
      startingSlide: 0
    });
  }

  $('#homepage_banner .banner, .right_banner, .content_banner')
    .hover(
      function(){
        sel = $(this).find('.title a');
        if(sel && sel.length) {
          $(this).addClass('hover');
        }
      },
      function(){
        sel = $(this).find('.title a');
        if(sel && sel.length) {
          $(this).removeClass('hover');
        }
      }
    );

  $('#homepage_banner .banner')
    .click(function() {
      sel = $(this).find('.title a');
      if(sel && sel.length) {
        link = $(this).find('.title a').attr('href');
        pageTracker._trackEvent('BANNER', 'homepage_top', 'url:' + link);
        window.location = link;
      }
    });

  $('.right_banner')
    .click(function() {
      sel = $(this).find('.title a');
      if(sel && sel.length) {
        link = $(this).find('.title a').attr('href');
        pageTracker._trackEvent('BANNER', 'right', 'url:' + link);
        window.location = link;
      }
    });

  $('.content_banner')
    .click(function() {
      sel = $(this).find('.title a');
      if(sel && sel.length) {
        link = $(this).find('.title a').attr('href');
        pageTracker._trackEvent('BANNER', 'content', 'url:' + link);
        window.location = link;
      }
    });
  
  // main menu
  $('#main_navigation li')
    .hover(
      function(){
        $(this).addClass('hover');
      },
      function(){
        $(this).removeClass('hover');
      })
    .click(
      function() {
        window.location = $(this).find('a').attr('href');
      });

  $('#top_tours .top_countries .country')
    .hover(
      function(){
        $(this).addClass('hover');
      },
      function(){
        $(this).removeClass('hover');
      })
    .click(
      function() {
        link = $(this).find('h3 a').attr('href');
        pageTracker._trackEvent('TOP_TOURS', 'homepage_countries', 'url:' + link);
        window.location = link;
    });

    $('#top_tours .top_resorts .resort')
    .click(
      function() {
        link = $(this).find('a').attr('href');
        pageTracker._trackEvent('TOP_TOURS', 'homepage_resorts', 'url:' + link);
//        return false;
    });

  // tours_countries
  $('#tours_navigation .countries .country')
  .hover(
    function(){
      $(this).addClass('hover');
    },
    function(){
      $(this).removeClass('hover');
    }
    )
  .click(
    function() {
      window.location = $(this).find('h2 a').attr('href');
    });
    
  $('#country_navigation .countries .bar')
  .hover(
    function(){
      $(this).addClass('hover');
    },
    function(){
      $(this).removeClass('hover');
    })
  .click(
    function() {
      window.location = $(this).find('h3 a').attr('href');
    });


  $('.resorts_list .resort')
  .hover(
    function(){
      $(this).addClass('hover');
    },
    function(){
      $(this).removeClass('hover');
    })
  .click(
    function() {
      link = $(this).find('h3 a').attr('href');
//      alert('/TILE_LIST/resorts/url:' + link);
      pageTracker._trackEvent('TOURS_LIST', 'resorts', 'url:' + link);
      window.location = link;
    });


  $('.widget.top_resorts .resort a').click(function(){
    link = $(this).attr('href');
    pageTracker._trackEvent('TOP_TOURS', 'top_resorts', 'url:' + link);
//    return false;
  });


    
  // FORMULARE

  //reset popisku
  var request_form_field_set = new Array();
  
  $('.request_form .text').click(function() {
    if(!request_form_field_set[$(this).attr('id')] && $('.request_form div.notice').size() == 0) {
      request_form_field_set[$(this).attr('id')] = true;
      $(this).val('');
    }
  });

// datepicker
  $('.reservation_form .cal, .request_form .cal').datePicker({
    startDate: '01/01/2009',
    endDate: '01/01/2011'
  });

  // omezeni data navratu podle odjezdu
  $('.request_form #requestForm_date_from').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('.request_form #requestForm_date_to').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);

  $('.reservation_form #reservationForm_date_from').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('.reservation_form #reservationForm_date_to').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);

  $('.reservation_form #infoForm_date_from').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('.reservation_form #infoForm_date_to').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);


  $('.wizard_head_next').click(function(){
      $('.wizard_head').height(28);
      $('#question_1').show();
  });


  // GALERIE
  // gallery - skimap
  $("a[rel='skimap']").colorbox({maxWidth: '90%', maxHeight: '90%', resize: true});
  
  $("a[rel='skimap']").bind('cbox_open', function(){
    $('#cboxClose').before('<div id="cboxDownload" style="display: block;"></div>');
  });
  $("a[rel='skimap']").bind('cbox_complete', function(){
    $('#cboxDownload').html('<a href="' + $('#cboxPhoto').attr('src') + '" target="_blank">na celou obrazovku</a>');
  });
  $("a[rel='skimap']").bind('cbox_closed', function(){
    $('#cboxDownload').remove();
  });

  // gallery - resort/facility/unit gallery
  $("a[rel^='gallery']").colorbox({maxWidth: '90%', maxHeight: '90%', resize: true, current: "{current} z {total}"});

  // gallery - resort gallery
  $("a.planek").colorbox({maxWidth: '90%', maxHeight: '90%', resize: true, current: "{current} z {total}"});

  // gallery - article & infopage gallery
  $("#left a:has(img)").attr('rel', 'gallery');
  $("#left a[rel='gallery']").colorbox({maxWidth: '90%', maxHeight: '90%', resize: true, current: "{current} z {total}"});

  // virtual tours
  $(".virtual_tour").colorbox({width: '945px', height: '570px', iframe: true});



  // facility tabs
  $('.facilities').tabs();
//  $('.facilities ul li').click(function () {location.hash = $(this).children('a').attr('href');});
//  $('.facilities ul li a').click(function () {location.hash = $(this).attr('href');});


  // tables
  $('tr').hover(
    function() {
      $(this).addClass('hover');
    }, function() {
      $(this).removeClass('hover');
    }
  );

  // tooltip
/*  $('.widget .info div').hover(
			function(){$(this).find('div.tooltip:hidden').fadeIn(500);},
			function(){$(this).find('div.tooltip:visible').fadeOut(500);}
		);*/
    $('div.hastip em').hover(
      function(){$(this).css('text-decoration','none');$(this).css('color','#595959')},
      function(){$(this).css('text-decoration','underline');$(this).css('color','#FFA200')}
    );
    $('div.hastip em').click(function(){
      var div = $(this).parent();
      $(div).find('div.tooltip:hidden').fadeIn(500);
    });

    $('div.tooltip div.close').click(function(){
      var tooltip = $(this).parent();
      $(tooltip).fadeOut(500);
    });

});

function wizard_next(hide, show){
  $(hide).removeClass('active');
  $(hide+' .question_info').hide();
  $(hide+' .bt_next').hide();
  $(hide+' .bt_show').show();
  $(hide).css('border-bottom','none');
  $(show).addClass('active');
  $(show+' .bt_next').show();
  $(show).show();
}

function wizard_show(show){
  if($(show+' .question_info').css('display') == 'none'){
    $(show+' .question_info').slideDown();
    $(show+' .bt_show').addClass('bt_hide');
  }
  else {
    $(show+' .question_info').slideUp();
    $(show+' .bt_show').removeClass('bt_hide');
  }
}