var $j = jQuery.noConflict();

$j.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;
};

$j.msJsonDateConvert = function(dt, format) {
    var date = null;
    return new Date(parseFloat(dt.slice(6, 19))).toDateString();
}

$j.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=/";
};

$j.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('&') > -1 || val.indexOf('=') > -1) {
                return jQuery.parseKeyValueString(val);
            }
            else return val;
        }
    }
    return null;
};

$j.eraseCookie = function(name) {
    $j.createCookie(name, "", -1);
};

$j.pauseCode = function(msec) {
    var date = new Date();
    var curDate = null;
    do { curDate = new Date(); }
    while (curDate - date < msec);
}

$j.parseKeyValueString = function(val) {
    var hash = [];
    var pieces = val.split('&');

    for (var i = 0; i < pieces.length; i++) {
        var pair = pieces[i].split('=');
        var val = pieces[i].replace(pair[0] + '=', '');
        hash[pair[0]] = val;
    }

    return hash;
};

$j.serializeKeyValues = function(h) {
    if (h != null) {
        var pairs = [];

        for (var key in h) {
            //this is needed since msn header script adds methods to base array class and show up as items in our hash - disregard
            if (key != 'last' && key != 'remove' && key != 'contains' && key != 'push' && key != 'shift' && key != 'splice')
            { pairs.push(key + '=' + h[key]); }
        }

        return pairs.join('&');
    }

    return null;
};

$j.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;
        }
    };
} ();

$j.preloadImages = function() {
    for (var i = 0; i < arguments.length; i++) {
        jQuery("<img>").attr("src", arguments[i]);
    }
}

$j.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;
};

$j.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;
};

$j.fn.clearOptions = function() {
    this.each(
		function() {
		    if (this.nodeName.toLowerCase() == 'select') {
		        this.options.length = 0;
		    }
		}
	);
};

$j.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;
};

$j.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;
};

$j.fn.DefaultValue = function(text) {
    return this.each(function() {
        if (this.type != 'text' && this.type != 'password' && this.type != 'textarea') return;
        var fld_current = this;

        if (this.value == '') { this.value = text; }
        else { return; }

        $j(this).focus(function() {
            if (this.value == text || this.value == '')
                this.value = '';
        });

        $j(this).blur(function() {
            if (this.value == text || this.value == '')
                this.value = text;
        });

        $j(this).parents("form").each(function() {
            $j(this).submit(function() {
                if (fld_current.value == text) {
                    fld_current.value = '';
                }
            });
        });
    });
};

$j.fn.maxLength = function(max) {
    this.each(function() {
        var type = this.tagName.toLowerCase();
        var inputType = this.type ? this.type.toLowerCase() : null;

        if (type == "input" && inputType == "text" || inputType == "password") {
            this.maxLength = max;
        }
        else if (type == "textarea") {
            this.onkeypress = function(e) {
                var ob = e || event;
                var keyCode = ob.keyCode;
                var hasSelection = document.selection ? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd;
                return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection);
            };
            this.onkeyup = function() {
                if (this.value.length > max) {
                    this.value = this.value.substring(0, max);
                }
            };
        }
    });
};

$j.fn.iFrameResize = function() {
    this.each(function() {
        if ($j.browser.safari || $j.browser.opera) {
            var oIframe = document.getElementById('ifAds');
            $j(this).load(function() {
                $j(this).css('height', this.contentDocument.body.offsetHeight + 'px');
            });

            for (var i = 0, j = $j(this).length; i < j; i++) {
                var iSource = this.src;
                this.src = '';
                this.src = iSource;
            }
        } else {
            $j(this).load(function() {
                $j(this).css('height', this.contentWindow.document.body.offsetHeight + 'px');
            });
        }
        
    });
};

/*$('div').delay(1000).animate({height:200}).delay(1000).animate({height:400}).delay(500).fadeOut().delay(500).fadeIn();*/
$j.fn.delay = function(time, callback) {
    // Empty function:
    jQuery.fx.step.delay = function() { };
    // Return meaningless animation, (will be added to queue)
    return this.animate({ delay: 1 }, time, callback);
};

