/*
common.js is for common functions used throughout Sprint sites
This file is currently maintained by the following:
	Aaron Barker (abarke04)
	France Rupert (fruper01)

Please contact one of the above with any issues or suggestions
*/
var d = document;
actualVersion = ""; // for flash detection

/* general/utility */
function popUp(page, name, w, h, scroll) { // used for all pop up windows
	name = "foo";
	page = page.split(" ").join("%20");
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	//var resizable = false;
	winprop  = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=yes';
	win = window.open(page, name, winprop);
	win.focus();
}

function printThis() {
	var ua = navigator.userAgent.toLowerCase();
	var is_mac = ua.indexOf('mac') > 0;
	if (is_mac) { alert('To print:\n\nUse Command + P. on your keyboard\n') }
	else { print(); } 
}

function trim(s){ // remove extra whitespace before and after passed string
	toTrim = new Array(' ','\n','\r');
	for(x=0; x<toTrim.length; x++){
		while(s.substring(0,1) == toTrim[x]){
			s = s.substring(1,s.length);
		}
		while(s.substring(s.length-1,s.length) == toTrim[x]){
			s = s.substring(0,s.length-1);
		}
	}
	return s;
}

function getQueryVariable(variable) { // this function gets us the value of a passed variable name from the query string
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	} 
	return '';
}

function sprint_addLoadEvent(func) { // allows multiple window.onload's Thanks to Simon - http://simon.incutio.com/archive/2004/05/26/addLoadEvent
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

// finds the x,y position of an element thanks to PPK - http://www.quirksmode.org/js/findpos.html
function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	return curtop;
}

/* manipulate layers */
function showHide(layerName){ // shows and hides a given layer **DON'T USE THIS VERSION**... here for legacy only
	if (d.getElementById){
		if(!layerName.nodeName) layerName = d.getElementById(layerName);
		layerName.style.display = layerName.style.display ? '' : 'none';
		return;
	}
}

function sprint_showHide(layerName){ // shows and hides a given layer
	if (d.getElementById){
		if(!layerName.nodeName) layerName = d.getElementById(layerName);
		
		curClass = " "+layerName.className + " ";
		if(curClass.indexOf(" seen ") != -1){
			hideLayer(layerName);
		} else {
			showLayer(layerName);
		}
		return;
	}
}

function showLayer(layerName){ // shows a given layer
	if (d.getElementById){
		if(!layerName.nodeName) layerName = d.getElementById(layerName);
		if(layerName){
			layerName.style.display = ''; // left for legacy hardcoded versions
			sprint_addClass(layerName,'seen');
			sprint_delClass(layerName,'hidden');
			return;
		}
	}
}

function hideLayer(layerName){ // hides a given layer
	if (d.getElementById){
		if(!layerName.nodeName) layerName = d.getElementById(layerName);
		sprint_delClass(layerName,'seen');
		sprint_addClass(layerName,'hidden');
		return;
	}
}

function sprint_delClass(curLayer,oldClass){
	if (d.getElementById){
		if(!curLayer.nodeName) curLayer = d.getElementById(curLayer);
		curClass = " "+curLayer.className + " ";
		if(curClass.indexOf(" "+ oldClass + " ") != -1){
			curLayer.className = trim(curClass.replace(" "+oldClass+" "," "));
		}
	}
}

function sprint_addClass(curLayer,newClass){
	if (d.getElementById){
		if(!curLayer.nodeName) curLayer = d.getElementById(curLayer);
		curClass = " "+curLayer.className + " ";
		if(curClass.indexOf(" "+ newClass + " ") == -1){
			curLayer.className = trim(curLayer.className + " " + newClass);
		}
	}
}

function sprint_toggleClass(curLayer,tog1,tog2){
	if (d.getElementById){
		if(!curLayer.nodeName) curLayer = d.getElementById(curLayer);
		curClass = " "+curLayer.className + " ";
		if(curClass.indexOf(" "+ tog1 + " ") != -1){
			sprint_delClass(curLayer,tog1);
			sprint_addClass(curLayer,tog2);
		} else if(curClass.indexOf(" "+ tog2 + " ") != -1){
			sprint_delClass(curLayer,tog2);
			sprint_addClass(curLayer,tog1);
		}
	}
}

/*
// not using this one at the moment, so hiding it
function sprint_matchImgHeight(theImage,matchThis){
	if(!theImage.nodeName) theImage = d.getElementById(theImage);
	if(!matchThis.nodeName) matchThis = d.getElementById(matchThis);
	if(matchThis.offsetHeight <= theImage.height){
		theImage.parentNode.style.height = matchThis.offsetHeight+"px";
	}
}
*/

