
jQuery.extend({
    
    // Error templates
    ERROR_BROWSER_XMLDOCUMENT           : new Error('your browser doesnot understand xml documents'),
    ERROR_DEFINITON_REDECLARATION       : new Error("you can't define a declaration that allready exists"),
    ERROR_EXEPTION                      : new Error("An exeption has occured: %%"),
    ERROR_FILE_LOAD                     : new Error("can't find file: %%"),
    ERROR_LOAD                          : new Error('error loading document %%'),
    ERROR_NO_CLASSNAME                  : new Error('expect a classname: %%'),
    ERROR_NO_COMP_BROWSER               : new Error('your browser does not support xml or xhttp'),
    ERROR_NO_ARGUMENT                   : new Error('expect an argument: %%'),
    ERROR_NO_ATTRIBUTE                  : new Error('expect an attribute: %%'),
    ERROR_NO_ELEMENT                    : new Error('could not find element: %%'),
    ERROR_NO_LOADHANDLER                : new Error('expect a loadhandler'),
    ERROR_NO_MATCH_SELECTOR             : new Error('no matching targets for selector'),
    ERROR_NO_MULTI_ARGUMENT             : new Error('expect array or string'),
    ERROR_NO_SELECTOR                   : new Error('no selector attribute'),
    ERROR_NO_SUPPORT_CLIPBOARD          : new Error("your browser doesn't support copy to clipboard feature"),
    ERROR_NO_VALID_TYPE                 : new Error('%% must be of type ##'),
    ERROR_NO_VERSION                    : new Error('to create a new xapp instance provide a version number'),
    ERROR_NOT_COPIED_TO_CLIPBOARD       : new Error('text was not copied to clipboard'),
    ERROR_NOT_FOUND                     : new Error('## %% doesnt exist'),
    ERROR_NOT_STRING                    : new Error('## %% is not of type string'),
    ERROR_NOT_UNIQUE                    : new Error('## %% is not unique'),
    ERROR_NOT_VALID_COPY                : new Error('your current internet security settings do not allow data copying to clipboard, Do you want to learn more about enabling data copying to clipboard in your browser'),
    ERROR_PARSING_XMLCONV               : new Error('error parsing xml string %% for conversion'),
    ERROR_PROCESSING_DOCUMENT           : new Error('error proccessing document %%'),
    ERROR_TEST_NOT_VALID                : new Error('could not preform a valid test: %%'),
    ERROR_XML_LOAD                      : new Error('error loading xml document: %%'),
    ERROR_XML_NOT_VALID                 : new Error('expect a valid xml document'),    
    
    // Messages templates
    MSG_ATTEMP_CONV_STRINGDOC           : "attempting string to XMLdocument conversion",
    MSG_CMDDOC                          : "Command document detected",
    MSG_COPY_TO_CLIPBOARD               : "Copy to Clipboard",               
    MSG_COPY_SUCCESS_CLIPBOARD          : "Text successfully copied to clipboard",
    MSG_DETECT                          : "detect",
    MSG_FAILED                          : "failed",
    MSG_INVOKE_NOT_VALID_DOC            : "JXAPP invoked without valid document, nothing to process",                 
    MSG_NO_SUPPORT_CLIPBOARD            : "Your browser doesn't support copy to clipboard feature",
    MSG_NOT_VALID_COPY                  : "Your current internet security settings do not allow data copying to clipboard. Do you want to learn more about enabling data copying to clipboard in your browser",
    MSG_START_PROCESS                   : "Start process",
    MSG_SUCCES                          : "succesfull",
    
    transform : function transform(xml, xsl) {
        
        xsl = xsl || 'jxapp/tools/default.xsl';
        
        typeof xml === 'string' && (xml = $.xmldocument(xml));
        typeof xsl === 'string' && (xsl = $.xmldocument(xsl));        

        return xml.transformNode(xsl);
    }
    
    ,df: function df(){ 
        return window.document.createDocumentFragment();
    }
        
    ,xmldocument: function xmldocument(filename){
    
        try{ 
            var d = new ActiveXObject("Microsoft.XMLDOM");
        }catch(exception){
            var d = document.implementation.createDocument("", "", null);
        }
        
        // No support 
        !d && alert('Your browser cannot handle this script');
        
        // return document if filename is missing
        if(!filename) return d;
                
        // turn asynchroon handling off
        d.async = false;
        d.load(filename);
        
        return d;
    }
    
    ,parseFromString: function parseFromString(xml, doc) {

        !doc && (doc = $.xmldocument());	

        doc.async = false;
        doc.loadXML( $.cleanString(xml) );

        if(doc && doc.documentElement && doc.documentElement.tagName != 'parsererror')
        return doc; 

        alert( [doc.parseError.reason, doc.parseError.srcText].join('\n') );

        return null;
    }
	
	,isUndefined: function isu(x) {
        var t = typeof(x);
        return t == "undefined" || t == "unknown";
	}
    
    //
    ,compare: function compare(c1,c2) { 
        return c1 && c2 && c1.constructor === c2.constructor;
    }
    
	,isFunction: function isFunction(fn) {
	  
        if(typeof fn != "function") return false;

        // Now check the constructor property. 
        if(typeof fn.constructor != "function") return false;

        // If constructor is Function, then constructor.prototype will have call.
        return fn.constructor.prototype.hasOwnProperty( "call" );
	}

    ,copy2clipboard: function copy2clipboard(text) {

        // TODO move FF to behavior-source.xml
        var msie     = $.browser.msie;
        var mozilla  = $.browser.mozilla;
        var opera    = $.browser.opera;
        var safari    = $.browser.safari;

        text = text || arguments[0];
        	
        if(!confirm( $.MSG_COPY_TO_CLIPBOARD + ':\n' + text)) return;

        if (window.clipboardData || mozilla) 
        {
            if (msie && !mozilla) {
        	
                if (!window.clipboardData.setData("Text", text)) {
	                //throw $.ERROR_NOT_COPIED_TO_CLIPBOARD;
	                return false
                }
        	
            } else {
        		
                try {
	                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect")
                } catch (e) {
        		
	                if( confirm($.ERROR_NOT_VALID_COPY ) ) {
		                window.document.location.href = "jxapp/tools/copy-clipboard.html";
	                } else {
		                throw $.ERROR_NOT_COPIED_TO_CLIPBOARD;			              
	                }
	                return false;
                }
        		
                try {
	                e = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard)
                } catch (e) {
	                return false
                }
        		
                try {
	                b = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable)
                } catch (e) {
	                return false
                }
        	    
                b.addDataFlavor("text/unicode");
                o = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
                o.data = text;
                b.setTransferData("text/unicode", o, text.length * 2);
        		
                try {
	                t = Components.interfaces.nsIClipboard;
                } catch (e) {
	                return false
                }
                e.setData(b, null, t.kGlobalClipboard)
            }

        } else {

            opera ?
              oc(text):
              alert($.MSG_NO_SUPPORT_CLIPBOARD);
              
            return false
        }

        alert($.MSG_COPY_SUCCESS_CLIPBOARD + ":\n\n" + text);
        return true;
    }

    //remove pretty print
    ,pp : function pp(r,s){
       return r.responseText.split(/>[\n\r\t ]+</).join('><');
    }    
    
});