/**
 * CurlrObject - Curl RTE detection and embed.
 * 
 * Written by Steve Adams, derived from Geoff Stearns SWFObject 
 * (http://blog.deconcept.com/swfobject/)
 *
 * CurlrObject is (c) 2006 Steve Adams and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * SWFObject is (c) 2006 Geoff Stearns and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 *
 */
 
if(typeof curlr == "undefined")     var curlr      = new Object();
if(typeof curlr.util == "undefined")    curlr.util = new Object();
		
curlr.CurlrObject = function(appletUrl, width, height, version, redirectUrl, id){
        if (!document.getElementById) { return; }
		
		/* initialize */
        this.variables  = new Object();
        this.attributes = new Array();
				
		/* attributes from parameters */
        if(appletUrl) { this.setAttribute('appletUrl', appletUrl); }
        if(width)     { this.setAttribute('width',     width); }
        if(height)    { this.setAttribute('height',    height); }
        if(version)   { this.setAttribute('version',   version); }

		/* default detection */
		this.setAttribute('curlEnabled', false);
		this.setAttribute('versionEnabled', false);
		
		/* default redirect to the download page */
        this.setAttribute('redirectUrl', 'http://www.curl.com/download/index.php');
        if(redirectUrl) { this.setAttribute('redirectUrl', redirectUrl); }
		
		if(id) { this.setAttribute('id',        id); }

}

curlr.CurlrObject.prototype = {
        setAttribute: function(name, value){
                this.attributes[name] = value;
        },
        getAttribute: function(name){
                return this.attributes[name];
        },
        addVariable: function(name, value){
                this.variables[name] = value;
        },
        getVariable: function(name){
                return this.variables[name];
        },
        getVariables: function(){
                return this.variables;
        },
        getVariablePairs: function(){
                var variablePairs = new Array();
                var key;
                var variables = this.getVariables();
                for(key in variables){
                        variablePairs.push(key +"="+ variables[key]);
                }
                return variablePairs;
        },
		/* create the embed tag */
        getCURLHTML: function() {
		        /* create a query args list from the variables */
				var pairs = this.getVariablePairs().join("&");
				var appUrl = this.getAttribute('appletUrl')
                if (pairs.length > 0){ 
				   appUrl += "?" + pairs;
				}
				
                var curlNode = "";
				curlNode = '<embed type="txt/vnd.curl" src="'+ appUrl +'" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'"';
				if (this.getAttribute('id') != null) {
			       curlNode += ' id="'+ this.getAttribute('id') +'" name="'+ this.getAttribute('id') +'" ';
				}

                curlNode += '/>';
                return curlNode;
        },
		/* create the install message tag */
		getINSTALLCURLHTML: function() {
		    	var curlNode = "<p>Curl version " + this.getAttribute('version') + " is not installed.<br />";
				curlNode += '<a href= "'+this.getAttribute('redirectUrl')+'">Please download and install the Curl RTE</a></p>'
				return curlNode;
        },
		/* create the disabled message tag */
		getDISABLEDCURLHTML: function() {
				var curlNode = "<p>Curl version " + this.getAttribute('version') + " is disabled<br />";
				curlNode += 'You must enable the Curl RTE to view this applet</p>'
				return curlNode;
        },
        write: function(elementId){
		        /* the target element for writing */
				var n = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
				
		        /* check for enablement and installation at the time of the write */
		        this.RTEVersionExists(this.getAttribute('version'));
				
		        if (!this.getAttribute('versionInstalled')) {
				   /* write a message to install Curl */
				   n.innerHTML = this.getINSTALLCURLHTML();
				   return false;
		        }
				else if (this.getAttribute('curlEnabled')) {
				   /* Curl is installed and enabled */
                   n.innerHTML = this.getCURLHTML();
                   return true;
                }
				else
				   /* Curl is installed and disabled */
				   n.innerHTML = this.getDISABLEDCURLHTML();
                return false;
        },
		RTEVersionExists: function (version) {
		  /* Mozilla family */
          if (navigator.mimeTypes && navigator.mimeTypes.length) {
		    /* check for curl mime type for the version */
            x = navigator.mimeTypes["text/vnd.curl.surge." + version];
            if (x) {
			  this.setAttribute('versionInstalled', true);
			  if (x.enabledPlugin) this.setAttribute('curlEnabled', true);
			}
		  }
          /* Earlier versions don't support mimeTypes - but Curl may not support the browser */
		  /* so this code may never be called */
          else if (navigator.plugins && navigator.plugins.length) {
            		x = navigator.plugins["Curl RTE " + version + " Plug-in"];
            		if (x) {
			           this.setAttribute('versionInstalled', true);
			           if (x.enabledPlugin) this.setAttribute('curlEnabled', true);
			        }
               }
          /* IE family, avoiding VBScript */
          else {
             /* cannot tell the difference between diabled and not installed */
			 try {
			 	 x = new ActiveXObject("Curl.Surge." + version);
				 if (x != null) {
				     this.setAttribute('curlEnabled', true);
					 this.setAttribute('versionInstalled', true);
				 }
		     }catch(e){ // curl is not installed 
			 }
		  } 
		return false;
   }
}



/* add Array.push if needed (ie5) */
if (Array.prototype.push == null) { Array.prototype.push = function(item) { this[this.length] = item; return this.length; }}

var CurlrObject = curlr.CurlrObject;
