// Core
String.prototype.valid=function(type){
	var valid_regex={
		num_nat:/^([0-9]+)$/,
		username:/^[a-zA-Zа-яА-Я\s_\-,\.]{3,}$/,
		email:/^([a-z0-9_\-\.]+)@(([a-z0-9_\-]{2,})\.)+([a-z]{2,4})$/i
	};
	return typeof(regex=valid_regex[type])!="undefined" && regex.test(this);
}
String.prototype.modify_request=function(add,del,pr,frag){
	nofrag=this.replace(/\#.*$/,'');
	res=((apos=nofrag.indexOf('?'))==-1)?nofrag:nofrag.substring(0,apos);
	req=(apos==-1)?[]:(nofrag.substring(apos+1)).split('&');
	while(req.length && del.length && (cdel=(del.shift()+'='))) for(i in req) if(req[i].indexOf(cdel)==0) req.splice(i,1);
	if(add.length) for(i in add) req.push(add[i].k+'='+add[i].v);
	fin=(((typeof(pr)=='undefined'||!pr)?res:res.replace(new RegExp(pr.s,pr['f']?pr.f:''),pr.d))+(req.length?('?'+req.join('&')):''));
	if(typeof(frag)!='undefined' && frag) fin=fin.replace(/((#.*)|)$/i,'#'+frag);
	return fin;
}
// Jawoll
var smart_jawoll=function(ms,mod_add,mod_del,mod_uri,frag){
	window.setTimeout("core_redirect('"+document.location.href.modify_request(mod_add,mod_del,(typeof(mod_uri)=='undefined'||!mod_uri)?null:mod_uri,(typeof(frag)=='undefined'||!frag)?null:frag)+"');",ms);
	return;
}
var core_redirect=function(link_url){window.location.href=link_url;return;}
var rand4dig=function(){return Math.floor(Math.random()*8999+1000);}

// Input revolve
var check_input_focus=function(node,def)	{ if(node.value==def) node.value=''; return; }
var check_input_blur=function(node,def)	{ if(node.value=='') node.value=def; return; }

// Pop-up
var general_popup=function(href,wname,wini,width,height){
	if(wini=='auto') wini="status=0,toolbar=0,menubar=0,resizable=0,scrollbars=no";
	popupWindow=window.open(href,wname,((wini.length?(wini+','):'')+'width='+width+',height='+height));
	popupWindow.moveTo(($(window).width()-width)/2,($(window).height()-height)/2);
	return;
}

// Preloader
// ---------
var dc_preloader=function(){
	var o=this;
	o.list={};
	o.listIds={};
	o.listItems=0;
	o.dom={};
	o.arrows={};
	o.img_tmp=new Image();
	o.xCur=0;
	o.xDest=0;
	o.timer=null;
	o.cur=null;
	o.setup={
		scroll:{
			speed:40,
			steps:10,
			itemsVis:3,
			itemWidth:25,
			thresh:0.3
		},
		tmb:{
			active:{color:'#55f',cursor:'pointer',background:'none'},
			current:{color:'#f00',cursor:'default',background:'#f0ffe8'}
		},
		arrows:{
			active:{color:'#55f',cursor:'pointer'},
			current:{color:'#aac',cursor:'default'}
		}
	};
	// Init
	o.setDOMobj=function(oid,domId,isStyle,param){
		if(!(domObj=document.getElementById(domId))){alert('Не найден объект для "'+oid+'"');return;}
		o.dom[oid]={obj:domObj,iss:(arguments.length<3?false:isStyle),prm:(arguments.length<4?'innerHTML':param)};
	}
	o.setDOMpic=function(domId,isStyle,param){
		o.setDOMobj('pic',domId,isStyle,(arguments.length<3?'src':param));
	}
	o.setDOMarrow=function(type,domId){
		if(!(domObj=document.getElementById(domId))){alert('Не найден объект для стрелки');return;}
		o.arrows[type]=domObj;
	}
	// Push
	o.push=function(id,domId,params){
		if(!(domObj=document.getElementById(domId))){alert('Не найден объект для элемента');return;}
		o.list[id]={counter:o.listItems,obj:domObj,prm:params};
		o.listIds[o.listItems]=id;
		o.listItems++;
	}
	// Load
	o.load=function(id){
		if(!o.list[id] || o.cur==id)return;
		if(o.dom.pic.iss) o.dom.pic.obj.style.backgroundImage='';
		else o.dom.pic.src='';
		o.img_tmp.src=o.list[id].prm.pic;
		o.cur=id;
		o.xDest=Math.max((o.setup.scroll.itemsVis-o.listItems),Math.min(0,(o.setup.scroll.itemsVis/2-.5-o.list[o.cur].counter)))*o.setup.scroll.itemWidth;
		o.update_items();
		o.update_arrows();
		o.check_complete();
	}
	o.load_first=function(){o.load(o.listIds[0]);}
	o.load_last=function(){o.load(o.listIds[o.listItems-1]);}
	o.load_prev=function(){if(o.list[o.cur].counter>0) o.load(o.listIds[o.list[o.cur].counter-1]);}
	o.load_next=function(){if(o.list[o.cur].counter<o.listItems-1) o.load(o.listIds[o.list[o.cur].counter+1]);}
	// Load img
	o.check_complete=function(){
		if(o.img_tmp.complete){
			window.clearTimeout(o.timer);
			for(i in o.dom){
				if(!o.list[o.cur].prm[i]) continue;
				prmObj=o.dom[i].obj;
				prmKey=o.dom[i].prm;
				prmVal=o.list[o.cur].prm[i];
				if(o.dom[i].iss) prmObj.style[prmKey]=(i=='pic'?('url('+prmVal+')'):prmVal);
				else prmObj[prmKey]=prmVal;
			}
		}else o.timer=window.setTimeout(function(){o.check_complete();},50);
	}
	// Update
	o.update_items=function(){
		//o.xCur=o.xDest;
		dlt=(o.xDest-o.xCur)/o.setup.scroll.steps;
		if(Math.abs(dlt)<o.setup.scroll.thresh) o.xCur=o.xDest;
		else{
			o.xCur+=dlt;
			window.setTimeout(function(){o.update_items();},(1000/o.setup.scroll.speed));
		}
		for(id in o.list){
			if(o.listItems<2) $(o.list[id].obj).hide();
			else{
				var left=Math.floor(o.xCur+o.list[id].counter*o.setup.scroll.itemWidth);
				$(o.list[id].obj).
				css({position:'absolute',left:left+'px'}).
				css(o.setup.tmb[id==o.cur?'current':'active']);
			}
		}
	}
	o.update_arrow=function(id,type){
		if(o.listItems<o.setup.scroll.itemsVis) $(o.arrows[id]).hide();
		else $(o.arrows[id]).css(o.setup.arrows[type]);
	}
	o.update_arrows=function(){
		isFirst=(o.list[o.cur].counter==0);
		isLast=(o.list[o.cur].counter==(o.listItems-1));
		o.update_arrow('first',isFirst?'current':'active');
		o.update_arrow('prev',isFirst?'current':'active');
		o.update_arrow('next',isLast?'current':'active');
		o.update_arrow('last',isLast?'current':'active');
	}
};
var preloader=new dc_preloader;

// Dates
var input2limnum=function(o,lim){
	o.limnumNormalize=function(){
		o.value=o.value%lim;
		while(o.value.length<(''+lim).length) o.value='0'+o.value;
		o.blur();
		if(o.parentHnd) o.parentHnd.glue();
	}
	o.onclick=function(){
		o.value++;
		o.limnumNormalize();
	}
	o.onchange=function(){
		o.limnumNormalize();
	}
}
var input2datetime=function(o,dateSrc){
	if((hm=dateSrc.match(/^(.*) (\d{2}):(\d{2})$/))==null) return false;
	o.isDatetime=true;
	// dateRes
	o.dateResInput=document.createElement('input');
	o.parentNode.insertBefore(o.dateResInput,o.nextSibling);
	o.dateResInput.value=dateSrc;
	o.dateResInput.type='hidden';
	o.dateResInput.name=o.name;
	o.name='';
	o.value=hm[1];
	o.style.width='65px';
	// [space]
	tmpSpan=document.createElement('span');
	o.parentNode.insertBefore(tmpSpan,o.nextSibling);
	tmpSpan.innerHTML='&nbsp;&nbsp;';
	// hours
	o.hoursInput=document.createElement('input');
	o.parentNode.insertBefore(o.hoursInput,o.dateResInput);
	o.hoursInput.style.width='16px';
	o.hoursInput.value=hm[2];
	o.hoursInput.parentHnd=o;
	input2limnum(o.hoursInput,24);
	// :
	tmpSpan=document.createElement('span');
	o.parentNode.insertBefore(tmpSpan,o.dateResInput);
	tmpSpan.innerHTML=' : ';
	// minutes
	o.minutesInput=document.createElement('input');
	o.parentNode.insertBefore(o.minutesInput,o.dateResInput);
	o.minutesInput.style.width='16px';
	o.minutesInput.value=hm[3];
	o.minutesInput.parentHnd=o;
	input2limnum(o.minutesInput,60);
	// methods
	o.onchange=function(){o.glue();}
	o.glue=function(){
		o.dateResInput.value=o.value+' '+o.hoursInput.value+':'+o.minutesInput.value;
	}
	o.glue();
}

// Ajax response
var dcAjax=function(action,prm,cb,errcb){
	$.extend(prm,{action:action});
	$.post('/',prm,function(resp){
		resp=eval('('+resp+')');
		if(resp.ok!='yes'){
			alert(resp.error||resp);
			if(errcb) errcb(resp);
		}
		else if(cb) cb(resp);
	});
}

// Splash
jQuery.fn.splash=function(prm){
	return this.each(function(){
		prm=$.extend({
			text:'Превед!',
			stepTime:30,
			stepValue:1.5,
			maxValue:200,
			color:'red'
		},prm);
		var pos={};
		if(prm.e){
			pos.left=prm.e.pageX;
			pos.top=prm.e.pageY;
		}else{
			pos=$(this).position();
			pos.left+=Math.round($(this).width()/2);
			pos.top+=Math.round($(this).height()/2);
		}
		var o=$('<span></span>').text(prm.text).appendTo(document.body);
		var fs=1;
		var update=function(){
			$(o).css({position:'absolute',color:prm.color,fontSize:(fs+'px'),left:(pos.left-$(o).width()/2),top:(pos.top-$(o).height()/2)});
			fs=Math.ceil(fs*prm.stepValue);
			if(fs<prm.maxValue) window.setTimeout(function(){update();},prm.stepTime);
			else $(o).remove();
		}
		update();
	});
}