//tab click script
var sourceID;
var clickedTab=null;
var tStr=''; //USED FOR TRACKING, UPDATED WITH EVERY TAB CLICK
var s2;
function tabClick(e,doTracking) {
	if (doTracking==undefined) {doTracking=true;}
	if (e!=clickedTab) {
		clickedTab=e;
		targetID=false;
		checkTarget=e;
		while (!checkTarget.hasClassName('clickableTab')) {
			checkTarget=checkTarget.up();
		}
		checkTargetStr=checkTarget.id.split('-tab').join('');
		window.location.hash=checkTargetStr;
		$$('.clickableTab').each(function(e){e.addClassName('off').removeClassName('on')});
		checkTarget.addClassName('on').removeClassName('off');
		targetID=checkTargetStr+'-content';
		
		// kill active flash content (other than videoplayers)
		$$('.gcfFlashContent').each(function(e,i){
			var testList=e.ancestors();
			if (!e.hasClassName('saved') && !testList.include($(targetID))) {
				e.addClassName('saved');
				var savedContent=escape(e.innerHTML);
				e.innerHTML=savedContent;
			}
		});

		if ($('primaryContentHorzTab')) { 

		// pages with tab bar on the left and form on the right
			
			//HANDLE HORIZONTAL TABS, CONTENT CHANGES, AND TRACKING
			$$('#primaryContentHorzTab .tabContent').each(function(e){
				e.style.display=(e.id==targetID)?'inline':'none';
			});
			
			//update form iframe with source ID if content is not yet unlocked, or update right rail content if unlocked
			sourceID=$(targetID).select('.rrContent')[0].select('.sourceID')[0].innerHTML;
			if (!unlocked) {
				//DEV FORM:
				//$('formIframe').src='form.htm?SourceID='+sourceID;
				//REAL FORM:
				initialHref=$(targetID).select('.rrContent')[0].select('.formURL')[0].innerHTML;
				$('formIframe').src=initialHref.replace(new RegExp('\\[\\[SOURCEID\\]\\]','gi'),sourceID).replace(new RegExp('&amp;','gi'),'&');
			} else {
				moveContent($(targetID).select('.rrContent')[0]);
			}
			
		} else if ($('primaryContentVertTab') || $('tabSelectContent')) {

		// pages with tab bar at top
			
			//HANDLE VERTICAL TABS AND TRACKING
			$$('#primaryContentVertTab .tabContent').each(function(e){
				e.style.display=(e.id==targetID)?'block':'none';
			});
			
			// for non campaign page
		
			$$('#tabSelectContent .tabContent').each(function(e){
				e.style.display=(e.id==targetID)?'block':'none';
			});
						
		} else if ($('primaryContent') || $('primaryContentMenuBar')) {

		// pages with thin button bar at top

			//DEACTIVATE ALL VIDEOPLAYERS
			$$('.vPlayer').each(function(e){
				if (!e.select('img')[0]) {
					$(e).update('');
				}
			});

			//HANDLE MENU BAR AND TRACKING
			$$('#primaryContentMenuBar .tabContent').each(function(e){
				e.style.display=(e.id==targetID)?'block':'none';
			});
			//HANDLE MENU BAR AND TRACKING
			$$('#primaryContent .tabContent').each(function(e){
				e.style.display=(e.id==targetID)?'block':'none';
			});
			
			//activate videoplayer
			doVideo($(targetID));
			
		}

		// activate inactive flash content in this tab (other than videoplayers)
		$$('#'+targetID+' .gcfFlashContent').each(function(e,i){
			if (e.hasClassName('saved')) {
				e.removeClassName('saved');
				var retrievedContent=unescape(e.innerHTML);
				e.innerHTML=retrievedContent;
			}
		});

		//TRACKING - do tracking
		if (doTracking) {
			//do nothing
		}
		//END TRACKING
	
	}
}

// activate selected tab
var selectedTabName=window.location.hash.slice(1);	// Get name of tab from URL
var	selectedTab = document.getElementById(selectedTabName);
if (!selectedTab) {	// If there is no DIV with the given name  ...
	selectedTabName += '-tab';	// ... give it another try by appending '-tab'
	selectedTab = document.getElementById(selectedTabName);
}
var firstTab = null;
var divs = document.getElementsByTagName('div');	// Search all DIV elements ...
for (var i=0; i<divs.length; ++i)
	if (divs[i].className.indexOf('clickableTab') != -1) {	// ... for a DIV with the CSS class "clickableTab" ...
		firstTab = divs[i];	// ... and store the first in firstTab.
		break;
	}
