/**
  This file used to contian the Behaviour v1.1 script by Ben Nolan,
  but all of the rules have since been replaced with jQuery functions
  instead. This file now serves as a global loader for javascript functions
  that should be run once the document is fully loaded.
**/

$(document).ready(function(){
  /* Setup accordion effect on main navigation sections */
  $('.navcordion').each(function(){
    var h2 = $(this).find('h2:first');
    if (h2.children('a').length == 0)
      h2.html('<a href="#">' + h2.html() + '</a>');
    h2.find('a').click(function(event){
      event.preventDefault();
      $('.navcordion').each(function(){
        $(this)
          .removeClass('nav_current')
          .find('ul:first').hide();
      });
      $(this).parents('.navcordion')
        .addClass('nav_current')
        .find('ul:first').show();
    });
  });
  
  /* Setup product category hover menus */
  $('#nav ul.productNav > li').each(function(){
    $(this).mouseover(function(){ $(this).addClass('over'); });
    $(this).mouseout(function(){ $(this).removeClass('over'); });
  });
  
  /* Open all Privacy Policy links in a new window */
  $('a[href=/company/privacy.html]').each(function(){
    $(this).click(function(event){
      event.preventDefault();
      window.open('/company/privacy.html', 'Privacy', 'resizable=no,menubar=no,location=no,toolbar=no,status=no,scrollbars=yes,directories=no,width=400,height=400');
    });
  });
  
  /* Open all Legal Notice links in a new window */
  $('a[href=/company/legal.html]').each(function(){
    $(this).click(function(event){
      event.preventDefault();
      window.open('/company/legal.html', 'HittiteCopyright', 'resizable=no,menubar=no,location=no,toolbar=no,status=no,scrollbars=yes,directories=no,width=500,height=450');
    });
  });

  /* Set a cookie to control the nav scructure on the next load */
  $('a.nav_coerce').each(function(){
    $(this).click(function() {
      createCookie('nav_coerce', $(this).attr('rel'), 0);
    });
  });
  
  /* Remove the cookie if another link is clicked */
  $('a:not(.nav_coerce)').each(function(){
    $(this).click(function(){ eraseCookie('nav_coerce'); });
  });
  
  /* If there is a cookie, modify the nav scructure */
  var nav_coerce = readCookie('nav_coerce');
  if (nav_coerce) {
    var nav_coerce = $('#navcordion_' + nav_coerce);
    if (nav_coerce.length) {
      nav_coerce.find('h2:first > a:first').click();
    }
  }
});

// Cookie functions courtesy of http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

