//Treo Pro Marketing page Support Javascript
//slideshow globals
var globCurrentSubNav = "";
var objSlideShow;
/***************************************************************************************************
Thumbnail Slideshow for marketing pages
	arguments are as follows:
	menuNumber = sub menu number - begins with 1
	
	Usage:
	objVariable = new slideShow(myMenuNumber); //initializes the class
	objVariable.run(); //begin's slideshow
	objVariable.halt(); //stops the slideshow
**************************************************************************************************/
slideShow = function(menuNumber) {
	this.slideshowScreenNumber = 1;
	this.currentMenu = 0;
	this.autoSlideshow = true;
	this.slideshowInterval = 7000;
	this.objSlideShowTimer;
	this.menuNumber = menuNumber;
	this.elementCount = arrAutomate.subMenu[this.menuNumber-1].thumbnails.length;
	//$('debugOutput').innerHTML = $('debugOutput').innerHTML+ "<br/>slideShow Initialized";
}
slideShow.prototype.run = function() {
	this.slideshowScreenNumber++; //increment to next screen
	//$('debugOutput').innerHTML = $('debugOutput').innerHTML+ "<br/>slideShow screen number" + this.slideshowScreenNumber;
	if(this.slideshowScreenNumber > this.elementCount) {
		this.slideshowScreenNumber = 1; //reset slide show	
	}
	//$('debugOutput').innerHTML = $('debugOutput').innerHTML+ "<br/>Auto Slide Show" + this.slideshowScreenNumber + this.autoSlideshow;
	if(this.autoSlideshow) {
		//if flash is running
		if(!$defined(arrAutomate.subMenu[this.menuNumber-1].flashURL)) {
			renderThumbNailClk(this.slideshowScreenNumber);
		}
	}
	if(!$defined(this.objSlideShowTimer)) {
		this.objSlideShowTimer = window.setTimeout(function(){objSlideShow.run(globCurrentSubNav);}, this.slideshowInterval);
	} else {
		clearTimeout(this.objSlideShowTimer);	
		this.objSlideShowTimer = window.setTimeout(function(){objSlideShow.run(globCurrentSubNav);}, this.slideshowInterval);
	}
}
slideShow.prototype.halt = function() {
	clearTimeout(this.objSlideShowTimer);	
	this.autoSlideshow = false;
	this.slideshowScreenNumber = 1;
}
/******end slideshow stuff******/

/***************************************************************************************************
tweenObject for marketing pages
   	Arguments are as follows:
	targetObj = div/span/etc object ID
	ObjTiming = Tween duration in mS
	ObjStart = numeric value of tween at start: ex. '100px'	
	ObjEnd = Numeric value of tween at end: ex. '0px'
	ObjTargetRule =  target css rule: ex. 'top', 'left'
	ObjTransitionType = type of effect: ex. 'Expo', 'Elastic', 'Back' 
**************************************************************************************************/
function tweenObjEase(targetObj, ObjTiming, ObjStart, ObjEnd, ObjTargetRule, ObjTransitionType ) {
	this.tempObjId = targetObj;
	this.tempObjTiming = ObjTiming; 
	this.tempObjStart = ObjStart;
	this.tempObjEnd = ObjEnd;
	this.tempObjTargetRule = ObjTargetRule;
	this.tempObjTransType = eval('Fx.Transitions.' + ObjTransitionType + '.easeInOut'); 
}

