var mapInited ;

function showTestimonials(prodID) {
    /*
    * Nicer option would be top open a layer over the top of the site
    * as a 25% wide colum that fit's the screen height
    * let teh user scroll through reviews,, and close if clicked away from the box.
    */
    popurl = "/product_testimonials/?product_id="+prodID+"&product=yes"
    window.open(popurl,prodID,"width=700,height=500,scrollbars=yes,statusbar=yes,resizable=yes");
  }

  /**
  * Open the send to friend form which is an Inline page element
  */
  function openSendToFriendForm() {
    var nPos =$('#send').offset();
    window.scrollTo(0,nPos.top);
    $('#sendtoafriend').slideDown();
    var field = document.getElementById('your_name')
    field.focus()
  }

  function openContactCSForm(prodID) {
    //again facebox woudl be nicer
    popurl = "/product_customer_service/?product_id="+prodID
    window.open(popurl,"Map","width=620,height=550,scrollbars=yes,statusbar=no,address=no,location=no")
  }

  function openProductEmailForm(prodID) {
    //facebox / would be nicer
    popurl = "/product_email/?product_id="+prodID
    window.open(popurl,"Map","width=620,height=450,scrollbars=yes,statusbar=no,address=no,location=no")
  }

  /**
  * Add the current product to the wishlist
  */
  function addToWishList() {
    //do this via ajax woudl be nicer.
    document.frmWishList.submit();
  }

  /**
  * Open a third Party Car Hire website
  */
  function openCarHire(){
    url = '/redirect_carhire/?button=not_included_txt' ;
    window.open(url,'carTrawler','width=650,height=660,resizable,scrollbars=yes');
  }




  /**
  * Tabs Toggle
  */

  function showTab(nTabId) {
    //first remove class names for all the tabs
    for(n=1;n<6;n++) {
      if (document.getElementById('tab'+n)!== null) {
      document.getElementById('tab'+n).className='';
      }
    }
    //now set the class name for the current tab
    document.getElementById('tab'+nTabId).className='vis';

    //now hide the old content
    for(n=1;n<6;n++) {
      if (document.getElementById('tab'+n)!== null) {
      document.getElementById('tabcontent'+n).className='hid';
      //alert(document.getElementById('tabcontent'+n).className);
     }
    }
    document.getElementById('tab'+nTabId).className= 'current';
    document.getElementById('tabcontent'+nTabId).className='current';


    document.getElementById('tabcontent'+nTabId).focus();

    //Tab 5 is resort guides, intiate the callGuides function
    if (nTabId==1 && mapInited!=1) {
      //initABMap();
    }

    //if (nTabId==2) {
      //top.frames['ab_calendar_iframe'].initialiseiFrameHeight();
      //top.frames['ab_calendar_iframe'].initCalendarPage();
      //scrolltimer = setTimeout("MoveToTabs()",300);
    //} else {
      MoveToTabs();
    //}

    //Tab 5 is resort guides, intiate the callGuides function
    if (nTabId==5) {
      callGuides();
    }
    return false;
  }

  /**
   *
   * @access public
   * @return void
   **/
  function MoveToTabs(){
    var nPos = $('#tabs-container').offset();
    window.scrollTo(0,nPos.top);
    return true;
  }

  function jumpToFlightSection() {
    //show tab 2
    showTab(2)
    //scroll to correct part
    window.clearTimeout(scrolltimer) ;
    var nPos =$('#tabs-container').offset();
    nPos = nPos.top + 760
    window.scrollTo(0, nPos);
    //open section 4
    oFrame = document.getElementById('ab_calendar_iframe') ;
    oFrame.contentWindow.document.getElementById('matchflightsY').checked = 'checked' ;
    oFrame.contentWindow.slideOpen('flightfinderformcontainer',300);
  }


  var videoshown = false;

  /**
  * Open hidden divs and show the video flash player
  **/
  function ShowVideoButton() {
    document.getElementById('video-div-container').style.display='block';
    document.getElementById('video-blanket').style.display='block';
    document.getElementById('video').style.display='block';
    document.getElementById('currency_mode').style.display='none';

    FlashMaker();

    if (videoshown==false) {
      var docHeight  = document.height || document.body.offsetHeight;
      docHeight = docHeight + 50;
      document.getElementById('video-blanket').style.height=docHeight+'px'
      document.getElementById('video-blanket').style.width=screen.width;
      videoshown = true;
    }
    return false;
  }//end function

  /**
  * Hide the video divs again
  **/
  function hideVideo()  {
    document.getElementById('video').style.display='none';
    document.getElementById('video-blanket').style.display='none';
    document.getElementById('video-div-container').style.display='none';
    document.getElementById('currency_mode').style.display='inline';
    document.getElementById('placeholder').innerHTML='';
    return false;
  }


  /** SEND TO FRIEND **/
    function CheckSendToFriend(oForm) {
      if (oForm.your_name.value == '') {
        alert('Please enter your name');
        oForm.your_name.focus();
        return false;
      }
  		if (!IsEmailValid(oForm.your_email)) {
        alert('Please enter your email address');
        oForm.your_email.focus();
        return false;
      }
      if (oForm.message.value == '') {
        alert('Please enter your message');
        oForm.message.focus();
        return false;
      }
    }
  	function IsEmailValid(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))
  	}



  	/**
  	* Used for fading the Gallery Images in and Out
  	*/
    var aInvisible = new Array();
        aInvisible[0] = 'visible';
        aInvisible[1] = 'visible';
        aInvisible[2] = 'visible';
        aInvisible[3] = 'visible';
		function productfader(nId,out){
		  var oObj = '#imglist'+nId ;
		  if (out==true){
  		  if (aInvisible[nId] == 'invisible'){
	  	    return;
		    }
		    $(oObj).fadeOut();
	    } else {
  	    aInvisible[nId] = 'visible';
		    $(oObj).fadeIn();
  	  }
	  }


  /**
  * Flight Enquiry page is affiliate dependant
  */
  function openFlightEnquiry(nProduct) {
    url = '/redir/flights/?button=not_included_txt&product='+nProduct ;
    window.open(url,'FlightEnquiryForm','width=800,height=600,resizable,scrollbars=yes');
  }



