/**
 * This jQuery plugin converts XML documents to different types of objects/arrays
 * depending methods used and options provided.
 * 
 * @author Rolf Håkon Rensaa
 * Depends on: jQuery v.1.2 or newer
 */
(function($){
	
	$.fromXML = {
		parentElements: [],
		parentElementValues: [],
		elements: [],
		elementvalues: [],
		attrvalues: [],
		xmlloaded: false
	};


    $.fromXML.getParentElements = function(options){ //ancestor
        /** DEFAULTS **/
		var defaults = {
            xmlFile: 'images.xml',
			parentelement: null,
			callback: function(){}
        };
        var opts = $.extend(defaults, options); //defaults
        
        $.get(opts.xmlfile, {}, function(xml){
            $(opts.parentelement, xml).each(function(i){
				n = $(this);
				$.fromXML.parentElements[i] = n; 
				$.fromXML.parentElementValues[i] = n.text();
            });
			$.fromXML.xmlloaded = true;
			$(document).trigger("tr_getParentElements", {message: "message from trigger in getParentElements()"});
			opts.callback(options); // Callback must exist here - use it to avoid race conditions...
        });
    }; // End $.fromXML.getParentElements()
	
	
	$.fromXML.getElements = function(options){
		if ($.fromXML.xmlloaded == false){
			alert("binding dispatcher...returning");
			$(document).bind("tr_getParentElements", {funksjon: "getElements" ,op:options}, dispatcher);
			return;
		}
		/** DEFAULTS **/
        var defaults = {
            elementarray: [],
			callback: function(){}
        };
		var opts = $.extend(defaults, options);

		$(document).unbind("tr_getParentElements", dispatcher);
		var elements = new Object();
		var elementvalues = new Object();
 
		$.each(opts.elementarray, function(){
			//Factory producing object hash -> entities with data arrays; elements["name1"]...
			elements[this] = Array();
			elementvalues[this] = Array();
		});
		
		$.each($.fromXML.parentElements, function(i){
			var element = $(this);
			//Populate object entity arrays with values
			$.each(opts.elementarray, function(n, val){
				var tmp = element.find(val);
				elements[val][i] = tmp;
				elementvalues[val][i] = tmp.text();
			});
		});
		$.fromXML.elements = elements;
		$.fromXML.elementvalues = elementvalues;
		//return elements;
		opts.callback(options); // Callback must exist here - use it to avoid race conditions...
	}; // End $.fromXML.getElements()
    
	
	$.fromXML.getAttrFromElementsArray = function(options ){
		if ($.fromXML.xmlloaded == false) {
			return;
		}

		//var start = (new Date).getTime(); // used for effectivity testing
		var attributes = new Object();
		if ($.fromXML.xmlloaded == false){
			var x1 = "bind dispatcher";
		} 
		/** DEFAULTS **/
        var defaults = {
			elementarray: $.fromXML.parentElements,
			attribarray: [],
			find_subelement: false,
			callback : function(){}      
        };
        var opts = $.extend(defaults, options);
		
		$.each(opts.attribarray, function(){
			attributes[this] = Array();
		});
		
		$.each(opts.elementarray, function(i){
			var element = $(this);
			if (!opts.find_subelement == false){ alert("Finding sub-element..."); element = element.find(opts.find_subelement); }
			//Populate attribute objects with values
			$.each(opts.attribarray, function(n, val){
				
				var tmp = element.attr(val);
				if (tmp == undefined ){ tmp = ""; };
				attributes[val][i] = tmp;
			});
		});
		$.fromXML.attrvalues = attributes;
		opts.callback(attributes); // Callback must exist here - use it to avoid race conditions...
		// call dispatcher -> will come in later version!
		//var diff = (new Date).getTime() - start; //used for effectivity testing
		//alert(diff); //getTime()
		
	}; //End $.fn.getAttrFromElementsArray()
	
	
	// Dispatcher -> this is under construction - "trigger-binded" by functions above
	function dispatcher(e_rhr, options){
		n = e_rhr.data.funksjon;
		switch(n){
			case "getElements":
				alert("case = getElements; dispatcher here!");
				//alert( e_rhr.data.op.elementarray[0] );
				$.fromXML.getElements(e_rhr.data.op);
			break;
			case 2:
  				var x1 = "execute code block 1";
  			break;

			default:
 				var x3 = "code to be executed if n is different from case 1 and 2";
		}
	}; // End dispatcher()
    
})(jQuery);
