/* is this stuff defined? */
if (!document.ELEMENT_NODE) {
	document.ELEMENT_NODE = 1;
	document.ATTRIBUTE_NODE = 2;
	document.TEXT_NODE = 3;
	document.CDATA_SECTION_NODE = 4;
	document.ENTITY_REFERENCE_NODE = 5;
	document.ENTITY_NODE = 6;
	document.PROCESSING_INSTRUCTION_NODE = 7;
	document.COMMENT_NODE = 8;
	document.DOCUMENT_NODE = 9;
	document.DOCUMENT_TYPE_NODE = 10;
	document.DOCUMENT_FRAGMENT_NODE = 11;
	document.NOTATION_NODE = 12;
}

document._importNode = function(node, allChildren) {
	/* find the node type to import */
	switch (node.nodeType) {
		case document.ELEMENT_NODE:
			/* create a new element */
			var newNode = document.createElement(node.nodeName);
			/* does the node have any attributes to add? */
			if (node.attributes && node.attributes.length > 0)
				/* add all of the attributes */
				for (var i = 0, il = node.attributes.length; i < il;)
					newNode.setAttribute(node.attributes[i].nodeName, node.getAttribute(node.attributes[i++].nodeName));
			/* are we going after children too, and does the node have any? */
			if (allChildren && node.childNodes && node.childNodes.length > 0)
				/* recursively get all of the child nodes */
				for (var i = 0, il = node.childNodes.length; i < il;)
					newNode.appendChild(document._importNode(node.childNodes[i++], allChildren));
			return newNode;
			break;
		case document.TEXT_NODE:
		case document.CDATA_SECTION_NODE:
		case document.COMMENT_NODE:
			return document.createTextNode(node.nodeValue);
			break;
	}
};

var PlaylistController = 
{
    _playlist: null,
    _current: null,
    
    init: function(xmlToLoad)
    {
        new Ajax.Request(xmlToLoad, { onComplete: this.playlistLoaded.bind(this)});
    },
    
    playlistLoaded: function(r)
    {
        var items = $A(r.responseXML.getElementsByTagName('item'));
        this._playlist = $A();
        
        items.each(function(item) {
        
        	var id = item.getElementsByTagName('id');
            id = (id.length > 0)?(id[0].text||id[0].textContent):'';
            
            var title = item.getElementsByTagName('title');
            title = (title.length > 0)?(title[0].text||title[0].textContent):'';
            
            var author = item.getElementsByTagName('author');
            author = (author.length > 0)?(author[0].text||author[0].textContent):'';
            
            var description = item.getElementsByTagName('description');
            description = (description.length > 0)?(document._importNode(description[0], true)):'';
            
            var votes = item.getElementsByTagName('votes');
            votes = (votes.length > 0)?(votes[0].text||votes[0].textContent):'';
            
            var pi = {
            	id: id,
                author: author,
                title: title,
                description: description,
                votes: votes
            };
            
            this._playlist.push(pi);
        }.bind(this));
        
        this.setCurrent(this._current);
    },
    
    setCurrent: function(index)
    {
        this._current = index;
        
        if(index == null || this._playlist == null)
            return;

        var t = $('videoHeader');
        if(t)
        {
            t.update('<strong>'+this._playlist[index].title+'</strong>  ');
        }

        var c = $('videoDescription');
        if(c)
        {
            c.innerHTML = '';
            var d = this._playlist[index].description;
            for(var i = 0; i < d.childNodes.length; i++)
            {
                c.appendChild(d.childNodes[i].cloneNode(true));
            }
        }
        
        var i = $('vote');
        if(i)
        {
            i.href = "/casting/formulaire.php?c=" + this._playlist[index].id;
        }
        
        var v = $('compteur');
        if(v)
        {
            v.update('<strong>'+this._playlist[index].votes+'</strong> vote(s)');
        }
    }
}
    
function getUpdate(type, pr1, pr2, more)
{
    if(type == 'item')
        PlaylistController.setCurrent(pr1);
    if((type == "state") && (pr1 != undefined))
    { 
        currentState = pr1;
        if((currentState == "0") && ( !playingFlag))
        {
            playingFlag = true;
            var i = 0;
            var p = $('player');
            var cd = null;
            var d = p.itemData(i);
            while(cd == null && d != null)
            {
                if(d.file == startFile)
                {
                    cd = d;
                }
                else
                {
                    i++;
                    d = p.itemData(i);
                }
            }
            if(cd != null)
            {
                p.sendEvent('playitem', i);
            }
            else
                p.sendEvent('playitem', 0);
        }
    }
}