

/* This library is designed to give you three pieces of information:

	a. info.device -- the name/ID of the specific device: e.g. "Motorola Droid"
	b. info.os -- the name/ID of the operating system, e.g. "Android"
	c. info.category -- what class of device this is (mobile, tablet, desktop, appliance, etc.)
	
*/

/* version 0.9
   4/16/2010 - ABE
   added winOSFamily to desktopDeviceCategory
   changed categories sequence to detect desktops first -- and will set deviceCategory=unknownDeviceCategory.id if no match can be found.
*/

/* TODO: incorporate all known desktop OS's  --> good resource is http://www.geekpedia.com/code47_Detect-operating-system-from-user-agent-string.html */

/* Copyright (c) 2010 Adobe Systems Incorporated. * All rights reserved. * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */



	var droidDeviceProfile = {id:"Motorola Droid",frag:/droid build/};
	var nexusDeviceProfile =  {id:"Google Nexus One",frag:/nexus one build/i};
	var palmPreDeviceProfile = {id:"Palm Pre", frag:/525.27.1 pre/i};
	

	var win311DeviceProfile = {id:"Windows 3.11",frag:/win16/i};
	var win95ADeviceProfile = {id:"Windows 95",frag:/windows 95/i};
	var win95BDeviceProfile = {id:"Windows 95",frag:/win95/i};
	var win95CDeviceProfile = {id:"Windows 95",frag:/win_95/i};
	var win2000ADeviceProfile = {id:"Windows 2000",frag:/windows 2000/i};
	var win2000BDeviceProfile = {id:"Windows 2000",frag:/windows nt 5.0/i};

	var winServer2003DeviceProfile = {id:"Windows Server 2003",frag:/windows nt 5.2/i};
	
	var winNT40ADeviceProfile = {id:"Windows NT 4.0",frag:/windows nt 4.0/i};
	var winNT40BDeviceProfile = {id:"Windows NT 4.0",frag:/winnt/i};
	var winNT40CDeviceProfile = {id:"Windows NT 4.0",frag:/windows nt/i}; // need to make sure this gets processed last as it would otherwise prevent correct id
	// of windows 2000 for example or replace with regular expression that is more strict.

	var winmeDeviceProfile = {id:"Windows ME",frag:/windows me/i};

	var openBSDDeviceProfile = {id:"OpenBSD",frag:/openbsd/i};
	var sunOSDeviceProfile = {id:"Sun OS",frag:/sunos/i};
	var linuxADeviceProfile = {id:"Linux",frag:/linux/i};
	var linuxBDeviceProfile = {id:"Linux",frag:/x11/i};
	var QNXDeviceProfile = {id:"QNX",frag:/qnx/i};
	var beosDeviceProfile = {id:"BeOS",frag:/beos/i};
	var os2DeviceProfile = {id:"OS2",frag:/OS\/2/i};

	var winxpDeviceProfile = {id:"Windows XP",frag:/windows xp/i};
	var winxp2DeviceProfile = {id:"Windows XP",frag:/windows nt 5.1/i};
	var win7ADeviceProfile = {id:"Windows 7",frag:/windows nt 6.1/i};
	var win7BDeviceProfile = {id:"Windows 7",frag:/windows nt 7.01/i};
	var winvistaDeviceProfile = {id:"Windows Vista",frag:/windows nt 6.0/i};
	var macosx106DeviceProfile =  {id:"Snow Leopard",frag:/mac os x 10.6/i};
	var macosx105DeviceProfile =  {id:"Leopard",frag:/mac os x 10.5/i};
	var macosA = {id:"Mac OS",frag:/mac_powerpc/i};
	var macosB = {id:"Mac OS",frag:/macintosh/i};


/* OPERATING SYSTEMS */
	
	var androidOSFamily = {id:"Android OS",frag:/android /i,
	flashfrag:/android (2.2|2.3|2.4|2.5|2.6|2.7|2.8|2.9|3|4|5|6|7|8|9)/i, devices:[droidDeviceProfile,nexusDeviceProfile]};
	var webOSFamily = {id:"webOS",frag:/webOS\/1.3.5/i,devices:[palmPreDeviceProfile]};
	
// windows mobile family
// RIM
// etc...



	// winmoOSFamily
	var macOSFamily = {id:"Mac OS",frag:/mac os/i,devices:[macosx105DeviceProfile,macosx106DeviceProfile,macosA, macosB]};
	// windowsOSFamily

	var winOSFamily = {id:"Windows",frag:/windows/i,devices:[winxpDeviceProfile,winxp2DeviceProfile,win7ADeviceProfile,win7BDeviceProfile,winvistaDeviceProfile,win311DeviceProfile,win95ADeviceProfile,win95BDeviceProfile,win95CDeviceProfile,winServer2003DeviceProfile,winNT40ADeviceProfile,winNT40BDeviceProfile,winNT40CDeviceProfile,winmeDeviceProfile]};

	var linuxOSFamily = {id:"Linux",frag:/linux/i,devices:[openBSDDeviceProfile,sunOSDeviceProfile,linuxADeviceProfile, linuxBDeviceProfile, QNXDeviceProfile,beosDeviceProfile,os2DeviceProfile]};