/*****************************************************************************
* This is needed to ensure that the gallery works. It must not be removed
*
**/

  // These are used to decide whether it's ok to go to the previous/next pages or not
   var bNextOK = true;
   var bPrevOK = false;
   var nOffset = 0; // Offset

/***
*  THis is the function that calls in the PHP and gets the array back.
*/
  function changeGallerySelection(bNext){

    if (bNext==true){
      if (bNextOK==false){
        return;
      }
      nOffset = nOffset + 4;
    } else {
      if (bPrevOK==false){
        return;
      }
      nOffset = nOffset - 4;
    }
  if (nOffset < 0){
    nOffset = 4;
    return;
  }
     url = '&product_id='+nProductId+'&offset='+nOffset;
     $.get('/ajax/Updategallery/'+url,'',UpdateGalleryIcons);
  //ajaxObj.call('action=UpdateGallery'+url,UpdateGalleryIcons);

  }

/**
 *
 * @access public
 * @return void
 **/
function changeGallerySelectionPackage(bNext){

    if (bNext==true){
      if (bNextOK==false){
        return;
      }
      nOffset = nOffset + 4;
    } else {
      if (bPrevOK==false){
        return;
      }
      nOffset = nOffset - 4;
    }
  if (nOffset < 0){
    nOffset = 4;
    return;
  }
     url = '&accommodation='+nAccommodation+'&activity='+nActivity+'&package='+nPackage+'&offset='+nOffset;
     $.get('/ajax/UpdateGalleryPackage/'+url,'',UpdateGalleryIcons);
  //ajaxObj.call('action=UpdateGalleryPackage'+url,UpdateGalleryIcons);
}

