jQuery.preloadImages = function() {
 for(var i = 0; i<arguments.length; i++) {
  jQuery("<img>").attr("src", arguments[i]);
 }//for
}//function

$(function() {
 imgLoadingImage = document.createElement("img");
 $(imgLoadingImage).attr("src", "images/loading.gif");
 $(imgLoadingImage).attr("alt", "Loading...");
 $(imgLoadingImage).attr("width", "16");
 $(imgLoadingImage).attr("height", "16");
 $(imgLoadingImage).attr("id", "loadingimage");
 $("body").append(imgLoadingImage);
 hideLoadingImage();
});

function loadImage (url, strBackgroundColor, intBorderWidth) {

 if (typeof strBackgroundColor == 'undefined' ) strBackgroundColor = '#112d80';
 if (typeof intBorderWidth == 'undefined' ) intBorderWidth = '1';

 showOverlayDiv();
 showLoadingImage();

 imgPreload = new Image();
 $(imgPreload).load(function () {
  arrPageSize = getPageSize();
  arrPageScroll = getPageScroll();

  imageContainer = document.createElement("div");
  $(imageContainer).attr("id", "largeimagecontainer");

  imgClose = document.createElement("img");
  $(imgClose).attr("src", "images/close.gif");
  $(imgClose).attr("id", "closebutton");
  $(imgClose).attr("width", "14");
  $(imgClose).attr("height", "14");
  $(imgClose).attr("alt", "Close");
  $(imgClose).attr("border", "0");
  $(imgClose).click(hideImage);

  imgImage = document.createElement("img");
  $(imgImage).attr("src", url);
  $(imgImage).attr("alt", "");
  $(imgImage).attr("width", imgImage.width);
  $(imgImage).attr("height", imgImage.height);

  $(imageContainer).append(imgImage);
  $(imageContainer).append(imgClose);

  $("#largeimagecontainer").hide();
  $("body").append(imageContainer);

  $("#largeimagecontainer").css("position", "absolute");
  $("#largeimagecontainer").css("z-index", "300");
  $("#largeimagecontainer").css("background-color", strBackgroundColor);
  $("#largeimagecontainer").css("border", intBorderWidth + "px solid #ffffff");

  if ($.browser.msie) {
   if ((arrPageSize[2] < arrPageSize[0])) {
    imagecontainertop = (((arrPageSize[3] - imgImage.height - 22) / 2) + 9);
   } else {
    imagecontainertop = (((arrPageSize[3] - imgImage.height - 22) / 2));
   }//if

   if ((arrPageSize[3] < arrPageSize[1])) {
    imagecontainerleft = (((arrPageSize[2] - imgImage.width - 22) / 2) + 9);
   } else {
    imagecontainerleft = (((arrPageSize[2] - imgImage.width - 22) / 2));
   }//if
  } else {
   imagecontainertop = (((arrPageSize[3] - imgImage.height - 22) / 2) - 9);
   imagecontainerleft = (((arrPageSize[2] - imgImage.width - 22) / 2) - 9);
  }//if

  if (imagecontainertop < 0) { imagecontainertop = 0; }//if
  if (imagecontainerleft < 0) { imagecontainerleft = 0; }//if

  $("#largeimagecontainer").css("top", arrPageScroll[1] + imagecontainertop + "px");
  $("#largeimagecontainer").css("left", arrPageScroll[0] + imagecontainerleft + "px");

  $("#largeimagecontainer").show();
  hideLoadingImage();
 });

 imgPreload.src = url;
}//function

function hideImage () {
 $("div#largeimagecontainer").remove();
 hideOverlayDiv();
}//function

