addLoadEvent(initialiseEvents);
addLoadEvent(enableSelects);

//this function is a hack for a bug where ajax disable states remain after a
//search when back from the browser is selected
function enableSelects(){
  $("scuAirportSelect").disabled = false;
  $("scuDurationSelect").disabled = false;
  $("scuDestinationSelect").disabled = false;
  $("scuResortSelect").disabled = false;
}

//when order of js includes can be changed this function needs to be moved to interstitialDisplay.js
function initialiseInterstitial(){
  if(!document.body) return false;
  var docBody = document.body;
  var backgroundImage = '/images/backgrounds/background.jpg';
  var mainPhotoPath = '/images/interstitual/falcon-interstial-logo.png';
  var mainPhotoPathIE = '/images/interstitual/falcon-interstial-logo.gif';

  var interstitialHtml = '<img style="display:none" src="' + backgroundImage +'" alt="" />';
  var bgClassName = '';

  //IE6 does not handle form submit and animated gif's well hence script based animation for IE.
  if(!browser.isIE){
     bgClassName = 'bg01';
     interstitialHtml = interstitialHtml + '<img src="' + mainPhotoPath + '" class="intImg" alt="">' +
         '<br />' +
         '<img class="progressBar" src="/images/interstitual/progress-bar.gif" alt="" />';
  }else{
     interstitialHtml = interstitialHtml + '<img src="' + mainPhotoPathIE + '" class="intImgIE" alt="">' +
         '<br />' +
         '<img class="progressBar" id="progressBar0" src="/images/interstitual/progressBar0.gif" alt="" />' +
         '<img class="progressBar" id="progressBar1" src="/images/interstitual/progressBar1.gif" alt="" />' +
         '<img class="progressBar" id="progressBar2" src="/images/interstitual/progressBar2.gif" alt="" />' +
         '<img class="progressBar" id="progressBar3" src="/images/interstitual/progressBar3.gif" alt="" />' +
         '<img class="progressBar" id="progressBar4" src="/images/interstitual/progressBar4.gif" alt="" />' +
         '<img class="progressBar" id="progressBar5" src="/images/interstitual/progressBar5.gif" alt="" />' +
         '<img class="progressBar" id="progressBar6" src="/images/interstitual/progressBar6.gif" alt="" />' +
         '<img class="progressBar" id="progressBar7" src="/images/interstitual/progressBar7.gif" alt="" />' +
         '<img class="progressBar" id="progressBar8" src="/images/interstitual/progressBar8.gif" alt="" />' +
         '<img class="progressBar" id="progressBar9" src="/images/interstitual/progressBar9.gif" alt="" />';
  }
 interstitialHtml = interstitialHtml + '<p>Searching for your dream holiday</p>';
 /* Your interstitial message here, id='interstitialCancel' is required in the a tag */
 var interstitialDiv = document.createElement("div");
 interstitialDiv.setAttribute("id","interstitialContainerEmbedded");
 interstitialDiv.setAttribute("class",bgClassName);
 interstitialDiv.innerHTML = interstitialHtml;
 docBody.appendChild(interstitialDiv);
}

//global variable
var selectedDestination;
var selectedDurationValue;
var selectedResort;
var selectedDepartureAirportValue;

