var body_offset;

jQuery(function($){
	$('div.login-key > a').toggle(
		function(){ $('div.login-form').fadeIn(); },
		function(){ $('div.login-form').fadeOut(); }
	);
	
	
	var h = $('div.main-zone').height();
	h = h + 212;
	$('div.body').height(h);
	
	
	body_offset = $('div.body').offset().left;
	resize();
	$(window).resize(function(){resize();});
	
	// submenu init
	var speed = 100;
	var submenu_timer;
	$('div.body div.content-menu > div.content-menu-sub a, div.body div.content-menu > div.content-menu-sub div.content-menu-sub-top, div.body div.content-menu > div.content-menu-sub div.content-menu-sub-bottom, div.body div.content-menu > div.content-menu-sub div.content-menu-sub-center').mouseover(function(){ window.clearTimeout(submenu_timer); });
	$('div.body div.content-menu > a').mouseover(function(){
		// hide all visible submenu
		var div = $(this).next();
		$.each($('div.body div.content-menu > div.content-menu-sub'), function(){
			if ($(this).hasClass('visible') && (!div.is('div.content-menu-sub') || $('div.body div.content-menu > div.content-menu-sub').index(div) != $('div.body div.content-menu > div.content-menu-sub').index(this))) {
				$(this).removeClass('visible');
				$(this).fadeOut(speed);
				$(this).prev().removeClass('selected');
			}
		});
		if (div.is('div.content-menu-sub')) {
			var p = $(this).position();
			p.top += 18;
			p.left += 136;
			div.css(p);
			div.addClass('visible');
			div.fadeIn(speed);
			div.prev().addClass('selected');
			div.mouseout(function(){
				var self = this;
				submenu_timer = window.setTimeout(function(){
						$(self).removeClass('visible');
						$(self).fadeOut(speed);
						$(self).prev().removeClass('selected');
						$(self).unbind('mouseout');
					},
					500
				);
			});
		}
	});
	$('div.body div.content-menu > a').each(function(){
		if ($(this).next().is('div.content-menu-sub'))
			$(this).addClass('havesub');
	});
	$('div.body div.content-menu > a.strelka').click(function(){
		if ($(this).hasClass('strelka-hidden'))
			show_content_menu();
		else
			hide_content_menu();
	});
})

function obj_coffe() {
	$('.obj-paper').css({left: 758, top: 407});
	$('.obj-coffe').css({left: 838 + body_offset, top: 425}).draggable();
}

function obj_get_random_coord(o) {
if(o.hasClass('left'))
	var left = Math.round(Math.random()*body_offset)-o.width()/5;
else if(o.hasClass('right'))
	var left = body_offset+Math.round(Math.random()*80)+1000;
else
	var left = Math.round(Math.random()*1)*1000 + body_offset;

	var top = Math.round(Math.random()*500)+150;
	return {left: left, top: top};
}

function obj_random() {
	$('.obj-set-random').each(function(){
		$(this).css(obj_get_random_coord($(this))).draggable();
	});
}

function resize() {
	var start_width = $(window).width();
	if (start_width < 1003)
		start_width = 1003;
	var start_height = $(document).height();
	$('div.body-box').width(start_width);
	//$('div.body-box').height(start_height);
	var h = $('div.main-zone').height();
	h = h + 212;
	$('div.body').height(h);
	$('div.body-box').height(h);
	
	$('div.content-text-container > div.content-text-container-left').height($('div.content-text-container').height());
	
	var offset = $('div.body').offset().left;
	if (body_offset != offset)
		$('div.obj').each(function(){
			var left = $(this).offset().left + (offset - body_offset);
			$(this).css({left: left});
		});
	body_offset = offset;
}

function hide_content_menu() {
	$('div.body div.content-menu > a.strelka').addClass('strelka-hidden');
	$('div.body div.main-zone-content').addClass('main-zone-wide');
	resize();
}

function show_content_menu() {
	$('div.body div.content-menu > a.strelka').removeClass('strelka-hidden');
	$('div.body div.main-zone-content').removeClass('main-zone-wide');
	resize();
}
