﻿var messagesDiv;
var networkID = 3000207;
var detectedBandwidth = "";

/*
* This is the main function that deals with detecting both the proper browser and the proper version of Flash.
* 
* The proper version of Flash is necessary for the user controls and the proper browser version is necessary for proper workings of the player period.
*/
function StartDetection()
{
    var flashSpeedDetect = document.getElementById("SpeedDetection");
    DetectBrowser();
    DetectFlash();
    flashSpeedDetect.BeginSpeedCheck();
}

/*
* This will detect the proper version of Flash. For our purposes, we require Flash 9+ as the controls are compiled in Flash 9
*
* This function will write to the appropriate messages div and then return success or failure to be handled by the calling function.
*/
function DetectFlash()
{
    // Major version of Flash required
    var requiredMajorVersion = 9;

    // Minor version of Flash required
    var requiredMinorVersion = 0;

    // Minor version of Flash required
    var requiredRevision = 0;
    
    // Version check for the Flash Player that has the ability to start Player Product Install (6.0r65)
    var hasProductInstall = DetectFlashVer(6, 0, 65);
    
    // Version check based upon the values defined in globals
    var hasRequestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
    
    // Get the div to set information
    var flashDiv = document.getElementById("flashDiv");
    
    
    // Check to see if a player with Flash Product Install is available and the version does not meet the requirements for playback
    if ( hasProductInstall && !hasRequestedVersion ) 
    {
                   
        flashDiv.innerHTML = "<div class=\"statusFlash\"> \
            <img src=\"img/requirement_warning.gif\" /> \
            </div><div class=\"categoryFlash\">Flash</div> \
            <div class=\"requirementsFlash\"> \
            The Knot TV requires the Adobe Flash Player version 9 or above. \
            <a href=\"http://www.adobe.com/go/getflash/>Get Flash</a>\"";
    } 
    else if (hasRequestedVersion) 
    {
        flashDiv.innerHTML = "<div class=\"statusFlash\"> \
            <img src=\"img/requirement_passed.gif\" /> \
            </div><div class=\"categoryFlash\">Flash</div> \
            <div class=\"requirementsFlash\"> \
            Flash 9.0 or higher detected!</div>";
            
    } 
    else 
    {  
        flashDiv.innerHTML = "<div class=\"statusFlash\"> \
            <img src=\"img/requirement_warning.gif\" /> \
            </div><div class=\"categoryFlash\">Flash</div> \
            <div class=\"requirementsFlash\"> \
            The Knot TV requires the Adobe Flash Player version 9 or above.\
            <a href=\"http://www.adobe.com/go/getflash/>Get Flash</a>\"";
    }

    
   return false;
}
/*
* Detects whether the proper browser is being used. For our purposes, this is Firefox 1.9+ (OSX/Windows/Linux), IE6/7 or Mac Safari 
*
* This function will write to the appropriate messages div and then return success or failure to be handled by the calling function.
*
*/
function DetectBrowser()
{
   var BrowserDetect = {
	    init: function () {
		    this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		    this.version = this.searchVersion(navigator.userAgent)
			    || this.searchVersion(navigator.appVersion)
			    || "an unknown version";
		    this.OS = this.searchString(this.dataOS) || "an unknown OS";
	    },
	    searchString: function (data) {
		    for (var i=0;i<data.length;i++)	{
			    var dataString = data[i].string;
			    var dataProp = data[i].prop;
			    this.versionSearchString = data[i].versionSearch || data[i].identity;
			    if (dataString) {
				    if (dataString.indexOf(data[i].subString) != -1)
					    return data[i].identity;
			    }
			    else if (dataProp)
				    return data[i].identity;
		    }
	    },
	    searchVersion: function (dataString) {
		    var index = dataString.indexOf(this.versionSearchString);
		    if (index == -1) return;
		    return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	    },
	    dataBrowser: [
		    { 	string: navigator.userAgent,
			    subString: "OmniWeb",
			    versionSearch: "OmniWeb/",
			    identity: "OmniWeb"
		    },
		    {
			    string: navigator.vendor,
			    subString: "Apple",
			    identity: "Safari"
		    },
		    {
			    prop: window.opera,
			    identity: "Opera"
		    },
		    {
			    string: navigator.vendor,
			    subString: "iCab",
			    identity: "iCab"
		    },
		    {
			    string: navigator.vendor,
			    subString: "KDE",
			    identity: "Konqueror"
		    },
		    {
			    string: navigator.userAgent,
			    subString: "Firefox",
			    identity: "Firefox"
		    },
		    {
			    string: navigator.vendor,
			    subString: "Camino",
			    identity: "Camino"
		    },
		    {		// for newer Netscapes (6+)
			    string: navigator.userAgent,
			    subString: "Netscape",
			    identity: "Netscape"
		    },
		    {
			    string: navigator.userAgent,
			    subString: "MSIE",
			    identity: "Explorer",
			    versionSearch: "MSIE"
		    },
		    {
			    string: navigator.userAgent,
			    subString: "Gecko",
			    identity: "Mozilla",
			    versionSearch: "rv"
		    },
		    { 		// for older Netscapes (4-)
			    string: navigator.userAgent,
			    subString: "Mozilla",
			    identity: "Netscape",
			    versionSearch: "Mozilla"
		    }
	    ],
	    dataOS : [
		    {
			    string: navigator.platform,
			    subString: "Win",
			    identity: "Windows"
		    },
		    {
			    string: navigator.platform,
			    subString: "Mac",
			    identity: "Mac"
		    },
		    {
			    string: navigator.platform,
			    subString: "Linux",
			    identity: "Linux"
		    }
	    ]

    };
    
    BrowserDetect.init();
    
    // Get the div to later set.
    var browserDiv = document.getElementById("browserDiv");
    switch (BrowserDetect.browser)
    {
        case "Explorer":
            browserDiv.innerHTML = "<div class=\"statusBrowser\"> \
                <img src=\"img/requirement_passed.gif\" /> \
                </div><div class=\"categoryBrowser\">Browser</div> \
                <div class=\"requirementsBrowser\"> \
                Your browser is compatible!</div>";
            break;
        case "Firefox":
            browserDiv.innerHTML = "<div class=\"statusBrowser\"> \
                <img src=\"img/requirement_warning.gif\" /> \
                </div><div class=\"categoryBrowser\">Browser</div> \
                <div class=\"requirementsBrowser\"> \
                Your browser is compatible, but Internet Explorer 6 or higher is preferred.</div>";
            break;
        default:
            browserDiv.innerHTML = "<div class=\"statusBrowser\"> \
                <img src=\"img/requirement_warning.gif\" /> \
                </div><div class=\"categoryBrowser\">Browser</div> \
                <div class=\"requirementsBrowser\"> \
                Your browser is incompatible. The browsers supported are Internet Explorer 6 or higher and \
                Firefox 1.9 and higher.</div>";
            break;
    }
}