/***
* THis deals with the server response, repopulating the images
**/

  var oGlobalResponse;
  function UpdateGalleryIcons(oResponse){
    var sExtra = '';
    oGlobalResponse = oResponse;
    var nDelay = 490;
    var sDebug = '';

  for (nId = 0;nId<4;nId++){
    productfader(nId,true);
    setTimeout('ChangeOne('+nId+')',nDelay);
    nDelay = nDelay + 200;
  }
  if (oResponse['aImages'][0]['type']=='video') {
    setTimeout('ShowVideoButton()',nDelay+250);
  } else {
    setTimeout('ChangeMainProductImage(0)',nDelay+250);
  }

  var oNext = document.getElementById("gallery_nav_next");
  var oPrev = document.getElementById("gallery_nav_prev");

 if (oResponse['bNext']==true){
    oNext.className ='gallerynav active';
    var sNext = '/images/gallery_next.gif';
    bNextOK = true;
  } else {
    oNext.className ='gallerynav';
    var sNext = '/images/gallery_next_grey.gif';
    bNextOK = false;
  }
  if (oResponse['bPrev']==true){
    oPrev.className ='gallerynav active';
    var sPrev = "/images/gallery_prev.gif";
    bPrevOK = true;
  } else {
    oPrev.className ='gallerynav';
    var sPrev = "/images/gallery_prev_grey.gif";
    bPrevOK = false;
  }

    setTimeout('ChangeArrows("gallery_nav_next","'+sNext+'")',nDelay + 400);
    setTimeout('ChangeArrows("gallery_nav_prev","'+sPrev+'")',nDelay + 400);

    //setTimeout(oPrev.src = sPrev,nDelay);

  }

  /**
   *
   * @access public
   * @return void
   **/
  function ChangeArrows(sObj,sSrc){
    document.getElementById(sObj).src = sSrc;
  }

/****
* This controls changing one image, based on ['aImages'][i] in oGlobalResponse
**/
  function ChangeOne(i){


    //alert(typeof(oGlobalResponse['aImages'][i]));

    var sString = '';
    if (oGlobalResponse['aImages'][i]!=null){
          Images = oGlobalResponse['aImages'][i];

          aInvisible[i]='visible';
          if (Images['type']=='video') {
            sHTML = '<img id="thumbnail'+Images['num']+'" src="'+Images['image']+'" width="30" height="30" onmouseover="ShowVideoButton('+Images['num']+')"  alt="'+Images['description']+'"  title="'+Images['description']+'"/>';
          } else {
            sHTML = '<img id="thumbnail'+Images['num']+'" src="'+Images['image']+'" width="30" height="30" onmouseover="ChangeMainProductImage('+Images['num']+')"  alt="'+Images['description']+'"  title="'+Images['description']+'"/>'
          }
          MakeChange(i,sHTML);
          productfader(i,false);
          /* setTimeout('MakeChange("'+i+'","'+sHTML+'")',500);
          setTimeout('productfader("'+i+'",false)',400); */
          document.getElementById('thumbnail'+i).style.cursor = 'pointer';
        } else {
          document.getElementById('thumbnail'+i).style.cursor = 'auto';
          aInvisible[i]='invisible';
        }

  }


  function MakeChange(i,sHTML){
    var oList = document.getElementById('imglist'+i);
    oList.innerHTML = sHTML;
  }

  function ShowVideo(){
    if (document.getElementById('imgvideobutton')!== null) {
      document.getElementById('imgvideobutton').style.display = 'none';
      document.getElementById('main_image').style.display = 'none';
      document.getElementById('imgvideoouter').style.display = 'block';
    }
  }

  function ShowVideoButton(){
    document.getElementById('main_image').style.display = 'none';
    if (document.getElementById('imgvideoouter')!== null){
      document.getElementById('imgvideoouter').style.display = 'none';
    }
    document.getElementById('imgvideobutton').style.display = '';
    document.getElementById('thumbnail'+nPrevImg).className ='';
    document.getElementById('thumbnail0').className ='selected';
    nPrevImg = 0;

  }

  var nPrevImg = 0;
  function ChangeMainProductImage(nCurImg){
    if (aInvisible[nCurImg]=='invisible') {
      return;
    }

    var sSrc = document.getElementById('thumbnail'+nCurImg).src;

    sSrc = sSrc.replace('/rs-30x30/','/rs-200x200/');
    var sAlt = document.getElementById('thumbnail'+nCurImg).alt;

    //  UnSelectProductImages();
    document.getElementById('thumbnail'+nPrevImg).className ='';
    document.getElementById('thumbnail'+nCurImg).className = 'selected';
    nPrevImg = nCurImg;
    document.getElementById('main_image').src = sSrc;
    document.getElementById('main_image').alt = sAlt;

    if (document.getElementById('imgvideobutton')!=null) {
    document.getElementById('imgvideobutton').style.display = 'none';
    }

    if (document.getElementById('imgvideoouter')!= null){
      document.getElementById('imgvideoouter').style.display = 'none';
    }
    document.getElementById('main_image').style.display = 'block';
    document.getElementById('main_image').description = sAlt;
  }
