function Dklab_Realplexor(fullUrl, namespace, viaDocumentWrite)
{
	var VERSION = "1.32";

	var host = document.location.host;
	
	if (!this.constructor._registry) this.constructor._registry = {}; // all objects registry
	this.version = VERSION;
	this._map = {};
	this._realplexor = null;
	this._namespace = namespace;
	this._login = null;
	this._iframeId = "mpl" + (new Date().getTime());
	this._iframeTag = 
		'<iframe'
		+ ' id="' + this._iframeId + '"'
		+ ' onload="' + 'Dklab_Realplexor' + '._iframeLoaded(&quot;' + this._iframeId + '&quot;)"'
		+ ' src="' + fullUrl + '?identifier=IFRAME&amp;HOST=' + host + '&amp;version=' + this.version + '"'
		+ ' style="position:absolute; width:200px; height: 200px; left: -1000px; visibility:hidden"' +
		'></iframe>';
	this._iframeCreated = false;
	this._needExecute = false;
	this._executeTimer = null;
	
	this.constructor._registry[this._iframeId] = this;
	
	if (!fullUrl.match(/^\w+:\/\/([^/]+)/)) {
		throw 'Dklab_Realplexor constructor argument must be fully-qualified URL, ' + fullUrl + ' given.';
	}
	var mHost = RegExp.$1;
	if (mHost != host && mHost.lastIndexOf("." + host) != mHost.length - host.length - 1) {
		throw 'Due to the standard XMLHttpRequest security policy, hostname in URL passed to Dklab_Realplexor (' + mHost + ') must be equals to the current host (' + host + ') or be its direct sub-domain.';
	} 
	
	if (viaDocumentWrite) {
		document.write(this._iframeTag);
		this._iframeCreated = true;
	}
	
	document.domain = host;	
}

Dklab_Realplexor._iframeLoaded = function(id)
{
	var th = this._registry[id];
	setTimeout(function() {
		var iframe = document.getElementById(id);
		th._realplexor = iframe.contentWindow.Dklab_Realplexor_Loader;
		if (th.needExecute) {
			th.execute();
		}
	}, 50);
}

Dklab_Realplexor.prototype.logon = function(login) {
	this._login = login;
}

Dklab_Realplexor.prototype.setCursor = function(id, cursor) {
	if (!this._map[id]) this._map[id] = { cursor: null, callbacks: [] };
	this._map[id].cursor = cursor;
	return this;
}

Dklab_Realplexor.prototype.subscribe = function(id, callback) {
	if (!this._map[id]) this._map[id] = { cursor: null, callbacks: [] };
	var chain = this._map[id].callbacks;
	for (var i = 0; i < chain.length; i++) {
		if (chain[i] === callback) return this;
	}
	chain.push(callback);
	return this;
}

Dklab_Realplexor.prototype.unsubscribe = function(id, callback) {
	if (!this._map[id]) return this;
	if (callback == null) {
		this._map[id].callbacks = [];
		return this;
	}
	var chain = this._map[id].callbacks;
	for (var i = 0; i < chain.length; i++) {
		if (chain[i] === callback) {
			chain.splice(i, 1);
			return this;
		}
	}
	return this;
}
Dklab_Realplexor.prototype.execute = function() {
	// Control IFRAME creation.
	if (!this._iframeCreated) {
		var div = document.createElement('DIV');
		div.innerHTML = this._iframeTag;
		document.body.appendChild(div);
		this._iframeCreated = true;
	}
	
	// Check if the realplexor is ready (if not, schedule later execution).
	if (this._executeTimer) {
		clearTimeout(this._executeTimer);
		this._executeTimer = null;
	}
	var th = this;
	if (!this._realplexor) {
		this._executeTimer = setTimeout(function() { th.execute() }, 30);
		return;
	}
	
	// Realplexor loader is ready, run it.
	this._realplexor.execute(
		this._map, 
		this.constructor._callAndReturnException, 
		(this._login != null? this._login + "_" : "") + (this._namespace != null? this._namespace : "")
	);
}
Dklab_Realplexor._callAndReturnException = function(func, args) {
	try {
		func.apply(null, args);
		return null;
	} catch (e) {
		return "" + e;
	}
}


$(document).ready(
  function() {
if (autorized)setTimeout("mail()",10);
//$('#text').fck({path: '/fck/',toolbar:'Basic' });
$('.delete_comment').bind('click',function(){
	return false;
})

	//Специально для старых IE
	if ($('#float_msges').css('position') != 'fixed')
	{	
		$('#float_msges').css('position','absolute');
		$(window).scroll(function () {   
		var offset = $(document).scrollTop();  
		offset = offset + ($(window).height()-$('#float_msges').height()-2);
		offset = offset + "px";
        //$('#float_msges').animate({top:offset},{duration:500,queue:false});  
        $('#float_msges').css('top',offset);
		})
	}
	
	$(".smiles img").bind('click',function(){
		$("#comment_text").attr('value',$("#comment_text").attr('value')+$(this).attr('alt'));
	})
        

  
})