/**
 * Determines what to do based on the speed detection results.
 *
 */
function OnSpeedDetectionComplete(kbps)
{
    detectedBandwidth = kbps;
    
    var speedDetectionDiv = document.getElementById("speedDetectionDiv");
    
    try {
        speedDetectionDiv.innerHTML = "<div class=\"statusBandwidth\"> \
            <img src=\"img/requirement_passed.gif\" /> \
            </div><div class=\"categoryBandwidth\">Bandwidth</div> \
            <div class=\"requirementsBandwidth\"> \
            Bandwidth detection complete.</div>";
            
        if (logToConsole) {
            console.log('OnSpeedDetectionComplete() - playlist URL: ' + asxGeneratorUrl);
        }

        // This keeps the system check results visible for a while
        // so user can read results.
        setTimeout('SetStreamURL()', systemCheckResultsDisplayTime);
    }
    catch (e) {
        if (logToConsole) {
            console.log('OnSpeedDetectionComplete() - Exception occurred.  ' + e);
        }
    }
}

function SetStreamURL() {
    var selectedStreamID;

    // Determine which stream to use.
    for (var i = 0; i < streamRates.length; i++) {
        selectedStreamID = streamRateIDs[i];
        if (parseInt(streamRates[i]) >= parseInt(detectedBandwidth)) break;
    }

    if (false == browserIsIE) {
        //alert('timeoutForAdRequestedCheck = ' + timeoutForAdRequestedCheck);
        setTimeout("startAdSequencing('OnSpeedDetectionComplete() spawned timer');", timeoutForAdRequestedCheck);
    }

    // Hide systemcheck div so it isn't seen during the player load.
    document.getElementById("systemcheck").style.visibility = "hidden";

    var randomnumber = Math.floor(Math.random() * 100000);  // Random number for URL to prevent caching.
    SetURL(asxGeneratorUrl + "?sid=" + selectedStreamID + "&networkID=" + networkID + "&rndnum=" + randomnumber);

    //alert("Playlist URL = " + asxGeneratorUrl + "?sid=" + selectedStreamID + "&networkID=" + networkID);
}

