
$('document').ready(function($) {

  // attach facebox to any facebox links
  // $('a[rel*=facebox]').facebox() ;
  $('#facebox').livequery(function() {
    $(this).bgiframe();
  });

  $('a[rel*=facebox]').livequery(function() {
    $(this).facebox();
    return false;
  });


  //specials Tabbing
  $('a[rel*=specTab]').click(function(e){

     //make all spectabs class = off
     $('a[rel*=specTab]').removeClass('on');
     $(this).addClass('on');

     $('#special-offers-panel h2').html('<img src="/img/candystripe.gif" width="180" height="19" alt="Animation:searching..." style="vertical-align:middle;margin:2px"/>&nbsp;Searching...') ;

    $.getJSON($(this).attr('href'),{homepage:'1'},function(data){
        updateSpecOffers(data);
    });

    return false;
    }) ;

  //cycle through any slideshows
  $('#res-extra-photos').cycle('fade');
  $('#hotel-extra-photos').cycle('fade');

});


/** Generic Functions used throughout the site **/

function clearMe(oObj,val) {
  if (oObj.value==val) {
    oObj.value=''
  }
}

function fillMe(oObj,val) {
  if (oObj.value=='') {
    oObj.value=val
  }
}


/** Homepage Update Special Offers **/
function updateSpecOffers(oData) {
  $('#special-offers-panel h2').html(oData.h2) ;
  if (oData.error!='' && oData.error!=null) {
    $('#special-offers-panel div.content div.inner').html(oData.error) ;
  } else {
    $('#special-offers-panel div.content div.inner').html(oData.html) ;
  }
}