function showElement(elemID) {
 showOverlayDiv();
 showLoadingImage();

 arrPageSize = getPageSize();
 arrPageScroll = getPageScroll();

 var objElement = $('#' + elemID);

 if ($.browser.msie) {
  if ((arrPageSize[2] < arrPageSize[0])) {
   objElementTop = (((arrPageSize[3] - objElement.height()) / 2) + 9);
  } else {
   objElementTop = (((arrPageSize[3] - objElement.height()) / 2));
  }//if

  if ((arrPageSize[3] < arrPageSize[1])) {
   objElementLeft = (((arrPageSize[2] - objElement.width()) / 2) + 9);
  } else {
   objElementLeft = (((arrPageSize[2] - objElement.width()) / 2));
  }//if
 } else {
  objElementTop = (((arrPageSize[3] - objElement.height()) / 2) - 9);
  objElementLeft = (((arrPageSize[2] - objElement.width()) / 2) - 9);
 }//if

 if (objElementTop < 0) { objElementTop = 0; }//if
 if (objElementLeft < 0) { objElementLeft = 0; }//if

 objElement.css("top", arrPageScroll[1] + objElementTop + "px");
 objElement.css("left", arrPageScroll[0] + objElementLeft + "px");

 objElement.show();
 hideLoadingImage();
}//function

function hideElement(elemID) {
 var objElement = $('#' + elemID);
 objElement.hide();
 hideOverlayDiv();
}//function

function showLoadingImage () {
 arrPageSize = getPageSize();
 arrPageScroll = getPageScroll();

 $("#loadingimage").css("position", "absolute");
 $("#loadingimage").css("z-index", "2");
 $("#loadingimage").css("top", (arrPageScroll[1] + ((arrPageSize[3] - 16) / 2)) + "px");
 $("#loadingimage").css("left", (arrPageScroll[0] + ((arrPageSize[2] - 16) / 2)) + "px");
 $("#loadingimage").show();
}//function

function hideLoadingImage () {
 $("#loadingimage").hide();
}//function

function showOverlayDiv () {
 arrPageSize = getPageSize();

 overlayDiv = document.createElement("div");
 $(overlayDiv).attr("id", "overlay");
 $(overlayDiv).css("height", arrPageSize[1] + "px");
 $(overlayDiv).css("position", "absolute");
 $(overlayDiv).css("top", "0");
 $(overlayDiv).css("left", "0");
 $(overlayDiv).css("width", "100%");
 $(overlayDiv).css("z-index", "100");

 $("body").append(overlayDiv);

 $("select").hide();
}//function

function hideOverlayDiv () {
 $("div#overlay").remove();
 $("select").show();
}//function

// Core code from - quirksmode.org
function getPageScroll(){
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {
		yScroll = document.body.scrollTop;
	}//if

	arrayPageScroll = new Array('', yScroll)
	return arrayPageScroll;
}

// Core code from - quirksmode.org
// Edit for Firefox by pHaez
function getPageSize(){
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}//if

	var windowWidth, windowHeight;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}//if

	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}//if

	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}//if

	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
	return arrayPageSize;
}

//Do destination tabs
function toggleTabs(strID) {
 $('div#tabscontainer div#tabscontainerinner div.tabSection').css('display', 'none');
 $('div#tabscontainer div#tabscontainerinner div.tabSection.' + strID).css('display', 'block');
 $('div#tabscontainer ul#tabs li').removeClass('selected');
 $('div#tabscontainer ul#tabs li#' + strID).addClass('selected');
}//function

