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('&') >= 0) {
                return jQuery.parseKeyValueString(val);
            }
            else return val;
        }
    }
    return null;
};

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.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;
        },
        all: function() {
            if (!_loaded)
                load();
            return _keyVals;
        },
        valueWithout: function(exceptions) {
            if (exceptions == undefined || exceptions == null || exceptions.length <= 0)
                return "";
            var newQS = "";
            var strExceptions = "," + exceptions.toString().toLowerCase() + ",";
            for (var currentKey in this.all()) {
                if (strExceptions.indexOf("," + currentKey.toLowerCase() + ",", 0) < 0) {
                    if (newQS.length > 0)
                        newQS += "&";
                    newQS += currentKey + "=" + this.all()[currentKey];
                }
            }
            if (newQS.length > 0)
                newQS = "?" + newQS;
            return newQS;
        },
        addParameter: function(url, name, val) {
            if (url == undefined || url == null || url.length <= 0) {
                return "?" + name + "=" + val;
            }
            else if (url.indexOf("?") < 0) {
                return url + "?" + name + "=" + val;
            }
            else {
                return url + "&" + name + "=" + val;
            }
        }
    };
} ();

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) { }
    
    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) { }
		        
				jQuery.initBehavior(this, type, metaData);
			}
		}
		catch (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;
};

jQuery.bookmark = function (bookmarkUrl, bookmarkTitle) {
    if (window.sidebar) { // FF
        window.sidebar.addPanel(bookmarkTitle, bookmarkUrl, "");
        return;
    }
    else if (document.all) { // IE
        try {
            window.external.AddFavorite(bookmarkUrl, bookmarkTitle);
        }
        catch(Error) {
            alert('Please press CTRL-D to bookmark this page');
        }
        return;
    }
    else if (window.opera && window.print) {
        alert('Please press CTRL-D to bookmark this page');
        return;
    }
    else {
        var chr = 'CTRL-D';
        var agt = navigator.userAgent.toLowerCase();
        if (agt.indexOf("opera") != -1) chr = 'CTRL-T';
        alert('Please press ' + chr + ' to bookmark this page');
        return;
    }
};
