var Base={
	spinner:'<span style="background:url(./images/loading.gif) no-repeat 0 50%;font-style:italic;font-size:12px;text-align:right;font-weight:bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Wait&#8230;</span>',
	onready:[],
	advanced:null,
	actionUrl:null,
	onReady:function(){
		for(var i=0;i<Base.onready.length;i++){
			var func=Base.onready[i];
			if(typeof(func)=='function'){
				func();
			}
		}
	},
	focusField:function(type,which) {
	   if(type) {
		  if(which.value == which.defaultValue) {
			 which.value = '';
		  } else {
			 which.select();
		  }
	   } else {
		  if(which.value == '') {
			 which.value = which.defaultValue;
		  }
	   }
	},
	emailWrong:function(email){
		if(typeof(email)!="string" || email.search(new RegExp("^[_a-zA-Z0-9-]+(\\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*(\\.[a-zA-Z]{2,3})$"))==-1){
			return true;
		}
		return false;
	},
	isSuccess:function(response){
		if(response.success==1){return true;}
	},
	loadRight:function(action,postAction){
		if(action)
			Base.postJSON({action:action},
				function(response){
					jQuery("#page1").html(response.html);
					if(postAction && typeof(postAction)=="function")
						postAction(response);
				}
			);
	},
	formatCalendar:function(){
		jQuery('.cal').datepicker({showOn:'button',buttonImageOnly:true,dateFormat:'dd.mm.yy',buttonImage:'images/b_calendar.png'});
	},
	formatTable:function(){
		jQuery(".tablesorter").tablesorter();
	},
	isExpired:function(response){
		if(response.expired!=undefined) {
			document.location.href='./index.php?'+Math.random();
			return true;
		}
		return false ;
	},
	onEnter:function(form,e,func){
		var keynum;
		if(window.event){
		  keynum = e.keyCode;
		}else if(e.which){
		  keynum = e.which;
		}
		if(keynum==13){
			if(typeof(func)=="function"){
				func(form);
			}
			return false;
		}else{
			return true;
		}
	},
	onEnterNoSpace:function(form,e,func){
		var keynum;
		if(window.event){
		  keynum = e.keyCode;
		}else if(e.which){
		  keynum = e.which;
		}
		if(keynum==13){
			if(typeof(func)=="function"){
				func(form);
			}
			return false;
		}else if(keynum==32){
			return false;
		}else{
			return true;
		}
	},
	postJSON:function(data,func){
		jQuery.post(this.actionUrl,data,
			function(response){
				if(!Base.isExpired(response)){
					if(typeof(func)=='object' && typeof(func.preOp)=='function'){
						func.preOp(response);
					}
					for (var propertyName in response) {
						if(new RegExp(","+propertyName+",").test(",html,message,expired,success,postAction,")!=true)
							jQuery("#"+propertyName).html(response[propertyName]);
					}
					if(response.postAction){
						eval("var postAction="+response.postAction+";");
						if(typeof(postAction)=='function')
							postAction(response);
					}
					if(typeof(func)=='function')
						func(response);
					if(typeof(func)=='object' && typeof(func.postOp)=='function')
						func.postOp(response);
				}
			},"json");
	},
	postForm:function(form){
		if(form.message)
			jQuery("#"+form.message.value).empty();
		if(form.target)
			jQuery("#"+form.target.value).html(this.spinner); // where to write http query result
		Base.postJSON(jQuery(form).serializeArray(),
			function(response){ // form.action.value - hidden should contains action string id for current post
				if(response.html && form.target)  // where to write http query result
					jQuery("#"+form.target.value).html(response.html);
				else if(form.target)
					jQuery("#"+form.target.value).empty();
				if(response.message)jQuery("#"+form.message.value).html(response.message); // where to write http query message
				if(form.postaction){ // if hidden variable exists - it containts js to run after http reesponse
					var func=eval(form.postaction.value);
					if(typeof(func)=='function'){
						func(response,form);
					}
				}
			});
	},
	runFunc:function(func,data){
		if(typeof(func)=="function"){
			func(data);
		}
	},
	parentPos:function(elem){
		var parent=jQuery(elem).offsetParent();
		return (/^body|html$/i.test(parent[0].tagName)) ? { top: 0, left: 0 } : parent.offset();
	},
	getPossition:function(elem){ // TODO: apply this
		var elPos    =jQuery(elem).position(); // jQuery(elem).offsetParent().get(0).tagName
		var parPos=Base.parentPos(elem);
		var elTopMargin=parseInt(jQuery(elem).css('margin-top').replace(/px/,""));
		var elTopPadding=parseInt(jQuery(elem).css('padding-top').replace(/px/,""));
		elPos.left+=parPos.left;
		elPos.top +=parPos.top+elTopMargin+elTopPadding;
		return elPos;
	},
	jQueryUi:function(func){
		if(jQuery.datepicker==undefined){
			var head = document.getElementsByTagName('head');
			if(head.length==0)
				$(document.createElement('head')).appendTo(document);
			head = document.getElementsByTagName('head')[0];
			jQuery(document.createElement('link'))
			    .attr({type: 'text/css', href: "css/jquery-ui-1.7.1.custom.css", rel: 'stylesheet', media:'screen'})
			    .appendTo(head); 
			jQuery.getScript("js/jquery-ui-1.7.1.custom.min.js",function(){
				jQuery(document).ready(function() {
					if(typeof(func)=="function")
						func();
					});
				});
		}else if(typeof(func)=="function")
			func();
	}
};