function sprint_changeTabs(curTab){
	curTab = d.getElementById(curTab);
	if(curTab.className.indexOf('active') == "-1"){
		tabSet = curTab.parentNode;
		
		tabs = tabSet.getElementsByTagName('LI');
		for(f=0; f<tabs.length; f++){
			hideLayer(tabs[f].id+'Detail');
			sprint_delClass(tabs[f],'active');
		}
		
		showLayer(curTab.id+'Detail');
		sprint_addClass(curTab,'active');
		this.blur;
	}
}

function innerTabs(section,tablist,tab,box){ // 
	if(!tablist.nodeName) tablist = d.getElementById(tablist);
	s = tablist.getElementsByTagName('li');
	for (var i=0; i<s.length; i++) {
		if(s[i].id == tab){
			sprint_addClass(s[i].id,'active');
		} else {
		sprint_delClass(s[i].id,'active');
		}
	}
	if(!section.nodeName) section = d.getElementById(section);
	f = section.getElementsByTagName('div');
	for (var i=0; i<f.length; i++) {
		if(f[i].id == box){
			//alert('show '+f[i].id);
			showLayer(f[i].id);
		} else if(f[i].id != ''){
			//alert('hide '+f[i].id);
			hideLayer(f[i].id);
		}
	}
}



var lastWindow;

function sprint_slide(slideWhat,startFrom,dir,offsetX,offsetY,winX,winY,winW,winH){
	theBox = d.getElementById(slideWhat);
	if(slideWhat != lastWindow && theBox.className.indexOf('show') == "-1"){
		if(!startFrom.nodeName) startFrom = d.getElementById(startFrom);
		imgs = startFrom.getElementsByTagName('img'); // firefox bug with anchors/images
		if(imgs.length >="1")startFrom = imgs[0];
		if(lastWindow){
			hideLayer(lastWindow);
		}
		
		if(winW >= "1") theBox.style.width = winW+"px";
		if(winH >= "1") theBox.style.height = winH+"px";
		
		newLeft = findPosX(startFrom)+offsetX;
		if(!isNaN(winX))newLeft = winY+offsetX;
		if(winX == "left")newLeft = findPosX(startFrom)+offsetX;
		if(winX == "right")newLeft = findPosX(startFrom)+startFrom.offsetWidth+offsetX;

		newTop = findPosY(startFrom)-106+offsetY; // -106 to compensate for being in the bodyArea
		if(!isNaN(winY))newTop = winX-106+offsetY;
		if(winY == "top")newTop = findPosY(startFrom)-106+offsetY;
		if(winY == "bot")newTop = findPosY(startFrom)-106+startFrom.offsetHeight+offsetY;

		sprint_addClass(slideWhat,"seen"); // show layer
		
		if(dir == "left"){
			linkW = startFrom.offsetWidth;
			theW = theBox.offsetWidth;
			newLeft = newLeft-theW;
		}
		
		theBox.style.top = newTop+"px";
		theBox.style.left = newLeft+"px";
		
		if(dir == "right"){
			sprint_slideRight(theBox.id, 10, 10, 0, 0);
		}
		if(dir == "left"){
			sprint_slideLeft(theBox.id, 10, 10, theW, 0);
		}
		if(dir == "down"){
			sprint_slideDown(theBox.id, 0, 5, 0, 0);
		}
		lastWindow = slideWhat;
	}
}

function sprint_slideRight(layerName, xInt, yInt, xClip, yClip){
	var theLayer = d.getElementById(layerName);
	if (xClip < theLayer.offsetWidth || yClip < theLayer.offsetHeight){
		xClip += xInt;
		yClip += yInt;
		theLayer.style.clip = "rect(auto, "+xClip+"px, "+yClip+"px, auto)";
		slideagain = setTimeout("sprint_slideRight('"+layerName+"', "+xInt+", "+yInt+", "+xClip+", "+yClip+");", 0);
	}
}

function sprint_slideLeft(layerName, xInt, yInt, xClip, yClip){
	var theLayer = d.getElementById(layerName);
	if (xClip > 0 || yClip < theLayer.offsetHeight){
		xClip -= xInt;
		yClip += yInt;
		theLayer.style.clip = "rect(auto, "+theLayer.offsetWidth+"px, "+yClip+"px, "+xClip+"px)";
		slideagain = setTimeout("sprint_slideLeft('"+layerName+"', "+xInt+", "+yInt+", "+xClip+", "+yClip+");", 0);
	}
}

