kui.player = {
	time: 0,
	offset: 0,
	offsetFlag: true,
	lastPlayerTime: 0,
    maxH:document.viewport.getDimensions().height+'px',

	addPlayer:function(options){
		if (options.id) {
			var playerWidth = $("player_"+options.id).getWidth();
            var playerHeight = (options.height) ? options.height : kui.player.maxH;
            
            var jwParams = { 
                flashplayer: "/images/flash/player.v5.swf", 
                height: parseInt(playerHeight,10), 
                width: parseInt(playerWidth,10),
                media_id:options.media_id||null,
                lnk_id:options.lnk_id||null,
                mod_id:options.mod_id||null,                        
                ext:options.ext||null,
                
                events: {
                    onPlaylistItem:function(){
                        var item = this.getPlaylistItem();
                        kui.player.track({
                            event:'load',
                            lnk_id:this.config.lnk_id||null,
                            mod_id:this.config.mod_id||null,
                            ext:this.config.ext||0,
                            media_id:item.media_id||this.config.media_id||null,
                            duration:this.getPosition(),
                            total:this.getDuration()
                        });
                    }, 
                    onPlay:function(){
                        if (this.getDuration()>0) {
                            var item = this.getPlaylistItem();                    
                            kui.player.track({
                                event:'play',
                                lnk_id:this.config.lnk_id||null,
                                mod_id:this.config.mod_id||null,
                                ext:this.config.ext||0,
                                media_id:item.media_id||this.config.media_id||null,
                                duration:this.getPosition(),
                                total:this.getDuration()
                            });
                        }
                    }, 
                    onPause:function(){ 
                        var item = this.getPlaylistItem();                    
                        kui.player.track({
                            event:'pause',
                            lnk_id:this.config.lnk_id||null,
                            mod_id:this.config.mod_id||null,
                            ext:this.config.ext||0,
                            media_id:item.media_id||this.config.media_id||null,
                            duration:this.getPosition(),
                            total:this.getDuration()
                        });
                    },
                    onComplete:function(){ 
                        var item = this.getPlaylistItem();
                        kui.player.track({
                            event:'complete',
                            lnk_id:this.config.lnk_id||null,
                            mod_id:this.config.mod_id||null,
                            ext:this.config.ext||0,
                            media_id:item.media_id||this.config.media_id||null,
                            duration:this.getPosition(),
                            total:this.getDuration()
                        });
                    } 
                },
                modes: [ 
                    { type: "flash", src: "/images/flash/player.v5.swf" }, 
                    { type: "html5" }, 
                    { type: "download" } 
                ] 
            };
            
            var setAjax = new Ajax.Request('/ajax/player', {
                parameters: Object.toQueryString({ 
                    lnk_id:options.lnk_id,
                    media_id:options.id,
                    mod_id:options.mod_id||0,
                    ext:options.ext||0,
                    preview:options.preview||0,
                    func:'get/config'
                }),
                onComplete: function (req) {
                    var json = req.responseJSON;
                    if (json && (json.file || json.playlist)) {
                        jwParams = Object.extend(json,jwParams);
                        jwplayer("player_"+options.id).setup(jwParams);
                    }
                }.bind(this)
            });
		}
	},
    
     track:function(options){
        var setAjax = new Ajax.Request('/ajax/player', {
            parameters: Object.toQueryString({ 
                func:'track',
                lnk_id:options.lnk_id||null,
                media_id:options.media_id||null,
                event:options.event,
                duration:options.duration||null,
                total:options.total||null                
            })
        });
    },
	
    addSwf:function(options){
        swfobject.switchOffAutoHideShow();
        swfobject.embedSWF(options.url, "player_"+options.id, 
            $("player_"+options.id).getWidth(), 
            options.height||kui.player.maxH, 
            "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"});
		}
	
	}
};

