/**
_______________________________
ANNEXES COMPONENT */

Colibri.annexes = function(XMLPresentation, XMLDocuments) {
	this.init(XMLPresentation, XMLDocuments);
};

$.extend(Colibri.annexes.prototype, {
	annexes:  {length: 0, currentAnnex: 0},
	currentAnnexesDisplayed: 0,

	  init: function(XMLPresentation, XMLDocuments) {
	    if (XMLDocuments && XMLDocuments.documents.attributes.count > 0) {
  	    //@quickfix #160
        //Iif there's no outlines synchronized, all documents are put in a unique outline element
        //Therefore, XMLPresentation.speech.content.outline is not an array and we're not able to
        //iterate it.
        var data = new Array();
        if (XMLPresentation.speech.content.outline.constructor.toString().indexOf("Array") == -1)
          data.push(XMLPresentation.speech.content.outline);
        else
          data = XMLPresentation.speech.content.outline;
          
        this.getAnnexes(this, data);
        //end quickfix
        this.getAnnexesFilename(this, XMLDocuments.documents.document);
	    }
	  },

	  getAnnexes: function (obj, data) { // build the annexes array, containing only annexes 
	    $.each(data, function() {
	    	if (this.document && this.document.attributes && this.document.attributes.zone == "annex") {
				obj.annexes[obj.annexes.length] = {
					num:		obj.annexes.length,
					id:			this.document.attributes.id,
					timecode:	this.document.attributes.timecode,
					zone:		this.document.attributes.zone,
					setted:		0
				}
				obj.annexes.length++;
	    	} else if (this.attributes && this.attributes.zone == "annex") {
	    		obj.annexes[obj.annexes.length] = {
	    			num:		obj.annexes.length,
	    			id:			this.attributes.id,
	    			timecode:	this.attributes.timecode,
	    			zone:		this.attributes.zone,
	    			setted:		0
	    		}
	    		obj.annexes.length++;
	    	} else if (this.document && this.document.length) {
	    		for (i = 0; i < this.document.length; i++) {
	    			if (this.document[i].attributes.zone == "annex") {
						obj.annexes[obj.annexes.length] = {
							num:		obj.annexes.length,
							id:			this.document[i].attributes.id,
							timecode:	this.document[i].attributes.timecode,
							zone:		this.document[i].attributes.zone,
							setted:		0
						}
						obj.annexes.length++;
	    			}
	    		}
	    	}
	    	if (this.outline) {
				obj.getAnnexes(obj, this.outline);
	    	}
	    });
	  },

	  getAnnexesFilename: function (obj, data) { // set filenames and titles to annexes array 
	    //if there's just one document, make an array to be iterated
	    if(data.constructor != Array)
	      data = [ data ];
	    
	    $.each(data, function () {
			if (this.file) {
				for (i = 0; i < obj.annexes.length ; i++) {
					if (this.attributes.id == obj.annexes[i].id && obj.annexes[i].type != "slide") {
						$.extend(obj.annexes[i], {
							title: this.attributes.title,
							filename: this.file.filename,
							type: this.file.filename.substr(this.file.filename.lastIndexOf('.') + 1).toLowerCase()
						});
					}
				}
			} else if (this.url) {
				for (i = 0; i < obj.annexes.length ; i++) {
					if (this.attributes.id == obj.annexes[i].id && obj.annexes[i].type != "slide") {
						$.extend(obj.annexes[i], {
							title: this.attributes.title,
							url: this.url,
							type: 'url'
						});
					}			
				}
			}
		});
	  },  
	  
	  sync: function(timecode) {
	    if (theColibri.XMLDocuments && theColibri.XMLDocuments.documents.attributes.count > 0) {
			var annex = null;

			for (annex in this.annexes) {
				if (this.annexes[annex].setted == "0" && timecode * 1000 >= this.annexes[annex].timecode) {
					this.annexes[annex].setted = "1";
					this.addAnnex(this.annexes[annex]);
				}
			}
		}
	  },

	  addAnnex: function(doc) {
		if (doc.title.length > 80) {
		  var title = doc.title.substring(0, 80) + "...";
		}
		else {
		  var title = doc.title;
		}

		if (this.currentAnnexesNum == 4) {
		  $('#annex ul').empty();
		  this.currentAnnexesNum = 0;
		}
		++this.currentAnnexesNum;
		if (doc.type == 'url')
			var href = doc.url;
		else 
			var href = theColibri.relativeURL + '/documents/' + doc.filename;
		$('#annex ul').prepend("<li><img src=\"" + theColibri.relativeURL + "/images/watch/ico" + doc.type + ".png\" /><a id=\"annexN" + doc.num + "\" class=\"annexDoc\" href=\""+ href +"\" title=\"" + annexI18n + doc.title + ".\">  "+ title +" (" + doc.type + ")</a></li>");
		document.getElementById('annexN' + doc.num).onclick = function() {
			window.open(this.href); 
			return false;
		};
	  }
	});