function sprint_slideDown(layerName, xInt, yInt, xClip, yClip){
	var theLayer = d.getElementById(layerName);
	if (yClip < theLayer.offsetHeight){
		yClip += yInt;
		theLayer.style.clip = "rect(auto, "+theLayer.offsetWidth+"px, "+yClip+"px, "+xClip+"px)";
		slideagain = setTimeout("sprint_slideDown('"+layerName+"', "+xInt+", "+yInt+", "+xClip+", "+yClip+");", 0);
	}
}

function sprint_slideClose(layerName){
	var theLayer = d.getElementById(layerName);
	hideLayer(layerName);
	lastWindow = "";
}

function sprint_destroyFlashLayer(layerName){
	var theLayer = d.getElementById(layerName);
	d.getElementById('bodyArea').removeChild(theLayer);
	lastWindow = "";
}

function sprint_makeFlashLayer(layerName,flashW,flashH,flashVars,flashPath,pageName){
	if(!d.getElementById(layerName)){
		newLayer = d.createElement('DIV');
		newLayer.id = layerName;
		newLayer.className = "floatingWindow hidden";
		
		newLayerGuts = '	<div class="shadowBL">';
		newLayerGuts += '		<div class="shadowTR">';
		newLayerGuts += '			<h3 class="title"><span><a href="#" onclick="sprint_destroyFlashLayer(\''+layerName+'\'); return false;">X Close</a></span></h3>';
		newLayerGuts += '			<div class="content">';
		if(actualVersion >= 6){
			newLayerGuts += sprint_makeFlash(flashW,flashH,flashVars,flashPath);
		} else {
			newLayerGuts += 'Sorry, but you don\'t have Flash which is required to view this item.';
		}
		newLayerGuts += '			</div>';
		newLayerGuts += '		</div>';
		newLayerGuts += '	</div>';
		
		newLayer.innerHTML = newLayerGuts;
		d.getElementById('bodyArea').appendChild(newLayer);
		if(pageName)sprint_runOmniture(pageName);
	}
}

/* misc */
function toggleText(foo,tog1,tog2){ // toggles text that is within a span below the passed element
	if(!foo.nodeName) foo = d.getElementById(foo);
	s = foo.getElementsByTagName('span');
	for (var i=0; i<s.length; i++) {
		if(s[i].innerHTML == tog1){
			s[i].innerHTML = tog2;
		} else if(s[i].innerHTML == tog2){
			s[i].innerHTML = tog1;
		}
	}
}


function rolloverTo(picName,imgName) { // mouseover image
	d[picName].src = eval(imgName + ".src"); 
}


function sprint_runOmniture(pageName){
	s_pageName=pageName;
	void(setTimeout(s_gs(s_account),0));
}

// Omniture variables - setup all first, rewrite per page on page
var s_server=window.location.host;
var s_pageType="regular";
var s_pageName= s_channel= s_prop1= s_prop2= s_prop3= s_prop4= s_prop5= s_prop6= s_prop7= s_prop8= s_prop9= s_prop10="";
/* E-commerce Variables */
var s_campaign= s_state= s_zip= s_events= s_products= s_purchaseID= s_eVar1= s_eVar2= s_eVar3= s_eVar4= s_eVar5= s_eVar6= s_eVar7= s_eVar8= s_eVar9= s_eVar10="";


// flash detection
function sprint_flashDetect(reqVersion){ // detect users version of flash, and if it's high enough
	var maxFlashVersion = 9;
	if(actualVersion == ""){
		var gotIt = got2 = got3 = got4 = got5 = got6 = got7 = got8 = got9 = false;
		if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 && (navigator.appVersion.toLowerCase().indexOf("win") != -1)) {
			vbCode = '<scr'+'ipt language="VBScript"\> \n';
			vbCode += 'on error resume next \n';
			for(x=2; x<=maxFlashVersion; x++){
				vbCode += 'got'+x+' = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+x+'"))) \n';
			}
			vbCode += '</scr'+'ipt\> \n';
			d.write(vbCode);
			for (var i = 2; i <= maxFlashVersion; i++) {  
				if (eval("got" + i) == true) actualVersion = i;
			}
		} else {
			var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
			if (plugin) {
				actualVersion = parseInt(plugin.description.substring(plugin.description.indexOf(".")-1));
			}
		}
	}
	useFlashVer = getQueryVariable('useFlashVer');
	if(useFlashVer) actualVersion = useFlashVer;
	if(actualVersion >= reqVersion) gotIt = true;
	return gotIt;
}
sprint_flashDetect(6); // go ahead and run now for any future use

