// JavaScript Document

/*
* @authors:		Alessandro Mazzola, Fabrizio Natoli
* @company:		Marconi Channel WebTV
* @release:		2.0
* @date:			08/06/2011
*/

var delete_status = false;

if ( ! Array.indexOf )
{
	
  Array.prototype.indexOf = function ( obj, start )
	{
		
    for ( var i = ( start || 0 ); i < this.length; i++ )
		{
			
      if ( this [ i ] == obj )
			{
				
        return i;
				
      }
    }
		
    return -1;
		
  }
	
}

function getCookie ( target )
{
	
	var cookie_array = document.cookie.split ( ';' );
	
	for ( var i = 0; i < cookie_array.length; i++ )
  {
		
  	var x = cookie_array [ i ].substr ( 0, cookie_array [ i ].indexOf ( '=' ) );
  	var y = cookie_array [ i ].substr ( cookie_array [ i ].indexOf ( '=' ) + 1 );
  	x = x.replace ( /^\s+|\s+$/g, '' );
  	if ( x == target )
    {
			
    	return unescape ( y );
			
    }
		
  }
	
}

function setCookie ( target, value, expired )
{
	
	var exdate = new Date ();
	exdate.setDate ( exdate.getDate () + expired );
	var current_value = escape ( value ) + ( ( expired == null ) ? '' : '; expires=' + exdate.toUTCString() );
	document.cookie = target + '=' + current_value;
	
}

function checkID ( target )
{
	
	var id = getCookie ( 'ID' );
	var array_temp = new Array ();
	
	if ( id != null && id != '' )
  {
		
  	array_temp = id.split ( ',' );

		var get_index = array_temp.indexOf ( target.toString() );
		
		if ( get_index != -1)
		{
			
			document.getElementById( 'playlist' ).innerHTML = "<a class='playlist_red' href='javascript:void(0);' onclick='unsetID(" + target + ")' title='Rimuovi dalla playlist personale'><span class='hidden'>Rimuovi dalla playlist personale</span></a>";
			
		} else {
		
			document.getElementById( 'playlist' ).innerHTML = "<a class='playlist' href='javascript:void(0);' onclick='setID(" + target + ")' title='Aggiungi alla playlist personale'><span class='hidden'>Aggiungi alla playlist personale</span></a>";
		
		}
		
	} else {
		
		document.getElementById( 'playlist' ).innerHTML = "<a class='playlist' href='javascript:void(0);' onclick='setID(" + target + ")' title='Aggiungi alla playlist personale'><span class='hidden'>Aggiungi alla playlist personale</span></a>";
		
	}
	
	document.getElementById( 'counter' ).innerHTML = ( array_temp.length > 0 ) ? array_temp.length : '0';
	
}

function setID ( target )
{
	
		var id = getCookie ( 'ID' );
		var array_temp = new Array ();
		
		if ( id != null && id != '' )
  	{
			
  		var array_temp = id.split ( ',' );
			array_temp.push ( target );
			setCookie ( 'ID', array_temp, 365 );
			
			document.getElementById ( 'playlist' ).innerHTML = "<a class='playlist_red' href='javascript:void(0);' onclick='unsetID(" + target + ")' title='Rimuovi dalla playlist personale'><span class='hidden'>Rimuovi dalla playlist personale</span></a>";
			document.getElementById ( 'counter' ).innerHTML = array_temp.length;
			
		} else {
			
			setCookie ( 'ID', target, 365 );
			
			document.getElementById ( 'playlist' ).innerHTML = "<a class='playlist_red' href='javascript:void(0);' onclick='unsetID(" + target + ")' title='Rimuovi dalla playlist personale'><span class='hidden'>Rimuovi dalla playlist personale</span></a>";
			document.getElementById ( 'counter' ).innerHTML = array_temp.length + 1;
			
		}
		
		checkID ( target );
		
}

function unsetID ( target )
{
	
		var id = getCookie ( 'ID' );
		var array_temp = new Array ();
		
		if ( id != null && id != '' )
  	{
			
  		var array_temp = id.split ( ',' );
			var index_item = array_temp.indexOf ( target );
			array_temp.splice ( index_item, 1 );
			setCookie ( 'ID', array_temp, 365 );
			
			document.getElementById ( 'playlist' ).innerHTML = "<a class='playlist' href='javascript:void(0);' onclick='setID(" + target + ")' title='Aggiungi alla playlist personale'><span class='hidden'>Aggiungi alla playlist personale</span></a>";
			document.getElementById ( 'counter' ).innerHTML = array_temp.length;
			
		}
		
		checkID ( target );
		
}

function getPlayList ()
{
	
	var id = getCookie ( 'ID' );
	
	if ( id != null && id != '' )
  {
		
		window.location.href = '/playlist?string=' + id;
			
	} else {
		
		jAlert ( 'Attualmente non ci sono contenuti salvati nella playlist', 'Attenzione' );
	
	}
	
}

function remove_item ( target, returnHome )
{
	
	delete_status = true;
	
	jConfirm ( 'Sei sicuro di voler cancellare il video dalla Playlist?', 'Attenzione', function ( result ) {
    
		if ( result == true )
		{
			
			var id = getCookie ( 'ID' );
			var array_temp = new Array ();
			
			if ( id != null && id != '' )
			{
				
				var array_temp = id.split ( ',' );
				var index_item = array_temp.indexOf ( target );
				array_temp.splice ( index_item, 1 );
				
				setCookie ( 'ID', array_temp, 365 );
				
				if ( array_temp.length > 0 )
				{
					window.location.href = '/playlist?string=' + array_temp;
				} else {
					window.location.href = returnHome;
				}
				
				delete_status = false;
				
			}
						
		}
		
	});
}

function getMovie ( path )
{
	
	if ( ! delete_status )
	{
		
		window.location.href = path;
		
	}
	
}
