/*
 * jquery.transform.js
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */
(function($) {
	$.transform = function(o) {
		var t = this;
		t.c = {
			el:o.el,
			cache:o.cache == null?false:o.cache,
			async:o.async == null?true:o.async,
			xslstr:o.xslstr == null?false:o.xslstr,
			xmlstr:o.xmlstr == null?false:o.xmlstr,
			xslParams:o.xslParams == null?false:o.xslParams,
			error:o.error ? o.error : throwTransformError,
			success:o.success == null?false:o.success,
			complete:o.complete == null?false:o.complete
		};
		
		var checkReady = function(o) {
			if(o.c.xslstr && o.c.xmlstr) {
				var tel = $("<div>");
				if($.browser.msie) {
					var xml_X = ["MSXML4.DOMDocument", "MSXML3.DOMDocument", "MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XmlDom"];
					var objXML = false;
					var objXSL = false;
					for(var i = 0; i<xml_X.length;i++) {
						try {
							objXML = new ActiveXObject(xml_X[i]);
							objXSL = new ActiveXObject(xml_X[i]);
							i = xml_X.length;
						} catch(ex) {
							objXML = false;
							objXSL = false;
						}
					}
					
					objXSL.loadXML(o.c.xslstr);
					objXML.loadXML(o.c.xmlstr);
					
					var addparams = function(op, xObj) {
						for(var p in op) 
		                {
		                    var strParam = "//xsl:param[@name='" + p + "']";
		                    
		                    try {
		                        if(isNaN(parseInt(op[p])) && op[p].indexOf("'") < 0) {
									op[p] = op[p].indexOf("\"") >= 0 ? op[p] = op[p].replace(/"/, "'") : op[p] = "'" + op[p] + "'";
		                        };
		                        
			    				var xslParam = xObj.selectSingleNode(strParam);	    				
				    			xslParam.setAttribute("select",op[p]);
				    		    
				            } catch(ex) {
				                //param failed
				            }
		                }
					};
					
					addparams(o.c.xslParams,objXSL);
					
					tel.html(objXML.transformNode(objXSL));
					
				} else {
					var parser = new DOMParser();
					var processor = new XSLTProcessor();
					
					var objXSL = parser.parseFromString(o.c.xslstr,"text/xml");
					var objXML = parser.parseFromString(o.c.xmlstr,"text/xml");
					
					var addparams = function(op) {
						for(var p in op) {
							processor.setParameter(null, p, op[p]);
						}
					};
					
					addparams(o.c.xslParams);
					
					processor.importStylesheet(objXSL);
					var resultDoc = processor.transformToFragment(objXML, document);
					tel.empty().append(resultDoc);
				};
				
				if(o.c.el) {
					$(o.c.el).html(tel.html());
				};
				
				if(o.c.success) {
					if(typeof(o.c.success) == "function"){
						o.c.success(tel.html(),o.c.xslstr,o.c.xmlstr,o.c);
					} 
					else if(fn != null){var funcName = new Function(o.c.success);funcName(tel.html(),o.c.xslstr,o.c.xmlstr,o.c);}
				}
				
				if(o.c.complete) {
					if(typeof(o.c.complete) == "function"){
						o.c.complete(tel.html(),o.c.xslstr,o.c.xmlstr,o.c);
					} 
					else if(fn != null){var funcName = new Function(o.c.complete);funcName(tel.html(),o.c.xslstr,o.c.xmlstr,o.c);}
				}
				return tel.html();
			}
		};
		
		var throwTransformError = function(request,status,error) { };
		
		if(!t.c.xslstr) {
			$.ajax({
				cache:false,
				url:o.xsl,
				dataType:"xml",
				async:t.c.async,
				error:t.c.error,
				complete:function(r) {
					t.c.xslstr = r.responseText;
					if(t.c.async) {checkReady(t); }
				}
			});
		};
		
		if(!t.c.xmlstr) {
			$.ajax({
				cache:false,
				url:o.xml,
				dataType:"xml",
				async:t.c.async,
				error:t.c.error,
				complete:function(r) {
					t.c.xmlstr = r.responseText;
					if(t.c.async) {checkReady(t);}
				}
			});
		};
		
		if(!t.c.async || t.c.xmlstr && t.c.xslstr) {
			return checkReady(t);
		}
	};
	
	$.fn.transform = function(o) {
		return this.each(function() {
			o = o ? o : {};
			o.el = this;
			new $.transform(o);
		});
	};
})(jQuery);

