// FILE: main.js
// AUTH: Unknown
// DESC: main JS file for the Vitonomy Site
// LOG:
//   2011-07-22  GH  cleaned up file; fixed footer-9 issues (was
//                   pushing content way to far to the right when
//                   hovered) 


jQuery(document).ready(function(){
  var $ = jQuery.noConflict();
  $('#header-3 li.signOut a').text('Sign In/Out');
  var version = $.browser.version;
  // check for IE
  if($.browser.msie) {
	  
    if(version == '8.0')
      $('body').addClass('ie8');
    else if(version == '9.0')
      $('body').addClass('ie9');
  }

  // check for Safari
  if($.browser.safari)
    $('body').addClass('safari');

  // check for chrome
  if($.browser.chrome)
    $('body').addClass('chrome');

  // check for mozilla
  if ( $.browser.mozilla) {
    if(version == '3.6')
      $('body').addClass('moz36');
  }

  $('li#globalNavItem1 a.mainNode').attr('href','/');

  // CHANGED: 2011-07-22 GH
  // clicking "About Us" should redirect user to "Our Company"
   $('li#globalNavItem3 a.mainNode').attr('href','/info/Our_Company');
  $('li#globalNavItem3 ul li a').eq(0).attr('href',
					    '/info/Our_Company');
  $('li#globalNavItem3 ul li a').eq(1).attr('href',
					    '/info/Our_Science');
  $('li#globalNavItem3 ul li a').eq(2).attr('href',
					    '/info/Our_values');
  $('li#globalNavItem3 ul li a').eq(3).attr('href',
					    '/info/contactUs');

//change helpLink to faq
var helpLink = $('li.help a');
helpLink.click(function(e) {
  e.preventDefault();
  var url = 'http://www.vitonomy.com/info/FAQ';
  window.location = url;
});





  // dropdown behavior for "Wellness Products"
  $('#globalNavItem2 a.mainNode').hover(function() {
    // ADDED: 2011-07-25 GH 
    // fix absolute positioning issues in IE by forcing the left
    // to the proper place
    $('#footer-9').css('margin-left', '0px');
    var homeLeft = $('#globalNavItem1').offset().left;
    var spacing = 28;    
    $('#footer-9').css('left', homeLeft + spacing);
    $('#footer-9').show();
  });


// dymanic left nav and top dropsown

/*$('#bottom-9 .miniDetailTargetImage, #bottom-10 .miniDetailTargetImage').remove();
$('#bottom-9 .productHeader, #bottom-10 .productHeader').remove();
$('#bottom-9 .attributeList, #bottom-10 .attributeList').remove();
var wholebodyhtml = $('#bottom-10').html();
//alert(wholebodyhtml);
var targetedwellnesshtml = $('#bottom-9').html();
$('#wholebody').append($('#bottom-10'));
$('#targetedwellness').append($('#bottom-9'));
$('#wholebodyleft').html(wholebodyhtml);
$('#targetedwellnessleft').html(targetedwellnesshtml);*/


  // CHANGED: 2011-07-25 GH
  // made the hover behavior more robust; to add new items that
  // should hide the dropdown, just add their selector to the list
  // $("#globalNavItem1 a.mainNode, #globalNavItem3 a.mainNode,
  // #globalNavItem3 a.mainNode").hover(function () {
  //	$("#footer-9").hide();
  //    });

 var hideWellnessDropdown = [
    '#globalNavItem1 a.mainNode',
    '#globalNavItem3 a.mainNode',
    '#globalNavItem4 a.mainNode'
  ];

  for(var i in hideWellnessDropdown) {
    $(hideWellnessDropdown[i]).hover(function() {
      $('#footer-9').hide();
    });
  }

  $('#footer-9').hover(function() {
    $('#globalNavItem2').addClass('navigationHover');
  });

  $("#footer-9").mouseleave(function() {
    $("#footer-9").hide();
    $("#globalNavItem2").removeClass('navigationHover');
  });
 




/*var mylist = $('#footer-9 #wholebody ul.productList');
var listitems = mylist.children('li').get();
listitems.sort(function(a, b) {
   var compA = $(a).text().toUpperCase();
   var compB = $(b).text().toUpperCase();
   return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
})
$.each(listitems, function(idx, itm) { mylist.append(itm); });


var mylist2 = $('#footer-9 #targetedwellness ul.productList');
var listitems2 = mylist2.children('li').get();
listitems2.sort(function(a, b) {
   var compA2 = $(a).text().toUpperCase();
   var compB2 = $(b).text().toUpperCase();
   return (compA2 < compB2) ? -1 : (compA2 > compB2) ? 1 : 0;
})
$.each(listitems2, function(idx, itm) { mylist2.append(itm); });*/










/*var maxq = jQuery('#bottom-1').html();

var maxq = jQuery.trim(maxq);

var maxq = maxq.substring(425,445);
var maxq = maxq.replace('uantityLimit":', '');
var maxq = maxq.replace(',', '');
var maxq = maxq.replace('"For', '');

alert(maxq);
*/

  // mini cart
 $('p.itemsInCart').after('<p id="cartd"> </p>');

$('p#cartd').append($('span.itemCount'));
  $('p#cartd').append(' item(s)');
 $('p.itemsInCart').remove();

  // CHANGED: 2011-07-25 GH
  // made this append statement more robust (it's a hardcoded URL;
  // seems to me like it should be stored in a var, at least)
  // $('p#cartd').append(" item(s) <a href='/cart'><img
  // src='https://images-na.ssl-images-amazon.com/images/I/01TNbRw5E0L.png'
  // /></a>");	
//  var cartUrl = 'https://images-na.ssl-images-amazon.com/images/I/01TNbRw5E0L.png';
//  var cartImg = $('<img>').attr('src', cartUrl);
//  var cartLink = $('<a />').attr('href','/cart').append(cartImg);
 // $('p#cartd').append(cartLink);

  $('#header-7 div.viewCart').hide();

  // check contact form upon submission
  $('#formcontact').submit(function() {
    return checkform(this);
  });




var thisPage = location.href.substring((location.href.lastIndexOf("/"))+1),
url = window.location.href.split("?")[0],
thisURL = url.split("/"),
thisSubPage = thisURL[3].substring(0,11);
if(thisSubPage == "cartPreview")
{
window.location = "/cart";
};




if ($.browser.safari) {
    // handle safari specific style stuff
    $('button.primaryAction').attr('style','margin-top: 2px;');
    $('button.primaryAction span, .miniDetailPage div.com-amazon-webstore-ProductBuyBox-3 .buyBox form fieldset ul.buttonGroup.buyBoxActions button.primaryAction span').attr('style','margin-top: -1px;');    
  };
  
  
  var is_chrome = /chrome/.test(navigator.userAgent.toLowerCase());
  if(is_chrome == true) {
    $('button.primaryAction span, .miniDetailPage div.com-amazon-webstore-ProductBuyBox-3 .buyBox form fieldset ul.buttonGroup.buyBoxActions button.primaryAction span').attr('style','margin-top: 0;');
  }
});


function checkform(form) {
  if ($("#formcontact #email").val() == "") {
    alert( "Please enter your email address." );
    form.email.focus();
    return false;
  }
  return true;
}

