﻿function initOfferte() {
  // Offerte speciali scroller
  $('#offerte .pager .tot')
    .text($('#offerte .elenco li').length);

  $('#offerte .elenco')
    .cycle({
      fx: 'scrollHorz',
      timeout: 0,
      next: '#offerte .pager .next',
      prev: '#offerte .pager .prev',
      pause: 1,
      after: function (cur, slide) {
        $('#offerte .pager .cur').html($(slide).index() + 1);
        cufon();
      }
    });
}

function init() {
  // Fade offerte
  $('.offerta')
    .hover(
      function () {
        $(this)
          .children('img')
          .stop()
          .animate({ opacity: 0 }, 400);
      },
      function () {
        $(this)
          .children('img')
          .stop()
          .animate({ opacity: 1 }, 400);
      });

  // Tabs Ajax
  $('.ajaxTabs a')
    .click(function () {
      var $this = $(this);

      $.ajax({
        url: $this.attr('href'),
        success: function (data) {
          var $parent = $this.parents('.section');
          var id = $parent.attr('id');

          $parent
            .html(data);

          $('#' + id + ' .foto ul li')
            .cycle({
              fx: 'fade',
              speed: 'slow',
              timeout: 0,
              prev: '#' + id + ' .prev',
              next: '#' + id + ' .next'
            });
        }
      });

      return false;
    });

  // Tabs Categorie Offerte
  $('.ajaxOfferte a')
    .click(function () {
      var $this = $(this);

      $.ajax({
        url: $this.attr('href'),
        success: function (data) {
          $this
            .parents('.section')
            .html(data);

          initOfferte();
        }
      });

      return false;
    });
}

