$(document).ready(function(){

 $("a[rel=facebox]").facebox() ;

 $('#reference-search-product').focus(function(){clearMe($(this),'product ref')})
 $('#reference-search-product').blur(function(){fillMe($(this),'product ref')});

 $('#spemail').blur(function(){fillMe($(this),'Enter Email Address')}).focus(function(){clearMe($(this),'Enter Email Address')});

 //external links open in new window
 $('a[rel=external]').click(function(){
   var url = $(this).attr('src');
   window.open(url,'abexternal');
  });

});

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

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

/**************************************************
* LAYER ANIMATION HIDE/SHOW
**************************************************/

// Alter the alpha transparency of a layer.
function alphaMode(nMode) {

  var nOpacity = nMode==1 ? 1 : 0.2 ;
  $('#site-container').css('opacity',nOpacity);


  //IE is stupid - so make ALLOWANCES
	if (nMode==0) {
	  prepareIE('200%', 'auto') ;
	  hideSelects('hidden');
	} else {
	  prepareIE('auto', 'auto') ;
	  hideSelects('visible');
	}

  //Display the blackout layer
  var sDisplay = nMode==0 ? 'block' : 'none' ;
  $('#overlay').css('display',sDisplay) ;

  //prevent users clicking on anything in our faded container
  if (nMode==0) {
    toggleLinksByElement('site-container',0);
  }else{
    toggleLinksByElement('site-container',1);
  }
}

/* Used by the Alpha Mode Function Above */
function prepareIE(height, overFlow) {
	bod = document.getElementsByTagName('body')[0];
	bod.style.height = height;
	//bod.style.overflow = overFlow;

	htm = document.getElementsByTagName('html')[0];
	htm.style.height = height;
	htm.style.overflow = overFlow;
}

function hideSelects(visibility){
    $('select').each(function(){$(this).css('visibility',visibility)});

		//also toggle the visibility of the 'scrollingflashpromotions'
    $('#scrollingflashpromotions').css('visibility',visibility );

}

/**
* Disable/Enable Links in an alpha faded layer
*/

var anchors = new Array();
function toggleLinksByElement(el,blEnable) {

  if (document.getElementById && document.getElementsByTagName) {
    if (typeof(el) == 'string') {
      el = document.getElementById(el);
    }
    anchors = el.getElementsByTagName('a');
    for (var i=0, end=anchors.length; i < end; i++) {
      if (blEnable==0) {
        //remember any onclick events so that they can be reset when we re-enable the links
        anchors[i].oldonclick = anchors[i].onclick;
        anchors[i].onclick = function() {
          return false;
        };
      } else {
        //re-enable old on click events.
        anchors[i].onclick = anchors[i].oldonclick  ;
      }
    }
  }
}

function closeQuick() {
  //hide our container div.
  oCon = document.getElementById('quick-container') ;
  oCon.style.display = 'none'  ;
  document.getElementById('main_search_form').style.display = 'block';
  // make The Page look nice and bright again
  alphaMode(1);
}

function closeDiv(divID) {
  if ($(divID).length)  {
    $(divID).css('display','none');
  }
}

function getScrollXY() {
  var scrollLeft = 0;
  var scrollTop = 0;

  if(document.documentElement && typeof(document.documentElement.scrollLeft) == 'number') {
    scrollLeft = document.documentElement.scrollLeft;
    scrollTop = document.documentElement.scrollTop;
  } else {
    scrollLeft = document.body.scrollLeft;
    scrollTop = document.body.scrollTop;
  }

  return [scrollLeft, scrollTop];
}

/***************************************************
* SEARCH FORM
****************************************************/
var searchFading = 0
var alphaState = 0;
var opacityState = 0;
var searchFader ;
var dir = 'in' ;
function toggleSearch() {
  //get current status
  if (searchFading==1){
    return false;
  }
  if (alphaState>=100 && opacityState>=1) {
    // Instead of Hiding it Highlight it
    searchHighlighter   = window.setTimeout('highlighter(\'search-form\')',1);
  } else if ((alphaState<=1 && opacityState<=0.1)){
    alphaState = 100;
    opacityState = 1;
    $('#search-form').fadeIn('slow');
    //$('#search-form').css('opacity',1);
  }
  return false;
}

// Highlight the specified object by changing it's background to orange and then back to white again
var highlighterColor   = 'EA710E' ;
var highlighterCounter = 0 ;
var aHighlights = Array('EA710E','FA790F','FF9722','FFA415','FEFEFE','FDFDFD','FA790F','FF9722','FFA415','FFFFFF') ;
function highlighter(oObj) {
  oObj = document.getElementById(oObj) ;

  //first change the background color to the one chosen
  oObj.style.background = '#'+highlighterColor ;

  if (highlighterColor!='FFFFFF' || highlighterCounter<=6) {
    highlighterColor = aHighlights[highlighterCounter] ;
    highlighterCounter++ ;
    searchHighlighter   = window.setTimeout('highlighter(\'search-form\')',3);
  }else {
   window.clearTimeout(searchHighlighter)
   highlighterCounter = 0 ;
   highlighterColor   = 'EA710E' ;
   oObj.style.background = 'none' ;
  }
}

