var $jq = jQuery.noConflict();

/* jQuery selectbox related plugins */

jQuery.fn.containsOption = function(query) {
    var found = false;

    this.each(
		function() {
		    if (this.nodeName.toLowerCase() == 'select') {
		        for (var i = 0; i < this.options.length; i++) {
		            if (query.value) {
		                found = (query.value.constructor == RegExp) ?
							this.options[i].value.match(query.value) :
							this.options[i].value == query.value;
		            } else if (query.text) {
		                found = (query.text.constructor == RegExp) ?
							this.options[i].text.match(query.text) :
							this.options[i].text == query.text;
		            }

		            if (found)
		                break;
		        }
		    } else return this;
		}
	);

    return found;
};

jQuery.fn.addOption = function(o) {
    var opt = o;

    this.each(
		function() {
		    if (this.nodeName.toLowerCase() == 'select') {
		        var option = document.createElement('OPTION');
		        option.value = opt.value;
		        option.text = opt.text;

		        if (opt.selected)
		            option.selected = opt.selected;

		        this.options[this.options.length] = option;
		    }
		    else return this;
		}
	);

    return this;
};

jQuery.fn.clearOptions = function() {
    this.each(
		function() {
		    if (this.nodeName.toLowerCase() == 'select') {
		        this.options.length = 0;
		    }
		}
	);
};

jQuery.fn.removeOption = function(val) {
    this.each(
		function() {
		    if (this.nodeName.toLowerCase() == 'select') {
		        for (var i = 0; i < this.options.length; i++) {
		            if (this.options[i].value == val) {
		                this.options[i] = null;
		            }
		        }
		    } else return this;
		}
	);

    return this;
};

jQuery.fn.selectOptionByValue = function(val) {
    this.each(
		function() {
		    if (this.nodeName.toLowerCase() == 'select') {
		        for (var i = 0; i < this.options.length; i++) {
		            if (this.options[i].value == val) {
		                this.options[i].selected = true;
		            }
		            else {
		                this.options[i].selected = false;
		            }
		        }
		    } else return this;
		}
	);

    return this;
};

jQuery.fn.radioSelectByValue = function(val) {
    this.each(
		function() {
		    if (this.nodeName.toLowerCase() == 'input' && jQuery(this).attr('type').toLowerCase() == 'radio') {
		        if (jQuery(this).val() == val) {
		            this.checked = true;
		        }
		        else {
		            this.checked = false;
		        }
		    } else return this;
		}
	);

    return this;
};

jQuery.fn.radioSelectedValue = function() {
    var val = null;

    this.each(
		function() {
		    if (val == null) {
		        if (this.nodeName.toLowerCase() == 'input' && jQuery(this).attr('type').toLowerCase() == 'radio') {
		            if (this.checked) {
		                val = jQuery(this).val();
		            }
		        }
		    }
		}
	);

    return val;
};

jQuery.createCookie = function(name, value, days, isArray) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";

    if (arguments.length > 3 && isArray) {
        value = jQuery.serializeKeyValues(value);
    }

    document.cookie = name + "=" + value + expires + "; path=/";
};

jQuery.readCookie = function(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) {
            var val = c.substring(nameEQ.length, c.length);

            if (val.indexOf('&')) {
                return jQuery.parseKeyValueString(val);
            }
            else return val;
        }
    }
    return null;
};

jQuery.updateCookie = function(name, val) {
    var _name = name + "=";
    var cookieCollection = document.cookie.split(';');
    for (var i = 0; i < cookieCollection.length; i++) {
        var cookie = cookieCollection[i];
        while (cookie.charAt(0) == ' ') cookie = cookie.substring(1, cookie.length);
        if (cookie.indexOf(_name) == 0) {
            //var cookieVal = cookie.substring(_name.length, cookie.length);
            cookie = cookie + "&" + val;
            jQuery.createCookie(name, "", -1);
            document.cookie = cookie;
        }
    }
};

jQuery.eraseCookie = function(name) {
    jQuery.createCookie(name, "", -1);
};

jQuery.parseKeyValueString = function(val) {
    var hash = [];
    var pieces = val.split('&');

    for (var i = 0; i < pieces.length; i++) {
        var pair = pieces[i].split('=');
        hash[pair[0]] = pair[1];
    }

    return hash;
};

jQuery.serializeKeyValues = function(hash) {
    if (hash != null) {
        var pairs = [];

        for (var key in hash) {
            pairs.push(key + '=' + hash[key]);
        }

        return pairs.join('&');
    }

    return null;
};

jQuery.namespace = function(namespace) {
    var parts = namespace.split('.');

    var current = window;

    for (var i = 0; i < parts.length; i++) {
        if (!current[parts[i]])
            current[parts[i]] = {};

        current = current[parts[i]];
    }

    return current;
};
jQuery.logException = function(e) {
//    if (typeof debugging != undefined && typeof console != undefined) {
//        console.log("File name: " + e.fileName)
//        console.log("Message: " + e.message);
//        console.log("Stack: " + e.stack);
//    }
//    return this;
}
jQuery.queryString = function() {
    var _keyVals = [];
    var _loaded = false;

    var load = function() {
        if (!_loaded) {
            var raw = (window.location.search.length > 0) ? window.location.search.substring(1) : '';
            var pairs = raw.split("&");
            for (i = 0; i < pairs.length; i++) {
                var pair = pairs[i].split("=");

                _keyVals[pair[0]] = pair[1];
            }

            _loaded = true;
        }
    }

    return {
        get: function(key) {
            if (!_loaded)
                load();

            for (var currentKey in _keyVals) {
                if (currentKey.toLowerCase() == key.toLowerCase())
                    return _keyVals[currentKey];
            }

            return null;
        }
    };
} ();

jQuery.fn.addBehavior = function(behaviorName, instance) {
    this.each(
		function() {
		    if (!this.behaviors)
		        this.behaviors = [];

		    this.behaviors[behaviorName] = instance;
		}
	);

    return this;
};

jQuery.initBehavior = function(container, behaviorName, meta) {
    try {
        var ctl = eval('new ' + behaviorName + '()');
        ctl.init({
            "container": container,
            "meta": meta
        });

        jQuery(container).addBehavior(behaviorName, ctl);

        return ctl;
    }
    catch (e) {
        $jq.logException(e);
    }



    return null;
};

jQuery.fn.parseBehavior = function() {
    this.each(function() {
        try {
            var type = $jq(this).attr('behavior');

            if (type != null && type != '') {
                var metaData = null;

                try {
                    metaData = jQuery(this).metadata({ type: 'attr', name: 'meta' });
                }
                catch (e) {
                    $jq.logException(e);
                }



                jQuery.initBehavior(this, type, metaData);
            }
        }
        catch (e) {
            $jq.logException(e);
        }


    });

    return this;
};

jQuery.getBehavior = function(container, behaviorName) {
    if (container.behaviors)
        return container.behaviors[behaviorName];
};

jQuery.fn.behavior = function(behaviorName, eachFunc) {
    this.each(
		function() {
		    var behavior = jQuery.getBehavior(this, behaviorName);
		    if (behavior != null)
		        eachFunc(behavior);
		}
	);

    return this;
};

