// JavaScript Document

$(document).ready(function(){
//Start JQuery Code

//Button Hovers
  $("input.submit").hover(
    function() {
      off_img = $(this).attr("src");
      over_img = off_img.replace("_off","_on");
      $(this).attr("src", over_img);
    },
    function() {
      $(this).attr("src", off_img);
    }
  );


// Add more traveler link
$('#addmoretravelerLink').click(function() {
  $(this).parent().parent().parent().parent().parent().append('<input type="hidden" name="addtraveler" value="1" /><input type="hidden" name="traveler2" value="0" />').submit();
  return false;
});

// Change Location
  $('a#change-location, a#change-location-select').click(function(){
    $('.select-country').css("display","block");
    showOverlay();
    return false;
  });

  $("a#close-countries").click(function(){
    $('.select-country').css("display","none");
    hideOverlay();
    return false;
  });

//Utilities

  $("a#a-print").click(function(){
    window.print();
    return false;
  });

  $("a#a-email").click(function(){
    $('.email-page').css("display","block");
    showOverlay();
    return false;
  });

  $("a#close-email-page").click(function(){
    $('.email-page').css("display","none");
    hideOverlay();
    return false;
  });

/* Print button */
$("a#a-printlink").click(function(){
    window.print();
    return false;
});

/* Fire click on calendarS when input focus */
$('#edit-departing').focus(function() {
  var fireOnThis = document.getElementById('edit-departing-button');
  if( document.createEvent ) {
    var evObj = document.createEvent('MouseEvents');
    evObj.initEvent( 'click', true, false );
    fireOnThis.dispatchEvent(evObj);
  } else if( document.createEventObject ) {
    fireOnThis.fireEvent('onclick');
  }
});
$('#edit-returning').focus(function() {
  var fireOnThis = document.getElementById('edit-returning-button');
  if( document.createEvent ) {
    var evObj = document.createEvent('MouseEvents');
    evObj.initEvent( 'click', true, false );
    fireOnThis.dispatchEvent(evObj);
  } else if( document.createEventObject ) {
    fireOnThis.fireEvent('onclick');
  }
});
$('#edit-startdate').focus(function() {
  var fireOnThis = document.getElementById('edit-startdate-button');
  if( document.createEvent ) {
    var evObj = document.createEvent('MouseEvents');
    evObj.initEvent( 'click', true, false );
    fireOnThis.dispatchEvent(evObj);
  } else if( document.createEventObject ) {
    fireOnThis.fireEvent('onclick');
  }
});
$('#edit-depositdate').focus(function() {
  var fireOnThis = document.getElementById('edit-depositdate-button');
  if( document.createEvent ) {
    var evObj = document.createEvent('MouseEvents');
    evObj.initEvent( 'click', true, false );
    fireOnThis.dispatchEvent(evObj);
  } else if( document.createEventObject ) {
    fireOnThis.fireEvent('onclick');
  }
});

//Cover/Benefits

//Coverages/Annual Products
  $('ul.yellow-list li a.is-not-link').toggle(
    function() {
      $(this).parent('li').addClass('top-li');
       $(this).siblings('div.sub-yellow-list-block').css("display","block");
    },
    function() {
      $(this).parent('li').removeClass('top-li');
       $(this).siblings('div.sub-yellow-list-block').css("display","none");
    }
  );

//Your Price
  $('div.cb-header a').toggle(
    function() {
       $('div.coverage-benefits').removeClass("collapsed");
    },
    function() {
       $('div.coverage-benefits').addClass("collapsed");
    }
  );

//Traveler Info, Payment Info, Trip Insured
  $('a.view-all').toggle(
    function() {
      id = $(this).attr('rel');
       $('table#' + id).removeClass("block-hidden");
      $(this).removeClass("collapsed");
    },
    function() {
      id = $(this).attr('rel');
       $('table#' + id).addClass("block-hidden");
      $(this).addClass("collapsed");
    }
  );

//Popups Window - Coverage Tables
  $('tr.pph-hint td.favor p').hover(
    function() {
      popupshow($(this));
    },
    function() {
      popupnoshow($(this));
    }
  );

//Popups Window - Your Price Tables
  $('tr.pph-hint td.title p').hover(
    function() {
      popupshow($(this));
    },
    function() {
      popupnoshow($(this));
    }
  );

//Popups Window - Help Buttons
  $('img.q-icon').hover(
    function() {
      popupshow($(this).parents('span.q-spacer'));
    },
    function() {
      popupnoshow($(this).parents('span.q-spacer'));
    }
  );
  
//Popups Window - Help Buttons
  $('a.popup').hover(
    function() {
      popupshow($(this).parents('span.q-spacer'));
    },
    function() {
      popupnoshow($(this).parents('span.q-spacer'));
    }
  );
//Safari Hacks
  if ($.browser.safari) {
    $('div.f-15 p.font-17').css("margin-bottom","-20px !important");
    $('.b-product p.font-17').css("margin-bottom","0 !important");
    $('.bizpacklift').css("margin-bottom","14px !important");
  }

//Search Box - Sitewide
  $("input#edit-search-block-form-keys").focus(function(){
    if($(this).val() == 'SEARCH') $(this).val('');
  });
  $("input#edit-search-block-form-keys").blur(function(){
    if($(this).val() == '') $(this).val('SEARCH');
  });

// Hack nice-menu (bouh...) We need height because IE sux...
niceMenuHeights = Array();
$('#nice-menu-1 > li > div > div.drop-down-bg > ul').each(function(i){
   niceMenuHeights[i] = $(this).parent().parent().show().css('visibility', 'visible').find('ul').show().css('visibility', 'visible').height();
   $(this).css('display', '').css('visibility', '').parent().parent().css('display', '').css('visibility', ''); // Astuce pour redonner la main au CSS
 });
$('#nice-menu-1 > li > div > div.drop-down-bg-right').each(function(i){
   $(this).height(niceMenuHeights[i]+1);
 });

 /* In admin only */
 if(nodeAssistLinkUrl = $('#node_assist-link-').attr('href')) {
  nid  = $('#edit-nid').val();
  nodeAssistLinkUrl = nodeAssistLinkUrl.replace(/TB_iframe/i, 'product_id='+nid+'&TB_iframe');
  $('#node_assist-link-').attr('href', nodeAssistLinkUrl);
 }
/* Cant add this with drupal sooo... */
/*
 if(termsUrl = $('.footer-bottom-area ul.menu li.leaf.last a').attr('href')) {
  if(termsUrl.indexOf('?', 0) == -1) {
    termsUrl = termsUrl+'?KeepThis=true&TB_iframe=true&height=200&width=400';
  } else {
    termsUrl = termsUrl+'&KeepThis=true&TB_iframe=true&height=200&width=400';
  }
  $('.footer-bottom-area ul.menu li.leaf.last a').addClass('thickbox').attr('title',$('.footer-bottom-area ul.menu li.leaf.last a').text()).attr('href', termsUrl);
  tb_init('.footer-bottom-area ul.menu li.leaf.last a');
 }*/

//News Alerts
  $("span.closealert a").toggle(
    function() {
      $(this).parents("div.subheader").siblings("ul").addClass("collapse");
      $(this).parent().addClass("openalert");
      $(this).text("Open")
    },
    function() {
      $(this).parents("div.subheader").siblings("ul").removeClass("collapse");
      $(this).parent().removeClass("openalert");
      $(this).text("Close")
    }
  );


//End JQuery Code
});

