/**
 * Global
 *
 * @author Tobias Schibler <tobias@tujo.no>
 * @package JavaScript
 * @copyright 2011 tujo ANS
 */


/**
 * we need this, so normal users not get an error
 * console.log(/\.sf$/.test(location.host));
 *
 * @param {Object} s
 */
window.fire = window.warn = window.error = function(s){
//	if(/\.sf$/.test(location.host))
	typeof console !== 'undefined' && console.log(s);
};


/**
 * check if the obj is empty
 *
 * @param {Object} obj : the object, array, string, number (int) to check
 * @return {Boolean} false OR true
 *
 */
window.empty = function(e){
	switch(typeof e){
		case 'undefined':
			return true;
			break;
		case 'string':
			if(jQuery.trim(e) == '')
				return true;
			break;
		case 'object':
			if(e === null)
				return true;/* typeof null is an object */
			if(e.length === 0)
				return true;/* jquery element */
			break;
		case 'number':
			if(isNaN(e))
				return true;/* it the number is NaN (isFinite) */
			break;
		default:
			return false;
			break;
	}
	return false;
};


window.__ = window._ = function(str, args){
	if(typeof args === 'object'){
		for(var i in args){
			str = String(str).replace(new RegExp(i, 'ig'), args[i])
		}
	}
	return str;
};


/** some testings */
(function($){

	/** Test-Functions */
	$.isNumeric = function(n){
		return /^[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?$/.test(n);
	};

	$.isEmail = function(m){
		return /^([\w\-_\.])+@([\w\-\.])+\.([\w]){2,4}$/.test(m);
	//	return /\\w+([-+.\’]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*/.test(m);
	};

	$.apply = function(fn, self, args){
		return fn.apply(self, Array.prototype.slice.call(args||[]).concat(Array.prototype.slice.call(arguments)));
	};

	/**
	 * agent screen dimension or the scrolled position
	 *
	 * @return {Number} returns the screen width/height or hte scrollet position - $.screen().width, $.screen().height, $.screen().x, $.screen().y
	 */
	$.screen = function(){
		return {
			w: self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
			h: self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight,
			width: document.body.scrollWidth>document.body.offsetWidth? document.body.scrollWidth: document.body.offsetWidth,
			height: document.body.scrollHeight>document.body.offsetHeight? document.body.scrollHeight: document.body.offsetHeight,
//			x: self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
//			y: self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
			x: self.pageXOffset || ((document.documentElement && document.documentElement.scrollLeft)? document.documentElement.scrollLeft: null) || (document.body? document.body.scrollLeft: null),
			y: self.pageYOffset || ((document.documentElement && document.documentElement.scrollTop)? document.documentElement.scrollTop: null) || (document.body? document.body.scrollTop: null),
			centerX: function(e, c){
				return (+(this.w/2)+this.x-(e?($.isNumeric(e)?e:((parseFloat($(e).css('width'))||$(e).width())))/2:0)-((c?($.isNumeric(c)?c:$(c).position().left):null)||0));
			},
			centerY: function(e, c){
				return (+(this.h/2)+this.y-(e?($.isNumeric(e)?e:((parseFloat($(e).css('height'))||$(e).height())))/2:0)-((c?($.isNumeric(c)?c:$(c).position().top):null)||0));
			}
		};
	};


	$(document).ready( function(){
		$('a[href="javascript:;"]').removeAttr('href');

		($('a[href^="http"]').not('[href*="'+window.location.host+'"]').not('[onclick]').click( function(){
			var href = Snowflake.sendformUrl+'relocate?l='+encodeURIComponent($(this).attr('href'));
			var target = $(this).prop('target');
			if(target === '_blank')
				window.open(href, target);
			else
				window.location = href;
			if(href && href.length>0)
				return false;
			return false;
		}));
	});

	$.fn.block = function(){
		this.each(function(){
			try {
				this.onselectstart = function(){
					return false;
				};
				this.onmousedown = function(e){
					if(e && typeof e.preventDefault !== 'undefined')
						e.preventDefault();
				};
			} catch(e){
				warn(e);
			}
		});
		return this;
	};

})(jQuery);



