/* Global javascript definitions */
$(document).ready(function() {
	
	initEvents();	
//	preloadCssImages("tide");

});

/*---------------------*/
/* Event Assignments   */
/*-------------------- */

// assign general link events
function initEvents (){
	
	$('#navigation li').hover(
		function() {
			$(this).addClass('on');
		},
		function() {
			$(this).removeClass('on');
		}
	);
	
	$('#navigation li:last').hover(
		function() {
			$('#navigation ul').css('background','transparent url(/images/en-US/_global/nav-bg.png) no-repeat scroll right bottom');
		},
		function() {
			$('#navigation ul').css('background','transparent url(/images/en-US/_global/nav-bg.png) no-repeat scroll right top');
		}
	);
	
	$('#navigation').css('visibility','visible')
	
	$('#topnav li:last').addClass('last');
	
	$('a.printer-friendly-button').click( function() {
  	    openPopup($(this).attr('href'), 'popwin', 700, 700, 'yes', 'yes', 400, 400, 'yes');
      	return false;
	});

}

/*---------------------*/
/*      Breadcrumb     */
/*-------------------- */

function setBreadcrumb(sectionTitle, sectionLink, subsectionTitle){
	
	// if sectionTitle only
	if(typeof(subsectionTitle) == "undefined"){
		$("#breadcrumb #breadsection").html("/ "+ sectionTitle);

	// if sectionTitle, sectionLink, and subsectionTitle
	}else{
		$("#breadcrumb #breadsection a").html("/ "+ sectionTitle);
		$("#breadcrumb #breadsection a").attr("href", sectionLink);
		$("#breadcrumb #breadsubsection").html("/ " + subsectionTitle);
	}
	$("#breadcrumb").css("visibility", "visible");
}

/* 
 * setBreadcrumb2 was created to enable greater flexibility when creating 
 * the breadcrumb trail. It'll create the trail no matter how many levels
 * the trail should include. This was created for the product pages which have
 * the following structure:
 * 	home/product landing/category/product detail
 * It still writes the output to the existing breadcrumb HTML node since other pages
 * continue to use the older setBreadcrumb function.
 */

function setBreadcrumb2 (breadCrumbObj){
	
	var items = breadCrumbObj.items;
	var crumbs = "";
	
	for(key in items) {
	    
		var elm = "";
		
		if (items[key].target != '')
			elm = '/ <a href="'+items[key].target+'">'+items[key].title+'</a> '	
		else 
			elm = '/ <span>'+items[key].title+'</span> ';

		crumbs = crumbs + elm;
	}
	
	$('#breadsection').empty();
	$('#breadsection').html(crumbs);
	$("#breadcrumb").css("visibility", "visible");
}

/*---------------------*/
/*   Pop Up Window     */
/*-------------------- */

function openPopup( pageToLoad, winName, width, height, center, scroll, winXpos, winYpos, resize) {

	xposition = 0;
	xposition = winXpos;
	yposition = 0;
	yposition = winYpos;
	resizable = 0;
	resizable = resize;
	if ((parseInt(navigator.appVersion) >= 4 ) && (center)) {
		xposition = (screen.width - width) / 2;
		yposition = (screen.height - height) / 2;
	}
	args = "width=" + width + ","     + "height=" + height + ","
	+ "location=0," + "menubar=0,"  + "resizable=" + resizable + "," + "scrollbars=" + scroll + ","
	+ "status=0,"   + "titlebar=0," + "toolbar=0," +                   "hotkeys=0,"
	+ "screenx=" + xposition + ","  //NN Only
	+ "screeny=" + yposition + ","  //NN Only
	+ "left=" + xposition + ","     //IE Only
	+ "top=" + yposition;           //IE Only

	return window.open( pageToLoad,winName,args );
}



jQuery.fn.centerVertically = function(options) {
	var pos = {
		sTop : function() {
			return window.pageYOffset || document.documentElement && document.documentElement.scrollTop ||  document.body.scrollTop;
		},
		sLeft : function() {
			return window.pageXOffset || document.documentElement && document.documentElement.scrollLeft ||  document.body.scrollLeft;
		},
		wHeight : function() {
			return window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
		},
		wWidth : function() {
			return window.innerWidth || document.documentElement && document.documentElement.clientWidth || document.body.clientWidth;
		}
	};

	return this.each(function(index) {

		if (index == 0) {
			
			var $this = $(this);
			var elHeight = $this.height();
			var elWidth = $this.width();
			var elTop = pos.sTop() + (pos.wHeight() / 2) - (elHeight / 2);
			var elLeft = pos.sLeft() + (pos.wWidth() / 2) - (elWidth / 2);
			
			$this.css({
				position: 'absolute',
				marginTop: '0',
				marginLeft: '0',
				top: elTop,
				left: elLeft
			});
		}
	});
};	



function queryString(parameter) { 
  var loc = location.search.substring(1, location.search.length);
  var param_value = false;

  var params = loc.split("&");
  for (i=0; i<params.length;i++) {
      param_name = params[i].substring(0,params[i].indexOf('='));
      if (param_name == parameter) {
          param_value = params[i].substring(params[i].indexOf('=')+1)
      }
  }
  if (param_value) {
      return param_value;
  }
  else {
      return ""; //Here determine return if no parameter is found
  }
}