///////////////////////////////////////////////////////////////////////////////////
//  P L A Y L I S T 
///////////////////////////////////////////////////////////////////////////////////	

	
var pL = new Class({
	initialize: function(container){
		this.list=new Array();
		this.container=$(container);
	},
	
	inidrop: function(){
		
	},
	
	sortable: function(){
		new Sortables(this.container, {
			initialize: function(){},
			onComplete: function(){
				
			}
		});	
	},
	
	additem: function(item){
		if(this.isin(item.id)==false){
			// Beszúrás a lista végére
			this.list.include(item.id);
			// Beszúrás DOMba
			var plitem=new Element('div', {'class': 'item','id':item.id})
			var plplay=new Element('span', {'class': 'itemplay'}).injectInside(plitem);
			var pltitle=new Element('span', {'class': 'itemtitle'}).setHTML(item.title).injectAfter(plplay);
			var pldel=new Element('span', {'class': 'itemdelete'}).injectAfter(pltitle);
			var plprop=new Element('span', {'class': 'itemprop'}).injectAfter(pldel);
			plitem.injectInside(this.container);

			plplay.addEvent('click', function() {
				PLAYER.play(item.id,item.title);
			});
			pldel.addEvent('click', function() {
				PLAYLIST.removeitem(item.id);
				
			});
			plprop.addEvent('click', function() {
				PLAYLIST.prop(item.id);
			});
			// Sorbarakás frissítés
			this.sortable();
		}
	},
	
	removeitem: function(vid){
		this.list.remove(vid);
		if(PLAYER.nowplay==vid) PLAYER.stop(vid);
		$(vid).remove();
	},
	
	prop: function(item){
				
	},
	
	length: function(){
		return this.list.length;
	},
	
	isin: function(vid){
		return this.list.contains(vid);	
	},
	
	next: function(){
		if(this.length()==0) return -1;
		var plid=this.position(PLAYER.nowplay);
		var nid=plid+1;
		if((this.length()-1)==plid) nid=0;//ha az utolsó
		if(this.length()==1) nid=plid; //ha csak egy van
		return this.vid(nid);
	},
	
	prev: function(){	
		if(this.length()==0) return -1;
		var plid=this.position(PLAYER.nowplay);
		var nid=plid-1;
		if(plid==0) nid=this.length()-1;//ha az utolsó
		if(this.length()==1) nid=plid; //ha csak egy van
		return this.vid(nid);
	},
	
	position: function(vid){
		return this.list.indexOf(vid);
    },
	
	vid: function(position){
		return this.list[position];	
	},
   
	isearch: function(){
			
	},
	
	iid: function(vid){
			return list[vid].getProperty("id");
	},
	
	last: function(){
		return	list[list.length-1].getProperty("id").replace('sr-','');
	},
	
	randomitem: function(){
		return this.list.getRandom();
	},
	
	itemtitle: function(){
			for (i=0;i<list.length;i++){
				if(mode=="sr"){
					if(list[i].getProperty("id")==("sr-"+param)) return list[i].getProperty("title");
				}else{
					if(list[i].getProperty("id")==param) return list[i].getProperty("title");
				}
			}
	}
		
});

///////////////////////////////////////////////////////////////////////////////////
//  P L A Y E R  
///////////////////////////////////////////////////////////////////////////////////	

var pLr= new Class({				   
	options: {
			playback: 'repeatall',
			swf: '/layout/swf/player.swf'
	},
	
	initialize: function(container){
		this.nowplay="";
		this.container=container;
	},
	
	loadflash: function(){
		var fl = new SWFObject(this.options.swf,'mpl','100%','120','8');
		fl.addParam('quality', 'best');
		fl.addParam('wmode', 'transparent');
		fl.write(this.container);	
		if(navigator.appName.indexOf('Microsoft')!=-1){
			this.fl=window['mpl'];
	   	}else{
			this.fl=document['mpl'];
	   	}
	},
	
	play: function(file,title){
		this.fl.flpplay(file,title);
		this.nowplay=file;
	},
	
	stop: function(){
		this.fl.flpstop();
	},
	
	info: function(){
		return this.fl.flpinfo();
	},
	
	
	next: function(lastplay){	
		var next=PLAYLIST.next();
		if(next!=-1) this.play(next);		
	},
	
	prev: function(lastplay){	
		var prev= PLAYLIST.prev(); 
		if(prev!=-1) this.play(prev);	
	},

	playback: function(){
		var next;
		switch(this.options.playback){
			case 'repeatall':
				next=PLAYLIST.next();
			break;
			case 'repeatone':
				next=this.nowplay;
			break;
			case 'random':
				next= PLAYLIST.randomitem();
			break;
		}
		if(next!=-1) this.play(next);
	}
});

///////////////////////////////////////////////////////////////////////////////////
//  S E A R C H  
///////////////////////////////////////////////////////////////////////////////////	