/**
* jQuery.query - Query String Modification and Creation for jQuery
* Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
* Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
* Date: 2009/02/08
*
* @author Blair Mitchelmore
* @version 2.1.2
*
**/
new function(settings) {
    // Various Settings
    var $separator = settings.separator || '&';
    var $spaces = settings.spaces === false ? false : true;
    var $suffix = settings.suffix === false ? '' : '[]';
    var $prefix = settings.prefix === false ? false : true;
    var $hash = $prefix ? settings.hash === true ? "#" : "?" : "";
    var $numbers = settings.numbers === false ? false : true;

    jQuery.query = new function() {
        var is = function(o, t) {
            return o != undefined && o !== null && (!!t ? o.constructor == t : true);
        };
        var parse = function(path) {
            var m, rx = /\[([^[]*)\]/g, match = /^(\S+?)(\[\S*\])?$/.exec(path), base = match[1], tokens = [];
            while (m = rx.exec(match[2])) tokens.push(m[1]);
            return [base, tokens];
        };
        var set = function(target, tokens, value) {
            var o, token = tokens.shift();
            if (typeof target != 'object') target = null;
            if (token === "") {
                if (!target) target = [];
                if (is(target, Array)) {
                    target.push(tokens.length == 0 ? value : set(null, tokens.slice(0), value));
                } else if (is(target, Object)) {
                    var i = 0;
                    while (target[i++] != null);
                    target[--i] = tokens.length == 0 ? value : set(target[i], tokens.slice(0), value);
                } else {
                    target = [];
                    target.push(tokens.length == 0 ? value : set(null, tokens.slice(0), value));
                }
            } else if (token && token.match(/^\s*[0-9]+\s*$/)) {
                var index = parseInt(token, 10);
                if (!target) target = [];
                target[index] = tokens.length == 0 ? value : set(target[index], tokens.slice(0), value);
            } else if (token) {
                var index = token.replace(/^\s*|\s*$/g, "");
                if (!target) target = {};
                if (is(target, Array)) {
                    var temp = {};
                    for (var i = 0; i < target.length; ++i) {
                        temp[i] = target[i];
                    }
                    target = temp;
                }
                target[index] = tokens.length == 0 ? value : set(target[index], tokens.slice(0), value);
            } else {
                return value;
            }
            return target;
        };

        var queryObject = function(a) {
            var self = this;
            self.keys = {};

            if (a.queryObject) {
                jQuery.each(a.get(), function(key, val) {
                    self.SET(key, val);
                });
            } else {
                jQuery.each(arguments, function() {
                    var q = "" + this;
                    q = q.replace(/^[?#]/, ''); // remove any leading ? || #
                    q = q.replace(/[;&]$/, ''); // remove any trailing & || ;
                    if ($spaces) q = q.replace(/[+]/g, ' '); // replace +'s with spaces

                    jQuery.each(q.split(/[&;]/), function() {
                        var key = this.split('=')[0];
                        var val = this.split('=')[1];

                        if (!key) return;

                        if ($numbers) {
                            if (/^[+-]?[0-9]+\.[0-9]*$/.test(val)) // simple float regex
                                val = parseFloat(val);
                            else if (/^[+-]?[0-9]+$/.test(val)) // simple int regex
                                val = parseInt(val, 10);
                        }

                        val = (!val && val !== 0) ? true : val;

                        if (val !== false && val !== true && typeof val != 'number')
                            val = decodeURIComponent(val);

                        self.SET(key, val);
                    });
                });
            }
            return self;
        };

        queryObject.prototype = {
            queryObject: true,
            has: function(key, type) {
                var value = this.get(key);
                return is(value, type);
            },
            GET: function(key) {
                if (!is(key)) return this.keys;
                var parsed = parse(key), base = parsed[0], tokens = parsed[1];
                var target = this.keys[base];
                while (target != null && tokens.length != 0) {
                    target = target[tokens.shift()];
                }
                return typeof target == 'number' ? target : target || "";
            },
            get: function(key) {
                var target = this.GET(key);
                if (is(target, Object))
                    return jQuery.extend(true, {}, target);
                else if (is(target, Array))
                    return target.slice(0);
                return target;
            },
            SET: function(key, val) {
                var value = !is(val) ? null : val;
                var parsed = parse(key), base = parsed[0], tokens = parsed[1];
                var target = this.keys[base];
                this.keys[base] = set(target, tokens.slice(0), value);
                return this;
            },
            set: function(key, val) {
                return this.copy().SET(key, val);
            },
            REMOVE: function(key) {
                return this.SET(key, null).COMPACT();
            },
            remove: function(key) {
                return this.copy().REMOVE(key);
            },
            EMPTY: function() {
                var self = this;
                jQuery.each(self.keys, function(key, value) {
                    delete self.keys[key];
                });
                return self;
            },
            load: function(url) {
                var hash = url.replace(/^.*?[#](.+?)(?:\?.+)?$/, "$1");
                var search = url.replace(/^.*?[?](.+?)(?:#.+)?$/, "$1");
                return new queryObject(url.length == search.length ? '' : search, url.length == hash.length ? '' : hash);
            },
            empty: function() {
                return this.copy().EMPTY();
            },
            copy: function() {
                return new queryObject(this);
            },
            COMPACT: function() {
                function build(orig) {
                    var obj = typeof orig == "object" ? is(orig, Array) ? [] : {} : orig;
                    if (typeof orig == 'object') {
                        function add(o, key, value) {
                            if (is(o, Array))
                                o.push(value);
                            else
                                o[key] = value;
                        }
                        jQuery.each(orig, function(key, value) {
                            if (!is(value)) return true;
                            add(obj, key, build(value));
                        });
                    }
                    return obj;
                }
                this.keys = build(this.keys);
                return this;
            },
            compact: function() {
                return this.copy().COMPACT();
            },
            toString: function() {
                var i = 0, queryString = [], chunks = [], self = this;
                var addFields = function(arr, key, value) {
                    if (!is(value) || value === false) return;
                    var o = [key];
                    if (value !== true) {
                        o.push("=");
                        o.push(encodeURIComponent(value));
                    }
                    arr.push(o.join(""));
                };
                var build = function(obj, base) {
                    var newKey = function(key) {
                        return !base || base == "" ? [key].join("") : [base, "[", key, "]"].join("");
                    };
                    jQuery.each(obj, function(key, value) {
                        if (typeof value == 'object')
                            build(value, newKey(key));
                        else
                            addFields(chunks, newKey(key), value);
                    });
                };

                build(this.keys);

                if (chunks.length > 0) queryString.push($hash);
                queryString.push(chunks.join($separator));

                return queryString.join("");
            }
        };

        return new queryObject(location.search, location.hash);
    };
} (jQuery.query || {}); // Pass in jQuery.query as settings object

/* 
* SI Files (0.1)
* by Sagie Maoz (n0nick.net)
* n0nick@php.net
*
* A jQuery implementation of the SI Files JS lib by Shaun Inman
* See <http://www.shauninman.com/archive/2007/09/10/styling_file_inputs_with_css_and_the_dom>
*
* Copyright (c) 2009 Sagie Maoz <n0nick@php.net>
* Licensed under the GPL license, see http://www.gnu.org/licenses/gpl-3.0.html 
*
*
* NOTE: This script requires jQuery to work.  Download jQuery at www.jquery.com
*
*/

; (function($) {
    $.fn.si_files = function(options) {

        var settings = {
            pointer: true,
            width: 150,
            button: {
                src: 'file_choose.gif',
                width: 79,
                height: 22
            },
            label: true,
            label_class: 'file_label',
            cabinet_class: 'file_cabinet',
            debug: false
        };
        if (options) {
            $.extend(settings, options);
        }

        return this.each(function() {

            // file input CSS
            var opacity = settings.debug ? '0.5' : '0';
            $(this).css({
                'position': 'relative',
                'width': 'auto',
                'opacity': opacity,
                '-moz-opacity': opacity,
                'filter': 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')'
            });
            if (settings.pointer) {
                $(this).css('cursor', 'pointer');
            }

            // file input wrapper (cabinet)
            $(this).wrap('<div class="' + settings.cabinet_class + '"></div>');
            var cabinet = $(this).parent()[0];

            $(cabinet).css({
                'width': settings.width + 'px',
                'height': settings.button.height + 'px',
                'background': 'url("' + settings.button.src + '") 0 0 no-repeat',
                'display': 'block',
                'overflow': 'hidden'
            });
            if (settings.pointer) {
                $(cabinet).css('cursor', 'pointer');
            }
            cabinet.file_input = $(this);

            // calculate cabinet's dimensions
            cabinet.min_x = $(cabinet).offset()['left'];
            cabinet.min_y = $(cabinet).offset()['top'];
            cabinet.file_w = $(this).width();
            cabinet.file_h = $(this).height();

            $(cabinet).mousemove(function(e) {
                this.file_input.css('left', e.pageX - this.min_x - (this.file_w - 30));
                this.file_input.css('top', e.pageY - this.min_y - (this.file_h / 2)); //
            });

            if (settings.label) {
                $(cabinet).append('<span class="' + settings.label_class + '"></span>');
                this.label = $(cabinet).children('span.' + settings.label_class);
                this.label.css({
                    'display': 'block',
                    'line-height': settings.button.height + 'px',
                    'height': settings.button.height + 'px',
                    'width': settings.width - settings.button.width - 5 + 'px',
                    'vertical-align': 'middle',
                    'overflow': 'hidden',
                    'position': 'relative',
                    'top': -settings.button.height + 'px',
                    'left': settings.button.width + 'px'
                });

                $(this).change(function(e) {
                    this.label.text($(this).val());
                });
            }

        });
    };
})(jQuery);