tweenObjEase.prototype.run = function() {
		if(typeof($(this.tempObjId)) != "undefined") {
			if(Browser.Engine.webkit){ //no movement
				var tempStr = "document.getElementById('" + this.tempObjId + "').style."+this.tempObjTargetRule+"='"+this.tempObjEnd+"';";
				eval(tempStr);
			}	else {
				var graphicLoad = new Fx.Tween(this.tempObjId,{duration:this.tempObjTiming,transition:this.tempObjTransType});
				graphicLoad.start(this.tempObjTargetRule,this.tempObjStart,this.tempObjEnd);
				graphicLoad = null;
			}
		}
}
subMenuClkEvtHandler = function(targetMenu) {
	//if there is a slideshow running, halt it b4 creating new one
	renderSubmenuScreen(targetMenu);
	if($defined(objSlideShow)) {
		objSlideShow.halt();
	}
/*	if($defined(flashSlideShow)) { //palm_shortcuts only
		flashSlideShow.halt();
	}*/
	if(typeof(arrAutomate.subMenu[targetMenu-1].clickHandler)=="undefined") { //not flash
		objSlideShow = new slideShow(targetMenu);
		objSlideShow.objSlideShowTimer = window.setTimeout(function(){objSlideShow.run();},objSlideShow.slideshowInterval);
	}
}
renderSubmenuScreen = function(targetScreen) {
	//reset all thumbnails
	globCurrentSubNav = targetScreen;
	for(i=1; i < 6; i++) {
		$("thumb"+String(i)).style.display = "none";
	}

	omUp(arrAutomate.subMenu[targetScreen-1].title);

	if(typeof(arrAutomate.subMenu[targetScreen-1].clickHandler)=="undefined") { //not flash
		//tween thumbnails in 90px/100mS intervals	
		for(i=0; i < arrAutomate.subMenu[targetScreen-1].thumbnails.length; i++) {
			$("thumbImage"+String(i+1)).style.background = "url("+arrAutomate.subMenu[targetScreen-1].thumbnails[i].imgURL+") no-repeat transparent";
			var tempTween = new tweenObjEase("thumb"+String(i+1), 350+(i*100), '573px', '0'+(i*90)+'px', 'left', 'Expo').run();
			$("thumb"+String(i+1)).style.display = "block";
		}
		renderThumbNailClk(1);
		$('textWrapper').innerHTML = $("menu"+targetScreen+"text").innerHTML;
		if($('featureContentWrapper').style.top != '32px' && arrAutomate.subMenu[targetScreen-1].thumbnails.length > 0) { //if not already in proper location
			$('featureContentWrapper').style.height = "269px" ;
			var tempTween1 = new tweenObjEase('featureContentWrapper', 250, '82px', '32px', 'top', 'Expo').run();
		} else {
			if($('featureContentWrapper').style.top != '82px' && arrAutomate.subMenu[targetScreen-1].thumbnails.length == 0) { //lower content
				$('featureContentWrapper').style.height = "210px" ;
				var tempTween1 = new tweenObjEase('featureContentWrapper', 550, '32px', '82px', 'top', 'Expo').run();
			}
		}
		$('feature_background').className = 'mo_bg_on';
	} else {  //is flash
		eval(arrAutomate.subMenu[targetScreen-1].clickHandler);
	}
	var elementCount=arrAutomate.subMenu.length;
	for(i=0; i < elementCount; i++) {
		$('subMenu'+(i+1)).style.fontWeight = "normal"
	}
	$('subMenu'+targetScreen).style.fontWeight = "bolder"
	htmlOutput = "";
	htmlOutput += '<div id="subitem_on_left" class="subitem_on_left" style="left:'+String(arrAutomate.subMenu[targetScreen-1].elLeft-5)+'px;"></div>';
	htmlOutput += '<div id="subitem_on_center" class="subitem_on_center" style="left:'+String(arrAutomate.subMenu[targetScreen-1].elLeft)+'px; width:' + String(arrAutomate.subMenu[targetScreen-1].elWidth*1.0) + 'px;"></div>';
	htmlOutput += '<div id="subitem_on_right" class="subitem_on_right" style="left:' + String(arrAutomate.subMenu[targetScreen-1].elLeft+(arrAutomate.subMenu[targetScreen-1].elWidth*1.0)) + 'px;"></div>';
	$('subMenuOnStates').innerHTML = htmlOutput;
}


