Ext.ns('app');

app.player = Ext.extend(Ext.Panel,{
	region: 'center',
	//html : 'player',
	test : 'sd 78',
	player : null,
	border : false,
	info : null,
	infoTpl  : null,
	initComponent : function(){
		app.playerFinish = this.finishPlayer.createDelegate(this);
		this.infoTpl = new Ext.XTemplate(
			'<h1>{name}</h1>',
			'<img src="'+www.url+'{img}"/>',
			'<tpl if="artist != null">',
				'<div class="artist"> Дуучин: ',
					'<tpl for="artist">',
						'<a href="#" mea="artist" a_id="{artist_id}" >',
							'<tpl if="!Ext.isEmpty(ovog)">',
								'{ovog}. ',
							'</tpl>',
						'{ner}</a> ',
					'</tpl>',
				'</div>',
			'</tpl>',
			'<tpl if="album != null">',
				'<div class="album"> Цомог: ',
					'<tpl for="album">',
						'<a href="#" mea="album" a_id="{album_id}">{ner}</a> #{track} ',
					'</tpl>',
				'</div>',
			'</tpl>',
			'<div  class="is-error">Энэ <a href="'+www.url+'/info/error?sound_id={sound_id}" target="_blank">тоглохгүй</a> байна.</div>'
		);
		
		var dh =Ext.DomHelper;
		this.on('render',function(){
			this.body.addClass('player');
			var swfID = 'swf-mp3Player';
			this.renderSWF(swfID);
			if (navigator.appName.indexOf("Microsoft") != -1) {
				this.player= window[swfID];
	         } else {
	        	 this.player= document[swfID];
	         }
			
			var dh =Ext.DomHelper;
			this.info = dh.append(this.body,{
				cls : 'info'
			},true);
			this.info.on('click',this.infoClick,this);
			this.doLayout();
		},this);
		app.player.superclass.initComponent.call(this);
	},
	infoClick : function(e,t,o){
		if(t.tagName == 'A'){
			var mea=t.getAttribute('mea');
			if(mea == "artist"){
				e.stopEvent();
				var id=t.getAttribute('a_id');
				this.fireEvent('showListFromArtists',id,t.innerHTML);
			}else if(mea == "album"){
				e.stopEvent();
				var id=t.getAttribute('a_id');
				this.fireEvent('showListFromAlbums',id,t.innerHTML);
			}
		}
	},
	updateInfo : function(sound_id){
		
		Ext.Ajax.request({
			url: www.url+'/index/info',
			params : {
				sid : sound_id
			}, 
			success: function(response, opts) {
				var obj = Ext.decode(response.responseText);
				var img=null; 
				if(obj.er == false){
					var albums = obj.data.album;
					Ext.each(albums,function(it){
						if(!Ext.isEmpty(it.img)){
							img =it.img;
						}
					});
					if(img == null){
						img="/album-img/none.png";
						img="/css/w/album.png";
					}
					
					obj.data.img = img;
					obj.data.ovog = obj.data.ovog?obj.data.ovog:'';
					//console.log(obj.data)
					this.infoTpl.overwrite(this.info, obj.data);
				}else{
					alert('Error :'+obj.msg);
				}
			},
			scope :this
		});
	},
	play: function(sID,plID){
		
		this.sound_id = sID;
		this.playL_id = plID;
		this.updateInfo(this.sound_id);
		var url = '/sounds?sound_id='+this.sound_id;
		this.player.load(url,true);
		this.isTmpPlay = false;
		//this.updatePlayer();
		
		this.fireEvent('play_begin',this.sound_id,this.playL_id);
	},	
	tmpPlay:function(sid){
		this.updateInfo(sid);
		var url = '/sounds?sound_id='+sid;
		
		//url = "http://new.sound.me/sounds?sound_id=29";
		//url = "http://new.sound.me/php/22.mp3";
		
		this.player.load(url,true);
		this.isTmpPlay = true;
	},
	finishPlayer:function(){
		//console.log('finish');
		if(this.isTmpPlay == true) return;
		this.fireEvent('play_finish',this.sound_id,this.playL_id);
	},
	renderSWF:function (swfID){
		
		var swfW = 250;
		var swfH = 80;
		var swfUrl = "/player/mp3Player.swf";
		var dh =Ext.DomHelper;
		dh.append(this.body,{
			cls : 'swf-player',
			cn : [{
				tag : 'object',
				classid : 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000',
				codebase : 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0',
				id : swfID,
				width : swfW,
				height : swfH,
				cn : [{
					tag : 'param',
					name : 'allowFullScreen',
					value : 'false'
				},{
					tag : 'param',
					name : 'allowScriptAccess',
					value : 'sameDomain'
				},{
					tag : 'param',
					name : 'movie',
					value : swfUrl
				},{
					tag : 'param',
					name : 'quality',
					value : 'high'
				},{
					tag : 'param',
					name : 'wmode',
					value : 'transparent'
				},{
					tag : 'embed',
					src : swfUrl,
					quality : 'high',
					wmode : 'transparent',
					name : swfID,
					allowscriptaccess : 'sameDomain',
					allowfullscreen : 'false',
					type : 'application/x-shockwave-flash',
					pluginspage : 'http://www.adobe.com/go/getflashplayer',
					width : swfW,
					height : swfH
				}]
			}]
		});
	}
});
