function confirmDel(act) {
	if(act==1)	{
		if(window.confirm('Вы уверены, что хотите удалить сообщение?')!=true) return false
		return true;
	}
}

function confirmReject(act) {
	if(act==1)	{
		if(window.confirm('Вы уверены, что хотите отменить публикацию сообщения?')!=true) return false
		return true;
	}
}

function collapsElement(obj) { 
  var el = document.getElementById(obj);
    if ( el.style.display != "none" ) {
      el.style.display = 'none';
	}
	else {
      el.style.display = '';
	}
}

function trim(str) {
	return str.replace(/^\s*/,'').replace(/\s*$/,'');
}
function selValue(el,str) {
	for (var i=0;i<el.length;i++) {
		if ((!el[i].value && el[i].text==str) || el[i].value==str) {
			el.selectedIndex=i;
			return;
		}
	}
	el.selectedIndex=0;
}
function flash(version, filename, name, width, height, wmode, bgcolor, menu, flashvars) {
	document.write("<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version="+version+"' width='"+width+"' height='"+height+"' id='"+name+"' align='middle'>");
	document.write("<param name='allowScriptAccess' value='always' />");
	document.write("<param name='wmode' value='"+wmode+"' />");
	document.write("<param name='bgcolor' value='"+bgcolor+"' />");
	document.write("<param name='salign' value='lt' />");
	document.write("<param name='movie' value='"+filename+"' />");
	document.write("<param name='menu' value='"+menu+"' />");
	document.write("<param name='quality' value='high' />");
	document.write("<param name='flashvars' value='"+flashvars+"' />");
	document.write("<embed src='"+filename+"' wmode='"+wmode+"' menu='"+menu+"' quality='high' flashvars='"+flashvars+"' salign='lt' width='"+width+"' height='"+height+"' bgcolor='"+bgcolor+"' name='"+name+"' align='middle' allowScriptAccess='always' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />");
	document.write("</object>");
}

function ShowWarningsWindow(userid,usern,pnum,modname) 
{
	var wnd = document.getElementById('moder_window');
	var htmlCode = '<form method=post id=warning_form><table width=400 border=0 cellspacing=0 cellpadding=0><tr class=r0 height=20><input type=hidden name=moder_action value=set_warning><input type=hidden name=w_uid value='+userid+'><input type=hidden name=modname value='+modname+'><input type=hidden name=pnum value='+pnum+'><td width=400 colspan="3" class=r0><font class=textwhite>Предупреждение пользователю '+usern+' </font></td></tr><tr class=w0><td width=400 height="70" valign="top" colspan="3"><font class=textblack><br>Причина <input type=text name=war_text id=war_text class=fld size=60><br><input type=checkbox name=del_all_message value=1> Полностью заменить сообщение на предупреждение</font></td></tr><tr class=r0 height=25 valign=middle><td width=50>&nbsp;</td><td width=345 align="right"><input type="submit" value="Вынести предупреждение" class=btn>&nbsp<input type=button value=Закрыть class=btn onclick="CloseWindow();"></td><td width=5>&nbsp;</td></tr></table></form>';
	if(wnd)
	{		
		wnd.style.left = document.body.clientWidth/2+document.body.scrollLeft+'px';
		wnd.style.top = document.body.clientHeight/2+document.body.scrollTop+'px';
		wnd.innerHTML = htmlCode;
	}
	return true;
}

function CloseWindow() {
	var wnd = document.getElementById('moder_window');
	if(wnd)
	{
		wnd.innerHTML = '';
	}
	return true;
}
function refreshNumbers() {
	var pcnt = document.getElementById('posts_count');
	var result_object = document.getElementById('multiNumber');
	var i;
	var chk;
	var results = '';
	if(pcnt)
	{
		for(i=0;i<pcnt.value;i++)
		{
			var tmp = 'post_check_'+i;
			chk = document.getElementById(tmp);
			if(chk)
			{				
				if(chk.checked)
					results += results==''?chk.value:','+chk.value;
			}
		}
	}
	result_object.value = results;
	
}

