var __currentPlayer;
var __currentIndexAlbum;
var __raccoltaBrani = [];
var __raccoltaBraniMapIndex = [];
var _lastQueryTextLabel;
var _setQueryTextLabel = function(txt) { _lastQueryTextLabel = txt; document.getElementById('q').value = txt; };
var _resetQueryTextLabel = function() { var obj = document.getElementById('q'); if (obj.value == _lastQueryTextLabel) obj.value = ''; };

// MagPlayer
function magPlayer() {
	window.open( defaultPath + 'magplayer.aspx' ,'magplayer','width=780,height=538,resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no');
}
	
function mp_cerca() {
	mp_imgLoad();
	jQuery("#cercaPaging").val(jQuery("#cerca").val());
	jQuery.post( defaultPath + 'ext/magplayer/default.aspx', {paginacorrente: 1, parolachiave: jQuery("#cerca").val()}, function(data) {
		jQuery("#risultatoricerca").html(data);
		});
	}
	
function mp_paging( nIndex ) {
	mp_imgLoad();
	jQuery.post( defaultPath + 'ext/magplayer/default.aspx', {paginacorrente: nIndex, parolachiave: jQuery("#cercaPaging").val()}, function(data) {
		jQuery("#risultatoricerca").html(data);
		});
	}

function mp_imgLoad( nIndex ) {
	jQuery("#risultatoricerca").html('<img src="' +defaultPath + 'img/ajax-loader-big.gif" />');
	}
	
function mp_show( sBrano ) {
	jQuery('#info-' + sBrano).toggle();
	}
	
function mp_loadPlayList() {
	if (jQuery.cookie("magplayerBrani")) {
		jQuery.post( defaultPath + 'ext/magplayer/default.aspx', { azione: "load", brani: jQuery.cookie("magplayerBrani") }, function(data) {
			var jsonItems = eval(data);
			jQuery.each(jsonItems, function(i,item){ mp_writeItem( item.brano, item.titolo ); });
			mp_save();
			});
		}
	};
	
function mp_save() {
	__raccoltaBrani = [];
	__raccoltaBraniMapIndex = [];
	 jQuery('#playlist li.item-brano').each(function(index, value) { 
		var sBrano = value.id.replace('li-album-','');
		__raccoltaBrani.push(sBrano); 
		__raccoltaBraniMapIndex[sBrano] = index;
		});
	jQuery.cookie("magplayerBrani", null)	
	jQuery.cookie("magplayerBrani", __raccoltaBrani, { expires: 365 });	
	jQuery("#playlist li.item-brano").removeClass('odd');
	jQuery("#playlist li.item-brano:odd").addClass('odd');
	}

function mp_playBrano( sIDPlayer, sBrano ) {
	playBrano( sIDPlayer, __raccoltaBraniMapIndex[sBrano] );
	}
	
function mp_add( sBrano, sTitolo ) {
	if (!document.getElementById('li-album-' + sBrano)) mp_writeItem(sBrano, sTitolo);
	mp_save();
	}

function mp_remove( sBrano, sTitolo ) {
	if (__raccoltaBraniMapIndex[sBrano]==__currentIndexAlbum) {
		document.getElementById('PlayerBig').sendToActionScript('play');
		__currentIndexAlbum = 0;
		}
	jQuery('#li-album-' + sBrano).remove();
	mp_save();
	}

function mp_move( sIdLi, nStep ) {
	var oItem = jQuery('#' + sIdLi)
	var nIndex = jQuery('#' + sIdLi).index()
	var nMaxIndex = jQuery('#playlist li.item-brano:last').index()
	jQuery('#' + sIdLi).remove();
	if ((nIndex==0)&&(nStep<0))	oItem.insertAfter('#playlist li.item-brano:last');
	else if ((nIndex==nMaxIndex)&&(nStep>0)) oItem.insertBefore('#playlist li.item-brano:first');
	else if (nStep > 0)	oItem.insertAfter('#playlist li.item-brano:nth-child(' + (nIndex+1) + ')');
	else oItem.insertBefore('#playlist li.item-brano:nth-child(' + nIndex + ')');
	mp_save();
	}
	