function del_post(post_id)
{
	if (confirm('Удалить ваше сообщение?'))
	{
		$.get("/ajax/forum.php?del="+post_id, function(data){
			if (data>0){
			$("#post"+post_id).fadeOut("slow",function(){$(this).remove()});
			}
			else
			{
				alert('Невозможно удалить комментарий.');
			}
	});	
	}
	return false;
}

function del_comment(comment_id)
{
	if (confirm('Удалить ваш комментарий?'))
	{
		$.get("/ajax/comments.php?del="+comment_id, function(data){
			if (data>0){
			$("#comment_"+comment_id).fadeOut("slow",function(){$(this).remove()});
			}
			else
			{
				alert('Невозможно удалить комментарий.');
			}
	});	
	}
	return false;
}

function edit_comment(comment_id)
{
	$("#comment_"+comment_id+" div.txt").fadeOut(function(){
	$("#comment_"+comment_id+" span").fadeIn();
	$("#comment_"+comment_id+" div.edit").hide();	
	});
	return false;
}

function save_comment(comment_id)
{
	vars=$("#comment_"+comment_id+" form").serializeArray( );
	$.post("/ajax/comments.php?id="+comment_id, vars,
  			function(data){
    			if (data.length>0){
    				
    			
    				$("#comment_"+comment_id+" span").fadeOut(function(){
					$("#comment_"+comment_id+" div.edit").show();
					$("#comment_"+comment_id+" div.txt").html(data).fadeIn();
    				
    				});				
}else(alert('Невозможно отредактировать'))
});
	return false;
}

	$('#add_dir').submit(function() {	
			vars=$(this).serializeArray( );
			title=$("#add_folder").attr("value");
			$.post("/ajax/my_works.php", vars,
  			function(data){
    			if (data>0){
    				add_dir(data,title);
    			}
  			});					
			return false;
		})

function hide_float()
{
	$("#float").fadeOut('slow',function () {
	$("#float_bg").hide();	
	});
}

function show_float()
{
	$("#float_bg").show();
	$("#float_bg").css('height',$('body').outerHeight());
	$("#float").css('top',document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	$("#float").fadeIn('slow');
}

function select_in_select (select_id,select_value)
{
	$(select_id).children().filter(function (index) {
         return $(this).attr("value") == select_value;
    }).attr({'selected':'true'})
}

function vote (id,div,user,from)
{
	if (confirm('Повысить рейтинг?'))
	{
		$.get("/ajax/rate.php?id="+id+"&div="+div+"&user="+user, function(data){
			if (data>0)
			{
				$(from).parent().html(data);
			}
			else
			{
				alert('Невозможно проголосовать.');
			}
		});	
	}
	return false;
	
}

function favorite (id)
{
	
		$.get("/ajax/favorite.php?id="+id, function(data){
			if (data==1)
			{
				$("#delete_from_favotite").show();
				$("#add_to_favotite").hide();
				alert('Добавлено.');
			}
			else if (data==2)
			{
				$("#delete_from_favotite").hide();
				$("#add_to_favotite").show();
				alert('Удалено.');
			}
			else
			{
				alert('Ошибка.');
			}
		});	
	
	return false;
}

function friend (id)
{
		$.get("/ajax/favorite.php?friend="+id, function(data){
			if (data==1)
			{
				$("#delete_from_friends").show();
				$("#add_to_friends").hide();
				alert('Добавлено.');
			}
			else if (data==2)
			{
				$("#delete_from_friends").hide();
				$("#add_to_friends").show();
				alert('Удалено.');
			}
			else
			{
				
				alert('Невозможно добавить либо автор уже у вас в друзьях.');
			}
		});	
	return false;
}

function mail()
{
	$.getJSON("http://arts.in.ua/ajax/mail.php?new=1&rand="+Math.random(),
        function(data){
         $.each(data, function(i,item){
            
            if (!$('#msg'+item.id).length > 0)
            {

            		$("#float_msges").prepend("<a href='"+item.url+"' id=msg"+item.id+" style=display:none><b title='"+item.alt+"'>"+item.username+"</b> "+item.title+"</a>");

   
            	$("#msg"+item.id).fadeIn('slow');
            }
            
            $("#new_mail").html("("+(i+1)+")");
            
          });
        });
	setTimeout("mail()",10000);
}