function core()
{
	core.prototype.init = function()
	{
		var oInstance=this;
		if(oInstance.bIsInited)
			return;
		oInstance.bIsInited=true;
	}
	core.prototype.loadVKSDK=function(bFix)
	{
		(function (x){
			var saved_decodeURIComponent=decodeURIComponent;
			x.decodeURIComponent=function(uri){ return uri.match("%init%")?uri:saved_decodeURIComponent(uri); };
		})(window);
		var el = document.createElement("script");
		    el.type = "text/javascript";
		    el.charset = 'utf-8';
		    el.src = "http://vkontakte.ru/js/api/openapi.js";
		    el.async = true;
		    document.getElementById("vk_api_transport").appendChild(el);
	}
	core.prototype.initVKCallback=function(sHost,bDebug)
	{
		if(typeof(VK)!="object")
		{
			if(bDebug===true)
				console.log('Error initializing VK object!');
			return false;
		}
		VK.init({
			"apiId": 2688825, //2685394,//
			"nameTransportPath":sHost+'export/xd_receiver.html'
		});
		return true;
	}
	core.prototype.initVK=function(sHost,bDebug)
	{
		var oInstance=this;
		 window.vkAsyncInit = function() {
			if(!oInstance.initVKCallback(sHost,bDebug))
				return;
			var box=$('#vk_post_box');
			if(box.length > 0)
			{
				VK.Auth.getLoginStatus((function(response)
				{
					if(bDebug===true)
						console.log(response);
					if (response.status == 'unknown')
					{
						$('#vk_post_box_inner_span').css('display','inline');
						box.click(function(){
							if(this.checked)
							{
								VK.Auth.login((function(response)
								{
									if(bDebug===true)
										console.log(response);
									if (response.session) 
									{
										if(bDebug===true)
											console.log('Authorized');
									}
									else
									{
										if(bDebug===true)
											console.log('Non authorize or cancelled');
									}
								}),8192);
							}							
						});
						return;
					}
					$('#vk_post_box_inner_span').css('display','inline');
				}));
			}
		 };
		oInstance.loadVKSDK();
	}
	core.prototype.PostToVK=function(sHost,sComment,bDebug)
	{
		var oInstance=this;
		window.vkAsyncInit = function() 
		{
			if(!oInstance.initVKCallback(sHost,bDebug))
				return;
			VK.Auth.login((function(response)
			{
				if (response.status == 'unknown')
				{
					if(bDebug===true)
						console.log('Not authorized or cancelled');
					return;
				}
				
				VK.Api.call('wall.post', {
					message: sComment						
					}, function(r) 
					{
						if(r.error && r.error.error_code == 14)
						{ //"captcha is needed"
							$("#vk_captcha_img").attr('src',r.error.captcha_img);
							oInstance.ShowDeadCenterDiv(460,219,'vk_captcha_box');
							oInstance.captcha_sid = r.error.captcha_sid;
							$('#vk_captcha_box #btn_close').click(function(){
					    		$('#vk_captcha_box').hide();
					    	})
					    	$('#vk_captcha_box #btn_send').click(function(){
					    		var captcha_text = $('#vk_captcha_text').val();
					    		try{
					    			if(captcha_text != '')
					    				oInstance.PostToVKWithCaptcha(sHost,sComment,bDebug,captcha_text); 
					    		} catch(e){}
					    	})
						}	
						if(bDebug===true)
							console.log(r);
					});				
			}), 1);
		};
		oInstance.loadVKSDK();
	}
	core.prototype.PostToVKWithCaptcha=function(sHost,sComment,bDebug, sCaptchaKey)
	{
		var oInstance=this;
		if(typeof(VK)!="object")
			return false;
		$('#vk_captcha_box').hide();
		VK.Api.call('wall.post', {
			message: sComment,
			captcha_sid: oInstance.captcha_sid,
			captcha_key: sCaptchaKey
			}, function(r) 
			{
				if(bDebug===true)
					console.log(r);
			});
	}
	core.prototype.ShowDeadCenterDiv=function(Xwidth,Yheight,divid) { 
		// First, determine how much the visitor has scrolled
		var scrolledX, scrolledY; 
		if( self.pageYoffset ) { 
		scrolledX = self.pageXoffset; 
		scrolledY = self.pageYoffset; 
		} else if( document.documentElement && document.documentElement.scrollTop ) { 
		scrolledX = document.documentElement.scrollLeft; 
		scrolledY = document.documentElement.scrollTop; 
		} else if( document.body ) { 
		scrolledX = document.body.scrollLeft; 
		scrolledY = document.body.scrollTop;
		}
		// Next, determine the coordinates of the center of browser's window
		var centerX, centerY; 
		if( self.innerHeight ) {
		//Non-IE
		centerX = self.innerWidth; 
		centerY = self.innerHeight; 
		} else if( document.documentElement && document.documentElement.clientHeight ) {
		//IE 6+ in 'standards compliant mode'
		centerX = document.documentElement.clientWidth; 
		centerY = document.documentElement.clientHeight;
		} else if( document.body ) {
		centerX = document.body.clientWidth; 
		centerY = document.body.clientHeight;
		}
		// Xwidth is the width of the div, Yheight is the height of the 
		// div passed as arguments to the function:
		var leftoffset = scrolledX + (centerX - Xwidth) / 2; 
		var topoffset = scrolledY + (centerY - Yheight) / 2;
		// the initial width and height of the div can be set in the 
		// style sheet with display:none; divid is passed as an argument to // the function
		$('#'+divid).css({
			'position': 'absolute',
			'top': topoffset + 'px',
			'left': leftoffset + 'px',
			'display': 'block'
		}); 
	} 
	core.prototype.PostToITUT=function(sHost,sComment,bDebug)
	{
		var news_url = encodeURIComponent(sHost);
		var encoded_news_url = news_url.replace(/~/g,'%7E').replace(/%20/g,'+');
		var comment = encodeURIComponent(sComment);
		var encoded_comment = comment.replace(/~/g,'%7E').replace(/%20/g,'+');
		window.open('http://i.tut.by/api/share.php?u='+encoded_news_url+'&act=1&comment='+encoded_comment,'','fullscreen=1,status=1,toolbar=1,location=1,directories=1,menubar=1,resizable=1,scrollbars=1');
	}
};
var runCore = new core();
runCore.init();

