var justPushedState = false;
var clickDenied = false;

$(document).ready(function() {

	$(".navi, .sub_menu, #window").hide();


	$(document).mousemove(function(e) {
		var qwe = 0.5 - ($(window).width() - e.clientX) / $(window).width();
		$('.navi').css('left', qwe*10);
		$('#window.small').css('right', 50+qwe*10);
		$('#window:not(.small)').css('margin-left', -500+qwe*5);
	})

	initFlash();
	initHistory();

});

function onFlashLoaded() {
	initLinks();
	$('.navi').fadeTo('slow', 0.9);
}

function onPageLoaded(num) {
	var el;
	if ($('.top_menu .active').length) {
		el = $('.top_menu .active');

		if ($('.sub_menu .active').length) {
			$("#window").removeClass('small');
		} else {
			$("#window").addClass('small');
		}

		if($('#content').html().replace(" ", "").replace("\t", "").replace("\n", "").replace("\r", "").length > 10 ) $("#window").fadeIn();


	} else {
		el = $('.top_menu .clicked');
		el.removeClass('clicked').addClass('active');
	}
	showSubMenu(el);
	clickDenied = false;
}

function showSubMenu(el) {
	var pos = 0;
	var startPos = el.position().left + el.parent().position().left + el.parent().parent().position().left;
	var rightBorder = el.parent().position().left + el.parent().parent().position().left + el.parent().parent().width();

	var sub = $('.sub_menu[rel="' + el.attr('rel') + '"]');

	sub.css('opacity', 0).css('display', 'block');

	var lastel = sub.find('li:first');
	var delta = 0;
	sub.find('li').each(function() {
		if (startPos + sub.width() > rightBorder) {
			lastel = $(this).next();
			if (!lastel.length) lastel = $(this);
			delta += ($(this).width() + 16 * 2 + 5);
			startPos -= ($(this).width() + 16 * 2 + 5);
			;
		}
	});

	pos = startPos + (el.width() - lastel.width()) / 2;

	sub.find('.snivel').css('left', delta + lastel.width() / 2 + 8);
	sub.css('left', pos).css('display', '').css('opacity', '').fadeIn();
}

function onNextPressed() {
	$('.top_menu .active').next().children('a').trigger('click');
}

function onPrevPressed() {
	$('.top_menu .active').prev().children('a').trigger('click');
}


function initFlash() {

	openOnStartup = null;
	if ($(".top_menu .active").length) {
		openOnStartup = $(".top_menu .active a").attr('rel');
		if (openOnStartup == 0) openOnStartup = "_home"
	}

	swfobject.embedSWF(sHtmlRoot + "justapp.swf", "justapp", "100%", "100%", "9.0.0", "", {'openOnStartup': openOnStartup}, {'wmode':'transparent'}, {'id':'justapp', 'name':'justapp'});
}


function loadContent(url, doState) {

	var u = clearUrl(url);

	var win;
	var cont;
	win = $("#window");
	cont = $('#content');

	if (u.search("/") > -0) {
		win.removeClass('small');
	} else {
		win.addClass('small');
	}

	cont.load(url, function(resp) {
		document.title = $(resp).filter('title').text();
		cont.find('title').remove();
		if(cont.html().replace(" ", "").replace("\t", "").replace("\n", "").replace("\r", "").length > 10 ) {
			win.fadeIn();
		}
	});

	if (doState !== undefined) {
		stateObj = { url: url };
		justPushedState = true;
		History.pushState(stateObj, '', url);
		justPushedState = false;
	}
}

function clearUrl(url) {
	var u = url.replace(document.location.protocol + "//" + document.location.host, "");

	if (u.substr(-1) == "/") u = u.substr(0, u.length - 1);
	if (sHtmlRoot.length > 1) u = u.replace(sHtmlRoot, "");
	if (u.substr(0, 1) == "/") u = u.substr(1);

	return u;

}


function initHistory() {
	// Prepare
	var History = window.History;

	// Bind to StateChange Event
	History.Adapter.bind(window, 'statechange', function() { // Note: We are using statechange instead of popstate
		var state = History.getState();
		if (state !== null && !justPushedState) {
			//loadContent(state.url);
			var u = clearUrl(state.url);
			$(".navi a[href*='" + u + "']:first").trigger('click');
		}
	});
}


function initLinks() {
	$(".navi a").click(function(event) {

		if( !clickDenied ) {
		
			/*if() {
			}*/
			
			if($(this).attr('href').search(/http:/)>-1) {
				document.location.href=$(this).attr('href');
				return;
			}
			

			$(".top_menu .active").removeClass();
			$(".sub_menu .active").removeClass();



			if ($(this).parents('.sub_menu').length > 0) {
				$(".sub_menu").hide();
				$(this).parents('.sub_menu').fadeIn();

				$(".top_menu li[rel='" + $(".sub_menu:visible").attr('rel') + "']").addClass('active');
				$(this).parent().addClass('active');
			} else {
				clickDenied = true;
				$(this).parent().addClass('clicked');
				$(".sub_menu").fadeOut();
				swfobject.getObjectById("justapp").goTo(parseInt($(this).attr('rel')));
			}

			var url = $(this).attr("href");

			$("#window").fadeOut(function() {
				loadContent(url, true);
			});
		}

		event.preventDefault();
		return false;
	})


	$("#window .close").click(function(event) {
		$(".top_menu li[rel='" + $(".sub_menu:visible").attr('rel') + "'] a").trigger('click');

		event.preventDefault();
		return false;
	})


}