createSecondaryMenus = function() {
	var htmlOutput = "";
	var xLoc = 0 ;
	var tMenuCapOverhead = 5;
	var tTotalMenuItems = 0 ;
	var subMenuEffectWidth = 0 ;
	var subMenuStartPos = arrAutomate.subMenuLeft;
	var subMenuSpacing = arrAutomate.subMenuSpacing;
	var elementCount = arrAutomate.subMenu.length;

	//gather information from our array and calculate element spacing, etc.
	tTotalMenuItems = ((elementCount+1)*subMenuSpacing);//handle spacing at ends
	for(i=0; i < elementCount; i++) {
		tTotalMenuItems += arrAutomate.subMenu[i].elWidth; 
	}

	//build background
	htmlOutput += '<div id="submenu_bg_left" class="submenu_bg_left" style="left:'+String(subMenuStartPos)+'px;"></div>';
	htmlOutput += '<div id="submenu_bg_center" class="submenu_bg_center" style="top:-26px; left:'+String(subMenuStartPos+tMenuCapOverhead)+'px; width:' + String(tTotalMenuItems) + 'px;"></div>';
	htmlOutput += '<div id="submenu_bg_right" class="submenu_bg_right" style="left:' + String(subMenuStartPos+tTotalMenuItems+tMenuCapOverhead) + 'px;top:-52px;"></div>';

	subMenuEffectWidth = tTotalMenuItems - 148;//sky only	
	htmlOutput += '<div id="submenu_fx_left" class="submenu_fx_left" style="left:'+String(subMenuStartPos)+'px;"></div>';
	htmlOutput += '<div id="submenu_fx_center" class="submenu_fx_center" style="left:'+String(subMenuStartPos+74)+'px; width:' + String(subMenuEffectWidth) + 'px;height:4px;"></div>';
	htmlOutput += '<div id="submenu_fx_right" class="submenu_fx_right" style="left:' + String(subMenuStartPos+subMenuEffectWidth+74) + 'px;"></div>';
	//end sky only
	$('subMenuTween').innerHTML = htmlOutput;

	//build menu overlay
	htmlOutput = "";
	xLoc = subMenuStartPos + subMenuSpacing;
	for(i=0; i < elementCount; i++) {
    	htmlOutput += '<div id="subMenu'+(i+1)+'" style="left:'+xLoc+'px;width:'+arrAutomate.subMenu[i].elWidth+'px;" class="subMenuEl" onclick="subMenuClkEvtHandler('+(i+1)+');">' + arrAutomate.subMenu[i].title + '</div>\n';
		arrAutomate.subMenu[i].elLeft = xLoc;
		xLoc += arrAutomate.subMenu[i].elWidth+subMenuSpacing;
	}
	$('subMenuOverlay').innerHTML = htmlOutput;
	//sub menu effects

} 
preloadArrayImages = function() {
	//thumbnails first
	for(iOut=0; iOut < arrAutomate.subMenu.length; iOut++) {
		for(iIn=0; iIn < arrAutomate.subMenu[iOut].thumbnails.length; iIn++) {
			locImage = new Asset.image(arrAutomate.subMenu[iOut].thumbnails[iIn].imgURL);
		}
	}
	//load hero shots after delay
	window.setTimeout(function() {
		for(iOut=0; iOut < arrAutomate.subMenu.length; iOut++) {
			for(iIn=0; iIn < arrAutomate.subMenu[iOut].thumbnails.length; iIn++) {
				locImage = new Asset.image(arrAutomate.subMenu[iOut].heros[iIn].imgURL);
			}
		}
	}, 1400);
}

thumbNailClkEvt = function(targetThumbnail) {
	if($defined(objSlideShow)) {
		objSlideShow.halt();
	}
	renderThumbNailClk(targetThumbnail);
}
renderThumbNailClk = function(targetThumbnail) {
	$('heroOverviewLeft').innerHTML = "";
	//exception for hero only
	if(arrAutomate.subMenu[globCurrentSubNav-1].thumbnails.length >0) {
		$('thumb1Bg').className = "thumbBgOff" ;
		$('thumb2Bg').className = "thumbBgOff" ;
		$('thumb3Bg').className = "thumbBgOff" ;
		$('thumb4Bg').className = "thumbBgOff" ;
		$('thumb5Bg').className = "thumbBgOff" ;
		$('thumb'+targetThumbnail+'Bg').className = "thumbBgOn" ;
	}
	//do we have a flash file?
	if($defined(arrAutomate.subMenu[globCurrentSubNav-1].flashURL) && Browser.Plugins.Flash.version > 6) {
		//no background
		if(Browser.Engine.trident4) {
			$('heroOverviewLeft').style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', sizingMethod='crop', src='')";
			$('heroOverviewLeft').style.background = "transparent";
		} else {
			$('heroOverviewLeft').style.background = "url() no-repeat transparent";
		}
		var mainFlash = new Swiff(arrAutomate.subMenu[globCurrentSubNav-1].flashURL, {
			id: 'flashMain',
			width: 475,
			height: 325,
			container : 'heroOverviewLeft',
			params: {
				wmode: 'transparent',
				allowScriptAccess: 'sameDomain'
			}
		});	
//		window.setTimeout(function() {flashRunning('flashMain');}, slideshowInterval); //checks for flash complete, 5 seconds from now.
	} else {
		if(arrAutomate.subMenu[globCurrentSubNav-1].heros.length >0) {
			if(Browser.Engine.trident4) {
				$('heroOverviewLeft').style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', sizingMethod='crop', src='"+arrAutomate.subMenu[globCurrentSubNav-1].heros[targetThumbnail-1].imgURL+"')";
				$('heroOverviewLeft').style.background = "transparent";
			} else {
				$('heroOverviewLeft').style.background = "url("+arrAutomate.subMenu[globCurrentSubNav-1].heros[targetThumbnail-1].imgURL+") no-repeat transparent";
			}
		}
	}
}

