/*
 * Copyright (c) 2010 RAYNET s.r.o., All rights reserved.
 * RAYNET s.r.o. PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * www.raynet.cz
 */

/**
 * @namespace SP
 * @author Miroslav Raska
 * @version 20100903
 * @require jQuery
 */
SP = {};
SP.apply = function(target, source, /* private */ force) {
	if (typeof force !== "boolean") force = true;

	// if namespace given instead of object
	if (typeof target == "string") {
		target = SP.ns(target);
	}
	
	// create target if not given
	if (typeof target !== "object") {
		target = {};
	}

	// apply
	if (typeof source === "object") {
		for (var prop in source) {
			if (force === true || (!force && typeof target[prop] === "undefined")) {
				target[prop] = source[prop];
			}
		}
	}
	
	return target;
};

SP.apply(SP, {
	/* browser detection */
	isIE: jQuery.browser.msie,
	browserVersion: parseFloat(jQuery.browser.version),

	/* basic */
	applyIf: function(s,t) {
		return SP.apply(s, t, false);
	},
	each: function(arr, fn){
		var length = arr.length;
    for (var idx = 0; idx < length; idx++) {
    	fn.call(arr[idx], arr[idx], idx);
    }
	},
	ns: function(){
		var ret = undefined;
		SP.each(arguments, function(arg){
			var obj = window;
			SP.each(arg.split('.'), function(name){
				obj = obj[name] = obj[name] || {};
			});
			// return first object
			if (SP.isUndefined(ret)) ret = obj;
		});
		return ret;
	},
	
	/* general helpers */
	isString: function(o) {
		return typeof o === "string";
	},
	isBoolean: function(o) {
		return typeof o === "boolean";
	},
	isFunction: function(o) {
		return typeof o === "function";
	},
	isUndefined: function(o) {
		return typeof o === "undefined";
	},
	isNull: function(o) {
		return o === null;
	},
	isNaN: function(o) {
		return isNaN(o);
	},
	isArray: function(o) {
		return Object.prototype.toString.apply(o) === '[object Array]';
	},
	isObject: function(o) {
		return typeof o === "object" && !this.isArray(o);
	},
	isPureObject: function(o) {
		return Object.prototype.toString.apply(o) === '[object Object]';
	},
	isEmpty: function(o, includeZeroString) {
		return this.isUndefined(o) || this.isNull(o)
						|| (includeZeroString !== false && this.isString(o) && o.length == 0);
	}
});
SP.isBlank = SP.isEmpty;

/* components helpers */
SP.apply(SP, {
	genId: (function(){
		var counter = 1;
		return function(){
			var id = "sp-comp-"+counter;
			counter++;
			return id;
		}
	})(),
	
	// adds id to all elements matching selector
	// optionally calls callback (arguments: id, $element)
	genIds: function(selector, cb){
		$(selector).each(function(){
			var id = SP.genId();
			var el = $(this);
			el.attr('id', id);
			if (SP.isFunction(cb)) {
				cb(id, el);
			}
		});
	}
});
// extend jQuery
SP.apply(jQuery.fn, {
	getSelector: function(){
		if (SP.isEmpty(this.attr('id'))) {
			this.attr('id', SP.genId());
		}
		return "#"+this.attr('id');
	}
});

/**
 * @namespace SP.events
 * @author Miroslav Raska
 * @version 20100903
 * @require jQuery
 */
SP.apply("SP.events", {
	isTarget: function(e, element) {
	  var target = $(e.target);
		return (target.get(0) == element.get(0) || target.parents(element.getSelector()).size() > 0);
	},
	getParent: function(e, selector) {
	  var target = $(e.target);
		return target.parents().andSelf().filter(selector);
	},
	isLeft: function(e) {
		return ((!SP.isIE && e.button === 0) ||
					 (SP.isIE && e.button === 1));
	}
});

/* Prototype overrides */
SP.apply(String.prototype, {
	trim: function() {
		return this.replace(/^\s+|\s+$/g,"");
	}
});
SP.apply(Array.prototype, {
	indexOf: function(o){
    for (i = 0; i < this.length; i++){
      if (this[i] === o){
        return i;
      }
    }
    return -1;
  }
});
