// JavaScript Document
var casuale = false;
var maxId;
document.observe("dom:loaded", function(){
	maxId = document.getElementById('maxId').value;
})
function rand(checked){
	casuale = checked;
}

function seleziona(id){
	var e = document.getElementById(id);
	var id0 = document.getElementById('id0');
	if(id0.value != ""){	
		var e0 = document.getElementById(id0.value);
	}
	var c0 = document.getElementById('c0');
	if(id0.value != "" && c0.value != ""){	
		e0.className = c0.value;
	}
	id0.value = id;
	c0.value = e.className;
	e.className = 'selected';
	var l = document.getElementById(id+'[link]').value;
	l = l.replace('&','%26');
	var titolo = document.getElementById(id+'[titolo]').innerHTML;
	titolo.replace('&','%26');
	var artista = document.getElementById(id+'[artista]').innerHTML;
	artista.replace('&','%26');
	var IE = navigator.appName.indexOf("Microsoft") != -1;
 var oggetto = IE ? window.player : window.document.player;
	oggetto.SetVariable('link',l);
	oggetto.SetVariable('titolo',titolo);
	oggetto.SetVariable('artista',artista);
}

function play(){
	var IE = navigator.appName.indexOf("Microsoft") != -1;
 var oggetto = IE ? window.player : window.document.player;
	oggetto.SetVariable('esegui','true');
}

function primo(){
	var id = document.getElementById('id0').value;
	if(id != ""){
			seleziona(id);
			//play();
	}
}

function indietro(){
	var id0 = document.getElementById('id0').value;
	if(id0 != ""){
		if(casuale){
			var id = 'brano[' + Math.round(Math.random()*maxId) + ']';
			while(!$(id) || id == id0){
				id = 'brano[' + Math.round(Math.random()*maxId) + ']';
			}
		}else{
			var id = document.getElementById(id0+'[prev]').value;
		}
		if(id != ""){
			seleziona(id);
			//play();
		}
	}
}

function avanti(){
	var id0 = $('id0').value;
	if(id0 != ""){
		if(casuale){
			var id = 'brano[' + Math.round(Math.random()*maxId) + ']';
			while(!$(id) || id == id0){
				id = 'brano[' + Math.round(Math.random()*maxId) + ']';
			}
		}else{
			var id = document.getElementById(id0+'[succ]').value;
		}
		if(id != ""){
			seleziona(id);
			//play();
		}
	}
}

function titolo(d){
	var f = document.getElementById('form');
	if(d == 'desc'){
		f.setAttribute('action','?titolo&desc');
	}else{
		f.setAttribute('action','?titolo');
	}
	f.submit();
}
function artista(d){
	var f = document.getElementById('form');
	if(d == 'desc'){
		f.setAttribute('action','?artista&desc');
	}else{
		f.setAttribute('action','?artista');
	}
	f.submit();
}
function genere(d){
	var f = document.getElementById('form');
	if(d == 'desc'){
		f.setAttribute('action','?genere&desc');
	}else{
		f.setAttribute('action','?genere');
	}
	f.submit();
}

function setcasuale(b){
		casuale = b;
}

function lyrics(url) {
	var width = 500,
		height = 500,
		left = (screen.availWidth - width) / 2,
		top = (screen.availHeight - height) / 2;
	
	window.open(url, 'Lyrics', 'width='+width+',height='+height+',left='+left+',top='+top+',scrollbars=1', true);
}

document.onkeydown = checkKeycode;
function checkKeycode(e) {
	var keycode = window.event ? window.event.keyCode : e.which ? e.which : e.charCode;
		if(keycode == 40){ //freccia in gił
			avanti();
		}
		if(keycode == 38){ //freccia in su
			indietro();
		}
		if(keycode == 34){ //freccia in gił
			avanti();
			play();
		}
		if(keycode == 33){ //freccia in su
			indietro();
			play();
		}
		
		if(keycode == 13){ //invio
			play(); // o pause
			if(location.href.indexOf('#') == -1){
		//		location.href+="#content";
			}else{
				//location.href = location.href;
			}
				
		}
		
	}