function mp_writeItem( sBrano, sTitolo ) {
jQuery("#playlist").append('<li id="li-album-' + sBrano +'" class="item-brano">' +
							   '<span class="contentlabelplayer header">' +
							   		'<label>' +
								   '<a title="Ascolta il brano ' + sTitolo + '" href="javascript:mp_playBrano(\'PlayerMagPlayer\', \'' + sBrano + '\');">' +
									   '<span>' + sTitolo + '</span>' +
								   '</a>' +
								   '</label>' +
							   '</span>' +
							   '<div class="songoptions">' +
								  '<ul>' +
									 '<li class="delete"><a href="javascript:mp_remove(\'' + sBrano + '\');" title="Rimuovi dalla playlist il brano ' + sTitolo + '"><span>Elimina</span></a></li>' +
									 '<li class="before"><a href="javascript:mp_move(\'li-album-' + sBrano + '\', -1);" title="Sposta il brano ' + sTitolo + ' nel posto precedente"><span>Sposta nel posto precedente</span></a></li>' +
									 '<li class="after"><a href="javascript:mp_move(\'li-album-' + sBrano + '\', 1);" title="Sposta il brano ' + sTitolo + ' nel posto successivo"><span>Sposta nel posto successivo</span></a></li>' +
								  '</ul>' +
							   '</div>' +
							'</li>');
	}

function sendToJavaScript(value, chi, IDBrano) {
	  // play - stop - fine
	  //alert(value + ' ' + chi + ' ' + IDBrano);
	  switch(value)
			{
			case 'play':
			  if ((IDBrano=='')&&(__raccoltaBrani.length>0)) playBrano( chi, 0 )
			  if (__raccoltaBrani.length!=0)	 {
				if ((__currentPlayer)&&(__currentPlayer!=chi)) document.getElementById(__currentPlayer).sendToActionScript(value);
				if (!__currentIndexAlbum)	{
					__currentIndexAlbum = 0;
					jQuery('#contentplaylist li').removeClass('sel');				
					}
				jQuery('#li-album-' + __raccoltaBrani[__currentIndexAlbum]).addClass('sel');	
				} 
			  else	{
				  if ((__currentPlayer)&&(__currentPlayer!=chi)) {
					jQuery('#contentplaylist li').removeClass('sel');				
					document.getElementById(__currentPlayer).sendToActionScript(value);
						}
					}
			  __currentPlayer = chi;
			  break;
			case 'stop':
			  __currentPlayer = undefined;
			  break;
			case 'fine':
			  if ( __raccoltaBrani.length>0) playBrano(__currentPlayer, __currentIndexAlbum + 1);  else __currentPlayer = undefined;
			  break;
			  }
     };
	 
function OLD_sendToJavaScript(value, chi) {
	  // play - stop - fine
	  //alert(__currentPlayer);	
	  //alert(value + ' ' + chi);
	  switch(value)
			{
			case 'play':
			  if ((chi=='PlayerBig')&&(__raccoltaBrani.length!=0))	 {
				if ((__currentPlayer)&&(__currentPlayer!=chi)) document.getElementById(__currentPlayer).sendToActionScript(value);
				if (!__currentIndexAlbum)	{
					__currentIndexAlbum = 0;
					jQuery('#contentplaylist li').removeClass('sel');				
					}
				jQuery('#li-album-' + __raccoltaBrani[__currentIndexAlbum]).addClass('sel');	
				} 
			  else	{
				  if (__currentPlayer) {
					jQuery('#contentplaylist li').removeClass('sel');				
					document.getElementById(__currentPlayer).sendToActionScript(value);
						}
					}
			   if (chi=='PlayerBig')		
				__currentPlayer = chi;
			   else
				__currentPlayer = 'player_' + chi;
			   break;
			case 'stop':
			  __currentPlayer = undefined;
			  break;
			case 'fine':
			  if ( __raccoltaBrani)	 {
					playBrano(__currentIndexAlbum + 1);
					}
			  else		
				__currentPlayer = undefined;
			  break;
			  }
     };