$(function () {
  // Generico Loader
  $('#ajax_loader')
    .ajaxStart(function () {
      $(this)
        .stop()
        .css({
          opacity: 0,
          display: 'block'
        })
        .animate({
          opacity: 0.5
        },
        400);
    })
    .ajaxStop(function () {
      $(this)
        .stop()
        .animate({
          opacity: 0
        },
        800,
        function () {
          $(this).css({ display: 'none' });
        });

      init();
      cufon();
    });

  // Scroll
  $('a[href*=#]')
    .click(function () {
      if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
        var $target = $(this.hash);

        $target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']');

        if ($target.length) {
          var targetOffset = $target.offset().top;

          $('html,body')
            .animate({ scrollTop: targetOffset }, { duration: 1000, easing: 'easeOutQuint', queue: false });

          $('#flash')
            .hide();
          $('.play')
            .show();

          return false;
        }
      }
    });

  $('a.play')
    .click(function () {
      $('#flash')
        .show();
      $('.play')
        .fadeOut();

      return false;
    });

  $(document).pngFix();

  // Maps
  $('a.map').fancybox({
    onComplete: function () {
      var myLatlng = new google.maps.LatLng(44.178819, 12.42734);
      var myOptions = {
        zoom: 16,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.HYBRID
      }
      var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

      var marker = new google.maps.Marker({
        position: myLatlng,
        title: "Tiffany Hotel & Resort"
      });

      // To add the marker to the map, call setMap();
      marker.setMap(map);
    },
    onClosed: function () {
      $("map_canvas").html('');
    }
  });

  // Fotogallery
  $('a.photogallery')
    .fancybox();

  // Cambio lingua
  $("#nav_main .language > li ")
    .mouseover(function () {
      $('ul', $(this))
        .stop()
        .animate({ height: 90 }, { queue: false, duration: 600 });
    })
    .mouseout(function () {
      $('ul', $(this))
        .stop()
        .animate({ height: 0 }, { queue: false, duration: 600 });
    });

  // Form prenota
  var focus = false;

  $("#nav_main .prenota")
    .mouseover(function () {
      $('#prenota')
        .stop()
        .animate({ height: 250 }, { queue: false, duration: 600 });
    })
    .mouseout(function () {
      if (!focus)
        $('#prenota')
          .stop()
          .animate({ height: 0 }, { queue: false, duration: 600 });
    });

  $("#nav_main .prenota input")
      .focusin(function () {
        focus = true;
        $("#nav_main .prenota").mouseover();
      })
      .focusout(function () {
        focus = false;
        $("#nav_main .prenota").mouseout();
      });

  // Select Box
  $('.selectbox').selectbox();

  // Scroller foto pagina
  $('#camere .foto ul li')
    .cycle({
      fx: 'fade',
      speed: 'slow',
      timeout: 0,
      prev: '#camere .prev',
      next: '#camere .next'
    });

  $('#piscina .foto ul li')
    .cycle({
      fx: 'fade',
      speed: 'slow',
      timeout: 0,
      prev: '#piscina .prev',
      next: '#piscina .next'
    });

  $('#ristorante .foto ul li')
    .cycle({
      fx: 'fade',
      speed: 'slow',
      timeout: 0,
      prev: '#ristorante .prev',
      next: '#ristorante .next'
    });

  // Scroller foto pagina a tema
  $('.genre')
    .each(function (i) {
      var started = false;
      var $this = $(this);

      $this
        .hover(function () {
          if (!started) {
            started = true;
            $this
              .find('.auto ul li')
              .cycle({
                fx: 'fade',
                speed: 'slow',
                timeout: 4000
              });
          }
        });
    });

  $('.fotoOfferta ul')
    .cycle({
      fx: 'fade',
      speed: 'slow',
      timeout: 4000
    });



  // Scroller offerte pagina a tema
  $('.offerte')
    .each(function () {
      var $this = $(this);

      if ($('.scroller ul li', $this).length < 3)
        $('.prev, .next', $this).hide();
      else
        $('.prev, .next', $this).show();
    });

  $('.offerte .prev')
    .click(function () {
      var list = $('ul', $(this).parent());
      var $first = $('li:first', list);
      var $last = $('li:last', list);

      $last
      .css('marginLeft', -$first.width())
      .insertBefore($first);

      $last.animate({
        marginLeft: 0
      },
      400,
      function () {
        $(this).removeAttr('style');
      });
    });

  $('.offerte .next')
    .click(function () {
      var list = $('ul', $(this).parent());
      var $first = $('li:first', list);

      $first
        .animate({
          marginLeft: -$first.width()
        },
        400,
        function () {
          $(this).appendTo(list).removeAttr('style');
        });
    });

  // Tab ajax pagine genere
  $('.ajaxTabsGenere a')
    .click(function () {
      var $this = $(this);

      $.ajax({
        url: $this.attr('href'),
        success: function (data) {
          $('div', $this.parents('.dx'))
            .html(data);
        }
      });

      return false;
    });

  // Contattaci
  $('#stanze, #bambini, #adulti')
    .change(function () {
      var stanze = $('#stanze').val();
      var bambini = $('#bambini').val();
      var adulti = $('#adulti').val();

      $('#contattaci .stanza')
        .remove();

      var adulti_options = '';
      var bambini_options = '';

      for (i = 1; i <= adulti; i++)
        adulti_options += '<option value="' + i + '">' + i + '</option>'

      for (i = 0; i <= bambini; i++)
        bambini_options += '<option value="' + i + '">' + i + '</option>'

      for (i = 0; i < stanze; i++)
        $('<tr class="stanza"><td>' + lbl_camera + ' ' + (i + 1) + '</td>' +
          '<td class="mini">' + lbl_adulti + '</td>' +
          '<td><select name="stanza[' + i + '].adulti">' + adulti_options + '</select></td>' +
          '<td class="second">' + lbl_bambini + '</td>' +
          '<td><select name="stanza[' + i + '].bambini">' + bambini_options + '</select></td></tr>')
            .appendTo('#contattaci table');

      if (this.id == 'bambini') {
        $('#contattaci .eta')
          .remove();

        var bambini_eta = '';

        for (i = 0; i < bambini; i++) {
          bambini_eta += '<select name="eta[' + i + ']">';

          for (k = 0; k <= 12; k++)
            bambini_eta += '<option value="' + k + '">' + ((k < 1) ? '<1' : k) + '</option>';

          bambini_eta += '</select> ';
        }

        $('<tr class="eta"><td>' + lbl_eta + '</td>' +
          '<td colspan="4">' + bambini_eta + '</td></tr>')
          .insertAfter('#contattaci .stanza:last-child');
      }
    });


  $('#contattaci form')
    .validate({
      highlight: function (element, errorClass) {
        $(element).addClass('input-validation-error');
        $(element).parent().find('label').addClass('invalid');
      },
      success: function (errorLabel) {
        errorLabel.parent().find('input').removeClass('input-validation-error');
        errorLabel.parent().find('label').removeClass('invalid');
      }
    });

  init();
  initOfferte();
});

// FLASH HOME
var swfVersionStr = "9.0.124";
var xiSwfUrlStr = "";
var params = {};
params.quality = "high";
params.bgcolor = "#ffffff";
params.play = "true";
params.loop = "true";
params.wmode = "opaque";
params.scale = "noscale";
params.menu = "true";
params.devicefont = "false";
params.salign = "";
params.allowscriptaccess = "sameDomain";
var flashvars = {};
flashvars.pathFoto = "/Content/flash/images/";
flashvars.numFoto = 5;
flashvars.timeFoto = 5;
var attributes = {};
attributes.id = "flash";
attributes.name = "flash";
attributes.align = "middle";
swfobject.embedSWF("/Content/flash/tiffany.swf", "flashContent", "100%", "100%", swfVersionStr, xiSwfUrlStr, flashvars, params, attributes);

params.loop = "false";
params.wmode = "transparent";
params.scale = "none";
params.id ='slideshow';
params.name = 'slideshow';
params.movie = 'slideshow';
swfobject.embedSWF("/Content/flash/Photogallery.swf", "photogallery", 844, 562, swfVersionStr, xiSwfUrlStr, null, params, null);
