// ************************************************************************************************
// *** Constructs the ig playlist object (It wraps the general playlist object)
// ************************************************************************************************

function PlaylistIg(playlistName) {
	// ************************************************************************************************
	// *** Interface
	// ************************************************************************************************
	
	// Fields
	// ******
	this.cookieManager = new CookieManager('megaplayer_session', null, window.location.toString().match('^(http://)?([^/]+)')[2]);
	this.playlist = new Playlist(); // General playlist
	this.playlistName = playlistName;
	this.offSet = 17;
	this.allowDefaulPlaylist = true; // Informs if the addition of 'default playlist' items is still allowed
	
	// Methods
	// *******	
	this.add = pl_Add;
	this.clear = pl_Clear;
	this.count = pl_Count;
	this.indexOf = pl_IndexOf;
	this.moveItem = pl_moveItem;
	this.remove = pl_Remove;
	this.setCurrentMediaItem = pl_SetCurrentMediaItem;

	// ************************************************************************************************
	// *** Implementation
	// ************************************************************************************************	
	
	// Fields
	// ******
	this.items = new Array();
	
	// Methods
	// *******
	this.updatePlItemsPosition = pl_UpdatePlItemsPosition;
	this.writeCookie = pl_WriteCookie;
	
	// Bind events
	this.playlist.onChange.add(this.playlistName, 'writeCookie');

	// Adds a MediaItem in the playlist
	function pl_Add(item) 
	{
		var divElem;
		
		// Blocks the default playlist on a regular 'add'
		if(!item.defaultPlaylist) this.allowDefaulPlaylist = false;

		// Creates a div
		divElem = parent.playlist.document.createElement('div');
		divElem.onmouseover = new Function("playlist.UpdateMouseOver(this, " + item.contentId + ", 'img" + item.contentId + "', '" + item.title + "' ,'" + item.duration + "' ,'" + item.rating + "' ," + item.canAccess + " ," + item.canBuy + ");");
		divElem.innerHTML = '<img class="thumb" id="img' + item.contentId + '" src="' + item.imgUrl + '" width="111" height="78" /> <img src="image/playListCorePipe.gif" />';
		divElem.className = 'plItem';
		
		// Append element to document		
		parent.playlist.document.body.appendChild(divElem);
		
		// Hides the mouseover, in case it's still hanging there
		parent.playlist.HideMouseOver();
		
		// Insert the div element at begin of array
		this.items.splice(0, 0, divElem);
		
		// Set the items position
		this.updatePlItemsPosition();		
		
		// Insert the media item into wrapped playlist
		this.playlist.insertItem(0, item);
	}
	
	// Clears the playlist
	function pl_Clear() {
		var i;
		
		// Remove the divs from body
		for(i = 0; i < this.items.length; i++)
			parent.playlist.document.body.removeChild(this.items[i]);
		
		// Clear divs array
		this.items = new Array();
		
		// Clear internal playlist
		this.playlist.clear();
	}
	
	// Returns the size of playlist
	function pl_Count() {
		return this.items.length;
	}
	
	// Returns the index of a div
	function pl_IndexOf(divItem) {
		var i;
		
		for(i = 0; i < this.items.length; i++)
			if(this.items[i] == divItem)
				return i;
				
		return -1;
	}
	
	function pl_moveItem(oldIndex, newIndex) {
		var temp;

		if(oldIndex == newIndex) return; // Do not exchange the same position

		// Blocks the default playlist on a 'move' operation
		this.allowDefaulPlaylist = false;
		
		// Removes from old position and save in a temporary variable
		temp = this.items.splice(oldIndex, 1)[0];
		
		// Insert in the new position
		this.items.splice(newIndex, 0, temp);
		
		// Move the internal playlist items too
		this.playlist.moveItem(oldIndex, newIndex);
		
		// Update positions
		this.updatePlItemsPosition();		
	}

	
	// Removes a media item (and the related div) from playlist
	function pl_Remove(divItem) {
		var index;

		// Blocks the default playlist on a 'remove' operation
		this.allowDefaulPlaylist = false;
		
		// Get the index of item that will be removed
		index = this.indexOf(divItem);
		
		// Remove the div and its related media item in internal playlist
		if(index > -1) {
			// Remove the div
			parent.playlist.document.body.removeChild(divItem);
			this.items.splice(index, 1);
			
			// Remove the media item
			this.playlist.removeAt(index);
			
			// Set the items position
			this.updatePlItemsPosition();
		}
	}
	
	// Sets the items' position
	function pl_UpdatePlItemsPosition() {
		var i;
		
		for(i = 0; i < this.items.length; i++)
			this.items[i].style.left = (i * 130 + this.offSet) + 'px';
		
		parent.playlist.document.body.style.width = (this.items.length * 130 + this.offSet) + 'px';
	}
	
	// Stores the playlist in the cookie
	function pl_WriteCookie() {
		var i, contents = '';
		
		// Build a comma separated list of playlist contents id
		for(i = 0; i < this.playlist.count(); i++) 
		{
			if(i > 0) // Separator
				contents += '|';
				
			contents += this.playlist.getItem(i).contentId;
			//contents += this.playlist.getItem(i).preview;
			
			//alert (contents);
		}
		
		// Write the list in a cookie
		this.cookieManager.setValue('plIds', contents);

		// Writes the "default playlist" status in a cookie
		this.cookieManager.setValue('defPl', this.allowDefaulPlaylist);
	}
	
	// Show a mark in the current media item
	function pl_SetCurrentMediaItem(index) {
		var i;

		// Change the div class to indicate the current media		
		for(i = 0; i < this.items.length; i++)
			this.items[i].className = (i == index) ? 'playing' : 'plItem';
		
		// Set the items position
		this.updatePlItemsPosition();
	}
}