/* CATEGORIES */

	var desktopDeviceCategory = {id:"Desktop",osFamilies:[macOSFamily,winOSFamily,linuxOSFamily]};
	
	var mobileDeviceCategory = {id:"Mobile",osFamilies:[androidOSFamily]};
	
	var unknownDeviceCategory = {id:"Unidentified Platform"};
	
	var categories = [mobileDeviceCategory,desktopDeviceCategory /*,mobileDeviceCategory*/];



	function identifyDevice(d,ua) {
//		document.write('<div style="color:white">'+d.frag+' =? '+ua+'</div>');
  		if (ua.search(d.frag) > -1) {
//			document.write('<div style="color:white">MATCH!</div>');
		  	return {device:d.id, deviceReference:d};
		} else
		  return null;
	}

	function identifyOS(os,ua) {
		var deviceInfo=null;
		var olen = os.devices.length;
		for(var k=0;k<olen;k++) {
			deviceInfo = identifyDevice(os.devices[k],ua);
			if(deviceInfo!=null) break;
		}
		if(deviceInfo!=null) {
			deviceInfo.os = os.id;
			deviceInfo.osReference = os;
		}
		return deviceInfo;
	}


	function identifyCategory(cat,ua) {
		var osInfo=null;
		var jlen = cat.osFamilies.length;
		for(var j=0;j<jlen;j++) {
			osInfo = identifyOS(cat.osFamilies[j],ua);
			if(osInfo!=null) break;
		}
		if(osInfo!=null) osInfo.category = cat.id;
		return osInfo;
	}

	
	function identifyCategories(cats,ua) {
		var categoryInfo=null;
		var clen = cats.length;
		for(var i=0;i<clen;i++) {
			categoryInfo = identifyCategory(cats[i],ua);
			if(categoryInfo!=null) break;
		}
		
		if(!categoryInfo) categoryInfo = {};
		if(!categoryInfo.device) categoryInfo.device='unknown';
		if(!categoryInfo.os) categoryInfo.os = 'unknown';
		if(!categoryInfo.category) categoryInfo.category = unknownDeviceCategory.id;
		return categoryInfo;
	}
	
	function isBlacklistedDevice(d) {
		var retVal = false;
		
		// as exceptions are known they would be added here...
		if(d.id==droidDeviceProfile.id) retVal = false;
		
		return retVal;
	}
	
	function isFlashCapable(cInfo) {
		var retVal= false;
		var ua = navigator.userAgent.toLowerCase();
		
		// true if it's mac, linux, or windows 
		// otherwise, based on the OS...
		// true if it's an "Android [s]" where x>=2.2
		// unless the specific model is blacklisted (should firmware be taken into account?)
		
		if(cInfo.category==null) {
			if(ua.search(androidOSFamily.flashfrag)>-1) {
						retVal = true;

			}		
		} 
		
		// else if(cInfo.category==desktopDeviceCategory.id)  { 
		// 			retVal = true; 
		// 		} else {
		// 			if(ua.search(cInfo.osReference.flashfrag)>-1) {
		// 				if(!isBlacklistedDevice(cInfo.deviceReference)) {
		// 					retVal = true;
		// 				}
		// 			}
		// 		}
		
		return retVal;
	}
	
	// external method
	// returns an object that should contain
	//		category (such as 'Desktop' or 'Mobile'
	//		os			(such as 'Windows', 'Mac OS', 'Android', etc.
	//		device		(such as 'Windows XP','Android', etc.
	
	// TBD:
	//		osversion
	//		carrier
	//		deviceregion
	
	function getCategoriesInfo() {
		return identifyCategories(categories, navigator.userAgent.toLowerCase());
	}
	
	function displayCategoriesInfo() {
		alert('isDesktop:'+isDesktop()  + ', os:' + info.os + ', device:' + info.device);
	} 
	
	function isDroid() {
		return (info.device==droidDeviceProfile.id);
	}
	
	function isNexus() {
		return(info.device==nexusDeviceProfile.id);
	}
	
	function isDesktop() {
		return(info.category==desktopDeviceCategory.id);
	}
	
	function simulateDroid() {
		info.device = droidDeviceProfile.id;
		info.category = mobileDeviceCategory.id;
		info.os = androidOSFamily.id;
	}
	
	function setMobileCookie(name, value) {
		
		var exp = new Date();     //set new date object
		exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24));
		
		document.cookie = name + "=" + escape(value) + "; path=/" + ((exp == null) ? "" : "; expires=" + exp.toGMTString()); 
	}
	
	
	// function getCookie (name) {
	//     var dc = document.cookie;
	//     var cname = name + "=";
	// 
	//     if (dc.length > 0) {
	//       begin = dc.indexOf(cname);
	//       if (begin != -1) {
	//         begin += cname.length;
	//         end = dc.indexOf(";" begin);
	//         if (end == -1) end = dc.length;
	//         return unescape(dc.substring(begin, end));
	//         }
	//       }
	//     return null;
	// }
	// 
	function getCookie(cookieName) {
			var theCookie=""+document.cookie;
			var ind=theCookie.indexOf(cookieName);
			if (ind==-1 || cookieName=="") return ""; 
			var ind1=theCookie.indexOf(';',ind);
			if (ind1==-1) ind1=theCookie.length; 
			return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
		}
	
	
	
	function delCookie(name) {
		document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/";
	}
	
	function forceDesktop() {
		
		setMobileCookie('eosMobile', 'DESKTOP');
		window.location = "http://localhost/templates/home.html"; 
		
	}
	
	function getQueryString() {
	    var assoc = new Array();
	    var queryString = unescape(location.search.substring(1));
	    var keyValues = queryString.split('&');
	    for (var i in keyValues) {
	        var key = keyValues[i].split('=');
	        assoc[key[0]] = key[1];
	    }
	    return assoc;
	}
	
	
