//define namespace "Goldwyn" if not defined already
if (!co_isDef("Goldwyn", CitrixOnline)) {
    CitrixOnline.Goldwyn = {};
}

//include dependencies
CitrixOnline.Core.include(CitrixOnline.Goldwyn.flashResourcesUrl + "/AC_OETags.js");
CitrixOnline.Core.include(CitrixOnline.Goldwyn.flashResourcesUrl + "/history.js");
CitrixOnline.Core.include(CitrixOnline.Goldwyn.flashResourcesUrl + "/FABridge.js");
		    

/**
 * Goldwyn Flash Player class
 */
CitrixOnline.Goldwyn.FlashPlayer = Class.create({
	
	/**
	 * Constructor
	 * 
	 * @param divId				the player's container element id
	 * @param recordingUrl		the url of the recording to play
	 * @param flashParameters	A hash of extra flash parameters to pass to the flash player
	 * @param swfURL			The url to the swf executable, defaults to the url of the player as given by the app-server
	 */
	initialize: function(divId, recordingId, recordingUrl, flashParameters, swfUrl, applicationType, showShareIt, playInstantly) {
	    this.div = $(divId);
	    this.recordingUrl = recordingUrl;
	    this.recordingId = recordingId;
	    this.showShareIt = showShareIt == undefined ? true : showShareIt;
	    this.playInstantly = playInstantly == undefined ? false : playInstantly;
	    this._hasRequiredVersion = undefined;
	    this.applicationType = (applicationType != undefined) ? applicationType : "Player";

		if(!this._validate)
			return undefined;
		
		if(flashParameters)	
			this.flashParameters = new Hash(flashParameters);
		else
			this.flashParameters = new Hash();
		
		this.swfUrl = (swfUrl == undefined)? CitrixOnline.Goldwyn.playerURL : swfUrl;
	    this._bootstrap();
	    this.closeWindowOnExit = false;
	    this.playerWindow = null;
	},
	
	setCloseWindowOnExit: function(value){
		this.closeWindowOnExit = value;
	},
	
	setRecordingUrl: function(recordingUrl){
		this.recordingUrl = recordingUrl;
	},
	
	/**
	 * Exits the flas application redireccting the user back to the recordings page.
	 */
	exitApplication: function(){
		window.close();
	},
	
	/**
	 * Returns the player instance
	 */
	getPlayer: function(){
		return this.player;
	},
	
	/**
	 * Removes player from the DOM (HTML page)
	 */
	removePlayer: function(){
		this.div.remove();
		this.div = undefined;
	},
	
	/**
	 * Returns true if the client has the required version of the Flash player, false otherwise.
	 */
	hasRequiredVersion: function() {
	    if (this._hasRequiredVersion === undefined) 
	        this._hasRequiredVersion = DetectFlashVer(CitrixOnline.Goldwyn.FlashPlayer._requiredVersion[0], CitrixOnline.Goldwyn.FlashPlayer._requiredVersion[1], CitrixOnline.Goldwyn.FlashPlayer._requiredVersion[2]);
	    return this._hasRequiredVersion;
	},
	
	share: function(){
		if($("emailLink") != undefined)
			location.href = $("emailLink").href;
	},
	
	popUpAdd:function() {
		window.open(CitrixOnline.Goldwyn.adUrl);
	},
	
	/**
	 * Pops up a new window with the given recording loaded in the Player 
	 * @param recordingName the key name of the recording (not its id), example: "QUICK_DEMO". 
	 * 						The name must match the enum name (case sensitive)
	 * 						See com.citrixonline.goldwyn.util.DemoRecordings
	 */
	popUpRecording:function(recordingId){
	  	if(this.playerWindow == null || this.playerWindow.closed){
	   		this.playerWindow = window.open(CitrixOnline.Goldwyn.applicationServerUrl+"/spring/play?method=playerDemo&recordingId="+window._demoRecordingId,"GoldwynRecording","left=0,top=0,width=4000,height=4000,location=no,menubar=no,toolbar=no,directories=no,status=no,scrollbars=no,resizable=yes"); 
	   	}
	   	else{
	   		this.playerWindow.focus();
	   	}
	   	return false;
	},	
	
	/**
	 * Brings in required javascript, some required initialization
	 */
	_bootstrap: function() {
		
		//set flash parameters
	    this.flashParameters.set('historyUrl', CitrixOnline.Goldwyn.flashResourcesUrl + '/history.htm?');
	    this.flashParameters.set('lconid', lc_id);
	    this.flashParameters.set('recordingId', this.recordingId);	
	    this.flashParameters.set('recordingUrl', this.recordingUrl);	
	    this.flashParameters.set('applicationType', this.applicationType);
		this.flashParameters.set('showShareIt', this.showShareIt);	 
		this.flashParameters.set('playInstantly', this.playInstantly);
	    this.flashParameters.set('storageServerHosts', CitrixOnline.Goldwyn.storageServerHosts);
	    this.flashParameters.set('rtmpServerHosts', CitrixOnline.Goldwyn.rtmpServerHosts);
		this.flashParameters.set('storageServerPort', CitrixOnline.Goldwyn.storageServer.port);
		this.flashParameters.set('httpsPort', CitrixOnline.Goldwyn.httpsPort);
		this.flashParameters.set('storageServerContextPath', CitrixOnline.Goldwyn.storageServer.contextPath);
		this.flashParameters.set('rtmpServerContextPath', CitrixOnline.Goldwyn.rtmpServer.contextPath)
		this.flashParameters.set('storageServerUploadContextPath', CitrixOnline.Goldwyn.storageServer.uploadContextPath);
		this.flashParameters.set('user', CitrixOnline.Goldwyn.user);
		this.flashParameters.set('token', CitrixOnline.Goldwyn.token);
		this.flashParameters.set('password', CitrixOnline.Goldwyn.password);
		this.flashParameters.set('embed', 'false');

	    //flash settings
	    var args = [
	        "placeholder", this.div.id,
	        "src", this.swfUrl,
	        "width", "100%",
	        "height", "100%",
	        "align", "middle",
	        "id", "flash-" + this.div.id,
	        "quality", "best",
	        "bgcolor", "#869ca7",
	        "wmode", "opaque",
	        "name", this.div.id,
	        "flashvars",this.flashParameters.toQueryString(),
	        "allowScriptAccess","sameDomain",
	        "type", "application/x-shockwave-flash",
	        "pluginspage", "http://www.adobe.com/go/getflashplayer",
	        "allowFullScreen", "true",
	        "allowScriptAccess", "always"
	    ];
	    
	    //place the flash on the DOM
	    AC_FL_PlaceContent.apply(co_global, args);	    	
	},	
	
	/**
	 * Returns true if a player can be created, false otherwise
	 */
	_validate: function(){
		if(this.div == undefined){
			jslog.error("The Player's container cannot be null");
			return false;
		}
		else if(this.recordingUrl == undefined){
			jslog.error("The recording's url cannot be null");
			return false;
		}
		else if(this.recordingId == undefined) {
			jslog.error("The recording's id cannot be null");
			return false;
		}
		else if(!this._hasRequiredVersion){
			jslog.error("The client does not have the correct flash version installed");
			return false;			
		}
		
		return true;
	},
	
	createCookie: function(cookieName, value, days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = cookieName+"="+value+expires+"; path=/";
	},
	
	readCookie: function(cookieName) {
		var nameEQ = cookieName + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	
	deleteCookie: function(cookieName) {
		this.createCookie(cookieName,"",-1);
	}
	

});