var _promo_timeout = null;

jQuery(function($){
  
  $('#homeFader').innerfade({ speed: 2000, timeout: 5000, type: 'sequence', containerheight: '466px' });
	$('#fader').innerfade({ speed: 2000, timeout: 4000, type: 'sequence', containerheight: '526px' });
	$('#fader1').innerfade({ speed: 2000, timeout: 8000, type: 'sequence', containerheight: '529px' });
	
	$('#textFaderWrap').innerfade({ speed: 2000, timeout: 9000, type: 'sequence', containerheight: '116px' });
  
  // set up the ad rotator if this page has one
  if($('#promo').html())
  {
    _promo_timeout = setTimeout($.initPromo,5000);
	$('#PromoText a[href*="popup=true"]').live('click', function(){
		winPop(this, 550, 400);
		return false;
	});
  }
      
  // add validation to job application form
  if(typeof jQuery().validate != 'undefined')
  {
	  $("#frmSpaBooking.jobapplication").validate({
			rules: {
				Guestname: "required",
				ContactPhone: "required",
				Email: {
					required: true,
					email: true
				},
				Position: "required"
			}  
	  });
	  
	  $('#frmSubscribe.reserve').validate({
			rules: {
		    FirstName: "required",
		    Surname: "required",
		    CheckIn: {
		  		required: true,
		  		dateEN: true
	  		},
		    CheckOut:  {
		  		required: true,
		  		dateEN: true
	  		},
		    Phone: "required",
		    Adults: "digits",
		    Children: "digits",
		    ChildrenAges: {
	  			required: function(){ return parseInt($('#frmSubscribe.reserve #Children').val()) > 0; }
	  		},
			Email: {
				required: true,
				email: true
			}
		} 
	  });	  
  }

  
  /* handle the multi-language brochure download */
  $('#brochureLang').change(function(){
    var url = $(this).val();
    if(url.length > 0)
    {
      document.location.href = '/download-brochure.asp?file=' + encodeURIComponent(url);
    }
  });
  
  /* Calendar for booking page */
  if(typeof jQuery().datepicker != 'undefined')
  {
	  $('#CheckOut, #CheckIn, #BookingDate').datepicker({ dateFormat: 'dd/mm/yy' });
  }
  
  /* click to enlarge top images */
 $('body.staySub #topImage')
   .css('cursor', 'pointer')
   .attr('title', 'Click on this image to see larger version')
   .each(function(){
     // preload the rollover images
      pic1 = new Image();
      pic1.src = $(this).attr('src').replace('.jpg', '-lg1.jpg');
      pic2 = new Image();
      pic2.src = $(this).attr('src').replace('.jpg', '-lg2.jpg');
      pic3 = new Image();
      pic3.src = $(this).attr('src').replace('.jpg', '-lg3.jpg');
   })   
   .click(function(e){
     
     var idx = 0;
     var $this = this;
     var x = e.pageX - $(this).offset().left;
     
     if(x > 412) 
       idx = 3;
     else if(x > 206) 
       idx = 2;
     else 
       idx = 1;
       
     $('#fader img')
       .animate({opacity: 0, speed: 'slow'}, function(){
         $(this).attr('src', $($this).attr('src').replace('.jpg', '-lg' + idx + '.jpg'));
       });
       $('#fader img').animate({opacity: 1, speed: 'slow'});
   });
   
   $('a.video-link').click(function(){
     clearTimeout(_promo_timeout); 
     clearTimeout(_innerfade_timeout);
   });
	 
   // small hack to remove the arrow from page links that have a class of floorplan
   $('#mainColumn ul.links a.floorplan').parent().css({
     backgroundImage: 'none',
     paddingLeft: '2px'
   });
	 
   // add the nice quotes to the blockquotes
   $('#mainColumn blockquote p')
     .prepend('<span class="quoteMark">&#147; </span>')
     .append('<span class="quoteMark"> &#148;</span>');
		 
	 // popup video window
	 $('a.videopop').click(function(){
	 window.open($(this).attr('href'), 'vid', 'toolbar=0,resizable=1,scrollbars=0,width=505,height=315');
		 return false;
	 });
	 
	 // popup video window
	 $('a.videopop2').click(function(){
	 window.open($(this).attr('href'), 'vid', 'toolbar=0,resizable=1,scrollbars=0,width=510,height=425');
		 return false;
	 });	 

  
});

jQuery.initPromo = function()
{
  $.get('/promo_xml.asp?PromoId=' + escape($('#PromoLink').attr('class').replace(' thickbox', '')), function(xml){
    $('#PromoHeading').fadeOut(1000);
    $('#PromoText').fadeOut(1000, function(){
      $('#PromoHeading').text($('heading',xml).text());
      $('#PromoText')
        .text($('content',xml).text())
        .append(' <a href="' + $('url',xml).text() + '" id="PromoLink" class="' + $('id',xml).text() + '">&gt; more</a>');
       $('#PromoHeading').fadeIn(1000);
       $('#PromoText').fadeIn(1000, function(){
        if($.browser.msie)
        {
          document.getElementById('PromoText').style.removeAttribute('filter');
          document.getElementById('PromoHeading').style.removeAttribute('filter');
        }
      });
    });
    _promo_timeout = setTimeout($.initPromo,5000);
  });
}

function winPop(sLocation,sWidth,sHeight,sScrollbars){
	window.open(sLocation, '', 'toolbar=0,resizable=1,scrollbars=' + sScrollbars + ',left=2,top=2,width=' + sWidth + ',height=' + sHeight);
}

jQuery.validator.addMethod(
        "dateEN",
        function(value, element) {
            var check = false;
            var re = /^\d{2}\/\d{2}\/\d{4}$/
            if( re.test(value)){
                var adata = value.split('/');
                var gg = parseInt(adata[0],10);
                var mm = parseInt(adata[1],10);
                var aaaa = parseInt(adata[2],10);
                var xdata = new Date(aaaa,mm-1,gg);
                if ( ( xdata.getFullYear() == aaaa ) && ( xdata.getMonth () == mm - 1 ) && ( xdata.getDate() == gg ) )
                    check = true;
                else
                    check = false;
            } else
                check = false;
            return this.optional(element) || check;
        }, 
        "Please enter a valid date in the format dd/mm/yyyy"
    );
 