(function($){
    Sizzle.selectors.order.push('NEQNAME');

    $.extend(Sizzle.selectors.match, {
        NAME: /(?:(?:\[name=['"]?)|(?:(?:^|[^!]+)@))((?:[\w\u00c0-\uFFFF_-]|\\.|:)+)(?:['"]*\])?/,
		PSEUDO: /(?:^|[^:]+):((?:[\w\u00c0-\uFFFF_-]|\\.)+)(?:\((['"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/,
		TAG: /^((?:[\w\u00c0-\uFFFF\*_-]|\\.)+)(?!.*@)/,
        NEQNAME: /^((?:[\w\u00c0-\uFFFF\*_-]|\\.)*)!@((?:[\w\u00c0-\uFFFF_-]|\\.|:)+)/
    });

    $.extend(Sizzle.selectors.find, {
        NEQNAME: function(match, context, isXML) {
			if ( typeof context.getElementsByTagName !== 'undefined' ) {
				var ret = [], results = context.getElementsByTagName(match[1] || '*');

				for ( var i = 0, l = results.length; i < l; i++ )
					if ( !results[i].getAttribute('name') || results[i].getAttribute("name") !== match[2] )
						ret.push( results[i] );

				return ret.length === 0 ? null : ret;
			}
        }
    });

    $.extend(Sizzle.selectors.filter, {
        NAME: function(elem, match) {
			return elem.getAttribute('name') == match[1];
        }
    });
})(jQuery);

(function(jQuery) {
    // We override the animation for all of these color styles
    jQuery.each(['background', 'backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function(i,attr){
        jQuery.fx.step[attr] = function(fx){
            if ( fx.state == 0 ) {//console.log(fx);
                fx.start = getColor( fx.elem, attr );
                fx.end = getRGB( fx.end );
            }

            fx.elem.style[attr] = "rgb(" + [
                Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
                Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
                Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
            ].join(",") + ")";
        }
    });

    // Parse strings looking for color tuples [255,255,255]
    function getRGB(color) {
        var result;

        // Check if we're already dealing with an array of colors
        if ( color && color.constructor == Array && color.length == 3 )
            return color;

        // Look for rgb(num,num,num)
        if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
            return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];

        // Look for rgb(num%,num%,num%)
        if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
            return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];

        // Look for #a0b1c2
        if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
            return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];

        // Look for #fff
        if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
            return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];

        // Look for rgba(0, 0, 0, 0) == transparent in Safari 3
        if (result = /rgba\(0, 0, 0, 0\)/.exec(color))
            return colors['transparent'];

        // Otherwise, we're most likely dealing with a named color
        return [0,0,0];
    }

    function getColor(elem, attr) {
        var color,
            attr = attr || "backgroundColor",
            elem = elem || $(this).get(0);

        do {
            color = jQuery.curCSS(elem, attr);

            // Keep going until we find an element that has color, or we hit the body
            if ( color != '' && color != 'transparent' || jQuery.nodeName(elem, "body") )
                break;

        } while ( elem = elem.parentNode );

        return getRGB(color);
    };

    $.calcRGB = function(op1, op2, mult) {
        var color = [];

        for ( var i = 0; i < 3; i++ )
            color[i] = Math.max(Math.min(op1[i] + (op2[i] * mult), 255), 0);

        return color;
    }

    $.getRGB = getRGB;
    $.getColor = getColor;
})(jQuery);

(function(jQuery) {
    var _innerHeight = jQuery.fn.innerHeight;
    var _innerWidth = jQuery.fn.innerWidth;
    var _settings = {
        padding: true,
        border: true
    };

    jQuery.fn.innerHeight = function(settings) {
        if (typeof settings == 'object')
            settings = $.extend({}, _settings, settings);
        else if (typeof settings == 'boolean')
            settings = {padding: settings};
        else settings = _settings;
        
        delta = (settings.padding) ? 0 : (parseInt(this.css('paddingTop')) || 0) + (parseInt(this.css('paddingBottom')) || 0);

        return _innerHeight.apply(this, arguments) - delta;
    };

    jQuery.fn.innerWidth = function(settings) {
        if (typeof settings == 'object')
            settings = $.extend({}, _settings, settings);
        else if (typeof settings == 'boolean')
            settings = {padding: settings};
        else settings = _settings;
        
        delta = (settings.padding) ? 0 : (parseInt(this.css('paddingLeft')) || 0) + (parseInt(this.css('paddingRight')) || 0);

        return _innerWidth.apply(this, arguments) - delta;
    };

    jQuery.fn.scrollHeight = function(settings) {
        if (typeof settings == 'object')
            settings = $.extend({}, _settings, settings);
        else if (typeof settings == 'boolean')
            settings = {border: settings};
        else settings = _settings;
        
        delta = (settings.border) ? (parseInt(this.css('border-top-width')) || 0) + (parseInt(this.css('border-bottom-width')) || 0) : 0;

        return this[0].scrollHeight + delta;
    };

    jQuery.fn.scrollWidth = function(settings) {
        if (typeof settings == 'object')
            settings = $.extend({}, _settings, settings);
        else if (typeof settings == 'boolean')
            settings = {border: settings};
        else settings = _settings;
        
        delta = (settings.border) ? (parseInt(this.css('border-left-width')) || 0) + (parseInt(this.css('border-right-width')) || 0) : 0;

        return this[0].scrollWidth + delta;
    };
})(jQuery);


// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
(function($){
  var cache = {};
 
  $.tmpl = function tmpl(str, data) {
    if ( !cache[str] && !/\W/.test(str) && !$('#'+str).length ) return '';

    // Figure out if we're getting a template, or if we need to
    // load the template - and be sure to cache the result.
    var fn = !/\W/.test(str) ?
      cache[str] = cache[str] ||
        $.tmpl($('#'+str).html()) :
     
      // Generate a reusable function that will serve as a template
      // generator (and which will be cached).
      new Function("obj",
        "var p=[],print=function(){p.push.apply(p,arguments);};" +
       
        // Introduce the data as local variables using with(){}
        "with(obj){p.push('" +
       
        // Convert the template into pure JavaScript
        str
          .replace(/&lt;%/g, '<%')
          .replace(/%&gt;/g, '%>')
          .replace(/[\r\t\n]/g, " ")
          .split("<%").join("\t")
          .replace(/((^|%>)[^\t]*)'/g, "$1\r")
          .replace(/\t=(.*?)%>/g, "',$1,'")
          .split("\t").join("');")
          .split("%>").join("p.push('")
          .split("\r").join("\\'")
      + "');}return p.join('');");
   
    // Provide some basic currying to the user
    return data ? fn( data ) : fn;
  };
})(jQuery);


(function($) {
    $.fn.defval = function(text,opts) {
        o = $.extend({
            _color: null,
            color: "#aaa",
            e:"focus",
            force : false,
            keep : true,
            plugin: 'defval'
        }, opts || {});

        var clearInput = function (e) {
            var target = $(e.target);
            var value = $.trim(target.val());
            var def_value = target.data('def_value');

            if (e.type == e.data.obj.e && value == e.data.obj.innerText) {
                $(target).css("color", "").val("");
                if (!e.data.obj.keep) {
                    $(target).unbind(e.data.obj.e+" blur",clearInput);
                }
            } else if (e.type == "blur" && value == "" && e.data.obj.keep) {
                $(this).css("color", e.data.obj.color).val(e.data.obj.innerText);
            }
        };

        return this.each(function () {
            var self = $(this);

            o.innerText = $.trim( (typeof text != "string") ? $(text).text() : (text || false) );
            o._color = self.css('color');

            if (o.force || !self.val().length)
                self.css({color: o.color}).val(o.innerText);

            if ( self.data('defval') == undefined ) {
                self.data('defval', {value: o.innerText, lamda: clearInput}).
                    bind(o.e+' blur', {obj:o}, clearInput);
            } else {
                $.each(self.data('events').focus, function(i, event) {
                    if ( event.data && event.data.obj && event.data.obj.plugin )
                        $.extend(event.data.obj, o);
                });
            }
        });
    };
})(jQuery);


(function($) {

    $.fn.diff = function(delta) {
        return $.grep(this, function(n) {
            return $.inArray(n, delta) == -1;
        });
    }

    $.fn.invert = function() {
        return this.pushStack(this.get().reverse(), arguments);
    };
})(jQuery);


(function($) {
    $.fn.toggle = function() {
        var args = $.makeArray(arguments);
        var evnt = typeof args[0] == 'string' ?
            args.shift() : 'click';

        return this.each(function() {
            var n = 0, m = args.length;

            $(this).bind(evnt, function(e, i) {
                n = ( i || n ) % m;
                return args[n++].call(this, e);
            });
       });
    };
})(jQuery);


(function($) {
    $.uid = function () {
        var rand = new Array();

        for (n=0; n < 8; n++)
            rand[n] = ((Math.random()*0x11111)|0).toString(16);

        return rand[0]+rand[1]+"-"+rand[2]+"-"+rand[3]+"-"+rand[4]+"-"+rand[5]+rand[6]+rand[7];
    };
})(jQuery);


(function($) {
    $.fn.diff = function(delta) {
        return $($.grep(this, function(n) {
            return $.inArray(n, delta) == -1;
        }));
    }

    $.fn.invert = function() {
        return this.pushStack(this.get().reverse(), arguments);
    };
})(jQuery);


var $$ = function(param) {
    var key = null;

    if ( !$(param).length ) {
        key = param.toString();
        param = document;
    }

    var node = $(param)[0];
    var id = $.data(node);

    $.cache[id] = $.cache[id] || {};
    $.cache[id].node = node;

    if ( key ) {
        $.cache[id][key] = $.cache[id][key] || {};
        return $.cache[id][key];
    }

    return $.cache[id];
};
