/**
 * @param {Object} context the 'this' value to be used.
 * @param {arguments} [1..n] optional arguments that are
 * prepended to returned function's call.
 * @return {Function} a function that applies the original
 * function with 'context' as the thisArg.
 */
Function.prototype.delegate = function(context){
  var fn = this,
      ap, concat, args,
      isPartial = arguments.length > 1;
  // Strategy 1: just bind, not a partialApply
  if(!isPartial) {
    return function() {
        if(arguments.length !== 0) {
          return fn.apply(context, arguments);
        } else {
          return fn.call(context); // faster in Firefox.
        }
      };
    } else {
    // Strategy 2: partialApply
    ap = Array.prototype,
    args = ap.slice.call(arguments, 1);
    concat = ap.concat;
    return function() {
      return fn.apply(context,
        arguments.length === 0 ? args :
        concat.apply(args, arguments));
    };
  }
};

(function() {
	if (typeof console == 'undefined') {
		window['console'] = {};
	}
	if (typeof console.debug == 'undefined') {
		window['console'].debug = function() {
			if (typeof console.log == 'undefined') {
			}
			else console.log(arguments);
		};
	}
	window.fieldsetInitState = {};
}());

(function($) {
	$.extend({
	    keys:    function(obj){
	        var a = [];
	        $.each(obj, function(k){ a.push(k) });
	        return a;
	    }
	});
	$.getBaseUri = function(){
		var b = $("base").attr("href");
		if(b) return $("base").attr("href");
		else return "";
	};
	$.fn.setClassAttributs = function(attributeName) {
		return this.each(function() {
			var $this = $(this);
			var splitting = this.className.split(" ");
			for(var i= 0; i<splitting.length; i++) {
				if(splitting[i].indexOf(attributeName) != -1){
					var value = splitting[i].substring(splitting[i].indexOf("(")+1, splitting[i].indexOf(")"));
					if(value != "") {
						$this.attr(attributeName, value);
					}
					$(this).removeClass(splitting[i]);
				}
			}
		});
	};
	$.fn.inpPrefilled = function() {
		return this.each(function() {
			$(this).setClassAttributs('dvalue');
			$(this).setClassAttributs('showlabel');
			if($(this).attr('showlabel') != "true"){
				$("label[for='"+this.id+"']").addClass("inv");
			};
			var dv = $(this).attr('dvalue') || $("label[for='"+this.id+"']").text();
			if(this.value == ""){
				this.value = dv;
			}
			$(this).click(function(){
				if(this.value == dv){
					this.value = "";
					$(this).removeClass('preFilled');
				}
			});
			$(this).blur(function(){
				if(this.value == ""){
					this.value = dv;
					$(this).addClass('preFilled');
				}
			});

		});
	};
})(jQuery);


