function _$(id){return document.getElementById(id)}
function dump(el){var msg = '';for(i in el){if(el[i]) msg+=i+': '+el[i]+'\n';} return msg;}
function objToStr(el){var msg = '';for(i in el){if(el[i]) msg+=''+i+':"'+el[i]+'",';} return "{"+msg.substr(0,msg.length-1)+"}";}
function trim (strIn){
	strIn =''+strIn;
	var str = strIn.replace(/^\s*/,"");
	return str.replace(/\s*$/,"");
}

function openMessage(id){
	var w=window.open("/message/"+id+"/","MessageFor"+id,"width=501, height=550, resizable=1, toolbar=0, location=0, status=0, menubar=0, directories=0, scrollbars=yes, top=0, left=0");
	w.focus();
	return false;
}
function openPhoto(name){
	var w=window.open("/photo/"+name,"Photo","width=650, height=615, resizable=0, toolbar=0, location=0, status=0, menubar=0, directories=0, scrollbars=no, top=0, left=0");
	w.focus();
}
function redirect(ht){
	if(ht) location.href=ht;
}

function htmlspecialchars (strIn){
	strIn =''+strIn;
    reg_from = /&/g;
   	reg_to = "&amp;";
    var str = strIn.replace(reg_from, reg_to);
   	reg_from = /"/g;
    reg_to = "&quot;";
   	str = str.replace(reg_from, reg_to);
    reg_from = /'/g;
   	reg_to = "&#039;";
    str = str.replace(reg_from, reg_to);
   	reg_from = /</g;
    reg_to = "&lt;";
   	str = str.replace(reg_from, reg_to);
    reg_from = />/g;
   	reg_to = "&gt;";
    return str.replace(reg_from, reg_to);
}

function PNG(element,sizingMethod){
	if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)){
		var src;
		if (element.tagName=='IMG'){
			if (/\.png$/.test(element.src)){
				src = element.src;
				element.src = "/images/0.gif";
			}
		}else{
			src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i)
			if (src){
				src = src[1];
				element.runtimeStyle.backgroundImage="none";
			}
		}
		if(!sizingMethod) sizingMethod='crop';
		if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='"+sizingMethod+"')";
	}
}

function bodyHeight(){
	return (document.all?(document.body.parentElement.clientHeight==0?document.body.clientHeight:document.body.parentElement.clientHeight):window.innerHeight);
}

function checkKey(e,keyCode){
	if(e.keyCode == keyCode || e.which==keyCode) return true;
	return false;
}

function isEscPressed(e){
	if(!e) e = window.event;
	return checkKey(e,27);
}
function isEnterPressed(e){
	if(!e) e = window.event;
	return checkKey(e,13);
}
function isCtrlEnterPressed(e){
	if(!e) e = window.event;
	if(e.ctrlKey && (checkKey(e,13) || checkKey(e,10))) return true;
	return false;
}
function isCtrlLeftPressed(e){
	if(!e) e = window.event;
	if(e.ctrlKey && checkKey(e,37)) return true;
	return false;
}
function isCtrlRightPressed(e){
	if(!e) e = window.event;
	if(e.ctrlKey && checkKey(e,39)) return true;
	return false;
}
function isCtrlUpPressed(e){
	if(!e) e = window.event;
	if(e.ctrlKey && checkKey(e,38)) return true;
	return false;
}
function isCtrlDownPressed(e){
	if(!e) e = window.event;
	if(e.ctrlKey && checkKey(e,40)) return true;
	return false;
}
function isUpPressed(e){
	if(!e) e = window.event;
	if(checkKey(e,38)) return true;
	return false;
}
function isDownPressed(e){
	if(!e) e = window.event;
	if(checkKey(e,40)) return true;
	return false;
}
function isSpacePressed(e){
	if(!e) e = window.event;
	if(checkKey(e,32)) return true;
	return false;
}
function isShiftPressed(e){
	if(!e) e = window.event;
	if(e.shiftKey) return true;
	return false;
}
function showWindow(){
	if(!$('#window').attr('id') || !$('#cover').attr('id')) return;
	document.body.style.overflow='hidden';
	$('#cover').css('top','0px');
//	$('#cover').css('height',document.documentElement.clientHeight+document.documentElement.scrollTop);
	$('#cover').css('display',"block");
	centerWindow();
	$('#window').css('display',"block");
	window.windowOpen = true;
} 
function centerWindow(){
	if(!$('#window').attr('id')) return;
	$('#window').css('top',bodyHeight()/2-$('#window').height()/2 + document.documentElement.scrollTop);
	$('#window').css('left',(document.documentElement.clientWidth)/2-$('#window').width()/2);
//	alert(bodyHeight());
}
function hideWindow(){
	if(!$('#window').attr('id') || !$('#cover').attr('id')) return;
	$('#window').css('display',"none");
	$('#cover').css('display',"none");
	document.body.style.overflow='auto';
	window.windowOpen = false;
}
function closeWindow(){
	hideWindow();
}
function createWindow(windowData){
	if(!windowData){
		windowData = {'caption':'','content':'','statusLeft':'','statusRight':'','width':'','height':''};
	}else{
		if(!windowData.caption) windowData.caption='';
		if(!windowData.content) windowData.content='';
		if(!windowData.statusLeft) windowData.statusLeft='';
		if(!windowData.statusRight) windowData.statusRight='';
	}

	if(!$('#cover').attr('id')){
		div = document.createElement('DIV');
		div.id="cover";
		document.body.appendChild(div);
	}

	if(!$('#window').attr('id')){
		div = document.createElement('DIV');
		div.id="window";
		txt = '<table class="mtable">';
		txt+='<tr><td class="lt png" rowspan="2"></td><td class="ct2 pngS"></td><td class="rt png" rowspan="2"></td></tr>';
		txt+='<tr><td class="ct1"><div style="text-align:right"><div style="float:left" id="windowCaption">'+windowData.caption+'</div><a href="javascript:closeWindow()"><img src="/images/icons/01/delete.gif" style="border:none;"></a></div></td></tr>';
		txt+='<tr><td class="lm pngS"></td><td class="cm" id="windowContentContainer"><div class="cnt" id="windowContent">'+windowData.content+'</div></td><td class="rm pngS"></td></tr>';
		txt+='<tr><td class="lb png" rowspan="2"></td><td class="cb1"><div style="text-align:right"><div style="float:left" id="windowStatusLeft">'+windowData.statusLeft+'</div><span id="windowStatusRight">'+windowData.statusRight+'</span></div></td><td class="rb png" rowspan="2"></td></tr>';
		txt+='<tr><td class="cb2 pngS"></td></tr>';
		txt+='</table>';
		div.innerHTML=txt;
		txt = null;

		if(!windowData.width) windowData.width=100;
		if(!windowData.height) windowData.height=10;

		div.style.width=55+parseInt(windowData.width);
		div.style.height=84+parseInt(windowData.height);

		document.body.appendChild(div);
	}
}