setupUserEvents = function() {
	//setup events
	if($type($('menuMO'))=="element") {
		$('menuMO').addEvent('mouseenter', function(){$('menuMO').className='menuOn';});
		$('menuMO').addEvent('mouseleave', function(){$('menuMO').className='menuOff';});
	}
	if($type($('menuGPSWiFi'))=="element") {
		$('menuGPSWiFi').addEvent('mouseenter', function(){$('menuGPSWiFi').className='menuOn';});
		$('menuGPSWiFi').addEvent('mouseleave', function(){$('menuGPSWiFi').className='menuOff';});
	}
	if($type($('menuEmailWeb'))=="element") {
		$('menuEmailWeb').addEvent('mouseenter', function(){$('menuEmailWeb').className='menuOn';});
		$('menuEmailWeb').addEvent('mouseleave', function(){$('menuEmailWeb').className='menuOff';});
	}
	if($type($('menuMusicVideo'))=="element") {
		$('menuMusicVideo').addEvent('mouseenter', function(){$('menuMusicVideo').className='menuOn';});
		$('menuMusicVideo').addEvent('mouseleave', function(){$('menuMusicVideo').className='menuOff';});
	}
	if($type($('menuPalmShortcuts'))=="element") {
		$('menuPalmShortcuts').addEvent('mouseenter', function(){$('menuPalmShortcuts').className='menuOn';});
		$('menuPalmShortcuts').addEvent('mouseleave', function(){$('menuPalmShortcuts').className='menuOff';});
	}
	if($type($('menuGallery'))=="element") {
		$('menuGallery').addEvent('mouseenter', function(){$('menuGallery').className='menuOn';});
		$('menuGallery').addEvent('mouseleave', function(){$('menuGallery').className='menuOff';});
	}
	if($type($('menuSpecs'))=="element") {
		$('menuSpecs').addEvent('mouseenter', function(){$('menuSpecs').className='menuOn';});
		$('menuSpecs').addEvent('mouseleave', function(){$('menuSpecs').className='menuOff';});
	}
	if($type($('menuBuy'))=="element") {
		$('menuBuy').addEvent('mouseenter', function(){$('menuBuy').className='menuOn';});
		$('menuBuy').addEvent('mouseleave', function(){$('menuBuy').className='menuOff';});
	}
	//default clicks for thumnail...if hidden then event not called
	if($type($('thumb1'))=="element") { //test secondary page for thumbnail
		$('thumb1').addEvent('click', function() {thumbNailClkEvt(1);});
		$('thumb2').addEvent('click', function() {thumbNailClkEvt(2);});
		$('thumb3').addEvent('click', function() {thumbNailClkEvt(3);});
		$('thumb4').addEvent('click', function() {thumbNailClkEvt(4);});
		$('thumb5').addEvent('click', function() {thumbNailClkEvt(5);});
	}
}

/******flash interface code here******/
flashObjectComplete = function() {
	//console.log("flash is completed");
}
flashRunning = function(flashObject) {
    //trying a couple of things here...   
	if($type($(flashObject))=="element") {
		var callResult = getFlashHandle(flashObject) ;
		if($type(callResult)!=false) {
			if (callResult.IsPlaying()) { 
				return true; 
			} else {
				return false;	
			}
		} else {
			return false;
		}
	} else {
		return false;	
	}
}
getFlashHandle = function(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}
/******end flash interface code******/
/*omniture code for tracking*/
function omUp(omSubNav) {
	try {
		var s_account="palmglobal,palmall,palmus";
		var s1=s_gi(s_account,1)
		s1.pageName="us:us:smartphones:treopro";
		s1.channel="products:smartphones:subnav";
		s1.prop1="us";
		s1.prop2="us";
		s1.prop3="us";
		s1.prop4="palm";
		s1.prop5="smartphones";
		s1.prop6="treopro";
		s1.prop7="unlocked";
		s1.prop10="treopro:subnav:"+omSubNav;
		s1.prop18="en";
		s1.evar2="treopro:subnav:"+omSubNav;
		s1.products=";device:treopro;;;;";
		s1.t();
		return;
	} catch(err) {return;};
}



