/*
 * jParse Hacked For Portfolio
 * Robin Willis
 */

(function($){
	
	$.fn.extend({
		
	jParse : function(options) {
		
		var defaults = {
			ajaxOpts: {dataType: ($.browser.msie) ? "text" : "xml", contentType: 'text/xml'},
			parentElement : 'project',
			elementTag : ['title','date'],
			parentAttr : ['index','thumb'],
			output: '<div id="index" class="proj_item"><a href="#"><img src="thumb" width="32" height="32" /><h2>title</h2><span class="proj_abs">date</span></a></div>'
		};
		
		settings = $.extend(true, defaults, options);
		$(this).empty();
		//run precallback
        if(settings.precallback !== undefined){
            settings.precallback();
        }
		
		var selected = $(this);
		
	        //master colon regexp
            var colon = /\:/;	
			
			settings.ajaxOpts.success = function(data){
			
			var xml;
			
			if (typeof data == "string") {
                xml = new ActiveXObject("Microsoft.XMLDOM");
                xml.async = false;
                xml.loadXML(data);
            } else {
                xml = data;
            }
			
			var container = '';
			//create object array for each project
			
			var parentElementArray = new Array();
			parentElementArray = $(xml).find(settings.parentElement);
			
			//shortcut for referencing settings
            var o = settings;
			
			//BUILD PARENT ELEMENT ARRAY
		//	$(xml).find(o.parentElement).each(function(){
				
			//	parentElementArray[parentElementArray.length] = $(this);
				
			//});
			
			function nodeChecker(node){
                    //if there is a colon in the entryTag name
                    if(colon.test(node) === true){
                        //asign the elemTagName variable to a jQuery parsable attribute selector
                        elemTagName = '[nodeName=' + node + ']';
                    } else {
                        elemTagName = node;
                    }
                }
                
            //element value processor function
            function processElementValue(elementValue, elementParent){

                    //detect if the specified element tag is a string or an object
                    if(o.elementTag[n].elem === undefined){

                        //run the node checker function
                        nodeChecker(elementValue);
                        
                        //set the element's value
                        elemTagValue = $(elementParent).find(elemTagName).text();
                        
                        //strip any CDATA text from the result if it exists
                        elemTagValue = elemTagValue.replace(/^\[CDATA\[/, '').replace(/\]\]$/, '');
                        
                    } else {

                        //run the node checker function
                        nodeChecker(elementValue);
                        
                        //detect if the the user has not specified an attribute to pull
                        if(o.elementTag[n].attr === undefined){
                            
                            //detect if array of element tag was provided
                            if(o.elementTag[n].select !== undefined){
                             
                                var arrayElements = $(elementParent).find(elemTagName);
                                elemTagValue = $(arrayElements[o.elementTag[n].select]).text();
                                
                            } else if(o.elementTag[n].select === undefined) {

                                elemTagValue = $(elementParent).find(elemTagName).text();
                            
                            }
                            
                        } else {
                            
                            elemTagValue = $(elementParent).find(elemTagName).attr(o.elementTag[n].attr);
                            
                        }
                            
                        //detect for the exclude variable if specified
                        if(o.elementTag[n].exclude !== undefined){
                            var excluder = new RegExp(o.elementTag[n].exclude);
                            if(excluder.test(elemTagValue) === true){
                                
                                //increment the value of the excludeFound variable
                                excludeFound = true;
                                
                            }
                        }
                        
                        //Format function
                        if (o.elementTag[n].format !== undefined) {
                            elemTagValue = o.elementTag[n].format(elemTagValue);
                        }
                        
                        //JS Date Format
                        if(o.elementTag[n].dateFormat !== undefined){
                            elemTagValue = date(o.elementTag[n].dateFormat, elemTagValue);
                        }
                        
                    }
                }//END processElementValue Function			
			
			function processParentAtrributeValue(attrValue, elementParent){
					attrTagValue = $(elementParent).attr(attrValue);
				    
			}
			
			//MAIN LOOP through Project Entrys
			for(var i= 0; i < parentElementArray.length; i++){
			
				//define the output variable option
                var outputVar = o.output;
                    
			
				//sub loop for every element we want to retrieve
				for(var n = 0; n< o.elementTag.length; n++){
						
						var elemTag = o.elementTag[n].toString();
						 
						
						//detect if the elementTag is a simple string or object
                        if(o.elementTag[n].constructor == String){
                            processElementValue(o.elementTag[n], parentElementArray[i]);
                        } else if(o.elementTag[n].constructor == Object){
                            processElementValue(o.elementTag[n].elem, parentElementArray[i]);
                        }	
						
						//replace the position with the value of the selected element tag
                        outputVar = outputVar.replace(elemTag, elemTagValue);
						
					}
				
				//sub loop for every parent attribute we want to retreive
				for(var k = 0; k< o.parentAttr.length; k++){
					
					var parAttr = o.parentAttr[k].toString();
					//this value should get pulled from the xml

                   processParentAtrributeValue(o.parentAttr[k], parentElementArray[i]);        
					
					outputVar = outputVar.replace(parAttr, attrTagValue);	
				}
				//append the results to the container
                  
				
				  
                container += outputVar;
				
                
			}
			
			//output: '<div id="index" class="proj_item"><a href="#"><img src="thumb" width="32" height="32" /><h2>title</h2><span class="proj_abs">date</span></a></div>'
			
			$(selected).append(container);
                
				
				
                
                if(settings.callback !== undefined){
                    settings.callback();
                }
			
			}
		
            return this.each(function() {
                $.ajax(settings.ajaxOpts);
            });
		}
		
	});
	
})(jQuery);



