﻿/**
* $$ - shorthand to grab an object by ID
* @constructor
*/
$$ = function(id){ return document.getElementById(id); }

/**
* TDC Object - A collection of useful functions
* @constructor
*/
var TDC = {
	/* ----------- MEMBERS ----------- */
	version : "0.2.0 Beta",
	vars : {},
	onLoadChain : [],
	
	/**
	* Merges the members of one Object to that of another
	* @param {Object} Original Object to affect
	* @param {Object} Object whose members you wish to merge
	* @param {Object} Default Object that you want to merge first, to set defaults that may get overwritten
	* @return {Object} Returns your updated object, though you don't need to use it
	*/
	apply : function(o, c, defaults){
	    if(defaults){
	        TDC.apply(o, defaults);
	    }
	    if(o && c && typeof(c) == 'object'){
	        for(var p in c){
	            o[p] = c[p];
	        }
	    }
	    return o;
	},
	
	/**
	* Javascript's typeof function returns 'object' for null... this fixes that.
	* @param {Object}
	* @return {String}
	*/ 
	typeOf : function(obj){
		type = typeof obj;
		return type === "object" && !obj ? "null" : type;
	},
	
	/**
	* Do Not Use.  This function becomes bound to the window.onload event, use TDC.onLoad() to add additional functions
	* @see #onLoad
	*/
	onLoadComplete : function(){
		for(x in TDC.onLoadChain){
			if(TDC.typeOf(TDC.onLoadChain[x])=='function') TDC.onLoadChain[x]();
		}
	},
	
	/**
	* Each function passed into TDC.onLoad(func) will be added to the event chain that gets called after the document is loaded.
	* @param {Function} func
	*/
	onLoad : function(func){ 
		if(TDC.typeOf(func)=='function') this.onLoadChain.push(func);
		else if(TDC.typeOf(func)=='object' && func.length > 0) TDC.onLoadChain = TDC.onLoadChain.concat(func); 
	},
	
	/**
 	* Takes an object and converts it to an encoded URL. e.g. TDC.urlEncode({foo: 1, bar: 2}); would return "foo=1&bar=2".  Optionally, property values can be arrays, instead of keys and the resulting string that's returned will contain a name/value pair for each array value.
    * @param {Object} o
    * @return {String}
    */
   urlEncode : function(o){
       if(!o){
           return "";
       }
	   TDC.apply(o,{varClear:Math.random()});
       var buf = [];
       for(var key in o){
           var ov = o[key];
           var type = typeof ov;
           if(type == 'undefined'){
               buf.push(encodeURIComponent(key), "=&");
           }else if(type != "function" && type != "object"){
               buf.push(encodeURIComponent(key), "=", encodeURIComponent(ov), "&");
           }else if(ov instanceof Array){
               for(var i = 0, len = ov.length; i < len; i++) {
                   buf.push(encodeURIComponent(key), "=", encodeURIComponent(ov[i] === undefined ? '' : ov[i]), "&");
               }
           }
       }
       buf.pop();
       return buf.join("");
   },

   /**
    * Takes an encoded URL and and converts it to an object. e.g. Ext.urlDecode("foo=1&bar=2"); would return {foo: 1, bar: 2} or Ext.urlDecode("foo=1&bar=2&bar=3&bar=4", true); would return {foo: 1, bar: [2, 3, 4]}.
    * @param {String} string
    * @param {Boolean} overwrite (optional) Items of the same name will overwrite previous values instead of creating an an array (Defaults to false).
    * @return {Object} A literal with members
    */
   urlDecode : function(string, overwrite){
       if(!string || !string.length){
           return {};
       }
       var obj = {};
       var pairs = string.split('&');
       var pair, name, value;
       for(var i = 0, len = pairs.length; i < len; i++){
           pair = pairs[i].split('=');
           name = decodeURIComponent(pair[0]);
           value = decodeURIComponent(pair[1]);
           if(overwrite !== true){
               if(typeof obj[name] == "undefined"){
                   obj[name] = value;
               }else if(typeof obj[name] == "string"){
                   obj[name] = [obj[name]];
                   obj[name].push(value);
               }else{
                   obj[name].push(value);
               }
           }else{
               obj[name] = value;
           }
       }
       return obj;
   }
	
};

// ------------------------------ PLUGIN: FCK EDITOR UPLOADER -----------------------------
TDC.FCK = {};
TDC.apply(TDC.FCK, 
{
	//internal variables
	vars : {
		obj: 		null,
		fullPath: 	true,
		callback: 	null
	},
	
	//external SetUrl function used as callback from FCK
	SetUrl : function(fn){
		if(TDC.FCK.vars.fullPath){
        	if($$(TDC.FCK.vars.obj)) $$(TDC.FCK.vars.obj).value = fn;
		} else {
			if($$(TDC.FCK.vars.obj)) $$(TDC.FCK.vars.obj).value = fn.split('/').pop();
		}
		if(TDC.typeOf(TDC.FCK.vars.callback) == 'function'){
			TDC.FCK.vars.callback();
		}
    },
	
	//call with optional params to popup FCK uploader
	/* OPTIONAL PARAMS -- passed in as a struct with keys
			type: 		uploader media type - 'Image', 'File', 'Flash' or 'Media'
			browse:		browse folders? - true or false
			folder:		default folder path - '/' for root, or use '/folder/path/' format
			fullPath:	return full image path from root, or just filename - true or false
			callback:	callback function to call after returned filename
	*/
	uploader : function(obj, paramStruct){
        this.vars.obj = obj;
		var defaults = {
			type:'Image', 
			browse:true, 
			folder:'/', 
			fullPath:true, 
			callback:null
		};
		var param = TDC.apply({}, paramStruct, defaults);
		this.vars.fullPath = param.fullPath;
		this.vars.callback = param.callback;
        var day = new Date();
        var id = day.getTime();
        eval("page" + id + " = window.open('/FCKeditor/editor/filemanager/browser/default/browser.html?Type=" + param.type + "&BrowseFolders=" + (param.browse ? 1 : 0) + "&CurrentFolder=" + param.folder + "&Connector=../../connectors/cfm/connector.cfm', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=700,height=500,left=500,top=350');");
    }
}
);
//external SetUrl function used as callback from FCK
SetUrl = TDC.FCK.SetUrl;
// ------------------------------ PLUGIN: FCK EDITOR UPLOADER -----------------------------


// Override DOM onLoad event to execute the chain of events that may be defined in code
window.onload = TDC.onLoadComplete;