/**********************************************************************
File:pdp.js
Description: generic pdp handler for items like tabs, accordions, etc.

Requires: 
	YUI (implemented with YUI 2.7)
	http://developer.yahoo.com/yui/

Modules:
	Tab click event handler

Revisions: 
	Rev 1: 4/13/2009 - Created tab swapper - KTH
	Rev 2: 4/13/2009 - fixed issue with selected tab state - KTH

Author: Palm IT Web Team

Notes: Suggest compression of all js files!
***********************************************************************/

YAHOO.namespace('YAHOO.PALMPDP');

//global lightbox handling code		
YAHOO.PALMPDP.handler = function () {

	//PRIVATE VARIABLES and METHODS:
	var tabEvtHandler = function(e) {
		tg=YAHOO.util.Event.getTarget(e);
		while (tg.id != "tab-selectors") { //walk the event bubble 'down'
			if (tg.className.indexOf("tab0") != -1) { //supports up to 9 tabs
				YAHOO.util.Event.stopEvent(e);
				swapTab(tg.className, false);
				return;
			} else {
				tg = tg.parentNode;
			}
		}
	};	
	//swap the tab content and change tab state
	//tabID = "tab01", "tab02", etc.
	//initialState = true will not scroll, false to scroll to tab location
	var swapTab = function(tabID, initialState) {
		
		tabID = tabID.split(" ")[0];
		//animate page scroll:
		if(!initialState) {
			window.scrollTo(0,600); //TODO: add yahoo animation here
		  }			
		//get all 'tab' elements and turn them off
		var tempTabs = YAHOO.util.Dom.getElementsByClassName('tab-content', 'div', 'container-tab-content');
		for(i=0; i<tempTabs.length; i++){
			if(YAHOO.util.Dom.inDocument(tempTabs[i].id)){
				if(tempTabs[i].id!=tabID) {
				YAHOO.util.Dom.removeClass(tempTabs[i].id, 'active');
				}
			}
		}

		//get all tab  selectors and remove 'selected' from className
		var tempTabsSelectors = YAHOO.util.Dom.getChildren('tab-selectors'); 
		for(i=0; i<tempTabsSelectors.length; i++){
			if(tempTabsSelectors[i].className.indexOf(tabID) >-1) {
				YAHOO.util.Dom.addClass(tempTabsSelectors[i], 'selected');
			} else {
				YAHOO.util.Dom.removeClass(tempTabsSelectors[i], 'selected');
			}
		}
		
		//set the selected tab to 'active'
		if(YAHOO.util.Dom.inDocument(tabID)){
			YAHOO.util.Dom.addClass(tabID, 'active');
		}

	};
	
	//pdp private method randomly selects software accessories from a HTML file
 	var insertAuxAccessories = function() {
		var loadHTML = function(htmlHREF) {
			if(htmlHREF!="") {
				var totalBlocks = 3;
				var randomNumber = Math.random();
				var whichBlock = Math.round(randomNumber * (totalBlocks-1));
				var getThumbnails = YAHOO.util.Connect.asyncRequest('GET', (htmlHREF+whichBlock+".html"), callback, null); //start connection manager and get JSON data - need to create dynamic URL generation
			}
		}
		var responseSuccess = function(o,targetDiv) {
			var tempResponse = o.responseText.replace("1px solid #e7e7e7", "1px solid #e7e7e7;height:110px;overflow:hidden;");
			
			var tempResponse = tempResponse.replace('line-height: 1.4em;', 'line-height: 1.4em;height:90px;'); //fix some bad code from feed...
			var tempResponse = tempResponse.replace('</div></div>', '</div></div><div style="clear:left;"></div>'); //fix some bad code from feed...
			
			 
			//console.log(tempResponse);
			$('aux-accessory-target').innerHTML = tempResponse ;
			$('aux-accessory-target').style.height="280px"; //little extra for pre
		}
		var responseFailure = function(o) {
			//TODO: HANDLE XHR FAILURE PROPERLY
		}
		var callback = {
		  success:responseSuccess, failure:responseFailure, argument:"", cache:false
		}
		return {
			 init : function(htmlHREF, targetDiv) {
				loadHTML(htmlHREF,targetDiv);
			}
		};

	} ();

	//insertAuxAccessories.init PUBLIC METHODS:
	return  {
		init: function(){
			
			if(YAHOO.util.Dom.inDocument('tab-selectors')){
				swapTab('tab01', true);
				YAHOO.util.Event.addListener('tab-selectors', 'click', tabEvtHandler);
			}		
			if(YAHOO.util.Dom.inDocument('aux-accessory-target')){
				window.setTimeout(function(){insertAuxAccessories.init($('aux-accessory-target').title);}, 1500);
			}		
		},
		lightboxAutoload: function() {
			//check to see if someone passes a string to load a lightbox
			var tempHREF = window.location.href;
			if(tempHREF.indexOf("#") >-1) {
				if(tempHREF.indexOf("#gallery")+1 >1 || tempHREF.indexOf("#video")+1 >1 || tempHREF.indexOf("#wtb")+1 >1) { //check these in lightbox javascript
					if(window.navigator.userAgent.indexOf("webOS")>-1 || window.navigator.userAgent.indexOf("iPhone")>-1  || window.navigator.userAgent.indexOf("Android")>-1) { //if webOS or iPhone
					//	if(tempHREF.indexOf("&init=1")<0) {
					//		window.location.replace(tempHREF+"&init=1"); //set page stage to "ran" so we won't get into a loop with the external lightbox
					//	}
					}
					YAHOO.PALMLIGHTBOX.handler.loadLightbox(tempHREF.slice(tempHREF.indexOf("#")+1,tempHREF.length));
				}
				if(tempHREF.indexOf("#tab2")+1 >1) { //check these in lightbox javascript
					swapTab("tab02", true);
				}
			}

			
		}
	};
}(); //end insertAuxAccessories

YAHOO.PALMPDP.handler.init();
YAHOO.util.Event.onDOMReady(function() { 
		YAHOO.PALMPDP.handler.lightboxAutoload();
		window.setTimeout(function(){YAHOO.PALMMSIE.pnghander.init(["open-meet-img","open-gallery-img"]);}, 1000); //insert object id's with png images(IMG tag only)
		}); 
/**********end PDP Generic JavaScript*********/				
    		
