$(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) {
 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", "125");

  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;
}//function

// 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;
}//function

//Contact form stuff
doRequiredFields = function() {
 $('body#pageContact 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#pageContact 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

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);
  doSelection();
 }//if

 //$('#select-ContactMethod').val($('#hidden-ContactMethod').val());
 doSelection();
}//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();
 doContentPanel();
}//function

//Do destination tabs
function toggleTabs(strDivID) {
 $('div#contentinner.tabs div#intro').css('display', 'none');
 $('div#contentinner.tabs div#accommodation').css('display', 'none');
 $('div#contentinner.tabs div#tour').css('display', 'none');
 $('ul#tabs').removeClass().addClass(strDivID);

 $('div#contentinner.tabs div#' + strDivID).css('display', 'block');
}//function

//Font Size
function checkFontSize(strFontSize, intLimit, intNewSize) {
 if (parseFloat(strFontSize, 10) > intLimit) {
  strFontSize = intNewSize + 'px';
 }//if
 return strFontSize;
}//function

doContentPanel = function() {
 $('div#contentpanel').css('height', 'auto');
 intHeightContent = parseInt($('div#content').height());
 $('div#contentpanel').height(intHeightContent);
}//function

$(document).ready(function() {

 //Add link to main logo
 var aHomeURL = $('div#breadcrumb img.home').parent('a').attr('href');
 var aHome = $('<a></a>').attr('href', aHomeURL);
 $('div#banner h1').wrap(aHome);

 //Add an arrow separator to the breadcrumb trail
 if ($('body#pageGuides').length > 0) {
  var imgGuidesArrow = $("<img>").attr('src','images/breadcrumb/arrow_guides.png').attr('width','4').attr('height','12').attr('class','separator');
  $('body#pageGuides div#breadcrumb img.arrow').each( function() {
   $(this).before(imgGuidesArrow);
  });
 } else if ($('body#pageTrefoil').length > 0) {
  var imgTrefoilArrow = $("<img>").attr('src','images/breadcrumb/arrow_trefoil.png').attr('width','4').attr('height','12').attr('class','separator');
  $('body#pageTrefoil div#breadcrumb img.arrow').each( function() {
   $(this).before(imgTrefoilArrow);
  });
 } else {
  var imgScoutsArrow = $("<img>").attr('src','images/breadcrumb/arrow_scouts.png').attr('width','4').attr('height','12').attr('class','separator');
  $('body div#breadcrumb img.arrow').each( function() {
   $(this).before(imgScoutsArrow);
  });
 }//if

 //Font sizes
 var originalFontSize = checkFontSize($('body').css('font-size'), 20, 15);
 var fontIncrement = 0.1;

 // Increase Font Size
 $('area#textsizebigger').click(function(){
  var currentFontSize = checkFontSize($('body').css('font-size'), 30, 30);
  var currentFontSizeNum = parseFloat(currentFontSize, 10);
  var newFontSize = currentFontSizeNum * (1 + fontIncrement);
  $('body').css('font-size', newFontSize);
  $.cookie('fontsize', newFontSize, {path: '/'});
  doContentPanel();
  return false;
 });
 // Decrease Font Size
 $('area#textsizesmaller').click(function(){
  var currentFontSize = checkFontSize($('body').css('font-size'), 30, 30);
  var currentFontSizeNum = parseFloat(currentFontSize, 10);
  var newFontSize = currentFontSizeNum * (1 - fontIncrement);
  $('body').css('font-size', newFontSize);
  $.cookie('fontsize', newFontSize, {path: '/'});
  doContentPanel();
  return false;
 });

 //Restore any previous font size
 if ($.cookie('fontsize')) {
  $('body').css('font-size', $.cookie('fontsize') + 'px');
 }//if

 $('ul#tabs li#tabIntro').click( function() {toggleTabs('intro');} );
 $('ul#tabs li#tabAccommodation').click( function() {toggleTabs('accommodation');} );
 $('ul#tabs li#tabTour').click( function() {toggleTabs('tour');} );

 //Show intro tab by default
 toggleTabs('intro');
 $('ul#tabs li').hover( function() {$(this).addClass('hover');}, function() {$(this).removeClass('hover');} );

 //Make FAQ questions clickable
 $('div#content div.question h4').each( function() {

  $(this).addClass('clickable');

  if ($(this).parent('div.question').hasClass('collapsed')) {
   $(this).parent('div.question').children('*:not(h4)').each( function() {$(this).css('display', 'none');});
  }//if

  $(this).click( function() {
   if ($(this).parent('div.question').hasClass('collapsed')) {
    $(this).parent('div.question').removeClass('collapsed');
    $(this).parent('div.question').children().each( function() {$(this).css('display', 'block');});
   } else {
    $(this).parent('div.question').addClass('collapsed');
    $(this).parent('div.question').children('*:not(h4)').each( function() {$(this).css('display', 'none');});
   }//if
   doContentPanel();
  });

 });

 //Make map clickable
 $('img#map').click( function() {
  loadImage('images/map_large.gif');
 });
 $('img#magnifying_glass').click( function() {
  loadImage('images/map_large.gif');
 });

 //Make photo comps clickable
 $('div.photocomp a').click( function() {
  loadImage($(this).attr('href'));
  return false;
 });

  //Email to a friend stuff
 $('li#shortcutEmail a').click( function() {
  showElement('panelFriend');
  return false;
 });

 $('div#panelFriend img.closebutton').click( function() {
  hideElement('panelFriend');
 });

 //Newsletter signup stuff
 $('input#NewsletterEmailAddress').focus( function() {
  if ($(this).val() == 'your email') {
   $(this).val('');
  }//if
 });

 $('input#NewsletterButton').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#NewsletterEmailAddress').val());
  return false;
 });

 $('div#panelNewsletter img.closebutton').click( function() {
  $('div#banner embed').show();
  $('div#banner object').show();
  $('div#banner img#placeholder').hide();

  hideElement('panelNewsletter');
 });

 $('select#select-ContactMethod').change(doPhone);

 if ($('input#input-DoEnquiry').length > 0) {
  doPhone();
  $('input#input-DoEnquiry').click(function() {
   doContact();
  });
  $('input#input-DoBrochure').click(function() {
   doContact();
  });
  $('input#input-DoNewsletter').click(function() {
   doContact();
   $('input#input-RepeatContactYes').attr('checked', true);
  });
  doContact();
 }//if

 //Make content panel as high as the content div
 doContentPanel();

});

