//--------------------------------------------------------------------
// globals and constants
//--------------------------------------------------------------------
//

//This is to check if the user has ExternalInterface capability in their browser
//
var gExternalInterfaceNotAvailable = true;

//this is used to hold the div that the user of the corp site navigation is on
//
var gCorpDropDownDivName;

// Detect Client Browser type
// This should get cleaned up
//
var isIE            = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin           = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera         = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
var isSafari        = (navigator.userAgent.toLowerCase().indexOf("applewebkit") != -1) ? true : false;
var jsVersion       = 1.1;
var gPosition       = -1;
var gTimer;
var gIsDemoPlaying  = true;
var gLocationChange;
var gIsChangeLocation = false;

// -------------------------------------------------------------------
// Common Set, Get and Delete cookie methods. 
// -------------------------------------------------------------------
//
function SetCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
        ((expires)? "; expires=" + expires : "") +
        ((path)	  ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function GetCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function DeleteCookie(name, path, domain) {
	if (GetCookie(name)) document.cookie = name + "=" +
		((path)   ? ";path=" + path : "") +
		((domain) ? ";domain=" + domain : "" ) +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}




// -------------------------------------------------------------------
// General methods for presto corp
// -------------------------------------------------------------------
//


// Simple window opener for the corporate site
//
function MM_openBrWindow(theURL,winName,features) { //v2.0
    window.open(theURL,winName,features);
}

// This is a simple function when the demo page can open a link on its parent
// page and then shut itself
//
function parentWindowLocationChange(theUrl){
    window.opener.location=theUrl;
    window.close();
}

// This is a function to close the modal dialog box using the fade out close
// and then send the user to the buy now page. This is a better transition than the old way
//
function demoLocationChange(theUrl){
    CloseWindow();
    gLocationChange = theUrl;
    gIsChangeLocation = true;
}

//This is a method to walk the dom of the page it is called on an turn the visibilty of the selection boxes off
//
function selectionSwap(inOrOut){

    //lets first walk thru the DOM and set the visibilty of all of the selection boxes to hidden
    //
    var selectArray = document.getElementsByTagName("select");
    
    //This is to target IE 6 and its inablity to handle z-indexes and layers
    //
    if (window.external && (typeof window.XMLHttpRequest == "undefined")) {

        if(inOrOut == "outSwap")
        {
            for(var i = 0; i < selectArray.length; i++)
            {
                selectArray[i].style.visibility ="hidden";
            }
        }
        else
        {
             for(var i = 0; i < selectArray.length; i++)
            {
                selectArray[i].style.visibility ="visible";
            }
        }
    }
}

// -------------------------------------------------------------------
// Script for tracking within flash movies 
// -------------------------------------------------------------------
//
function flashOmnitureTracker(linkLocation, linkName, obj) {
	var s=s_gi(s_account);
	s.linkTrackVars='None';
	s.linkTrackEvents='None';
	s.tl(obj,'o',linkName);
	window.location=linkLocation;
}

function flashOmnitureTrackerPopUp(linkLocation, linkName, obj) {
	var s=s_gi(s_account);
	s.linkTrackVars='None';
	s.linkTrackEvents='None';
	s.tl(obj,'o',linkName);
	window.open(linkLocation);
}



// -------------------------------------------------------------------
// makeCallToFlash()
//     This is for talking to a flash movie 
// Arguments:
//	 moviename	- the id of the movie element
//	 str	    - the args to call into the movie. 
// -------------------------------------------------------------------
//
function makeCallToFlash(movieName,str) {
    var movie = (isIE) ? window[movieName] : movie = document[movieName];
    movie.movieControl(str);
}


// -------------------------------------------------------------------
// flashCEOVidFirstTime()
//     This is for talking to a flash movie 
// Arguments:
//	firstView	    - true or false
//  
//
// -------------------------------------------------------------------
//
function flashCEOVidFirstTime(firstView, domain)
{
    //set the expire time to 3 weeks from now
    //   
    var exDate=new Date();
    exDate.setDate(exDate.getDate()+21);
    
    document.cookie = "CEOFirstTime=" + escape(firstView) + "; expires=" + exDate.toGMTString()+ "; domain=" + domain;
}

// ----------------------------------------------------------------------
// This is a collection of builtin methods used for  flash
// ----------------------------------------------------------------------



// -------------------------------------------------------------------
// JSGetSwfVer()
//     JavaScript helper required to detect Flash Player PlugIn 
//     version information
// -------------------------------------------------------------------
//
function JSGetSwfVer(i){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
      		var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			descArray = flashDescription.split(" ");
			tempArrayMajor = descArray[2].split(".");
			versionMajor = tempArrayMajor[0];
			versionMinor = tempArrayMajor[1];
			if ( descArray[3] != "" ) {
				tempArrayMinor = descArray[3].split("r");
			} else {
				tempArrayMinor = descArray[4].split("r");
			}
      		versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
            flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
      	} else {
			flashVer = -1;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	// Can't detect in all other cases
	else {
		
		flashVer = -1;
	}
	return flashVer;
} 

// -------------------------------------------------------------------
// DetectFlashVer()
//     JavaScript helper to verify the version of the flash client. 
//     If called with no parameters this function returns a floating 
//     point value which should be the version of the Flash Player 
//     or 0.0  ex: Flash Player 7r14 returns 7.14
// -------------------------------------------------------------------
//
function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) 
{
 	reqVer = parseFloat(reqMajorVer + "." + reqRevision);
   	// loop backwards through the versions until we find the newest version	
	for (i=25;i>0;i--) {	
		if (isIE && isWin && !isOpera) {
			versionStr = VBGetSwfVer(i);
		} else {
			versionStr = JSGetSwfVer(i);		
		}
		if (versionStr == -1 ) { 
			return false;
		} else if (versionStr != 0) {
			if(isIE && isWin && !isOpera) {
				tempArray         = versionStr.split(" ");
				tempString        = tempArray[1];
				versionArray      = tempString .split(",");				
			} else {
				versionArray      = versionStr.split(".");
			}
			versionMajor      = versionArray[0];
			versionMinor      = versionArray[1];
			versionRevision   = versionArray[2];
			
			versionString     = versionMajor + "." + versionRevision;   // 7.0r24 == 7.24
			versionNum        = parseFloat(versionString);
        	// is the major.revision >= requested major.revision AND the minor version >= requested minor
			if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
				return true;
			} else {
				return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );	
			}
		}
	}	
	return (reqVer ? false : 0.0);
}