/**
* Newletter Signup Form
**/
$('document').ready(function($) {

  $('#brochure-field').click(function(e){
    if($(this).attr('checked')==true || $(this).attr('checked')=='checked'){
      $('#address-field').css('display','block');
    }else {
      $('#address-field').css('display','none');
    }
  });

  $('#address-field').focus(function(e){
    if($(this).html()=='Postal Address'){
      $(this).html('');
    }
  })

  $('#address-field').blur(function(e){
    if($(this).html()==''){
      $(this).html('Postal Address');
    }
  })

/*
  //resorts guide listing page
  $('a.resortname').click(function(e){
    //get the rel=rid
    var rId = $(this).attr('rel');
    //hide all
    $('div.res-summary').css({display:'none'});
    $('#'+rId).show();
  });
*/



  //Hotel Page - room types listings
  $('a.room-list').click(function(e){
    //get the rel=rid
    var rId = $(this).attr('rel');
    //hide all
    $('div.roominfo').css({display:'none'});
    $('#'+rId).show();
  });
      $('.open').show();

  if ($('#crnrs200').length){
   $('#crnrs200').parent().css('background','#ffffff');
   $('#crnrs200').corner();
  }



  /**
  * SEARCH FORM MAIN FUNCTIONS
  *
  */


/**
* Search Form Tab Switching
**/


$('document').ready(function($) {


  $('a[rel*=srchTab]').click(function(e){

     $('a[rel*=srchTab]').removeClass('on').addClass('off');
     //$('a[rel*=srchTab]');.addClass('off');
     $(this).removeClass('off').addClass('on');
     //$(this).addClass('on');
     if($(this).attr('id')=='srchtabflight') {
       $('#srchpanelh2').html('Flights Only Search..');
       $('#srchpanelh2').removeClass('srchhols');
       $('#srchpanelh2').addClass('srchflights');
       //hide fields / show fields
       $('[class=holsrch]').css('display','none');
       $('[class=flsrch]').css('display','block');
       //reset values
       $('#resort').val("");
       $('#flonly').val(1);
       $('#summerchk').attr('checked','');
       $('#winterchk').attr('checked','');
     }else {
       $('#srchpanelh2').removeClass('srchflights');
       $('#srchpanelh2').addClass('srchhols');
       $('#srchpanelh2').html('Holiday Search..');

       $('[class=flsrch]').css('display','none');
       $('[class=holsrch]').css('display','block');
       $('#flonly').val(0);
     }
  })

  //set the initial status if there was a post
  if ($('#flonly').val()==1) {
    $('#srchtabflight').addClass('on');
    $('#srchtabhol').removeClass('on');

    $('#srchpanelh2').html('Flights Only Search..').removeClass('srchhols').addClass('srchflights');
    $('[class=holsrch]').css('display','none');
    $('[class=flsrch]').css('display','block');
    $('#resort').val("");
    $('#summerchk').attr('checked','');
    $('#winterchk').attr('checked','');
  }



  //filter the Resort List depending on Season.
  $('#winterchk, #summerchk ').click(function(){
    var winterstatus = $('#winterchk').attr('checked');
    var summerstatus = $('#summerchk').attr('checked');

    //getJSON list of resorts for the resort drop down
    $.getJSON('/ajax/resortsFilter',
           {winter:winterstatus,summer:summerstatus},
           function(data){
             $('#srch-resort').empty();
               $('#srch-resort').append(data.sOptions);
           }
          );
    /*
    $.prompt('checkbox '+$(this).attr('name')+'clicked '+$(this).attr('checked'),{prefix:'srf'})
    $('#srf').corner('10px');
    */
  });

  // FILTER the arrive-at list depending on Departure airport
  $('#fly-from-field').change(function(){
    var flonly = $('#flonly').val();
    var depAirport = $('#fly-from-field').val();

    if (flonly==1) {
      //getJSON list of airports for the resort drop down
      $.getJSON('/ajax/arrAirportsFilter',
           {dep:depAirport},
           function(data){
             $('#arrive-at-field').empty();
               $('#arrive-at-field').append(data.sOptions);
           }
          );
    }
  });


 //Initialise date Picker to Allow ONLY Saturdays/Sundays  to be chosen
  Date.format = 'dd-mm-yyyy';
  $.dpConst.CAL_TITLE = 'Departure Date:' ;
  $(function(){ $('input.date-pick').datePicker(
                                                    {
                                                    clickInput:true,

                                            				renderCallback:function($td, thisDate, month, year)
                                            				{


                                                      var DepCode = $('#fly-from-field').val()
                                            				  var DepDay ='Sat'
                                            				  tmpDate = new Date.fromString(thisDate.asString());

                                            				  if (DepCode =='ORK') {
                                            				    DepDay = 'Mon' ;
                                            				  }else if (DepCode =='DUB') {
                                            				    DepDay = 'Mon' ;
                                            				  }else if (DepCode =='BFS') {
                                            				    DepDay = 'Sat' ;
                                            				  }

                                                        //if json array exists check it
                                                        /* NEW CODE */
                                                        if (jDays[DepCode]) {
                                                           for(var i in jDays[DepCode]) {
                                                             var jStartDate = new Date.fromString(jDays[DepCode][i]['from']);
                                                             if (jDays[DepCode][i]['to']!='00-00-0000' && jDays[DepCode][i]['to']!='')  {
                                                               var jEndDate = new Date.fromString(jDays[DepCode][i]['to']);
                                                             }else{
                                                               jEndDate = '';
                                                             }
                                                             if ( tmpDate >= jStartDate
                                                                 && ( jDays[DepCode][i]['to']=='00-00-0000' || tmpDate <= jEndDate )
                                                                )
                                                             {
                                                               DepDay = jDays[DepCode][i]['weekday'] ;
                                                             }
                                                           }
                                                        }
                                                        /*  END NEW CODE */

                                            					if (thisDate.getDayName(true)!=DepDay) {
                                            						$td.addClass('disabled');
                                            					}

                                            				}
                                            			}
                                                );
               }
      ); //End datePicker initiation
  });


});






  //Form Validation
  function validateSrchForm(oFrm) {

    var errCnt = 0;
    var errMsg = '<h2>warning...</h2> <p>There is a problem, please ensure you have filled out all fields</p>' ;

    if ($('#flonly').val()!=1 && (oFrm.resort.value=='' || oFrm.resort.value=='-Any-')) {
      errCnt++;
      errMsg+='<p> - Choose a Resort</p>';
      oFrm.resort.focus();
    }

    if (oFrm.fly_from.value=='' ) {
      errCnt++;
      errMsg+='<p> - Please select your Departure Airport</p>';
      oFrm.fly_from.focus();
    }

    if ($('#flonly').val()==1 && (oFrm.arrive_at.value=='')) {
      errCnt++;
      errMsg+='<p> - Choose an Arrival airport</p>';
      oFrm.resort.focus();
    }

    if ($('#flonly').val()==1 && (oFrm.arrive_at.value==oFrm.fly_from.value)) {
      errCnt++;
      errMsg+='<p> - Departure and Arrival airports <strong>cannot</strong> be the same</p>';
      oFrm.arrive_at.focus();
    }

    if ($('#srch-depdate').val()=='' || $('#srch-depdate').val()=='dd-mmm-yyyy' ) {
      errCnt++;
      errMsg+='<p> - Use the calendar to pick an available departure date.';
      oFrm.departure_date.focus();
    }

    if (oFrm.duration.value=='' || oFrm.duration.value==0 ) {
      errCnt++;
      errMsg+='<p> - Please select either 7 or 14 nights</p>';
      oFrm.duration.focus();
    }

    if (oFrm.adults.value=='' || oFrm.adults.value==0 ) {
      errCnt++;
      errMsg+='<p> - Please select at least 1 Adult</p>';
      oFrm.adults.focus();
    }

    if (errCnt==0) {
      return true ;
    } else {
      //launch errMsg in a popup.
      $.prompt(errMsg,{buttons:{Ok:true},prefix:'srf'});
      $('#srf').corner('10px');
      return false;
    }

  }


/**
Shopping Basket Page
***/
$('document').ready(function($) {

  //bind event to inajx links for adding insurance
  $('a[rel*=ajxlink]').click(function() {
      var sHref = $(this).attr('href') ;
      var sContainer = '#'+ $(this).attr('class')+'-extras' ;
      var sSelectedList = '#'+ $(this).attr('class')+'list' ;
      var sSibling = '#'+ $(this).attr('class')+'hide' ;
      $(this).show();
      $(sSelectedList).slideUp();

      //put loading extras message in to the container
      $(sContainer).css('height','38px');
      $(sContainer).html('<div style="text-align:center;font-weight:bold"><img src="/img/ajax.gif" width="32" height="32" alt="Loading animation" style="vertical-align:middle" />Loading options</div>');
      $(sContainer).addClass('extrasoptions');
      //getJSON list of resorts for the resort drop down
      $.get(sHref,
             {ajx:1},
             function(data){
               $(sContainer).html(data).css('height','auto').slideDown();
               //$(sContainer);
               //$(sContainer);
             }
            );

      $(this).hide();
      $(sSibling).show();
      return false;
  });



  $('a[rel*=closeme]').click(function(){
      var sContainer = '#'+ $(this).attr('class')+'-extras' ;
      var sSelectedList = '#'+ $(this).attr('class')+'list' ;
      var sSibling = '#'+ $(this).attr('class')+'show' ;

      $(sContainer).slideUp();
      $(sContainer).html('');
      $(sSelectedList).slideToggle();

      $(this).hide();
      $(sSibling).show();

      return false;
  });

});
