﻿/*
gingerpac.com copyright(c) 2010 All rights reserved
*/

gdn = function() {};

gdn.setActiveStyleSheet = function(title) {
    var i,
        a,
        links = document.getElementsByTagName("link"),
        len = links.length;
    for (i = 0; i < len; i++) {
        a = links[i];
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
            a.disabled = true;
            if (a.getAttribute("title") == title) a.disabled = false;
        }
    }
};

gdn.ScriptTagProxy = function(config){
    Ext.apply(this, config);
    gdn.ScriptTagProxy.superclass.constructor.call(this, config);
    
    this.head = document.getElementsByTagName("head")[0];
};

gdn.ScriptTagProxy.TRANS_ID = 1000;

Ext.extend(gdn.ScriptTagProxy, Ext.data.DataProxy, {
    timeout : 30000,
    callbackParam : "callback",
    txBridgeFunc: '',
    nocache : true,

    load : function(callback, params, reader, scope, arg){
        if(this.fireEvent("beforeload", this, params) !== false){

            var p = Ext.urlEncode(Ext.apply(params, this.extraParams));

            var url = this.url;
            url += (url.indexOf("?") != -1 ? "&" : "?") + p;
            if(this.nocache){
                url += "&_dc=" + (new Date().getTime());
            }
            var transId = ++Ext.data.ScriptTagProxy.TRANS_ID;
            var trans = {
                id : transId,
                //*tli 12/14/10 added remote callback function name
                cb : this.txBridgeFunc ? this.txBridgeFunc : "stcCallback"+transId,
                scriptId : "stcScript"+transId,
                params : params,
                arg : arg,
                url : url,
                callback : callback,
                scope : scope,
                reader : reader
            };
            var conn = this;
            window[trans.cb] = function(o){
                conn.handleResponse(o, trans);
            };

            url += String.format("&{0}={1}", this.callbackParam, trans.cb);

            if(this.autoAbort !== false){
                this.abort();
            }

            trans.timeoutId = this.handleFailure.defer(this.timeout, this, [trans]);

            var script = document.createElement("script");
            script.setAttribute("src", url);
            script.setAttribute("type", "text/javascript");
            script.setAttribute("id", trans.scriptId);
            this.head.appendChild(script);

            this.trans = trans;
        }else{
            callback.call(scope||this, null, arg, false);
        }
    },

    // private
    isLoading : function(){
        return this.trans ? true : false;
    },

    /**
     * Abort the current server request.
     */
    abort : function(){
        if(this.isLoading()){
            this.destroyTrans(this.trans);
        }
    },

    // private
    destroyTrans : function(trans, isLoaded){
        this.head.removeChild(document.getElementById(trans.scriptId));
        clearTimeout(trans.timeoutId);
        if(isLoaded){
            window[trans.cb] = undefined;
            try{
                delete window[trans.cb];
            }catch(e){}
        }else{
            // if hasn't been loaded, wait for load to remove it to prevent script error
            window[trans.cb] = function(){
                window[trans.cb] = undefined;
                try{
                    delete window[trans.cb];
                }catch(e){}
            };
        }
    },

    // private
    handleResponse : function(o, trans){
        this.trans = false;
        this.destroyTrans(trans, true);
        var result;
        try {
            result = trans.reader ? trans.reader.readRecords(o) : o;  //*tli 12/14/2010
        }catch(e){
            this.fireEvent("loadexception", this, o, trans.arg, e);
            trans.callback.call(trans.scope||window, null, trans.arg, false);
            return;
        }
        this.fireEvent("load", this, o, trans.arg);
        trans.callback.call(trans.scope||window, result, trans.arg, true);
    },

    // private
    handleFailure : function(trans){
        this.trans = false;
        this.destroyTrans(trans, false);
        this.fireEvent("loadexception", this, null, trans.arg);
        trans.callback.call(trans.scope||window, null, trans.arg, false);
    },
    
    // inherit docs
    destroy: function(){
        this.abort();
        gdn.ScriptTagProxy.superclass.destroy.call(this);
    }
});

gdn.menuLoader = function() {
};

gdn.menu = function(config) {
    Ext.apply(this, config);
    if (this.url) {
        var _this = this;
        Ext.Ajax.request({
            url: this.url,
            method: 'GET',
            success: function(response, options) {
                _this.items = Ext.util.JSON.decode(response.responseText);
                if (_this.callback) {
                    _this.callback(_this.items);
                }
            },
            failure: function(response, options) {
                Ext.Msg.alert("Ajax Error", response.responseText);
            }
        });
    }
};

Ext.ux.BubblePanel = Ext.extend(Ext.Panel, {
    baseCls: 'x-bubble',
    frame: true
});