function sprint_makeFlash(w,h,vars,fileName){ // generate flash code based on passed vars
	swf  = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="'+window.location.protocol+'//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+w+'" height="'+h+'" class="flashTitle">\n';
	swf += '	<param name="movie" value="'+fileName+'" />\n';
	swf += '	<param name="wmode" value="transparent" />\n';
	swf += '	<param name="flashvars" value="'+vars+'" />\n';
	swf += '	<embed src="'+fileName+'" flashvars="'+vars+'" width="'+w+'" height="'+h+'" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" class="flashTitle" wmode="transparent"><'+'/embed>\n';
	swf += '<'+'/object>\n';
	return swf;
}

function sprint_flashMessaging(reqVer,layerName,message){ // generate flash code based on passed vars
	if(sprint_flashDetect(reqVer)){
		showLayer(layerName);
	}else{
		document.write('<div class="noticeBox"><p>'+message+'</p></div>');
	}
}

/* legacy */
function makeButton(URL,btnText,btnColor) {

imgColor=(btnColor!="red")?'g':'r';
bgColor=(btnColor!="red")?'#999999':'#ff0000';
topColor=(btnColor!="red")?'#C4C4C4':'#fb533c';
btmColor=(btnColor!="red")?'#6c6c6c':'#822a15';

document.write('<table cellpadding="0" cellspacing="0" border="0"><tr><td align="right" rowspan="3"><a href='/personal/common/scripts/+URL+' onfocus="if(this.blur)this.blur()"  onmouseover="window.status=\''+btnText+'\';return true" onmouseout="window.status=\'\';return true"><img src="/media/Assets/Controls/button_'+imgColor+'_leftedge.gif" width="7" height="15" border="0"></a></td><td align="left" valign="middle" bgcolor='+topColor+'><img src="/media/Assets/Controls/clear.gif" width="1" height="1" border="0"></td><td align="left" rowspan="3"><a href='/personal/common/scripts/+URL+' onfocus="if(this.blur)this.blur()"  onmouseover="window.status=\''+btnText+'\';return true" onmouseout="window.status=\'\';return true"><img src="/media/Assets/Controls/button_'+imgColor+'_rightedge.gif" width="7" height="15" border="0"></a></td></tr><tr><td align="left" valign="middle" bgcolor= '+bgColor+' nowrap><b><a href='/personal/common/scripts/+URL+' onfocus="if(this.blur)this.blur()" class="buttonlink" onmouseover="window.status=\''+btnText+'\';return true" onmouseout="window.status=\'\';return true">'+btnText+'</a></b></td></tr><tr><td bgcolor='+btmColor+'><img src="/media/Assets/Controls/clear.gif" width="1" height="2" border="0"></td></tr></table>'); 
}



function wFix(whichElems){ // IE PC - Sliced capital W in Arial fix
	insideOf = document.getElementById('bodyArea');
	elemList = new Array('P','A','LABEL');
	for(x=0; x<elemList.length; x++){
		curList = insideOf.getElementsByTagName(elemList[x]);
		for(y=0; y<curList.length; y++){
			foo = curList[y].innerHTML.replace(/(W\w+) /g,'<span class="wFix">$1</span> ');//<span class="wFix">W</span>
			curList[y].innerHTML = foo;
		}
	}
}
// build left nav from page variables set in header script
function buildMenu(s,p) {
	for (var i=0; i<s.length; i++) {
		if(s[i] != 'none'){showLayer(s[i]);} 
		}
	for (var i=0; i<p.length; i++) {
		if(p[i] != 'none'){sprint_addClass(p[i],'active');	} 
		}
	var listItem = ('item'+p[0]);
	if (listItem != 'itemnone'){sprint_addClass(listItem,'activeLI');}
};


//var detect = navigator.userAgent.toLowerCase();
//if(detect.indexOf('msie') != "-1" && detect.indexOf('mac')=="-1")sprint_addLoadEvent(wFix);

//following functions might not be used anywhere
// Jump Menu

function jumpMenu(targ,selObj,restore){
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

//PopUp Window

function openWindow(url) {
  popupWin = window.open(url, name, 
'scrollbars,resizable,width=360,height=400,left=20,top=20')
}

//Footer PopUp Window
function openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