function initialiseEvents(){
// Adult default selection and Default duration selection .

  initialiseInterstitial();

  var scuAirportSelect         = $( 'scuAirportSelect' );

   selectedDepartureAirportValue = $( 'selectedDepartureAirport' );
  selectedDestination  = $( 'selectedDestination' );
  selectedDurationValue = $( 'selectedDurationValue' );
  selectedResort = $( 'selectedResort' );
  var scuDestinationSelect = $( 'scuDestinationSelect' );
  var scuResortSelect = $( 'scuResortSelect' );
  var scuDurationSelect = $( 'scuDurationSelect' );
  var durationChanged = false;
  var destinChanged = false;
  var airportChanged = false;

  if ( selectedDurationValue.value != scuDurationSelect.value )
  {
    if(selectedDurationValue.value != 0 && !selectOption( scuDurationSelect, selectedDurationValue.value ) && !airportChanged)
       {
            updateSearchPanel('IsuSelectDateOnLoad');
            durationChanged  = true;
        }
  }
  if ( selectedDepartureAirportValue.value != scuAirportSelect.value)
  {
    if(!selectOption($( 'scuAirportSelect' ),selectedDepartureAirportValue.value) && !durationChanged)
       {
            updateSearchPanel('IsuSelectDepartOnload');
            airportChanged = true;
        }
  }
  if ( selectedDestination.value != scuDestinationSelect.value || scuResortSelect.value != selectedResort.value)
  {
      if(selectOption( scuDestinationSelect, selectedDestination.value ) )
        {
                  updateSearchPanel('IsuSelectDepart');
                  destinChanged = true;
        }
       else if (!(durationChanged || airportChanged))
        {
                updateSearchPanel('IsuSelectload');
                    destinChanged = true;
        }
  }
  if (!durationChanged && !airportChanged && !destinChanged && scuDestinationSelect.value==0)
  {
   updateSearchPanel('IsuSelectload');
  }

 updateDayOfWeek();
 updateRoom();
 var maxNumberOfRooms = 5;
 scuDa = $("scuDate");
 scuDSel = $("scuDaySelect");
 scuMYSel = $("scuMonthYearSelect");
 scuDaCl = $("scuDateClose");
 scuDest = $("scuDestination");
 scuDestSel = $("scuDestinationSelect");
 scuDestCl = $("scuDestinationClose");
 scuAport = $("scuAirport");
 scuAportSel = $("scuAirportSelect");
 scuAportCl = $("scuAirportClose");
 scuDur = $("scuDuration");
 scuDurSel = $("scuDurationSelect");
 scuDurCl = $("scuDurationClose");
 scuWh = $("scuWho");
 numberOfRooms = $("numberOfRooms");
 var scuWhTASel = new Array(maxNumberOfRooms);
 var scuWhSenSel = new Array(maxNumberOfRooms);
 var scuWhChSel = new Array(maxNumberOfRooms);
 var scuWhInSel = new Array(maxNumberOfRooms);

 for(i=1;i<=maxNumberOfRooms;i++)
 {
  scuWhTASel[i] = $("scuWhoAdultsSelect"+i);
  scuWhSenSel[i] = $("scuWhoSeniorsSelect"+i);
  scuWhChSel[i] = $("scuWhoChildrenSelect"+i);
  scuWhInSel[i] = $("scuWhoInfantsSelect"+i);
 }
 scuWhCl = $("scuWhoClose");
 scuRes = $("scuResort");
 //Temporarily put these calls inline as returned as innerHTML (string)
 //and dom wont pick them up
 scuResSel = $("scuResortSelect");
 scuResCl = $("scuResortClose");
 scuAcc = $("scuAccommodation");
 scuAccSel = $("scuAccommodationSelect");
 scuAccCl = $("scuAccommodationClose");
 scuRate = $("scuRating");
 scuRateSel = $("scuRatingSelect");
 scuRateCl = $("scuRatingClose");
 scuFeat = $("scuFeatures");
 scuFeatCl = $("scuFeaturesClose");

 expandCollapseLink1=$("expandCollapse1");
 expandCollapseLink2=$("expandCollapse2");
 expandCollapseLink3=$("expandCollapse3");
 expandCollapseLink4=$("expandCollapse4");

 var y = 'yes';
 var h = 'hide';
 var s = 'show';

 configureIsuFeatures(true);

 scuRes.onclick = function(e){
   searchMoreOptionExpandHandler(detectBrows(e), scuRes);
   showElement('scuResortSelect');
   // if(searchDestinationIsSelected()){
   functionsPanelOverlay(scuRes,y, true);
   // }
 }

 scuResSel.onmousedown = function(){
  functionsPanelOverlay(scuRes,y, true);
 }
 scuResSel.onchange = function(){
  updateResortSelection();
  functionsPanelOverlay(scuRes,h, true);
  searchMoreOptionsUpdateAll();
  updateSearchPanel('IsuSelectResort');
 }

 expandCollapseLink1.onclick = function(e){
 var image = searchMoreOptionGetExpandCollapseImage( scuRes )
    if(image.alt == "Expand"){
        searchMoreOptionExpandHandler(detectBrows(e), scuRes);
     showElement(scuResSel);
     functionsPanelOverlay(scuRes,y, true);
    }else{
        searchMoreOptionCollapseHandler(detectBrows(e), scuRes);
     hideElement(scuResSel);
     functionsPanelOverlay(scuRes,h, true);
    }
 }

 expandCollapseLink2.onclick = function(e){
 var image = searchMoreOptionGetExpandCollapseImage( scuAcc )
    if(image.alt == "Expand"){
        searchMoreOptionExpandHandler(detectBrows(e), scuAcc);
     showElement(scuAccSel);
     functionsPanelOverlay(scuAcc,y, true);
    }else{
        searchMoreOptionCollapseHandler(detectBrows(e), scuAcc);
     hideElement(scuAccSel);
     functionsPanelOverlay(scuAcc,h, true);
    }
 }

 scuAcc.onclick = function(e){
  searchMoreOptionExpandHandler(detectBrows(e), scuAcc);
  showElement(scuAccSel);
  functionsPanelOverlay(scuAcc, y, true);
 }
 scuAccSel.onmousedown = function(){
  functionsPanelOverlay(scuAcc, y, true);
 }
 scuAccSel.onchange = function(){
  searchMoreOptionsUpdateAll();
  functionsPanelOverlay(scuAcc, h, true);
 }

 expandCollapseLink3.onclick = function(e){
 var image = searchMoreOptionGetExpandCollapseImage( scuRate )
    if(image.alt == "Expand"){
        searchMoreOptionExpandHandler(detectBrows(e), scuRate);
     showElement(scuRateSel);
     functionsPanelOverlay(scuRate,y, true);
    }else{
        searchMoreOptionCollapseHandler(detectBrows(e), scuRate);
     hideElement(scuRateSel);
     functionsPanelOverlay(scuRate,h, true);
    }
 }

 scuRate.onclick = function(e){
  searchMoreOptionExpandHandler(detectBrows(e), scuRate);
  showElement(scuRateSel);
  functionsPanelOverlay(scuRate, y, true);
 }
 scuRateSel.onchange = function(){
  searchMoreOptionsUpdateAll();
  functionsPanelOverlay(scuRate, h, true);
 }

 expandCollapseLink4.onclick = function(e){
 var image = searchMoreOptionGetExpandCollapseImage( scuFeat )
    if(image.alt == "Expand"){
        searchMoreOptionExpandHandler(detectBrows(e), scuFeat);
     showElement('scuFeaturesOptions');
     functionsPanelOverlay(scuFeat,y, true);
    }else{
        searchMoreOptionCollapseHandler(detectBrows(e), scuFeat);
     hideElement('scuFeaturesOptions');
     functionsPanelOverlay(scuFeat,h, true);
    }
 }

 scuFeat.onclick = function(e){
  searchMoreOptionExpandHandler(detectBrows(e), scuFeat);
  showElement('scuFeaturesOptions');
  functionsPanelOverlay(scuFeat, y, true);
 }
 scuDa.onclick = function(){
  functionsPanelOverlay(scuDa,y, true);
 }
 scuDSel.onclick = function(){
  functionsPanelOverlay(scuDa,y, true);
 }
 scuMYSel.onclick = function(){
  functionsPanelOverlay(scuDa,y, true);
 }
 scuDSel.onmousedown = function(){
  functionsPanelOverlay(scuDa,y, true);
 }
 scuDSel.onchange = function(e){
  //updateRoom();
 //updateSearchPanel('IsuSelectDate');
 }
 scuMYSel.onmousedown = function(){
  functionsPanelOverlay(scuDa,y, true);
 }
 scuMYSel.onchange = function(){
  //updateRoom();
  //updateSearchPanel('IsuSelectDate');
 }
 scuDur.onclick = function(){
  functionsPanelOverlay(scuDur,y, true);
 }
 scuDurSel.onclick = function(){
  functionsPanelOverlay(scuDur,y, true);
 }
 scuDurSel.onmousedown = function(){
  functionsPanelOverlay(scuDur,y, true);
 }
 scuDurSel.onchange = function(){
  clearMessage();
  functionsPanelOverlay(scuDur,h, true);
  updateSearchPanel('IsuSelectDuration');
 }
 scuAport.onclick = function(){
  functionsPanelOverlay(scuAport,y), true;
 }
 scuAportSel.onclick = function(){
  functionsPanelOverlay(scuAport,y, true);
 }
 scuAportSel.onmousedown = function(){
  functionsPanelOverlay(scuAport,y, true);
 }
 scuAportSel.onchange = function(){
  functionsPanelOverlay(scuAport,h, true);
  updateSearchPanel('IsuSelectDepart');
 }
 scuDest.onclick = function(){
  functionsPanelOverlay(scuDest,y, true);
 }
 scuDestSel.onclick = function(){
  functionsPanelOverlay(scuDest,y, true);
 }
 scuDestSel.onmousedown = function(){
  functionsPanelOverlay(scuDest,y, true);
 }
 scuDestSel.onchange = function(){
  functionsPanelOverlay(scuDest,h, true);
  searchMoreOptionsUpdateAll();
  updateSearchPanel('IsuSelectDest');
 }
 scuWh.onclick = function(){
  checkWhoType('global');
  functionsPanelOverlay(scuWh,y, true);
 }
 for (i=1;i<=maxNumberOfRooms;i++)
 {
  scuWhTASel[i].onmousedown = function(){
  checkWhoType('adult');
  checkChildren(s, this.id);
  functionsPanelOverlay(scuWh,y, true);
  }
  scuWhTASel[i].onchange = function(){
  checkWhoType('adult');
  functionsPanelOverlay(scuWh,h, true);
  }
  scuWhTASel[i].onclick = function(e){
  stopEventBubbling(detectBrows(e));
  return true;
  }

  scuWhSenSel[i].onmousedown = function(){
  checkWhoType('adult');
  checkChildren(s, this.id);
  functionsPanelOverlay(scuWh,y, true);;
  }
  scuWhSenSel[i].onchange = function(){
  checkWhoType('adult');
  functionsPanelOverlay(scuWh,h, true);
  }
  scuWhSenSel[i].onclick = function(e){
  stopEventBubbling(detectBrows(e));
  return true;
  }

  scuWhChSel[i].onmousedown = function(){
  checkWhoType('child');
  checkChildren(s, this.id);
  functionsPanelOverlay(scuWh,y, true);
  }
  scuWhChSel[i].onchange = function(){
  configChildAgesForRooms(this.id);
  checkWhoType('child');
  checkChildren(s, this.id);
  functionsPanelOverlay(scuWh,h, true);
  }
  scuWhChSel[i].onclick = function(e){
  stopEventBubbling(detectBrows(e));
  return true;
  }
  scuWhInSel[i].onmousedown = function(){
  checkWhoType('adult');
  checkInfant(s, this.id);
  functionsPanelOverlay(scuWh,y, true);
  }
  scuWhInSel[i].onchange = function(){
  checkWhoType('adult');
  functionsPanelOverlay(scuWh,h, true);
  }
  scuWhInSel[i].onclick = function(e){
  stopEventBubbling(detectBrows(e));
  return true;
  }
 }
 numberOfRooms.onchange = function(e){
 updateRoom();
 }
 scuDaCl.onclick = function(){
  functionsPanelOverlay(scuDa,h, true);
 }
 scuDurCl.onclick = function(){
  functionsPanelOverlay(scuDur,h, true);
 }
 scuAportCl.onclick = function(){
  functionsPanelOverlay(scuAport,h, true);
 }
 scuDestCl.onclick = function(){
  functionsPanelOverlay(scuDest,h, true);
 }
 scuWhCl.onclick = function(){
  checkWhoType('global');
  functionsPanelOverlay(scuWh,h, true);

 }
 scuResCl.onclick = function(){
  functionsPanelOverlay(scuRes,h, true);
 }
 scuAccCl.onclick = function(){
  functionsPanelOverlay(scuAcc,h, true);
 }
 scuRateCl.onclick = function(){
  functionsPanelOverlay(scuRate,h, true);
 }
 scuFeatCl.onclick = function(){
  functionsPanelOverlay(scuFeat,h, true);
 }

 searchMoreOptionsUpdateAll();

 /* Resizing window registers a mouseover event that updates overlay positions */
 document.body.onresize = function() {
  document.body.onmouseover = function() { updateOverlayPosition(); }
 }
 window.onresize = function() {
  document.body.onmouseover = function() { updateOverlayPosition(); }
 }

 /* Mouseover event for the body that updates overlay positions */
 document.body.onmouseover = function() {
  updateOverlayPosition();
 }

}