function showBlock( query, block ){
	$( block ).innerHTML = '...';
    new Ajax(query, {
			method: 'get',
			update: block
		}).request();
}

function sendSortable(block, xml) {
	new Ajax('sortobj', { postBody: {'type':block, 'sortobj':xml} }).request();
	/*
	var myAjax = new Ajax.Updater(
        block,
        'sortobj', 
        {
            method: 'post',
            parameters: {type: block, sortobj: xml}
        });
     */
}

var sortListXml;
function sortablesItems( block ){
	//onDrag = document.createElement('div');
	//onDrag.setClass = 'onDrag';
	//alert(onDrag+'-'+$$('#' + block +' ul .sort'));
	var mySort = new Sortables( $( block ), {
	handles: $$('#' + block +' ul .sort'),
	//handles: onDrag,
	onComplete: function() {
		var items = '<r>';
		var i=1;
		var nodes = document.getElementById( block ).childNodes;
		for(var item in nodes ){
			//alert( nodes[item].nodeName );
			if( item && nodes[item].nodeName == "UL" ) {
				var guid = nodes[item].getAttribute("guid");
				items = items + '<o><i>' + guid + '</i><u>' + i++ + '</u></o>';
			}
		}
		items = items + '</r>';
		sortListXml = items;
		//sendSortable( block, items );
	}
	});
}

var comment;
function sendComment( guid, rootComment ){
	if( $('guid').value && $('text').value){
		cont = document.createElement('div');
		cont.id = 'return' + guid;
		cont.innerHTML = 'Sending...';
		postBody={guid: $('guid').value, text: $('text').value};
		if( rootComment ) postBody[ 'subject' ] = $('subject').value;
		
		new Ajax('comments', {
			method: 'post',
			update: cont,
			postBody: postBody,
			onComplete: addComment( guid )
		}).request();
	}
}

function removeComment( guid ){
	if( guid ){
		cont = document.createElement('div');
		cont.id = 'remove' + guid;
		cont.innerHTML = 'Deleting...';
		
		new Ajax('comments', {
			method: 'post',
			update: cont,
			postBody: {guid: guid},
			onComplete: delComment( guid )
		}).request();
	}
}

function addComment( guid ){
	document.getElementById('item' + guid ).appendChild( cont );
	document.getElementById('reply' + guid ).innerHTML = '';
	var scrollTo = (cont.offsetTop - 200);
	scrollTo = scrollTo < 0 ? 0 : scrollTo;
	window.scrollTo(0, scrollTo );
	lastReply=null;
}

function delComment( guid ){
	//alert(guid + '-' + document.getElementById( guid ) + cont);
	var p2 = document.getElementById( guid );
	p2.parentNode.replaceChild( cont, p2 );
	lastReply=null;
}

var lastReply;
function replyComm( guid ){
	if( lastReply || lastReply==guid ) {
		document.getElementById('reply' + lastReply ).innerHTML = '';
	}
	if( lastReply!=guid ) document.getElementById('reply' + guid ).innerHTML = '<form id="replyForm" class="replyForm" method="post" onSubmit="sendComment(\'' + guid + '\'); return false;"><textarea name="text" id="text"></textarea><input name="guid" id="guid" type="hidden" value="' + guid + '"/><div class="submit"><input type="submit"/></div></form>';
	if( lastReply!=guid ) lastReply = guid; else lastReply=null;
}

function voteComment( location, variant, obj ){
	if( location && obj ){
		var span = obj.parentNode.parentNode;
		var parent = span.parentNode;
		var rating = parent.getElementsByTagName('font')[0];
		//var guid = parent.id;
		//alert( location + variant );
		new Ajax( location + variant ,{
			method: 'get',
			onComplete: function() {
				rating.innerHTML = parseFloat( rating.innerHTML ) + ( variant == 'vote_up' ? 1 : -1);
				span.innerHTML = '';
				return false;
			}
		}).request();	
	}
}

if( mailLocation ){
	(function(){
		new Ajax( mailLocation ,{
			method: 'get',
			update: $('mailbox'),
			onComplete: function() {
				//$('mailboxnum').innerHTML = parseFloat( $('mailboxnum').innerHTML );
			}
		}).request();
	}).periodical(30000);
}

typeShow={};
function recommendItems(elements, togglers, displayTab){
	new Accordion($$( elements ), $$( togglers ), {
		onActive: function(show){
			var type = show.id;
			if( !typeShow[ type ] && !$('inner'+show.id).innerHTML ){
				typeShow[ type ] = true;
				$('inner'+show.id).innerHTML='Loading...<br> '+show.id;
			}
		},
		display:displayTab
	});
}

var showVar = {};
function showBlockDiv( blockName ){
	if( !showVar[ blockName ] ){
		$( blockName ).style.display = 'inline';
		showVar[ blockName ] = true;
	}else{
		$( blockName ).style.display = 'none';
		showVar[ blockName ] = false;
	}
}