$(document).ready(function() {

 $('ul#tabs li').click( function() {
  var strID = $(this).attr('id');
  toggleTabs(strID);
 });

 $('ul#nav li').hover( function() {
 	$(this).addClass('hover');
 }, function() {
 	$(this).removeClass('hover');
 });

 //Show info tab by default
 toggleTabs('tabInfo');

 $('ul#tabs li').hover( function() {$(this).addClass('hover');}, function() {$(this).removeClass('hover');} );

 //Collapse any collapsible boxes in the leftpanel
 /*
 $('div#leftpanel div.box div.collapsed + div.boxbody').each( function() {
  $(this).css({display: 'none'});
 });

 //Make box headers clickable
 $('div#leftpanel div.box div.boxheader').each( function() {

  $(this).hover( function() {
   $(this).addClass('hover');
  }, function() {
   $(this).removeClass('hover');
  });

  $(this).click( function() {
   if ($(this).hasClass('collapsed')) {
    $(this).next('div.boxbody').css({display: 'block'});
    $(this).removeClass('collapsed');
    $(this).addClass('expanded');
   } else {
    $(this).next('div.boxbody').css({display: 'none'});
    $(this).removeClass('expanded');
    $(this).addClass('collapsed');
   }//if
  });

 });
 */

 //Make colapsible content boxes clickable
 $('div#content div.collapsible h3').each( function() {

  if (! $(this).parent('div.collapsible').hasClass('collapsed')) {
   $(this).parent('div.collapsible').addClass('collapsed');
  }//if

  //Add an arrow image to the header bar
  var imgArrowUp = $('<img>');
  imgArrowUp.attr('src','images/arrow_up.gif');
  imgArrowUp.attr('border','0');
  imgArrowUp.attr('width','17');
  imgArrowUp.attr('height','17');
  imgArrowUp.attr('class','arrowup');
  $(this).append(imgArrowUp);

  var imgArrowUp = $('<img>');
  imgArrowUp.attr('src','images/arrow_down.gif');
  imgArrowUp.attr('border','0');
  imgArrowUp.attr('width','17');
  imgArrowUp.attr('height','17');
  imgArrowUp.attr('class','arrowdown');
  $(this).append(imgArrowUp);

  $(this).hover( function() {
   $(this).addClass('hover');
  }, function() {
   $(this).removeClass('hover');
  });

  $(this).click( function() {
   if ($(this).parent('div.collapsible').hasClass('collapsed')) {
    $(this).parent('div.collapsible').removeClass('collapsed');
    $(this).parent('div.collapsible').addClass('expanded');
    //});
   } else {
    $(this).parent('div.collapsible').removeClass('expanded');
    $(this).parent('div.collapsible').addClass('collapsed');
    //});
   }//if
  });

 });

 //Make FAQ questions clickable
 $('body#pageFAQs div#content div.question h4').each( function() {

  $(this).addClass('clickable');

  if (! $(this).parent('div.question').hasClass('collapsed')) {
   $(this).parent('div.question').addClass('collapsed');
  }//if

  $(this).click( function() {
   if ($(this).parent('div.question').hasClass('collapsed')) {
    $(this).parent('div.question').addClass('expanded').removeClass('collapsed');
   } else {
    $(this).parent('div.question').addClass('collapsed').removeClass('expanded');
   }//if
  });

 });

 $('body#pageSafety div#content div.question h4').each( function() {

  $(this).addClass('clickable');

  if (! $(this).parent('div.question').hasClass('collapsed')) {
   $(this).parent('div.question').addClass('collapsed');
  }//if

  $(this).click( function() {
   if ($(this).parent('div.question').hasClass('collapsed')) {
    $(this).parent('div.question').addClass('expanded').removeClass('collapsed');
   } else {
    $(this).parent('div.question').addClass('collapsed').removeClass('expanded');
   }//if
  });

 });

 //Email to a friend stuff
 $('div#leftpanel li#emailtofriend a').click( function() {
  showElement('panelFriend');
  return false;
 });

 $('div#panelFriend img.closebutton').click( function() {
  hideElement('panelFriend');
 });

 //Newsletter signup stuff
 $('input#input-NewsletterEmail').focus( function() {
  if ($(this).val() == 'your email') {
   $(this).val('');
  }//if
 });

 $('#image-NewsletterSubmit').click( function() {

  //Hide Flash content
  $('div#banner embed').hide();
  $('div#banner object').hide();
  $('div#banner img#placeholder').show();

  showElement('panelNewsletter');
  $('input#input-PanelEmail').val($('input#input-NewsletterEmail').val());
  return false;
 });

 $('div#panelNewsletter img.closebutton').click( function() {
  $('div#banner embed').show();
  $('div#banner object').show();
  $('div#banner img#placeholder').hide();

  hideElement('panelNewsletter');
 });

 //Photo comp images
 $('body#pagePhoto div.photo a').click(function() {
  loadImage($(this).attr('href'));
  return false;
 });

 //Contact form stuff
 doRequiredFields = function() {
  $('body.contact div#content form div.field label').each( function() {
   if ($(this).text().substr($(this).text().length - 1, 1) == '*') {
    $(this).text($(this).text().substr(0, $(this).text().length - 1));
   }//if
  });
  $('body.contact div#content form div.required label').each( function() {
   $(this).append('*');
  });
 }//function

 clearRequiredFields = function() {
  $('input#input-Email').parents('div.field').removeClass('required');
  $('input#input-Phone').parents('div.field').removeClass('required');
  $('input#input-Address1').parents('div.field').removeClass('required');
  $('input#input-Town').parents('div.field').removeClass('required');
  $('input#input-County').parents('div.field').removeClass('required');
  $('input#input-Postcode').parents('div.field').removeClass('required');
 }//function

 doSelection = function() {
  clearRequiredFields();
  blnRequiredEmail = false;
  blnRequiredPhone = false;
  blnRequiredAddress = false;

  if ($('input#input-DoNewsletter').is(':checked') || $('select#select-ContactMethod').val() == 'Email') {
   blnRequiredEmail = true;
  }//if

  if ($('select#select-ContactMethod').val() == 'Phone') {
   blnRequiredPhone = true;
  }//if

  if ($('input#input-DoBrochure').is(':checked') || $('select#select-ContactMethod').val() == 'Post') {
   blnRequiredAddress = true;
  }//if

  if (blnRequiredEmail) {
   $('input#input-Email').parents('div.field').addClass('required');
  }//if

  if (blnRequiredPhone) {
   $('input#input-Phone').parents('div.field').addClass('required');
  }//if

  if (blnRequiredAddress) {
   $('input#input-Address1').parents('div.field').addClass('required');
   $('input#input-Town').parents('div.field').addClass('required');
   $('input#input-County').parents('div.field').addClass('required');
   $('input#input-Postcode').parents('div.field').addClass('required');
  }//if

  doRequiredFields();
 }//function

 doEnquiryType = function() {
  if ($('input#input-EnquiryType-Education').is(':checked')) {
   $('div#EnquiryTypeConcert').css('display','none');
   $('div#EnquiryTypeMusic').css('display','none');
   $('div#EnquiryTypeSki').css('display','none');
   $('div#EnquiryTypeEducation').css('display','block');
  }//if

  if ($('input#input-EnquiryType-Concert').is(':checked')) {
   $('div#EnquiryTypeConcert').css('display','block');
   $('div#EnquiryTypeMusic').css('display','none');
   $('div#EnquiryTypeSki').css('display','none');
   $('div#EnquiryTypeEducation').css('display','none');
  }//if

  if ($('input#input-EnquiryType-Music').is(':checked')) {
   $('div#EnquiryTypeConcert').css('display','none');
   $('div#EnquiryTypeMusic').css('display','block');
   $('div#EnquiryTypeSki').css('display','none');
   $('div#EnquiryTypeEducation').css('display','none');
  }//if

  if ($('input#input-EnquiryType-Ski').is(':checked')) {
   $('div#EnquiryTypeConcert').css('display','none');
   $('div#EnquiryTypeMusic').css('display','none');
   $('div#EnquiryTypeSki').css('display','block');
   $('div#EnquiryTypeEducation').css('display','none');
  }//if
 }//function

 $('input#input-EnquiryType-Education').click(doEnquiryType);
 $('input#input-EnquiryType-Concert').click(doEnquiryType);
 $('input#input-EnquiryType-Music').click(doEnquiryType);
 $('input#input-EnquiryType-Ski').click(doEnquiryType);

 var doPhone = function() {
  $('div#contact-ContactTime').remove();

  if ($('select#select-ContactMethod').val() == 'Phone') {

   var selectElem = $('<select></select>').attr('name', 'ContactTime').attr('id', 'select-ContactTime');
   selectElem.append($('<option>0700 - 0900 hrs</option>').attr('value', '0700 - 0900 hrs'));
   selectElem.append($('<option>0900 - 1100 hrs</option>').attr('value', '0900 - 1100 hrs'));
   selectElem.append($('<option>1100 - 1300 hrs</option>').attr('value', '1100 - 1300 hrs'));
   selectElem.append($('<option>1300 - 1500 hrs</option>').attr('value', '1300 - 1500 hrs'));
   selectElem.append($('<option>1500 - 1700 hrs</option>').attr('value', '1500 - 1700 hrs'));
   selectElem.append($('<option>1700 - 1900 hrs</option>').attr('value', '1700 - 1900 hrs'));

   var labelElem = $('<label>Preferred Contact Time</label>').attr('for', 'select-ContactTime');

   var divElem = $('<div></div>').attr('class', 'field required').attr('id', 'contact-ContactTime');
   divElem.append(labelElem);
   divElem.append(selectElem);

   $('select#select-ContactMethod').parent().after(divElem);
   //$('input#input-Phone').parents('div.field').addClass('required');
   doSelection();

  }//if

/*
  if ($('select#select-ContactMethod').val() == 'Email') {
   $('input#input-Email').parents('div.field').addClass('required');
  }//if

  if ($('select#select-ContactMethod').val() == 'Post') {
   $('input#input-Address1').parents('div.field').addClass('required');
   $('input#input-Town').parents('div.field').addClass('required');
   $('input#input-County').parents('div.field').addClass('required');
   $('input#input-Postcode').parents('div.field').addClass('required');
  }//if
 */

  $('#select-ContactMethod').val($('#hidden-ContactMethod').val());
  doSelection();
 }//function

 $('select#select-ContactMethod').change(doPhone);

 doSubject = function() {
  $('select#select-EducationPreferredDestinations option').remove();
  blnReplace = false;

  $.get("ajax/get_preferred_destinations.php", { Subject: $('select#select-Subject').val() }, function(xmlData) {
   elemSelect = $("<select></select>").attr('id', 'select-EducationPreferredDestinations').attr('name', 'EducationPreferredDestinations');
   $(xmlData).find('preferreddestination').each( function() {
    elemSelect.append('<option value="' + $(this).text() + '">' + $(this).text() + '</option>');
    blnReplace = true;
   });
  });

   if (blnReplace) {
    $('#label-EducationPreferredDestinations').after(elemSelect);
    $('#select-EducationPreferredDestinations').val($('#hidden-EducationPreferredDestinations').val());
   }//if

 }//function

 doSources = function() {
  $('select#select-WhereDetail').remove();
  var blnReplace = false;

  $.get("ajax/get_source_details.php", { Source: $('select#select-Where').val() }, function(xmlData) {
   elemSelect = $("<select></select>").attr('id', 'select-WhereDetail').attr('name', 'WhereDetail');
   $(xmlData).find('sourcedetail').each( function() {
    elemSelect.append('<option value="' + $(this).text() + '">' + $(this).text() + '</option>');
    blnReplace = true;
   });

   if (blnReplace) {
    $('#label-WhereDetail').after(elemSelect);
    $('#select-WhereDetail').val($('#hidden-WhereDetail').val());
   }//if

  });
 }//function

 doTourSearch = function() {
  $('div#firstlist ul li').hover( function() {
   $(this).addClass('hover');
  }, function() {
   $(this).removeClass('hover');
  });

  $('div#firstlist div#toursearchtour ul li').click( function() {
   $('div#firstlist div#toursearchtour').find('li').removeClass('selected');
   $('div#secondlist').find('li').removeClass('selected');
   $(this).addClass('selected');
   $.get("ajax/get_tour_countries.php", { TourType: $(this).text() }, function(xmlData) {

    elemCountryList = $('div#secondlist');
    elemCountryList.empty();

    //$(xmlData).find('tourcountry').each( function() {
    // elemCountryList.append('<li><a href="' + $(this).attr('url') + '">' + $(this).text() + '</a></li>');
    //});

    $(xmlData).find('tourtype').each( function() {
     elemCountryList.append('<h3><a href="' + $(this).attr('home') + '">' + $(this).attr('type') + '</a></h3>');

     if ($(this).find('tourcountry').length > 0) {
      elemList = $("<ul></ul>");

      $(this).find('tourcountry').each( function() {
       elemList.append('<li><a href="' + $(this).attr('url') + '">' + $(this).text() + '</a></li>');
      });

      elemCountryList.append(elemList);

     }//if

    })

   });
  });

  $('div#firstlist div#toursearchcountry ul li').click( function() {
   $('div#firstlist div#toursearchcountry').find('li').removeClass('selected');
   $('div#secondlist').find('li').removeClass('selected');
   $(this).addClass('selected');
   $.get("ajax/get_tour_types.php", { Country: $(this).text() }, function(xmlData) {

    elemCountryList = $('div#secondlist');
    elemCountryList.empty();

    $(xmlData).find('tourtype').each( function() {
     elemCountryList.append('<h3><a href="' + $(this).attr('home') + '">' + $(this).attr('type') + '</a></h3>');

     if ($(this).find('tourdestination').length > 0) {
      elemList = $("<ul></ul>");

      $(this).find('tourdestination').each( function() {
       elemList.append('<li><a href="' + $(this).attr('url') + '">' + $(this).text() + '</a></li>');
      });

      elemCountryList.append(elemList);

     }//if

    });

   });
  });

 }//function

 doContact = function() {
  if ($('input#input-DoEnquiry').is(':checked')) {
   $('div#contact-enquiry').css('display','block');
  } else {
   $('div#contact-enquiry').css('display','none');
  }//if

  if ($('input#input-DoBrochure').is(':checked')) {
   $('div#contact-brochure').css('display','block');
  } else {
   $('div#contact-brochure').css('display','none');
  }//if

  if ($('input#input-DoNewsletter').is(':checked')) {
   $('div#contact-newsletter').css('display','block');
  } else {
   $('div#contact-newsletter').css('display','none');
  }//if
  doSelection();
 }//function

 if ($('input#input-DoEnquiry').length > 0) {
  doEnquiryType();

  doPhone();

  $('select#select-Subject').change(doSubject);
  doSubject();

  $('select#select-Where').change(doSources);
  doSources();

  $('input#input-DoEnquiry').click(doContact);
  $('input#input-DoBrochure').click(doContact);
  $('input#input-DoNewsletter').click(function() {
   doContact();
   $('input#input-RepeatContactYes').attr('checked', true);
  });
  doContact();
 }//if

 doTourSearch();

 if(typeof doGoogleMap == 'function') {
  doGoogleMap();
 }//if

 //Make other destination forms submit via AJAX
 $('form#form-OtherDestinations').submit( function() {

 	strErrors = '';

 	//Check fields are completed
 	if ($('form#form-OtherDestinations input#text-Name').val() == '') {
 		strErrors = strErrors +  "\r\n" + '- Your Name is a required field but was empty';
 	}//if
 	if ($('form#form-OtherDestinations input#text-EmailAddress').val() == '') {
 		strErrors = strErrors + "\r\n" + '- Email Address is a required field but was empty';
 	}//if
 	if ($('form#form-OtherDestinations textarea#textarea-Destinations').val() == '') {
 		strErrors = strErrors + "\r\n" + '- Destinations is a required field but was empty';
 	}//if

 	if (strErrors.length > 0) {
 		alert('There were problems with your form:' + "\r\n" + strErrors);
 	} else {
   $.get("ajax/process_other_destinations_form.php", { Name: $('form#form-OtherDestinations input#text-Name').val()
                                                      ,EmailAddress: $('form#form-OtherDestinations input#text-EmailAddress').val()
                                                      ,Tour: $('form#form-OtherDestinations input#hidden-Tour').val()
                                                      ,Destinations: $('form#form-OtherDestinations textarea#textarea-Destinations').val() } );

   $('div#OtherDestinationsForm').animate({
        opacity: "0"
       }, 1000 );

   $('div#OtherDestinationsThanks').animate({
       opacity: "show"
       }, 1500 );

 	}//if

 	return false;

 });

});