/**
_______________________________
COLIBRI */

var Colibri = function(initOptions) {
  this.init(initOptions);
};
   
Colibri.prototype = {
  components:  {},
  XMLPresentation: null,
  XMLDocuments: null,
  vPosition:  0,
  vDuration:  0,
  isPlaying:  false,
  relativeURL: '',

  init: function(initOptions) {	
    var obj = this;
    var XMLPresentationURL = initOptions.relativeURL + "/watch/getXMLData";
    var XMLDocumentsURL = initOptions.relativeURL + "/watch/getXMLData";
    var noCache = Math.random(); // fix AJAX cache problems in IE
    var components = new Array();

    this.vDuration = initOptions.playerOptions.vDuration;
    this.isPlaying = true;

    if(initOptions.presentationData) {
    	obj.XMLPresentation = initOptions.presentationData;
    } else {
	    $.ajax({type: "GET", 
			async:false, 
			url: XMLPresentationURL, 
			data: ({presentationId: document.getElementById('presentationId').value, file: 'presentation', actualizeIe: noCache}), 
			dataType: "json",
			success:function(data, textStatus){/*console.info('Presentation success', textStatus);*/obj.XMLPresentation = data;},
			error:function(e){/*console.error('Presentation error', e)*/}});
    }
    
    if(initOptions.documentsData) {
    	obj.XMLDocuments = initOptions.documentsData;
    } else {
	    $.ajax({type: "GET", 
			async:false,
			url: XMLDocumentsURL, 
			data: ({presentationId: document.getElementById('presentationId').value, file: 'documents', actualizeIe: noCache}), 
			dataType: "json",
			success:function(data, textStatus){/*console.info('Documents success', textStatus);*/obj.XMLDocuments = data;},
			error:function(e){/*console.error('Documents error', e)*/}});
    }

    for (var component in initOptions.colibriComponents) {  // create and add components to Colibri
      if(component == "player") 
        this.components["player"] = new Colibri.player(initOptions);
      else if(component == "sidebar")
        this.components["sidebar"] = new Colibri.sidebar(obj.XMLPresentation, obj.XMLDocuments);
      else
        this.components[component] = eval("new Colibri." + initOptions.colibriComponents[component] + "(this.XMLPresentation, this.XMLDocuments);");
    }
  },

	sync: function () {
		if (this.isPlaying == true) {
			var component = null;

			for (component in this.components) { // call components sync functions
			  if(this.components[component].sync === undefined)
			    alert("No sync method for "+component+" of type "+typeof(this.components[component]));
				this.components[component].sync(this.vPosition);
			}
		}
		setTimeout("theColibri.sync();", 1000);
	}
};