function setWindowSize(w,h){
	if(!$('#window').attr('id')) return;
	$('#window').css('width',w+55);
	$('#windowContent').css('height',h);
	$('#windowContent').css('width',w);
	$('#windowContentContainer').css('height',h);
	$('#windowContentContainer').css('width',w);
}
function setContentScroll(type){
	$('#windowContent').css('overflow',type);
}

function tryNextPage(){
	var myElems = document.getElementsByTagName('a');
	for(i in myElems){
		 if(myElems[i].className=='pageButton' && myElems[i].innerHTML=='&gt;'){
			myElems[i].click();
			return; 
		}
	}
}
function tryPrevPage(){
	var myElems = document.getElementsByTagName('a');
	for(i in myElems){
		 if(myElems[i].className=='pageButton' && myElems[i].innerHTML=='&lt;'){
			myElems[i].click();
			return; 
		}
	}
}

function parseError(req){
	alert('Error!\n'+req.responseText);
}
function doAction(url,action,data,onError,onOk){
	var pars = JSON.stringify({"action":action,"data":data});
	var AJ = new Ajax.Request(url,{method:"post",postBody:pars,onFailure:onError,onComplete:onOk});
}
var gloHelpId = 0;
function writeHelp(helpId,msg){
	document.write('<span id="help'+helpId+'"><img class="imgHelp" src="/images/help_i.gif"></span>');
	
	var did='help'+helpId+'div';

	$(document.body).append('<div id="'+did+'" class="divHelp">'+msg+'</div>');
	$('#'+did).data('img',$($('#help'+helpId)[0].firstChild));
	                 
	$('#help'+helpId).bind('mouseenter',function(e){
		var bpX = 'left';
		var bpY = 'top'
		if(($($('#'+did).data('img')).offset().left+$('#'+did).width()+20)>=$(window).width()){
			bpX='right';
		   	$('#'+did).css('left',$($('#'+did).data('img')).offset().left-$('#'+did).width()-20);
		}else{
			bpX='left';
		   	$('#'+did).css('left',$($('#'+did).data('img')).offset().left-5);
		}
		if(($($('#'+did).data('img')).offset().top+$('#'+did).height()+20-document.body.scrollTop)>=$(window).height()){
			bpY='bottom';
		   	$('#'+did).css('top',$($('#'+did).data('img')).offset().top-$('#'+did).height()-20);
		}else{
			bpY='top';
		   	$('#'+did).css('top',$($('#'+did).data('img')).offset().top-5);
		}
		$('#'+did).css('background-position',bpX+' '+bpY);
	   	$('#'+did).fadeIn(500);
		
	});
	$('#'+did).bind('mouseleave',function(e){
	   	$(this).fadeOut(500);
	});
}
function simplePaginator(current_page, per_page, cnt, callback_func){
	
	var pag_html = '';
	all=parseInt(cnt/per_page+0.999999999999);
	if(all>1){
		pag_html+='<table cellspacing="0" cellpadding="0"><tr>';
		allp=all; d=0;
		if(all>5) { 
			all=5;
			d=(current_page>3)?current_page-3:0;
		}
					
		if(current_page>1) pag_html += '<td><a href="" style="dispay:block; border: none; color: #000;" title="В начало" onclick="'+callback_func+'(1); return false;">...</td>';
		for(i=1+d; i<=allp; i++){
			if(i==current_page+3) break;
			if(i!=current_page) pag_html += '<td style="padding: 0pt 5px;"><div style="border: 1px solid #CCC; padding: 0pt 5px;"><a class="noborder" style="dispay:block; color: #000;" href="" onclick="'+callback_func+'('+i+'); return false;">'+i+'</a></div></td>'; 
			else pag_html += '<td style="padding: 0pt 5px;"><div style="border: 1px solid #CCC; padding: 0pt 5px; background: #CCC;"><b>'+i+'</b></div></td> ';
		}
						
		if(current_page!=allp) pag_html += '<td><a href="" style="dispay:block; border: none; color: #000;" title="В конец" onclick="'+callback_func+'('+allp+'); return false;">...</a></td>';
		pag_html+='<tr></table>';
	}
	return pag_html;
}
