/**
 * Copyright (c) 2009, NVIDIA Corporation.  All rights reserved.
 *
 * THE INFORMATION CONTAINED HEREIN IS PROPRIETARY AND CONFIDENTIAL TO
 * NVIDIA, CORPORATION.  USE, REPRODUCTION OR DISCLOSURE TO ANY THIRD PARTY
 * IS SUBJECT TO WRITTEN PRE-APPROVAL BY NVIDIA, CORPORATION.
 * 
 * @author shaworth
 * @todo build as prototype plugin rather than singleton
 * 
 **/

var util = new function() {
	
	this.categoryID;
	this.containerBaseHeight = 250;
	
	this.handleFamilyMouseOver = function(event, theID) {
		var theTarget;
		if (event.target) { // Mozilla
			theTarget = $(event.target.id);
		} else { // IE
			theTarget = $(event.srcElement);
		}
		if (theTarget.className != 'subsectionProductFamilyCellSelected') {
			theTarget.className = 'subsectionProductFamilyCellOver';
			var divs = document.getElementsByTagName('div');
			for (var i = 0; i < divs.length; i++) {
				var theDiv = divs[i];
				if (theDiv.id.indexOf('family_link_') != -1 && theDiv.id != theTarget.id && theDiv.className != 'subsectionProductFamilyCellSelected') {
					theDiv.className='subsectionProductFamilyCell';
				}
			}
		}
	};
	this.handleItemMouseOver = function(event, theID) {
		var theTarget;
		if (event.target) { // Mozilla
			theTarget = $(event.target.id);
		} else { // IE
			theTarget = $(event.srcElement);
		}
		if (theTarget != null) {
			if (theTarget.className == 'item_product') { // Fix for nested children
				theTarget.className = 'item_product_on';
				var otherItems = $$('.item_product_on');
				otherItems.each(
						function (e, i) {
							if (theTarget.id != e.id && e.id.indexOf('family_item_') != -1) {
								e.className = 'item_product';
							}
						}
				);
			}
		}
	};
	this.handleItemMouseOut = function(event, theID) {
		var mouseTarget = event.relatedTarget || event.toElement;
		try {
			if (!mouseTarget.descendantOf(theID)) {
				if ($(theID).className == 'item_product_on') {
					$(theID).className = 'item_product';
				}
			}
		} catch (e) {
			
		}
	};

	this.handleFamilyMouseOut = function(event, theID) {
		var theTarget;
		if (event.target) { // Mozilla
			theTarget = $(event.target.id);
		} else { // IE
			theTarget = $(event.srcElement);
		}
		if (theTarget.className != 'subsectionProductFamilyCellSelected') {
			theTarget.className = 'subsectionProductFamilyCell';
		}
	};
	
	this.handleTabClick = function(event, theID) {
		var theTarget;
		var newTab = $(theID);
		var currentTabs = $$('.currentSection');
		var oldTab = currentTabs[0];
		var allTabs = new Array();
		var allFamilies = new Array();
		if (event.target) { // Mozilla
			theTarget = $(event.target.id);
		} else { // IE
			theTarget = $(event.srcElement);
		}
		oldTab.className = 'hiddenSection';
		newTab.className = 'currentSection';
		allTabs = $$('.custom_tab_on', '.custom_tab_off');
		if (theTarget.className != 'custom_tab_on') { 
			allTabs.each(
				function (e, i) {
					if (theTarget.id == e.id) {
						e.className = 'custom_tab_on';
					} else {
						e.className = 'custom_tab_off';
					}
				}
			);
		}
	};
	
	this.setCategoryID = function(x) {
		this.categoryID = x;
	};
	
	this.setInnerSectionHeightFromFamily = function(familyID) {
		var itemContainers;
	};
	this.highlightFirstFamilies = function() {
		var firstElements = $('tabs_bounding_box').select('[first=true]');
		firstElements.each(function(e, i) {e.className = 'subsectionProductFamilyCellSelected';});
	};
	
	this.handleFamilyClick = function(event, theID, sectionID, containerHeight) {
		var theTarget;
		if (event.target) { // Mozilla
			theTarget = $(event.target.id);
		} else { // IE
			theTarget = $(event.srcElement);
		}
		util.removeAllChildrenFromNode($('itemsContainer_' + sectionID));
		$('itemsContainer_' + sectionID).appendChild($('family_items_' + theID));
		$('family_items_' + theID).className = 'subsectionProductFamilyItemsOn';
		if (containerHeight > util.containerBaseHeight) {
			//$('innerSectionMiddleContainer_' + sectionID).style.height = containerHeight + 'px';
			var currentPixelHeight = $('innerSectionMiddleContainer_' + sectionID).getStyle('height');
			var diff = containerHeight - util.containerBaseHeight;
			var currentMathHeight = parseInt(currentPixelHeight.replace('px', ''));
			if (currentMathHeight < containerHeight - 1) {
				for (var i = 0; i < diff; i++) {
					var currentHeight = parseInt(currentPixelHeight.replace('px', ''));
					$('innerSectionMiddleContainer_' + sectionID).style.height = (currentHeight + i) + 'px';
				}
			}
		}
		util.setFamilySelectedClick(theID);
	};
	
	this.removeAllChildrenFromNode = function(element) {
		try {
			if (typeof(element) == 'string') {
				element = $(element);
			}
			while (element.hasChildNodes()) {
				var childElement = element.firstChild;
				childElement.className = 'subsectionProductFamilyItems';
				$('subsectionContentContainer').appendChild(childElement);
				element.removeChild(childElement);
			}
		} catch(e) {

		}
	};
	
	this.getBrowserName = function() {
		var theBrowser = navigator.appName;
		if (theBrowser == 'Microsoft Internet Explorer') {
			return 'IE';
		} else if (theBrowser == 'Netscape') {
			return 'MZ';
		} else {
			return 'MZ';
		}
	}
	
	this.showFirstFamilyItems = function() {
		var families = $$('.subsectionProductFamilyItems');
		families.each(
			function (e, i) {
				if (e.getAttribute('firstItem') == 'true') { // can use selectors here for optimization instead if necessary
					var containerID = e.getAttribute('containerValue');
					var containerHeight = e.getAttribute('containerHeight');
					util.removeAllChildrenFromNode($('itemsContainer_' + containerID));
					$('itemsContainer_' + containerID).appendChild(e);
					if (containerHeight > util.containerBaseHeight) {
						$('innerSectionMiddleContainer_' + containerID).style.height = containerHeight + 'px';
						// This is way too slow in IE
						/*var currentPixelHeight = $('innerSectionMiddleContainer_' + containerID).getStyle('height');
						var diff = containerHeight - util.containerBaseHeight;
						var currentMathHeight = parseInt(currentPixelHeight.replace('px', ''));
						if (currentMathHeight < containerHeight - 1) {
							for (var i = 0; i < diff; i++) {
								var currentHeight = parseInt(currentPixelHeight.replace('px', ''));
								$('innerSectionMiddleContainer_' + containerID).style.height = (currentHeight + i) + 'px';
							}
						}*/
					}
					e.className = 'subsectionProductFamilyItemsOn';
				}
			}
		);
		util.highlightFirstFamilies();
	};
	
	this.setFamilySelected = function(theID) {
		var theFamilyID = util.getFamilyID(theID);
		$('family_link_' + theFamilyID).className = 'subsectionProductFamilyCellSelected';
	};
	
	this.setFamilySelectedClick = function(theID) {
		var incomingMember = $('family_link_' + util.getFamilyID(theID)); 
		var familySibs = incomingMember.siblings();
		familySibs.each (
			function (e, i) {
				if (e.className == 'subsectionProductFamilyCellSelected') {	
					e.className = 'subsectionProductFamilyCell';
					incomingMember.className = 'subsectionProductFamilyCellSelected';
				}
			}
		);
	}
	
	this.getFamilyID = function(theID) {
		var lastIndex = theID.lastIndexOf('_');
		if (lastIndex != -1)
			return theID.substring(lastIndex + 1, theID.length);
		else
			return theID;
	};
	
	this.loadTip = function(theID, theTitle) {
		if (!$('tooltip_item_' + theID).prototip) {
			new Tip($('tooltip_item_' + theID), $('productTipContent_' + theID), 
					{
						hideOn: false,
						/*closeButton: true,*/
						hideAfter: 1,
						style: 'default',
						hook: {target: 'leftMiddle', tip: 'leftMiddle'}
					}
			);
			if (util.getBrowserName() == 'MZ') {
				if ($('productTipContent_' + theID)) {
					$('productTipContent_' + theID).className = 'subsectionProductTipContentOn';
					$('tooltip_item_' + theID).prototip.show();
				}
			}
		}
	};
	
	this.hideToolTips = function() {
		var elements = $$('.subsectionProductTipContent');
		elements.each(Element.hide);
	};
	
	this.buildToolTips = function () {
		var elements = $$('.subsectionProductTipContent');
		for (var i = 0; i < elements.length; i++) {
			var currentElement = elements[i];
		}
	}
	/**
	 * @deprecated
	 * This is a useful method for something
	 */
	this.findParentNode = function(classID, theTarget) {
		var theParent = theTarget.parentNode;
		if (theParent.className == classID) {
			return theParent;
		} else {
			// Recurse itself
			util.findParentNode(classID, theParent);
		}
	};
	
	this.hideView = function(theElement) {
		Effect.Fade(theElement,
			{ 
				duration: .5, 
				from: 1, 
				to: 0
			}	
		);
	};
	this.showView = function(theElement) {
		Effect.Appear(theElement,
				{ 
					duration: .5, 
					from: 0, 
					to: 1 
				}	
		);
	};
	

	this.handleCategoryMouseOver = function(targetID, theType) {
		switch(theType) {
			case 'custom': {
				$(targetID).className = '';
				$(targetID).className = 'customCategoryFaceOver';
				break;
			}
			case 'normal': {
				$(targetID).className = '';
				$(targetID).className = 'categoryFaceOver';
				break;
			}
		}
	};
	
	this.handleSingleItemMouseOver = function (theID) {
		var incomingElement = $(theID);
		incomingElement.className = 'item_single_product_on';
	};
	this.handleSingleItemMouseOut = function (theID) {
		var incomingElement = $(theID);
		incomingElement.className = 'item_single_product_off';
	};
	
	this.handleBackMouseOver = function() {
		$('backArrow').className = 'backToIndexHover';
	};
	
	this.handleBackMouseOut = function() {
		$('backArrow').className = 'backToIndex';
	};
	
	this.handleSingleItemClick = function(incoming) {
		window.location = incoming;
	};
	
	this.handleLocalizedBackIndexClick = function(event) {
		if (products.lang == '') {
			window.location = products.prodFolder + products.returnDocument();
		} else {
			window.location = products.prodFolder + products.returnDocument() + '?lang=' + products.lang;
		}
	};
	
	this.handleCategoryMouseOut = function(targetID, theType) {
		switch(theType) {
			case 'custom': {
				$(targetID).className = '';
				$(targetID).className = 'customCategoryFaceOut';
				break;
			}
			case 'normal': {
				$(targetID).className = '';
				$(targetID).className = 'categoryFaceOut';
				break;
			}
		}
	};
	
	this.getQueryVariable = function(variable) {
		var query = window.location.search.substring(1);
		var queryObject = $H(query.toQueryParams());
		var returnVar = queryObject.get(variable);
		if (returnVar != undefined)
			return returnVar;
		else
			return '';
	};
	
}

