/**
_______________________________
CHAPTERS COMPONENT */

Colibri.chapters = function(XMLPresentation, XMLDocuments) {
	this.init(XMLPresentation, XMLDocuments);
};

$.extend(Colibri.chapters.prototype, {
	chapters: {length: 0, currentChapter: 0},

	  init: function(XMLPresentation, XMLDocuments) {
		$('.ulevel0').empty();

		//@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;
	    //end quickfix

		this.getChapter(this, -1, XMLPresentation.speech.content.outline);
	  },
	  
	  /**
	   * 
	   * @param obj
	   * @param i depth
	   * @param data an outline array
	   */
	  getChapter: function (obj, i, data) {
		  if (!data.length) {
			  var tmp = data;
			  data = Array();
			  data[0] = tmp;
		  }
		++i;
	    $.each(data, function() {
	    	if(this.attributes.title != undefined)
	    	{
		    	$(".ulevel" + (i).toString() + ":last").append("<li class=\"ilevel" + (i).toString() + "\"><a href=\"javascript:theColibri.components.player.setTimecode(" + this.attributes.timecode + ");\" title=\"" + goToTime + obj.formatTime(Math.floor(this.attributes.timecode / 1000)) + ofTheVideo + " - " + this.attributes.title + "\"><span class=\"chapterTitle\">" + this.attributes.title + "</span></a></li>");
		    	obj.chapters[obj.chapters.length++] = {
		    		timecode: this.attributes.timecode,
		    		title: this.attributes.title
		    	};
	    	}
	    	if (this.outline) {
	    		$("li.ilevel" + (i).toString() + ":last").append("<ul class=\"ulevel" + (i + 1).toString() + "\"></ul>");
	    		obj.getChapter(obj, i, this.outline);
	    	}
	    });
	  },

	  sync: function(vPosition) { // set currentChapter
		var min = 0;
		var max = this.chapters.length;
		var mid = parseInt((min + max) / 2);

		//@quickfix #160
		if(max == 0)
			return;
		//end quickfix

		while (mid != min) {
			if (this.chapters[mid].timecode > Math.floor(vPosition * 1000)) {
				max = mid;
			} else {
				min = mid;
			}
			mid = parseInt((min + max) / 2);
		}

		if (min != this.chapters.currentChapter || !this.chapters.currentChapter) {
			$('#plan li:has(a.currentChapter):last').removeClass('currentChapter');
			$("#plan a:eq(" + this.chapters.currentChapter + ")").removeClass('currentChapter');
			$("#plan a:eq(" + min + ")").addClass('currentChapter');
			$('#plan li:has(a.currentChapter):last').addClass('currentChapter');
			this.chapters.currentChapter = min;
			if (this.chapters[min].title.length > 40) {
			  $('#plan span#currentChapter span').text(this.chapters[min].title.substr(0, 40) + '...');
			} else {
			  $('#plan span#currentChapter span').text(this.chapters[min].title);
			}
		}
	  },

	  formatTime: function(time) { // format time to hh:mm:ss
	    var t = new Array(time, 0, 0); // t[0] => seconds, t[1] => minutes, t[2] => hours

	    if (t[0] > 60) {
	      t[1] = parseInt(time / 60, 10);
	      t[0] = parseInt(time % 60, 10);
	    }
	    if (t[1] > 60) {
	      t[2] = parseInt(t[1] / 60, 10);
	      t[1] = parseInt(t[1] % 60, 10);
	    }

	    for (i = 0; i < 3; i++)
	      t[i] = t[i] < 10 ? '0' + t[i] : t[i];

	    if (t[2] != '00') {
	      return t[2] + ':' + t[1] + ':' + t[0];
	    } else {
	      return t[1] + ':' + t[0];
	    }
	  }
	});