/*******************************************************
* MISCELLANEOUS
*******************************************************/
function getElementsByClassName(strClass, strTag, objContElm) {
  strTag = strTag || "*";
  objContElm = objContElm || document;
  var objColl = objContElm.getElementsByTagName(strTag);
  if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
  var arr = new Array();
  var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
  var arrClass = strClass.split(delim);
  for (var i = 0, j = objColl.length; i < j; i++) {
    var arrObjClass = objColl[i].className.split(' ');
    if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
    var c = 0;
    comparisonLoop:
    for (var k = 0, l = arrObjClass.length; k < l; k++) {
      for (var m = 0, n = arrClass.length; m < n; m++) {
        if (arrClass[m] == arrObjClass[k]) c++;
        if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
          arr.push(objColl[i]);
          break comparisonLoop;
        }
      }
    }
  }
  return arr;
}

// To cover IE 5.0's lack of the push method
if (window.Array && !window.Array.prototype.push){
  Array.prototype.push = function(value) {
    this[this.length] = value;
  }
}







/**
* Quick Jump To Product By ID
*/
function isPackage() {
  oObj = document.getElementById('frmProdRef') ;
  oObj.name = 'frmProdRef' ;
  message = 'Please enter a valid product reference number. e.g. 1234 or pkg1234'
  if (document.getElementById('reference-search-product').value=='') {
      alert(message);
      return false;
  }

  if (document.getElementById('reference-search-product').value.substring(0,3)=='pkg')  {
    if (isNaN(document.getElementById('reference-search-product').value.substring(3))) {
      alert(message);
      return false;
    }

    sUrl = '/package/'
    document.getElementById('reference-search-product').name = 'package' ;
    document.getElementById('reference-search-product').value  =document.getElementById('reference-search-product').value.substring(3)
  } else  {
        if (isNaN(document.getElementById('reference-search-product').value)) {
      alert(message);
      return false;
    }

    sUrl = '/product/';
    document.getElementById('reference-search-product').name = 'product' ;
  }


  oObj.action = sUrl
  return true;

}


/**
* clears an input box of its default text onfocus
*/
function clearField(oObj,str) {
  if (oObj.value==str) {
    oObj.value='';
    oObj.style.color='#000000' ;
  }
}

function openSurveyWindow(){

var OpenWindow ="/survey/";
   window.open(OpenWindow,'Survey','height=500,width=760,resizable,scrollbars=yes,status=yes');
   return false;
}

/**
 *
 * @access public
 * @return void
 **/
function OpenHelp(){

var OpenWindow ="http://support.activitybreaks.com/live_support/request_email.php?l=activitybreaks&x=1&deptid=0";
   window.open(OpenWindow,'SupportWindow','height=300,width=500,resizable,scrollbars=yes,status=yes');
   return false;
}

/**
 *
 * @access public
 * @return void
 **/
function LonelyPlanetLink(){

var OpenWindow ="http://www.lonelyplanet.com/bookings/why_these_providers.jsp";
   window.open(OpenWindow,'LonelyPlanetAB','height=500,width=680,resizable,scrollbars=yes,status=yes');
   return false;
}

function toggleDisplay(elId) {
  curDisp = $(elId).css('display')
  newStyle = curDisp =='none' ? 'block' : 'none' ;
  $(elId).css('display',newStyle) ;
}

/**
* popUp the testimonials window
**/
function showTestimonials(prodID) {
  popurl = "/product_testimonials/?product_id="+prodID+"&product=yes"
   window.open(popurl,prodID,"width=700,height=500,scrollbars=yes,statusbar=yes,resizable=yes");
}

/**
 *
 * @access public
 * @return void
 **/
var spRan = false;
function clearspval(){

if (spRan==false) {
  document.getElementById('spemail').value='';
  spRan = true;
}


}

 function EmailValid(oEmail) {
   email = oEmail.value
   r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)")
   r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$")
   return (!r1.test(email) && r2.test(email))
 }
/**
 *
 * @access public
 * @return void
 **/
function checksbMail(){
  if (!EmailValid(document.getElementById('spemail'))) {
    alert('Please enter a valid email address');
    return false;
  }
   return true;
}

var nSubCatId = 0;

/**
 *
 * @access public
 * @return void
 **/
function SetSubCatId(n){
  nSubCatId = n;
}

function ShowCategoryList(nId,sCatName){
  $('#main-sub-cats').html('<img src="/img/ajaxloadersubcat.gif" />');
  $('#main-sub-cats').css('display','block');
  //ajaxObj.call('action=populateSubCatDiv&category='+nId+'&subcat='+nSubCatId,ShowCategoryListFinish);
  $.getJSON('/ajax/populateSubCatDiv',{category:nId,subcat:nSubCatId,category_name:sCatName},ShowCategoryListFinish);

  return false;
}

function ShowCategoryListFinish(aResponse){
  $('#catdiv > ul > li').removeClass('current');
  var curTab = '#catli'+aResponse.id;
  $(curTab).addClass('current');
  $('#main-sub-cats').html(aResponse['html']) ;
}