function showOverlay() {
  $('body').append('<span class="overlay"><!--[if lte IE 6.5]><iframe></iframe><![endif]--></span>');
  $('.overlay').css("opacity","0.75");
  $('.overlay').css("visibility","visible");
  if ($.browser.msie) $('.overlay').css("height",$('body').height());
  else $('.overlay').css("height",$('html').height());
  if ($.browser.safari) $('html').css("overflow","hidden");
}

function hideOverlay() {
  $('.overlay').css("opacity","0");
  $('.overlay').css("visibility","hidden");
  $('span.overlay').remove();
  if ($.browser.safari) $('html').css("overflow","scroll");
}

function getElementLeft(Elem) {
  var elem;
  if(document.getElementById) {
    var elem = document.getElementById(Elem);
  } else if (document.all){
    var elem = document.all[Elem];
  }
  xPos = elem.offsetLeft;
  tempEl = elem.offsetParent;
  while (tempEl != null) {
    xPos += tempEl.offsetLeft;
    tempEl = tempEl.offsetParent;
  }
  return xPos;
}
function getElementTop(Elem) {
  if(document.getElementById) {
    var elem = document.getElementById(Elem);
  } else if (document.all) {
    var elem = document.all[Elem];
  }
  yPos = elem.offsetTop;
  tempEl = elem.offsetParent;
  while (tempEl != null) {
    yPos += tempEl.offsetTop;
    tempEl = tempEl.offsetParent;
  }
  return yPos;
}

function popupshow(node) {
  popup = node.siblings('div.pph-content');
  popup.children('strong').before('<span class="pphtop"></span>').append('<span class="pphseparator"></span>').next('div').after('<span class="pphbottom"></span><!--[if lte IE 6.5]><iframe></iframe><![endif]-->').parent('div').css("display","block");
}

function popupnoshow(node) {
  popup = node.siblings('div.pph-content');
  $('span.pphtop').remove();
  $('span.pphseparator').remove();
  $('span.pphbottom').remove();
  popup.css("display","none");
}