function pubblicaCommento( sUrl, sGuid ) {
	jQuery.post(sUrl, { azione: "pubblica", guid: sGuid },
				   function(data){
					 if (data=='1') {
						jQuery('#pubblica-' + sGuid ).hide();
						jQuery('#spubblica-' + sGuid ).show();
						alert('Il commento e\' stato pubblicato');
							}
				   });
	};

function spubblicaCommento( sUrl, sGuid ) {
	jQuery.post(sUrl, { azione: "spubblica", guid: sGuid },
				   function(data){
					 if (data=='1') {
						jQuery('#spubblica-' + sGuid ).hide();
						jQuery('#pubblica-' + sGuid ).show();
						alert('Il commento e\' stato spubblicato');
							}
				   });
	};
function eliminaCommento( sUrl, sGuid ) {
	if (confirm("Cancellare il commento?")) {
			jQuery.post(sUrl, { azione: "elimina", guid: sGuid },
						   function(data){
							 if (data!='0') {
								jQuery('#commento-' + sGuid ).remove();
								jQuery('#quanti-commenti').html(data);
								alert("Commento cancellato")
									}
						   });
			}
	   };
	
function playBrano( sIDPlayer, nIndex ) {
	if (nIndex>__raccoltaBrani.length-1) nIndex = 0;
	__currentIndexAlbum = nIndex;
	jQuery('#contentplaylist li').removeClass('sel');
	jQuery('#li-album-' + __raccoltaBrani[nIndex]).addClass('sel');
	document.getElementById(''+sIDPlayer).sendToActionScript('play');
	document.getElementById(''+sIDPlayer).sendToActionScriptPlayer( __raccoltaBrani[nIndex], defaultPath + 'xmlplesterno-');	
	};	 
	
jQuery(function($){
  var lbl = {
    't_0': 'Cerca tutto...',
    't_1': 'Cerca brani...',
    't_2': 'Cerca compositori...',
    't_3': 'Cerca esecutori...',
    't_4': 'Cerca enti...'
  };
  var inp = $('#q');
  var but = $('#b');
  var clr = function(){ $.each(lbl,function(i,a){ if (inp.val() == a) inp.val(''); }); };
  inp.focus(clr); but.click(clr); var bset = false;
  $('li.seleziona input').each(function(i,item){
    var txt = lbl[item.id]; if (!txt) return;
    if (!bset) { inp.val(txt); but.val(txt); bset = true;}
    $(item).click(function(){
      inp.val(txt); but.val(txt);
      $('li.seleziona li').removeClass('sel');
      $(this).parent().addClass('sel');
    });
  });
  var iUsr = $('#txtUsr');
  var iPwd = $('#txtPwd');
  iUsr.focus(function(){ if (iUsr.val()=='nome utente') iUsr.val(''); });
  iPwd.focus(function(){ if (iPwd.val()=='password') iPwd.val(''); });
});
  
  
$(document).ready(function() {
	//Apre la slide che contiene il login al click sulla scritta login
	function openSlide(){
		$("#renderlogin").show("slide", { direction: "right" }, 250);
	};
	$("#loginbutton").click(function() {
		openSlide();
		return false;
	});
	
	//Chiude la slide che contiene il login al click sulla X
	function closeSlide(){
		$("#renderlogin").hide("slide", { direction: "right" }, 250);
	};
	$("#loginbuttonclose").click(function() {
		closeSlide();
		return false;
	});
	
	//Applica gli stili ai div dei tags per fare in modo che si comportino come TAB
	$(function() {
		$("#contenttags").tabs();
	});
	
	//Apre e chiude le finestrelle dei brani, generi ed esecutori
	$('.accordiontogglelist .buttonmore').click(function() {
	$(this).parent().parent().next().toggle('blind','fast');
	$(this).parent().parent().toggleClass('open');
	return false;
	}).parent().parent().next().hide();

});	




