/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html

 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @example $.cookie('the_cookie', 'the_value');
 * @example $.cookie('the_cookie', null);

 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


function $A(iterable) {
  if (!iterable) return [];
  if (iterable.toArray) return iterable.toArray();
  var length = iterable.length, results = new Array(length);
  while (length--) results[length] = iterable[length];
  return results;
};


jQuery.extend(Function.prototype, {

  bind: function() {
    if (arguments.length < 2 && (typeof arguments[0] == "undefined")) return this;
    var __method = this, args = $A(arguments), object = args.shift();
    return function() {
	  var dup_args = [];
	  for (var i = 0; i < args.length; i++) {
	  	dup_args[dup_args.length] = args[i];
	  }
	  for (var i = 0; i < arguments.length; i++) {
	  	dup_args[dup_args.length] = arguments[i];
	  }
      return __method.apply(object, dup_args);
    }
  }

});

jQuery.toQueryString = function(params) {
	var qryStr = '';
	for ( var key in params )
		qryStr += key + '=' + params[key] + '&';
	return qryStr;
};

jQuery.delegate = function (specs, callback) {

	return function (e) {
		
		callback = callback || {};
		var found = callback.found || function(){};
		var notfound = callback.notfound || function(){};
		var isParentBound = callback.isParentBound || function(t){ return (t.get(0) == null) || (t.get(0) == document.body) };
		var target = $(e.target);
		
		while (target.get(0)) {
			for (var key in specs) {
				if (target.hasClass(key)) {
						specs[key](target);
						found(e, target);
						return;						
				}
			}
			if (isParentBound(target)) {
				target = $('#');
			}	else {
				target = target.parent();
			}
		}

		notfound(e);			
		
	}

};

jQuery.delegatePreventDefault = { found : function(e) {
		e.stopPropagation();
		e.preventDefault();
}};

jQuery.valInClass = function (el, re) {
	return ($(el).get(0).className+" ").match(re)[1];	
};

jQuery.humanSize = function (s) {
	if (s > 1024 * 1024) {
		return (1.0 * Math.ceil(10.0 * s / 1024 / 1024) / 10) + " MB"
	}
	if (s > 1024) {
		return Math.ceil(1.0 * s / 1024) + " KB"								
	}
};

jQuery.intID = function (el) {
	return parseInt($(el).attr('id').replace(/[^0-9]+/,''));
};

jQuery.ieNoCache = function () {
	try {document.execCommand("BackgroundImageCache", false, true);}catch(e){}
};

jQuery.preventInitAction = function () {
	var h = {};
	var len = arguments.length;
	var f = function(){};
	for (var i = 0; i < len; i++) {
		h[arguments[i]] = f;
	}
	return $.delegate(h, $.delegatePreventDefault)	
}
