
/*
 *
 *
 */
(function($ektron){
    
    //JQuery plugin definition
    $ektron.fn.replaceHtml = function(uOptions){

        //Grab user options
        var options = $ektron.extend({},$ektron.fn.replaceHtml.defaults, uOptions);
        
	    return this.each(function(){
    		
    		//The element which will have its HTML replaced
    		var $this = $ektron(this);
    		var elementid = ($this.attr("id") == null) ? "id" : $this.attr("id");
    		var elementclass = ($this.attr("class") == null) ? "class" : $this.attr("class");
    		
    		//Generate unique id to be used for the new element.
    		var uniqueID = elementid + "_" + elementclass + "_" + options.UID;
    		
    		//Check the cache option
    		if(!options.cache){
    		    //If not using 'cache' go grab the new data from the server
		        $ektron.ajax({
		            type: options.type,
		            url: options.url,
                    data: options.data,
                    success: function(ret){
                        $this.html(ret);
                    }
                });
            }else{
                //If 'cache' is to be used:
                var children = $this.children();
                
                if(document.getElementById(uniqueID) != null){
                   children.css("display","none");
                   $ektron('#' + uniqueID).css("display","block");
                }else{
                    //Search for unique id in child nodes
                    //If uniqueid exists it is displayed otherwise we go to server and grab it.
                    $ektron.ajax({
		                type: options.type,
		                url: options.url,
                        data: options.data,
                        success: function(ret){
                            //Hide all child nodes from view, does not remove them.
                            var children = $this.children();
                            children.css("display","none");
                            $this.append("<div id=\"" + uniqueID +"\">" + ret + "</div>");
                        }
                    });
                }
            }
	    });
    	
    };    

    //Default options
    $ektron.fn.replaceHtml.defaults = {
        type: "POST",
        url: "",
        cache: true,
        data: "",
        UID: ""
    }
    
})($ektron)
