/**
 * 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 products = new function() {
	
	this.currentState = "";
	this.currentTypes = new Array();
	this.hist;
	this.tippyEngine;
	this.folder = "/content/products/";
	this.prodFolder = "/object/";
	this.sectionID;
	this.clearDiv = '<div style="clear:both; line-height:0; height:0; width:0; font-size:0px;">&nbsp;</div>';
	this.lang = '';
	this.backIndex = '';
	this.appName = '';
	
	this.init = function() {
		this.hist = new Prototype.History(products.pageLoad);
		this.tippyEngine = new Prototype.Tippy({showDebug: false});
		this.lang = (util.getQueryVariable('lang') == '') ? this.lang : util.getQueryVariable('lang');
	};
	
	this.getTypes = function() {
		new Ajax.Request(products.folder + 'gateway.php?func=getTypes&lang=' + products.lang,
				{
					method: 'get',
					onSuccess: this.getTypesResult,
					onError: this.handleFault
				}
		);
	};
	
	this.getTypesResult = function(response) {
		var theResponseObject = response.responseXML;
		var theResponse = response.responseText;
		var result = theResponseObject.getElementsByTagName('return')[0];
		var items = result.getElementsByTagName('item');
		var xmlTransformer = new transformer(products.folder + 'resources/xsl/types.xsl');
		var transformed = xmlTransformer.transform(theResponse, theResponseObject);
		$('productContainer').hide();
		$('featuresContainer').hide();
		$('typeContainer').innerHTML = transformed;
		$('typeContainer').show();
		var typeArray = new Array();
		for (var i = 0; i < items.length; i++) {
			var theType = {
					"id" : items[i].getElementsByTagName('id')[0].getAttribute('value'),
					"name" : items[i].getElementsByTagName('rowName')[0].getAttribute('value')
			};
			typeArray.push(theType);
			products.currentTypes = typeArray;
		}
		products.getCategories();
	};
	
	this.getCategories = function() {
		for (var i = 0; i < products.currentTypes.length; i++) {
			var currentID = products.currentTypes[i].id;
			new Ajax.Request(products.folder + 'gateway.php?func=getCategories&id=' + currentID + '&lang=' + products.lang,
					{
						method: 'get',
						onSuccess: this.getCategoriesResult,
						onError: this.handleFault
					}
			);
		}
	};
	
	this.getCategoriesResult = function(response) {
		var theResponseObject = response.responseXML;
		var theResponse = response.responseText;
		var result = theResponseObject.getElementsByTagName('return')[0];
		var items = result.getElementsByTagName('item');
		var typeID = items.length > 0 ? items[0].getElementsByTagName('typeID')[0].getAttribute('value') : 0;
		var xmlTransformer = new transformer(products.folder + 'resources/xsl/categories.xsl');
		var transformed = xmlTransformer.transform(theResponse, theResponseObject);
		$('type_' + typeID).innerHTML = transformed;
	};
	
	this.handleHistory = function(targetID, sectionName) {
		if (products.lang != '') {
			products.hist.historyLoad(targetID + '/' + sectionName + '/' + products.lang);
		} else {
			products.hist.historyLoad(targetID + '/' + sectionName);
		}
	};
	
	this.handleBreadCrumbs = function(targetID, sectionName) {
		var currentCrumbs = $('breadCrumbs').innerHTML;
		var updatedCrumbs = '';
		var pageTitleContainer = $$('h1.pageTitle')[0];
		pageTitleContainer.innerHTML = sectionName;
		if (products.lang != '') {
			updatedCrumbs = '<a href="/">NVIDIA</a> > <a href="' + products.prodFolder + products.returnDocument() + '">' + products.appName + '</a>' + ' > <a href="' + products.prodFolder + products.returnDocument() + '?lang=' + products.lang + '#' + targetID + '/' + sectionName + '">' + sectionName + '</a>' + products.clearDiv;
		} else {
			updatedCrumbs = '<a href="/">NVIDIA</a> > <a href="' + products.prodFolder + products.returnDocument() + '?lang=' + products.lang + '">' + products.appName + '</a>' + ' > <a href="' + products.prodFolder + products.returnDocument() + '#' + targetID + '/' + sectionName + '">' + sectionName + '</a>' + products.clearDiv;
		}
		$('breadCrumbs').innerHTML = updatedCrumbs;
	};
	
	this.handleInitCrumbs = function() {
		var pageTitleContainer = $$('h1.pageTitle')[0];
		pageTitleContainer.innerHTML = products.appName;
		if (products.lang == '') {
			$('breadCrumbs').innerHTML = '<a href="/">NVIDIA</a> > <a href="' + products.prodFolder + products.returnDocument() + '">' + products.appName + '</a>' + products.clearDiv;
		} else {
			$('breadCrumbs').innerHTML = '<a href="/">NVIDIA</a> > <a href="' + products.prodFolder + products.returnDocument() + '?lang=' + products.lang + '">' + products.appName + '</a>' + products.clearDiv;
		}
	};
	
	this.buildSections = function(targetID) {
		var incomingGet = targetID.split('/');
		var requestID = incomingGet[0];
		util.setCategoryID(requestID);
		new Ajax.Request(products.folder + 'gateway.php?func=getSections&id=' + requestID + '&lang=' + products.lang,
				{
					method: 'get',
					onSuccess: products.buildSectionsResult,
					onError: products.handleFault
				}
		);
		new Ajax.Request(products.folder + 'gateway.php?func=getFeatures&categoryID=' + requestID + '&lang=' + products.lang,
				{
					method: 'get',
					onSuccess: products.buildFeaturesResult,
					onError: products.handleFault
				}
		);
	};
	
	this.buildSectionsResult = function(response) {
		var theResponseObject = response.responseXML;
		var theResponse = response.responseText;
		var xmlTransformer = new transformer(products.folder + 'resources/xsl/products.xsl');
		var transformed = xmlTransformer.transform(theResponse, theResponseObject);
		$('typeContainer').hide();
		$('productContainer').hide();
		$('productContainer').innerHTML = transformed;  
		util.showView('productContainer');
		util.showFirstFamilyItems();
		if (products.tippyEngine == undefined) { // fix for IE trashing objects
			products.tippyEngine = new Prototype.Tippy({showDebug: false});
		}
		products.tippyEngine.sweep();
		$('backArrow').innerHTML = '<img src="/content/products/resources/images/back-arrow.gif" /> ' + products.backIndex;
		$('backArrow').observe('click', util.handleLocalizedBackIndexClick);  
	};
	
	this.buildFeaturesResult = function(response) {
		var theResponseObject = response.responseXML;
		var theResponse = response.responseText;
		var xmlTransformer = new transformer(products.folder + 'resources/xsl/features.xsl');
		var transformed = xmlTransformer.transform(theResponse, theResponseObject);
		$('featuresContainer').hide();
		$('featuresContainer').innerHTML = transformed;
		util.showView('featuresContainer');
	};
	
	this.getStaticMessaging = function() {
		new Ajax.Request(products.folder + 'gateway.php?func=getStaticMessaging&lang=' + products.lang,
				{
					method: 'get',
					asynchronous: false,
					onSuccess: products.messagingResult,
					onError: products.handleFault
				}
		);
	};
	
	this.messagingResult = function(response) {
		var theResponse = response.responseXML;
		var result = theResponse.getElementsByTagName('return').item(0);
		products.backIndex = result.childNodes.item(0).getAttribute('value');
		products.appName = result.childNodes.item(1).getAttribute('value');
	};
	
	this.handleFault = function(response) {
		alert('Application Fault: \n' + response.responseText);
	};
	
	this.pageLoad = function(hash) {
		if (!hash) {
			products.lang = (util.getQueryVariable('lang') == '') ? products.lang : util.getQueryVariable('lang');
			products.getStaticMessaging();
			products.handleInitCrumbs();
			products.getTypes();
		} else {
			var incomingGet = hash.split('/');
			var requestID = incomingGet[0];
			var sectionName = incomingGet[1];
			if (incomingGet.length > 2)
				products.lang = incomingGet[2];
			products.getStaticMessaging();
			products.handleInitCrumbs();
			products.handleBreadCrumbs(requestID, sectionName.replace(/%20/g, ' '));
			products.buildSections(hash);
		}
	};
	
	this.returnDocument = function() {
		//products_beta_us.html#1/Home
		//products_beta_us.html?lang=zh_CN#1/Home
		var file_name = document.location.href;
		// trim out deep linking
		if (file_name.indexOf("#") != -1) {
			file_name = file_name.substring(0, file_name.indexOf("#"));
		}
		var end = (file_name.indexOf("?") == -1) ? file_name.length : file_name.indexOf("?");
		return file_name.substring(file_name.lastIndexOf("/")+1, end);
	};
	
	

	
	
}
