/**
* @overview This library contains all the client side detection functionality
* @uses lib/Sureflix/Sureflix.js
* @uses lib/Prototype/Prototype.js
* @uses lib/Sureflix/Utilities/Utils.js
* @uses lib/Sureflix/Plugins/Plugins.js
* @uses lib/Sureflix/Error/ErrorHandler.js
* @uses ClientServerBridge.asp
*/

/**
 * @class This class contains client site browser and plugin detection functionality.
 * @property {String} browser The browser's name: Firefox, Explorer, Safari, etc. or Unknown
 * @property {Float} version The browser's version or Unknown
 * @property {String} OS The operating system's name: Windows, Mac, Linux, etc. or Unknown
 */
Sureflix.Plugins.Detector = {
	browser:	null,
	version:	null,
	OS:			null
}


// shorthand
SxDt = Sureflix.Plugins.Detector;


/**
* enumeration of platforms, which map to plugins depending on the OS and Browser
*/
Sureflix.Plugins.Detector.platforms = {
	RealPlayer: "r",
	WindowsMedia: "w",
	Silverlight: "s",
	Flash: "f",
	iPhone: "i",
	Android: "d"
}


/**
* Detects the browser and operating system, and sets the class's properties, which can then be accessed by you. 
*/
Sureflix.Plugins.Detector.detectBrowser = function() {
	//if browser is already detected, don't run this function again
	if (this.browser) {
		return this;
	}

	this.browser = this.searchString(this.dataBrowser) || "Unknown";
	this.version = this.searchVersion(navigator.userAgent)
		|| this.searchVersion(navigator.appVersion)
		|| "Unknown";
	this.OS = this.searchString(this.dataOS) || "Unknown";
	return this;
}


Sureflix.Plugins.Detector.isSmartphone = function() {
	this.detectBrowser();

	return (this.OS == "iPhone" || this.OS == "iPod" || this.OS == "iPad" || this.OS == "Android");
	identity: "iPod"
}


/**
 * Searches for the browser or OS (used internally).
 * @param {Object} data
 */
Sureflix.Plugins.Detector.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;
	}
}


/**
 * Searches for the version (used internally).
 * @param {Object} data
 */

Sureflix.Plugins.Detector.searchVersion = function (dataString) {
	var index = dataString.indexOf(this.versionSearchString);
	if (index == -1) return;
	return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
}


/**
 * Array of pre-defined browser strings used by searchString (used internally).
 */
Sureflix.Plugins.Detector.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"
	}
]


/**
 * Array of pre-defined OS strings used by searchString (used internally).
 */

Sureflix.Plugins.Detector.dataOS = [
	{
		string: navigator.platform,
		subString: "Win",
		identity: "Windows"
	},
	{
		string: navigator.platform,
		subString: "Mac",
		identity: "Mac"
	},
	{
		string: navigator.platform,
		subString: "iPhone",
		identity: "iPhone"
	},
	{
		string: navigator.platform,
		subString: "iPod",
		identity: "iPod"
	},
	{
		string: navigator.platform,
		subString: "iPad",
		identity: "iPad"
	},
	{
		string: navigator.userAgent,
		subString: "Android",
		identity: "Android"
	},
	{
		string: navigator.platform,
		subString: "Linux",
		identity: "Linux"
	}
]


/**
 * Returns true if cookies are enabled.
 */
Sureflix.Plugins.Detector.areCookiesEnabled = function () {

	function xDeleteCookie(name) {
		var oldDate = new Date(1970, 1, 1);
		document.cookie = x_cookieName + "=0; expires=" + oldDate.toGMTString();
	}
	
	//set cooke, then try to read cookie
	var x_cookieName = "xyzzy"; // unique

	var x_expires = new Date(); 
	x_expires.setFullYear(x_expires.getFullYear()+1); // testing persistent cookies
	xDeleteCookie(x_cookieName); // don't get false positive

	// add path if you want to. drop expires for session (non=persistent) cookie.
	document.cookie = x_cookieName + "=test";
	document.cookie.expires=" + x_expires.toGMTString() + ";

	// now look for it.
	var x_cookieString = document.cookie || "";
	var x_cookies = x_cookieString.split(/\s*;\s*/);
	var x_found = false;
	
	for (var i=0; i < x_cookies.length; i++) { // we can't use for - in loops with prototype (pg 25 in 1.5.1 manual)
		var cookie = x_cookies[i];
		if(cookie){
			var dough = cookie.split(/\s*=\s*/);
			if (dough[0] == x_cookieName) { x_found = true; break; }
		}
	}

	// ensure it's gone
	xDeleteCookie(x_cookieName);

	return x_found;
}