$(document).ready(function(){

    /* Sort Main Menu */
    $('header nav ul li').each(function(index) {
      $('a', this).html('<span><span>'+$('a', this).html()+'</span></span>');

      if ($(this).hasClass('active')) {
        $('header nav ul li:nth-child('+ (index) +')').addClass('activeLeft');
        $('header nav ul li:nth-child('+ (index+2) +')').addClass('activeRight');
      }
    });
    if (navigator.userAgent.match(/iPhone/i) !== null) {
      if ($('header nav ul li.last a span span').text() == 'Contact Us') {
        $('header nav ul li.last a span span').text('Contact');
      }
    }

    /* Casino Games Tips and Advice Tabs */
    $('.tipsAdvice .tipsTabs a').click(function() {
      if (!$(this).hasClass('tipsTabs_active')) {
        $('.tipsTabs a').removeClass('tipsTabs_active');
        $(this).addClass('tipsTabs_active');

        $('.tipsAdvice .tipsContent div').hide();
        $('.tipsAdvice .tipsContent div.'+($(this).attr('href')).replace('#', '')).show();
      }
      return false;
    });

    /* Trigger clicks to link under masls */
    $('.maskHome,.itemImg .newOffer').click(function(){
      window.location = $(this).prev('a').attr('href');
    });

    /* 360 YouTube Video Embeds */
    function loadVideo(playerUrl, autoplay) {
      swfobject.embedSWF(playerUrl + '&amp;rel=0&amp;border=0&amp;fs=1&amp;autoplay='+autoplay, 'youtubeplayer', '620', '320', '9.0.0', false, false, {allowfullscreen: 'true'});
    }

    /* Gallery Lightbox */
    $('#gallery a').colorbox({rel:'gallery', opacity:0.65});
    $('#viewgallery').click(function() {
      $('#gallery a:first').trigger('click');
      return false;
    });
    
    /* Newsletter Lightbox */
    $('#newslettersignup p a').colorbox({iframe:true, width:'880px', height:'700px', height:'90%', opacity:0.65});
    $('a.greenearthpopup').colorbox({iframe:true, width:'880px', height:'700px', height:'90%', opacity:0.65});
    $('.virtualtour a').colorbox({iframe:true, width:'880px', height:'700px', height:'90%', opacity:0.65});
    
    /* Match Heights of .listItem2s Items */
    var tallestListItem2s = 0;
     $('.listItem2s').each(function(n) {
      if ($(this).height() > tallestListItem2s) {
        tallestListItem2s = $(this).height();
      }
     });
     $('.listItem2s').css('height', tallestListItem2s+'px');
     
     if ($('.detailsCol1 .liveBookings').height() < $('.detailsCol2 .restaurantBarMenus').height()) {
        $('.detailsCol1 .liveBookings').css('height', $('.detailsCol2 .restaurantBarMenus').height()+'px');
     } else if ($('.detailsCol1 .liveBookings').height() > $('.detailsCol2 .restaurantBarMenus').height()) {
        $('.detailsCol2 .restaurantBarMenus').css('height', $('.detailsCol1 .liveBookings').height()+'px');
     }

    /* Home Slides */
    var initialSlide = true;
    var slidesShown = 1;
    var slidesCount = $(".homeSlidesImg div").length;
    var slidesStop = false;
    if ($(".homeSlidesImg div").length > 0){
      $('.homeSlidesImg div').cycle({
        prev: '.arrow-left',
        next: '.arrow-right',
        rev: 1,
        fx: 'scrollVert',
        speedIn:  1500, 
        speedOut: 1500, 
        easeIn: 'easeInOutBack',
        easeOut: 'easeInOutBack',
        cleartypeNoBg: true,
        timeout: 4000,
        pause: 1,
        before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
          if (initialSlide) {
            initialSlide = false;
          } else {
            // Use Ajax to load the images just before they're needed
            var nextImg =  $('.homeSlidesImg div div:nth-child('+ (options.nextSlide+1) +') img:first');
            if (nextImg.attr('rel')) {
              nextImg.attr('src', nextImg.attr('rel'));
              nextImg.attr('rel', '');
            }
            
            // Check stop looping etc
            if (forwardFlag) {
              slidesShown = slidesShown + 1;
            }
            if (options.nextSlide == 0 && slidesShown == slidesCount) {
              slidesStop = true;
              $('.homeSlidesImg div').cycle('pause');
            }
            
            // Custom slides for the text
            if (options.nextSlide == 1 && forwardFlag) {
              if ($('.slideTxt2').css('left') == '0px') {
                $('.slideTxt3,.slideTxt4,.slideTxt5,.slideTxt6,.slideTxt7').stop(true, true).fadeOut(800, function() {
                  $('.slideTxt3,.slideTxt4,.slideTxt5,.slideTxt6,.slideTxt7').css('left', '-500px').fadeIn(0);
                });
              } else {
                $('.slideTxt1').stop(true, true).fadeOut(800, function() {
                  $('.slideTxt2').css('left', '0px').stop(true, true).fadeIn(800);
                });
              }
            } else if (options.nextSlide == 6 && !forwardFlag) {
              if ($('.slideTxt7').css('left') == '0px') {
                $('.slideTxt2,.slideTxt3,.slideTxt4,.slideTxt5,.slideTxt6').stop(true, true).fadeOut(800, function() {
                  $('.slideTxt2,.slideTxt3,.slideTxt4,.slideTxt5,.slideTxt6').css('left', '-500px').fadeIn(0);
                });
              } else {
                $('.slideTxt1').stop(true, true).fadeOut(800, function() {
                  $('.slideTxt7').css('left', '0px').stop(true, true).fadeIn(800);
                });
              }
            } else if (options.nextSlide == 0) {
              if (!slidesStop) {
                $('.slideTxt2,.slideTxt3,.slideTxt4,.slideTxt5,.slideTxt6,.slideTxt7').stop(true, true).fadeOut(800, function() {
                  $('.slideTxt1').fadeIn(800);
                }).css('left', '-500px').stop(true, true).fadeIn(0);
              } else {
                $('.slideTxt2,.slideTxt3,.slideTxt4,.slideTxt5,.slideTxt6,.slideTxt7').css('left', '0px');
              }
            } else {
              if (forwardFlag) {
                $('.slideTxt'+(options.nextSlide+1)).stop(true, true).animate({left: '0px'}, 800);
              } else {
                if ($('.slideTxt2').css('left') == '0px') {
                  $('.slideTxt'+(options.nextSlide+2)).stop(true, true).animate({left: '-500px'}, 800);
                } else {
                  $('.slideTxt'+(options.nextSlide+1)).stop(true, true).animate({left: '0px'}, 800);
                }
              }
            }
          }
        },        
        after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
          /*if (forwardFlag) {
            slidesShown = slidesShown + 1;
          }
          if (options.nextSlide == 1 && slidesShown == slidesCount) {
            $('.homeSlidesImg div').cycle('pause');
          }*/
        }
      });
    }
    
  /* Event Promo Banners */  
  if ($(".eventPromos div.cyclediv").length > 0) {
    $('.eventPromos').after('<div id="eventpromos-nav" class="clearfix"></div>');
      $('.eventPromos div.cyclediv').cycle({
        prev: '.arrow-left',
        next: '.arrow-right',
        fx: 'scrollHorz',
        speedIn:  800, 
        speedOut: 800,
        cleartypeNoBg: true,
        timeout: 8000,
        pause: 1,
        fit: true,
        width: 987,
        pager: '#eventpromos-nav'
      });
  }

  /* Collapsable SEO Text */
  $('.readMore a').click(function() {
    $('.moreClubInfo div').slideToggle();

    if (!$('.readMore a').hasClass('opentext')) {
      $('html,body').animate({scrollTop: $(this).offset().top}, 400);
    }

    $('.readMore a').toggleClass('opentext');
  });

  /* Newsletter placeholders */
  $('#newslettersignup_name').attr('placeholder', 'Full Name');
  $('#newslettersignup_email').attr('placeholder', 'Your Email');
  $('#newslettersignup_name,#newslettersignup_email').placeholder();

  /* Event Calendar Rollovers */
  $('.calendarDayMoreEvents').mouseenter(function(){
    var extraHeight = $('.calendarDayMore', this).height();
    $(this).css('box-shadow', '0px 0px 10px #000');
    $(this).css('z-index', '5');
    $('.calendarLinkMore', this).fadeOut();
    $('.calendarDayMore', this).slideDown(200);
    $(this).stop().animate({
      height: (145 + extraHeight)+'px',
      paddingLeft: '17px',
      paddingRight: '17px',
      marginTop: '-'+ (extraHeight/2) +'px',
      marginLeft: '-10px'
    }, 200);
  });
  $('.calendarDayMoreEvents').mouseleave(function(){
    $('.calendarDayMore', this).slideUp(200);
    $(this).css('z-index', '4');
    $(this).stop().animate({
      height: '145px',
      paddingLeft: '7px',
      paddingRight: '7px',
      marginTop: '0px',
      marginLeft: '0px'
    }, 200, function () {
      $(this).css('box-shadow', '');
      $('.calendarLinkMore', this).fadeIn();
    });
  });

  /* Send to a friend click triggers */
  jQuery(".sendtoafriend").click(function(){
    jQuery('.st_email_large .stButton').trigger('click');
    return false;
  });
  
  // Ajax newsletter signup form
  $("#newslettersignup form").submit(function() {
    _gaq.push(['_trackEvent', 'Newsletter', 'Signup', 'Sitewide']);
    $("#newslettersignup img").show();

    var dataStr = $("#newslettersignup form").serialize();
    $.ajax({
      type: "POST",
      url: vrootdir + "/includes/newsletter.php",
      data: dataStr,
      success: function(response){
        $('#newslettersignup img').hide();

        if (response == 'success') {
          $('#newslettersignup form').hide();
          $('#newslettersignup h3').html('Thank You');
          $('#newslettersignupSuccess').fadeIn();
        } else {
          alert(response);
        }
      }
    });
    
    return false;
  });
  
  // FAQ Category Anchors
  $('.faqCategories 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}, 1000);
        return false;
      }
    }
  });
	
});
