var $rnd = new Date();

$rnd = $rnd.getTime();



$().ready(function() {

	$('.drag-item .toggle').click(function() {

		$item = $(this).parents('.drag-item');

		/*$('.data', $item).slideToggle('normal', function() {

			$item.toggleClass('drag-open').toggleClass('drag-close');

		});*/

		$item.find('.data').toggle().end().toggleClass('drag-open').toggleClass('drag-close');

		ajaxDrag();

		return false;

	});


	$('.drag-box').sortable({

		connectWith: ['.drag-box'],

		items: ".drag-item",

		placeholder: 'ui-state-highlight',

		handle: '.title-box',

		revert: true,

		start: function(e, ui) {

			$('.drag-box').css('background', '#fdfdfd');

			$('.ui-state-highlight').height(ui.item.height()-4);

		},

		stop: function(e, ui) {

			$('.drag-box').css('background', 'transparent');

			ajaxDrag();

		}

	});
	

	$('.draggable').draggable({
		handle:'h1'
	});

	

	$('#wannaenter, #login-layout .cross, .loginhrefintheform').click(function() {
		
		$loginbox = $('#login-layout');

		$loginbox.css({top: $(document).scrollTop()+192, left:'50%'});

		$loginbox.toggle();
		if ($loginbox.css('display')=='block') {
			$("input[name='logname']").focus();
		}

		if ($loginbox.css('display')=='block') $("input[name='logname']").focus();

		return false;

	});

	$('#error-layout .cross').click(function() {

		$('#error-layout').remove();

		return false;

	});

	

	/* Amazing funcions ^_~ */

	

	/*

	$(".drag-box a[href^='/privateroom/']").mouseover(

		function(){

			var docwidth = $('#layout').width(), docheight = $('#layout').height(), left = $(this).offset().left, top = $(this).offset().top, bubble = $("#user-fast-info");

			//if ($.browser.mozilla) console.log('doc:'+docwidth+', '+docheight);

			//if ($.browser.mozilla) console.log('itm:'+left+', '+top);

			bubble.show().css({ top: (top+bubble.height()>=docheight-20) ? top-bubble.height() : top, left:(left+bubble.width()>=docwidth-20) ? docwidth - bubble.width()  : left });

			bubble.data("pos", { left:bubble.offset().left, top:bubble.offset().top, show:true });

			

		}

	);

	$("#user-fast-info").mouseleave(function() { $(this).hide(); });

	*/

	
	var $lastcomment = $("#user-last-comment");
	$(".getlastcomment").hover(
		function(){
			var docwidth = $('#layout').width(), docheight = $('#layout').height(), left = $(this).offset().left, top = $(this).offset().top, bubble = $("#user-last-comment");
			//if ($.browser.mozilla) console.log('link:'+$(this).attr('href'));
			$query='do=getlastcomment&linkedid='+$(this).attr('href')+'&uid='+uid+'&rnd='+$rnd;
			$rnd++;
			$('#user-last-comment').html('<img src="/img/load.gif" />');
			$.post("/jqajax.php", $query,
			  function(data){
				$('#user-last-comment').html(data).width(0).width('auto').width($('#user-last-comment').width());
				$('#user-last-comment a').each(function() {
						$(this).html($(this).text().replace(/\//g, "/<wbr>&shy;"))
					});
				if ($('#user-last-comment').width() > 250) $('#user-last-comment').width(250);
				if ($('#user-last-comment').width()==0) $('#user-last-comment');
			  });

			bubble.show().css({ top: (top+bubble.height()>=docheight-20) ? top-bubble.height()-25 : top+25, left:(left+bubble.width()>=docwidth-20) ? docwidth - bubble.width()  : left });
			bubble.data("pos", { left:bubble.offset().left, top:bubble.offset().top, show:true });
		},
		function() { $("#user-last-comment").hide(); $('#user-last-comment').html(''); }
	);

    $(".getlastcomment").mousemove(function(e){
 	  var docwidth = $('#layout').width(), docheight = $('#layout').height();
	  $lastcomment.css({ 
		top: (e.pageY+$lastcomment.height()>=docheight-20) ? e.pageY-$lastcomment.height()-25 : e.pageY+25,
		left:(e.pageX+$lastcomment.width()>=docwidth-30) ? docwidth - $lastcomment.width()-30 : e.pageX
	  });
    });

	//$("#user-last-comment").mouseleave(function() { $(this).hide(); });

	var o=document.getElementById("lilchatmessageid");

	if (o) {

		o.scrollTop=o.scrollHeight-o.offsetHeight+20;

	}


	
	$("textarea").live('keypress', function(event) {
			if((event.ctrlKey) && ((event.keyCode == 0xA)||(event.keyCode == 0xD))) {
				$(this).parents("form").eq(0).submit();
			}											
	});

	$('input.gray-hovered')
		.focus(function() { $(this).stop().animate({ backgroundColor:'#ffffff' }, "normal"); })
		.blur(function() { $(this).stop().animate({ backgroundColor:'#f1f1f1' }, "normal"); });
		
	
	$('#openedphotodiv').draggable();
});