//---------------------------------------------------
// This is a method for the wayfinder site to allow
// emails to link to the cart and still get tracking
// this will redirect the user to the page 
//---------------------------------------------------
//this is to redirect the user to the cart page
function redirectToCart(urlVar)
{
window.location=urlVar;
}

//---------------------------------------------------
// This is a method for the users from the corporate
// site and transfering them somewhere else
//---------------------------------------------------
//this is to redirect the user to the passed in page
function redirectUserFromCorpSite(urlVar)
{
window.location=urlVar;
}

//---------------------------------------------------
// This is a method for changing a select list to a certain index
// PARAMS:
//        passedSelectName: the element id of the select list
//        passedIndexNum: the index of the select list to select
//        passedIsChange: boolean of to update of not
//---------------------------------------------------
//
function changeSelectionList(passedSelectName, passedIndexNum, passedIsChange)
{
    //lets check if we need to update this. The reason for this is that this could be on a selection list
    //with a non choice selection so we dont want to do any thing in that case
    //
    if(passedIsChange)
    {
        //ok lets get ahold of the id that has been passed in 
        //    
        var selectList = document.getElementById(passedSelectName);
        
        //now lets set the selected value to the passedIndexNum for the list
        //
        selectList.options[passedIndexNum].selected = true;
    }
}


//---------------------------------------------------
// This is a method for opening a section of the drop down nav for the corp site
// PARAMS:
//        passedIdName: the element id of the list
//---------------------------------------------------
//
function CorpMenuOpen(passedIdName)
{	
	//close menu div that we were on if there is a value
	//
	if(gCorpDropDownDivName)
	{ 
	    gCorpDropDownDivName.style.visibility = 'hidden';
	}

	//get the new div and replace the old div in the globalNav
	//
	gCorpDropDownDivName = document.getElementById(passedIdName);
	
	//now set the style to visible for the passedIdName element
	//
	gCorpDropDownDivName.style.visibility = 'visible';

}

//---------------------------------------------------
// This is a method for shutting a section of the drop down nav for the corp site
// PARAMS:
//        NONE
//---------------------------------------------------
//
function CorpMenuClose()
{

    //if the gCorpDropDownDivName has a value then set the visiblity of that element to false
    //
    if(gCorpDropDownDivName)
    { 
        gCorpDropDownDivName.style.visibility = 'hidden';
    }

}

// Open chat window with the given URL
function openChatWindow(url)
{
    window.open(url,'Presto_Support',"resizable=1,status=1,width=570,height=570");
}

//====================================================
// showElement takes an id and shows the element
//====================================================
function showElement(idOfElement)
{
    var styleElementId = document.getElementById(idOfElement);
    styleElementId.style.display = "block";
}

//====================================================
// Adds a show or hide css rule for passed in class
//====================================================
function showHideClass(passedStyleSheetNumber, passedIsShow, passedClassName)
{
    var cssRuleSet = document.styleSheets[passedStyleSheetNumber];
    var displayString = "display:"
    var stylingToUse = "none;";
    
    //now that we have a rule that has been added lets see if we need to show or 
    //hide it
    if(passedIsShow)
    {
	    stylingToUse = "block";
    }
	
	
    //this is to make our script cross browser compatible
    //
    //Check for Mozilla          
	if (cssRuleSet.insertRule) 
	{
		cssRuleSet.insertRule(passedClassName +'{' + displayString + stylingToUse + '}', 100 );					  
	}
	//Check for IE 
	else if (cssRuleSet.addRule) 
	{
		cssRuleSet.addRule(passedClassName, (displayString + stylingToUse));
	}
	

}

//====================================================
// This is the method that chat iframe calls back to
//====================================================
function chatAgentAvailableCallback(isTrue)
{
    if(isTrue)
    {
        showHideClass(0, true, ".chatLink");
    }
    else
    {
        showHideClass(0, true, ".chatLink");
    }
}