if (firstTab) {	// Make sure there was at least one DIV with the class "clickableTab"
	if ((!selectedTab) || (selectedTab.id == firstTab.id)) {	// If we have no tab name or its the name of the first tab ...
		firstTab.className = firstTab.className.replace(/\boff\b/, 'on');	// ... then activate first tab.
		document.getElementById(firstTab.id.split('-tab').join('')+'-content').style.display = 'block';
	} else {	// If a tab other than the first one is selected ...
		firstTab.className = firstTab.className.replace(/\bon\b/, 'off');	// ... then deactivate first tab ...
		document.getElementById(firstTab.id.split('-tab').join('')+'-content').style.display = 'none';
		selectedTab.className = selectedTab.className.replace(/\boff\b/, 'on');	// ... and activate the selected tab.
		document.getElementById(selectedTab.id.split('-tab').join('')+'-content').style.display = 'block';
	}
}

function addOnLoadHandler(handler) { 
	if (window.addEventListener) { 
		window.addEventListener('load', handler, false); 
		return true; 
	} else if (window.attachEvent)
		return window.attachEvent('onload', handler); 
	else 
		return false; 
}

//on load, apply tab click script, "click" first tab
addOnLoadHandler(function(){
	$$('.clickableTab').each(function(e){
		if (e.id.indexOf('-tab')<0) {
			e.id=e.id+'-tab';
		}
		e.onclick=function(){
			tabClick(this,true);
		}
	});
	if (BrowserDetect.browser=='Explorer') { // needed for IE6 weirdness fix
		setInterval(function(){document.title=storedWinTitle;},100);
	}	
	setTimeout(function() {
		if (selectedTabName.indexOf('-tab')<0)
			selectedTabName += '-tab';
		if (document.getElementById(selectedTabName)) {
			tabClick($(selectedTabName),true);
		} else {
			tabClick($$('.clickableTab')[0],true);		
		}
	},50);
});

var storedWinTitle=document.title;
//videoplayer activation code
function doVideo(which) {
    if ($(which).select('.videoURL')[0]) {
        targetVP=$(which).select('.vPlayer')[0];
	if (targetVP.hasClassName('largeVideo')) { version='-large'; } else { version=''; }
	switch (version) {
		case '-large':
			w=629;
			h=502;
		break;
		default:
			w=240;
			h=210;
	}
	var imageURL='';
        if (targetVP.select('img')[0]) {
            imageURL=targetVP.select('img')[0].src;
            $(which).insert({bottom:'<div class="imageURLstored" style="display:none;">'+imageURL+'</div>'});
        } else if ($(which).select('.imageURLstored')[0]) {
            imageURL=$(which).select('.imageURLstored')[0].innerHTML;
        }
        targetVP=targetVP.identify();
        videoURL=$(which).select('.videoURL')[0].innerHTML;
        var so = new SWFObject('http://www.emc.com/campaign/global/videoplayer/videoplayer'+version+'.swf', 'vPlayer'+targetVP, w, h, '8', '#ffffff');
        so.addVariable('videoURL',videoURL);
        so.addVariable('imageURL',imageURL);
        so.addVariable('videoPlayerCSSID','vPlayer'+targetVP);
	so.addVariable('isExplorer',BrowserDetect.browser=='Explorer');
        so.addVariable('popupVideoRolloverText',$('popupVideoRolloverText').innerHTML);
        so.addVariable('tStr',window.s.pageName+': '+tStr);
	so.addParam('wmode','transparent');
        so.addParam('allowFullscreen','true');
	so.addParam('quality','autohigh');
        so.addParam('scale','noscale');
        so.addParam('salign','tl');
        so.write(targetVP);
    }
}
//videoplayer resize callback -- called when player resizes for any reason, should be used to adjust HTML container's height to match
function videoPlayerLayoutCallback(vPlayerCSSID,updatedSpecs) {
	$(vPlayerCSSID).style.height=(updatedSpecs.videoHeight+updatedSpecs.interfaceHeight)+'px';
}

//omniture tracking wrapper for videoplayers (taken from /_admin/js/omniture_wrapper.js since this file was not included in the page...)
var originalPageName=false;
function emcTrack(tStr,filename,mode) {

	if (!originalPageName) {
		//originalPageName=window.s.pageName;
		originalPageName=window.location.pathname+((window.location.search!='')?window.location.search:'')+((window.location.hash!='')?window.location.hash:'');
	}

	//download and custom link tracking -- shows up in File Downloads or Custom Links
	var s=s_gi('emc-emccom');
	s.dynamicAccountSelection=true;
	s.dynamicAccountList=window.s.dynamicAccountList;
	if (mode=='d') {
		s.tl(true,'d',filename);
	} else if (mode=='o') {
		s.tl(true,'o',tStr);
	}

	//normal pageName tracking to enable pathing, etc. for both download and normal (but not custom)
	if (mode!='o') {
		s.pageName=originalPageName+': '+tStr;
		s.t();
	}

}