function searchdisplay(result){
	$('searchresult').setHTML("");
	var drop = $('playlist');
	result.each(function(item, i) {
		var resultitem = new Element('div',{'class':'item'}).inject("searchresult");
		var playnow = new Element('a',{'class':'itemplay','title':'Lejátszás'}).injectInside(resultitem);
		var handler = new Element('span',{'class':'itemtitle','title':'Lejátszás'}).setHTML(item.title).injectAfter(playnow);
		
		playnow.addEvent('click', function() {
				PLAYER.play(item.id, item.title);
			});
		
		handler.addEvent('mousedown', function(e) {
			e = new Event(e).stop();
			var clone = resultitem.clone()
				.setStyles(this.getCoordinates()) 
				.setStyles({'opacity': 0.7, 'position': 'absolute'})
				.addEvent('emptydrop', function() {
					this.remove();	
					drop.removeEvents();
				}).inject(document.body);
			// leejtés
			drop.addEvents({
				'drop': function() {
					drop.removeEvents();
					clone.remove();
					PLAYLIST.additem(item);					
				}
			});
			var drag = clone.makeDraggable({droppables: [drop], handle:clone.handler}); 
			drag.start(e); 
		});
	});	
 }




///////////////////////////////////////////////////////////////////////////////////
//  D O M  B E T Ö L T É S   U T Á N
///////////////////////////////////////////////////////////////////////////////////		


window.addEvent('domready', function() {
	// Flash Player Load
	PLAYLIST = new pL('playlist');
	PLAYER = new pLr('player',{mode:'repeatall', swf:'/layout/swf/player.swf'});
	PLAYER.loadflash();
	//Ajax Search inicializálás
	$('formsearch').addEvent('submit', function(e) {
		new Event(e).stop();
		if($('tag').getProperty('value')!=""){
		this.send({
			method: 'post',
			onRequest: function(){
				$('submitsearch').setStyle('display','none');
				$('searchloading').setStyle('display','block');
			},
			onComplete: function(req){
				$('searchloading').setStyle('display','none');
				$('submitsearch').setStyle('display','block');
				if(req!="error"){
					searchdisplay(Json.evaluate(req));
					$('resultslideknob').setStyle('display','block');
					//searchLive('');
					resultSlider();
				}else{
				}		 
			}

		});
		}			
	});
	
	//PLStatus
	plstatus = new Slider($('area'), $('knob'), {
		steps: 100,
		onComplete:function(step){
			if(step>(Math.round(currentPosition*(100/fullsize)))){
				currentPosition=step;
				sendEvent('scrub',step*(fullsize/100))
			}
		}

	}).set(0);
		
	//live search	
	//$('tag').observe({ callback: function() {  //this.setStyle('background-color','#ff0000');      
	//	var ls = new Ajax("live_search.php", {
	//		method: 'post',
	//		postBody: "tag="+$('tag').getProperty("value"),                          
	//		onComplete: function(req){
	//			searchLive(req);
	//		}
	//	});
	//	if($('tag').getProperty("value").length>1){
	//		ls.request();
	//	}else{
	//		searchLive('');
	//	}} 
	//});
		
	//cookie inicializálása
	var HashCookie = new Hash.Cookie('audia_playlist');
	//cookie lekérdezése
	if (HashCookie.get('playlist')) {
		alert("van cookie")
	}
});



function NumHeightOfElement(HeightOfElement){
	var NumHeightOfElement=HeightOfElement.replace(/px/, "");
	return NumHeightOfElement-420;
}

function resultSlider(){
	var mySlide3 = new Slider($('resultslide'), $('resultslideknob'), {	
	steps: NumHeightOfElement($('searchresult').getStyle('height')),	
	mode: 'vertical',
	offset: -2,
	onChange: function(step){  
	$("searchresult").style.marginTop = '-'+step+'px';
	// ctrl+ augmentation de 75px
	}
	}).set(0);
}


// Live Search
function searchLive(content){
	var sl=$("searchlive");
	if(content==""){
		//if(sl) sl.setHTML('');
	}else{
		//sl.setHTML(content);
		//var pos = $('tag').getPosition();
		//sl.setStyles({
			//'left': pos.x +160,
			//'top': pos.y -110			
		//});
	}
}

function tagsearch(tag){
	$('tag').setProperty('value',tag);
	var ls = new Ajax($('formsearch').getProperty("action"), {
			method: 'post',
			postBody: "tag="+$('tag').getProperty("value"),                          
			onRequest: function(){
				$('submitsearch').setStyle('display','none');
				$('searchloading').setStyle('display','block');
			},
			onComplete: function(req){
				$('searchloading').setStyle('display','none');
				$('submitsearch').setStyle('display','block');
				if(req!="error"){
					searchdisplay(Json.evaluate(req));
					$('resultslideknob').setStyle('display','block');
					//searchLive('');
					resultSlider();
				}else{
				}		 
			}

		}).request();
	
}

///////////////////////////////////////////////////////////////////////////////////
//  Á L T A L Á N O S  F V K.
///////////////////////////////////////////////////////////////////////////////////	

// Ablak bezárás - kérdése
/*window.onbeforeunload= function(){
	if(PLAYER.nowplay!="null"){
 		return 'Ha bezárod ezt az ablakot, a zene elhallgat! Biztosan ezt akarod?';
	}
};*/

