/*
 * Copyright (C) 2010, Skype Limited
 *
 * All intellectual property rights, including but not limited to copyrights,
 * trademarks and patents, as well as know how and trade secrets contained
 * in, relating to, or arising from the internet telephony software of Skype
 * Limited (including its affiliates, "Skype"), including without limitation
 * this source code, Skype API and related material of such software
 * proprietary to Skype and/or its licensors ("IP Rights") are and shall
 * remain the exclusive property of Skype and/or its licensors. The recipient
 * hereby acknowledges and agrees that any unauthorized use of the IP Rights
 * is a violation of intellectual property laws.
 *
 * Skype reserves all rights and may take legal action against infringers of
 * IP Rights.
 *
 * The recipient agrees not to remove, obscure, make illegible or alter any
 * notices or indications of the IP Rights and/or Skype's rights and ownership
 * thereof.
 */
if(typeof SkypeDetection=="undefined"){SkypeDetection=function(){var _detectionSwfUrl=(location.protocol=="http:")?"http://api.skype.com/detection/detection_as3.swf":"https://secure.skype.com/i/common/swf/detection_as3.swf";var _detectionSwfID="skypedetectionswf";var _containerID="skypedetectioncontainer";var _verbose=false;var _flashCreated=false;var _initalizing=false;var _successCallbacks=[];var _failureCallbacks=[];var _failureTimeout=5000;var createContainer=function(){var container=document.createElement("div");container.id=_containerID;container.style.position="absolute";container.style.width="5px";container.style.height="5px";container.style.top="0px";container.style.left="-10px";var div=document.body&&document.body.insertBefore(container,document.body.firstChild);if(!div){log("Seems like container creating failed.");return;}window.setTimeout(createFlash,10);};var createFlash=function(){if(typeof YAHOO!="undefined"&&YAHOO.widget&&YAHOO.widget.SWF){log("Using YUI SWF module to embed Flash content");var yuiswf=new YAHOO.widget.SWF(_containerID,_detectionSwfUrl,{version:9,fixedAttributes:{allowScriptAccess:"always",width:5,height:5}});_flashCreated=true;_detectionSwfID=yuiswf._id;}else{if(window.jQuery&&$&&$.flash&&typeof $.flash.create=="function"){log("Using jquery-swfobject to embed Flash content");$("#"+_containerID).flash({swf:_detectionSwfUrl,id:_detectionSwfID,width:5,height:5,hasVersion:9,params:{allowscriptaccess:"always"}});_flashCreated=true;}else{if(window.jQuery&&$&&$.fn.flash){log("Using jquery-flash to embed Flash content");$("#"+_containerID).flash({id:_detectionSwfID,src:_detectionSwfUrl,width:5,height:5,allowscriptaccess:"always",version:"9.0"});_flashCreated=true;}else{if(typeof swfobject!="undefined"&&swfobject.embedSWF){log("Using SWFObject 2.x to embed Flash content");swfobject.embedSWF(_detectionSwfUrl,_containerID,5,5,"9.0",null,null,{allowScriptAccess:"always"},{id:_detectionSwfID,style:"position: absolute; top: 0px; left: -10px;"},flashStatusCallback);}else{if(typeof deconcept!="undefined"&&deconcept.SWFObject){log("Using SWFObject 1.5 to embed Flash content");var so=new SWFObject(_detectionSwfUrl,_detectionSwfID,5,5,"9.0");so.addParam("allowScriptAccess","always");so.write(_containerID);_flashCreated=true;}else{log("No supported way of embedding Flash was found");detectionFail();return;}}}}}window.setTimeout(detectionFail,_failureTimeout);};var flashStatusCallback=function(e){if(e.success==false){log("Flash embedding via SWFObject embedding failed");detectionFail();}else{if(e.success==true){log("SWFObject callback indicated success");_flashCreated=true;}}};var detectionFail=function(){if(!SkypeDetection.ready){log("Detection seems to have failed, calling failure callbacks");for(var i=0;i<_failureCallbacks.length;i++){try{_failureCallbacks[i]();}catch(e){log("Failure callback "+_failureCallbacks[i]+" threw an error: "+e);}}}};var detectionSuccess=function(){log("Detection succeeded, calling success callbacks");for(var i=0;i<_successCallbacks.length;i++){try{_successCallbacks[i]();}catch(e){log("Success callback "+_successCallbacks[i]+" threw an error: "+e);}}};var log=function(msg){if(_verbose&&typeof console!="undefined"&&console.log){console.log("[SkypeDetection] "+msg);}};var registerCallback=function(stack,fn){for(var i=0;i<stack.length;i++){if(stack[i]===fn){return;}}stack.push(fn);};var readDetectionData=function(){var swf=document.getElementById(_detectionSwfID);try{var data=swf.getData();}catch(e){log("Getting data with swf.getData() failed, likely reason is browser issue with ExternalInterface setup");detectionFail();return;}SkypeDetection.installed=swf.isInstalled();SkypeDetection.internal.segment=data.segment||"";log("Reading detection data, Skype is "+(SkypeDetection.installed?"installed":"not installed"));if(SkypeDetection.installed){SkypeDetection.version=data.version;SkypeDetection.platform=data.platform;SkypeDetection.language=data.language;log("Using Skype version '"+data.version+"' on '"+data.platform+"' platform in language '"+data.language+"'");if(swf.getSharedObjectData){try{data=swf.getSharedObjectData();}catch(e){log("Could not read swf.getSharedObjectData()");}if(data.ui_timezone){SkypeDetection.internal.profileTimezone=data.ui_timezone;}if(data.os_timezone){SkypeDetection.internal.osTimezone=data.os_timezone;}else{SkypeDetection.internal.osTimezone=parseInt(new Date().getTimezoneOffset()/60);}if(data.ui_installdate){if(typeof data.ui_installdate=="string"){data.ui_installdate=parseInt(data.ui_installdate);}if(isNaN(data.ui_installdate)||data.ui_installdate==0){SkypeDetection.internal.profileAge=-1;}else{SkypeDetection.internal.profileAge=Math.floor(((new Date()).getTime()/1000-data.ui_installdate)/60/60/24);}}}if(swf.getSessionData){try{data=swf.getSessionData();}catch(e){log("Could not read swf.getSessionData()");}if(data.username){SkypeDetection.internal.username=data.username;var timeNow=(new Date()).getTime()/1000;if(typeof data.expires!="undefined"&&data.expires<timeNow){SkypeDetection.internal.username="";try{swf.clearSessionData();}catch(e){}}}}}detectionSuccess();};var setUserSegment=function(value){try{var swf=document.getElementById(_detectionSwfID);if(swf.setUserSegment){swf.setUserSegment(value);SkypeDetection.internal.segment=swf.getUserSegment();log("Set user segment to "+value);return true;}}catch(e){log("Setting user segment failed");}return false;};return{setVerbose:function(verbose){_verbose=verbose;log("Enabled verbose mode");},setReady:function(){log("Flash detection code indicated to JS that it is ready");SkypeDetection.ready=true;window.setTimeout(readDetectionData,10);},detect:function(successFn,failureFn){successFn&&registerCallback(_successCallbacks,successFn);failureFn&&registerCallback(_failureCallbacks,failureFn);if(SkypeDetection.ready){log("Detection has already been run before");window.setTimeout(SkypeDetection.installed?detectionSuccess:detectionFail,10);}else{if(!_flashCreated&&!_initalizing){_initalizing=true;log("Creating detection Flash helper");window.setTimeout(createContainer,10);}else{log("Unhandled case, marked not ready and flash somehow created?");}}},isQualifiedVersion:function(reqver){if(!SkypeDetection.ready||!SkypeDetection.installed){return false;}var ver=SkypeDetection.version;log("Comparing detected version "+ver+" to required version "+reqver);ver=ver.split(".");reqver=reqver.split(".");try{if(parseInt(ver[0])>parseInt(reqver[0])||(parseInt(ver[0])==parseInt(reqver[0])&&parseInt(ver[1])>parseInt(reqver[1]))||(parseInt(ver[0])==parseInt(reqver[0])&&parseInt(ver[1])==parseInt(reqver[1])&&parseInt(ver[3])>=parseInt(reqver[3]))){return true;}}catch(e){}return false;},ready:false,version:null,platform:null,language:null,installed:null,internal:{username:null,profileTimezone:null,osTimezone:null,profileAge:null,segment:null,setUserSegment:setUserSegment}};}();}if(SKYPE&&SKYPE.register){SKYPE.register("detection",SkypeDetection,{version:"1.0",build:"1"});}
