kui.player = {
	time: 0,
	offset: 0,
	offsetFlag: true,
	lastPlayerTime: 0,

	addPlayer:function(options){
		if (options.id) {
			var playerWidth = $("player_"+options.id).getWidth();
			var flashVars = {
				id:options.id,
				config:location.protocol+"//"+location.hostname+"/player/xml/"+options.lnk_id+'/'+options.id,
				showfsbutton:"true",
				overstretch:"uniform",
				volume:"100",
				enablejs:"true",
				javascriptid:"player"
			};
			var flashParameters = { 
				scale:"noscale", 
				allowScriptAccess:"sameDomain",
				menu:"false",
				allowfullscreen:"true"
			};
			swfobject.switchOffAutoHideShow();
			swfobject.embedSWF("/images/flash/player.v5.swf", 
				"player_"+options.id, 
				playerWidth, 
				options.height||"100%", 
				"9.0.0", "/scripts/expressInstall.swf", flashVars, flashParameters);
		}
	},
	
    addSwf:function(options){
        swfobject.switchOffAutoHideShow();
        swfobject.embedSWF(options.url, "player_"+options.id, 
            $("player_"+options.id).getWidth(), 
            options.height||"100%", 
            "9.0.0", 
            "/scripts/expressInstall.swf", {}, { 
                scale:"noscale", 
                allowScriptAccess:"sameDomain",
                menu:"false",
                allowfullscreen:"true"
        });
    },
    
	addComment : function(lastCmnt) {
		
		if ($('public') && $F('public')==1) { if (!formValidateBool()) { return; } }

		if ($('addComment') && $F('addComment').empty()) {
			alert('Please type in your comment, and try again!');
			$('addComment').focus();
		}
		else {
			var FormElements = $('mainForm').serialize();
			var AjaxPars = { 
				'inputs': FormElements,
				'func'	: 'addMediaComment',
				'id'	: $F('media_id'),
				'cmnt'	: $F('addComment')
			};
			var setAjax = new Ajax.Request(
			'/ajax/portal', 
			{
				method: 'post', 
				parameters: Object.toQueryString(AjaxPars), 
				onComplete: function (req) { 
					stateRes = req.responseText;
					if (stateRes!=='')
					{
						$('addComment').value = '';
						if ($('noCmnts')) { $('noCmnts').remove(); }
						$('cmntsWrap').insert({top:stateRes});
						var scrollEffect = new Effect.ScrollTo('cmnts', {offset: -20});
					}
				}
			});
		}
	},
	
	deleteComment : function(objID) {
		var AjaxPars = { 
			'func':'delMediaComment',
			'id':objID
		};
		var setAjax = new Ajax.Request(
		'/ajax/portal', 
		{
			method: 'get', 
			parameters: Object.toQueryString(AjaxPars), 
			onComplete: function (req) { 
				var jsonObj	= req.responseText.evalJSON();
				if (jsonObj.objID)  {
					if ($('c'+jsonObj.objID)) {
						var DropEffect = new Effect.DropOut('c'+jsonObj.objID, { afterFinish:function(){ $('c'+jsonObj.objID).remove(); }});
				}}
			}
		});

	},
	
	rateMedia : function(rate) {
		
		var AjaxPars = { 
			'func'		: 'rateMedia',
			'media_id' 	: $F('media_id'),
			'rate' 		: rate
		};
		var setAjax = new Ajax.Request(
		'/ajax/portal', 
		{
			method: 'get', 
			parameters: Object.toQueryString(AjaxPars), 
			onComplete: function (req) { 
				stateRes = req.responseText;
				if ($('starRater') && stateRes!=-1) { $('starRater').update(stateRes); }
			}
		});
	},
	
	getUpdate: function(type,playerTime) {
		if (type == 'state') {
			this.time = 0;
			this.offset = 0;
			this.offsetFlag = true;
		}
		else if (type == 'time') {
			if (this.offsetFlag) {
				this.offsetFlag = false;
				this.offset = playerTime;
			}
			
			if ((playerTime - this.lastPlayerTime).abs() > 1) {
				// a scrubbing has occurred
				this.offset = playerTime - this.time;
			}
			
			this.time = playerTime - this.offset;
			this.lastPlayerTime = playerTime;
		}
	},
	
	statisticsCall: function(ext, timeStamp) {
		if (ext=='swf') {
			var ajaxRequest = new Ajax.Request("/stats/"+this.media_id+"/event/close/calc/"+timeStamp, { method:"get"});		
		} else {
			var ajaxRequest = new Ajax.Request("/stats/"+this.media_id+"/event/pause/val/"+klPlayer.time, { method:"get"});
		}
	
	}
};
