Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

function isEmpty(o){ // Object.prototype.isEmpty - does not work!
	switch (typeof o){
		case 'object':
			for (var i in o) return false;
			return true;
		case 'function':
			return false;
		case 'number':
			return o == 0;
		case 'string':
			return o == '';
		case 'boolean':
			return o == false;
	}
	return true;
}

// Checks if the given value exists in the array  
function in_array (needle, haystack, argStrict) {
    var key = '', strict = !! argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
    return false;
}

var application = {
	'i18n'		: {},
	currentDate : '',
	currentTime	: 0,
	get			: function(name){
		if (!name) return 'UNDEFINED TRANSLATE PATTERN';
		return (typeof application.i18n[name] == 'undefined') ? ('T* '+name) : application.i18n[name];
	},
	set			: function(obj){
		application.i18n = $.extend(application.i18n, obj);
	}
};

function _T(name, repl){
	var str = application.get(name);
	if (typeof repl == 'object')
		for (var k in repl)
			str = str.replace(new RegExp(k, "g"), repl[k]);
	return str;
}

function doDialogBox(msg, title, opt){
	var text = '';
	if (title) text += '<b>'+title+':</b><br />';
	text += '<ul>';
	if (typeof msg=='string'){
		var arr = msg.split("\n");
		for (var i=0; i<arr.length; i++) text += '<li>'+arr[i]+'</li>';
	} else if (typeof msg=='object'){
		for (var i=0; i<msg.length; i++) text += '<li>'+msg[i]+'</li>';
	}
	text += '</ul>';
	(function($){
		if ($.siteDefaults && $.siteDefaults.dialogBox)
			opt = $.extend($.siteDefaults.dialogBox, opt);
		if ($.fn.jDialogBox)
			$('body').jDialogBox($.extend(opt, {'text' : text}));
		else if ($.fn.dialogBox)
			$('body').dialogBox($.extend({'text':text}, opt));
	})(jQuery);
}

function showMessage(msg, title, type, o){
	if (!type) type = 'msg';	// Possible types: err, warn, msg
	if (!o)  o = {};
	o.onSubmit = null;
	doDialogBox(msg, title, $.extend({"dialogType": type, "buttons": "ok"}, o || {}));
}

function showError(msg, title){
	var o = {};
	o.onSubmit = null;
	if ($.siteDefaults && $.siteDefaults.dialogError) o = $.siteDefaults.dialogError;
	showMessage(msg, title, 'err', o);
}

function askQuestion(msg, fn){
	if (typeof fn == 'function'){
		var opt = {
			'buttons'	: 'ok cancel',
			'btnClose'	: false,
			'btnCancel'	: true,
			'txtCancel'	: _T('Cancel'),
			'closeOnClick' : true,
			'btnOk'		: true,
			'txtOk'		: _T('Yes'),
			'dialogType' : 'warn',
			'onSubmit'	: function(result){
				fn.call(this, result);
			}
		};
		if (jQuery.siteDefaults && jQuery.siteDefaults.askQuestion)
			opt = $.extend(opt, jQuery.siteDefaults.askQuestion);
		doDialogBox(msg, '', opt);
	} else {
		var res = window.confirm(msg);
		if (typeof fn == 'function'){
			fn.call(this, res);
		} else return res;
	}
}

function redirect(url){
	if (!url) url = location.href;
	location.href = url;
}

function getWindowSize(key) {
	if (!key) key = '';
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	if (key=='width' || key=='pageWidth'){
		return pageWidth;
	} else if (key=='height' || key=='pageHeight'){
		return pageHeight;
	} else if (key=='windowWidth'){
		return windowWidth;
	} else if (key=='windowHeight'){
		return windowHeight;
	}
	return new Array(pageWidth,pageHeight,windowWidth,windowHeight);
}

function getWindowScroll(key) {
	if (!key) key = '';
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	if (key=='top' || key=='y'){
		return yScroll;
	} else if (key=='left' || key=='x'){
		return xScroll;
	}
	return new Array(xScroll,yScroll);
}

/**
 * options can be -margin, -padding, -border, so do not calculate padding, margin, border
 */
function getHeight(el){
	var $el = jQuery(el), res = $el.height(),
		arr = ['margin-top', 'margin-bottom', 'padding-top', 'padding-bottom', 'border-top-width', 'border-bottom-width'],
		ign = {'auto' : 0, 'thin' : 1, 'thick' : 2, 'medium' : 0, 'inherit' : 0},
		min = [];
	if (arguments.length > 1){
		for (i = 1; i < arguments.length; i++){
			if (arguments[i].substr(0,1) == '-') min.push(arguments[i].substr(1));
		}
	}
	for (var i = 0; i < arr.length; i++){
		var val = $el.css( arr[i] ), skip = false;
		for (j = 0; j < min.length; j++){
			if (val.substr(0, min[j].length) == min[j]) skip = true;
		}
		if (skip) continue;
		if (typeof ign[val] != 'undefined')
			res += ign[val];
		else
			res += parseInt(val);
	}
	return res;
}

function getWidth(el){
	var $el = jQuery(el), res = $el.width(),
		arr = ['margin-left', 'margin-right', 'padding-left', 'padding-right', 'border-left-width', 'border-right-width'],
		ign = {'auto' : 0, 'thin' : 1, 'thick' : 2, 'medium' : 0, 'inherit' : 0};
	for (var i = 0; i < arr.length; i++){
		var val = $el.css( arr[i] );
		if (typeof ign[val] != 'undefined')
			res += ign[val];
		else
			res += parseInt(val);
	}
	return res;
}

if (jQuery){
	jQuery.siteDefaults = {};
}

