var theQueue = $({});
var body;
var bg;
var arm;
var loader;
var black;
var logo;
var brilliant;
var menu;
var menu_container;
var menu_small;
var wrapper;
var contentBG;
var content;
var activeMenu = "nav1";

 

$(document).ready(init);
$(document).ready(backgroundFade);
$(document).ready(armFade);


function init() {
	body = $("body");
	bg = $("bg");
	arm = $('#arm');
	loader = $('#loader');
	black = $("#black");
	logo = $('#logo');
	brilliant = $('#brilliant');
	menu = $("#menu");
	menu_container = $("#menu_container");
	menu_small = $("#menu_small");
	wrapper = $("#wrapper");
	content = $("#content");
	contentBG = $("#contentBG");
	menu.hide();
	menu_small.hide();
	black.hide();
	contentBG.hide();
	
		
	menu.find("a").each(
		function(init, objImg) {
			var icon = $(this);
			var iconID = icon.parent("li").attr("id");
			var iconHref = icon.attr("href");
			if (iconHref != 'login.html') {
				icon.bind("click",{elementID : iconID, href : iconHref}, centerIcon);
			}
		}
	);

	menu.find("a:first").unbind();
/* 	menu.find("a:eq(2)").unbind(); */
/*	menu.find("a:eq(3)").unbind(); */
/* 	menu.find("a:eq(5)").unbind(); */

	
	menu_small.find("a").each(
		function(init, objImg) {
			var icon = $(this);
			var iconID = icon.parent("li").attr("id");
			var iconHref = icon.attr("href");
			if (iconHref != 'login.html') {
				icon.bind("click",{elementID : iconID, href : iconHref}, loadContentQueue);
			}
		}
	);	
	
	menu_small.find("a:first").unbind().bind("click", showMenu);
/* 	menu_small.find("a:eq(2)").unbind(); */
/*	menu_small.find("a:eq(3)").unbind(); */
/* 	menu_small.find("a:eq(5)").unbind(); */
	
	
	logo.bind("click", showMenu);
	brilliant.bind("click", showMenu);
	menu.find("a:first").bind("click", showMenu);
	$('#imprint a').bind("click", {elementID : "n-nav0", href : "impressum.html"}, loadContentQueue);	
	
/* 	menu.find("a:eq(2)").trigger("click"); */

	$('#fernseher a').live("click", function(event) {
		event.preventDefault();
		readyToShowContent = true;
		$('#left').fadeOut("slow", function() {
			wrapper.addClass("loading");
			$('#right').animate({"width": "330px"},"fast");
			$('#left').load('video.html', function() {
				wrapper.removeClass("loading");
				$('#left').fadeIn('slow');
			});			
		});	
		
	});
	
/* 	setInterval(brilliantAnim, 5000); */
	
	
}

/** Brilliant-Animation **/
function brilliantAnim() {
	brilliant.jani({ frameWidth: 71, frameHeight: 72, speed: 100, totalFrames: 31, loop: false });
	brilliant.jani.play();
}

function backgroundFade() {
	
	var img = new Image();

	$(img).load(function () {
		//$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
		$(this).hide();
		loader.removeClass('loading').append(this);
		$(this).fadeIn(1500, function() {
			menu.fadeIn(1000, function() {
				contentBG.css({"opacity" : "0.7"}).fadeIn(1000);
				$('#skypedetectionswf').hide();
			})
		});
		
	}).error(function () {
		// notify the user that the image could not be loaded
	}).attr('src', 'images/ct.jpg');
	
	
}



function armFade() {
	
	var img = new Image();
	$(img).load(function () {
		//$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
		$(this).hide();
		arm.removeClass('loading').append(this);
	}).error(function () {
		// notify the user that the image could not be loaded
	}).attr('src', 'images/arm.png');
	
	
}

/*
function clickIcon(event) {
	activeMenu = event.data.elementID.substring(2);
	var href = event.data.href;
	body.attr({id : activeMenu});
	$.log(activeMenu, "-> ActiveMenu");
	fadeOutMenuQueue();
	wrapper.parent().nextAll().first().hide();

	loadContentQueue(href);
	event.preventDefault();
	
	menu_small.bind("mouseover", showMenu);
}
*/


function showMenu(event) {
	event.preventDefault();
	menu.css({"left" : "0px"});
	body.attr({id : "nav1"});	
	fadeInMenuQueue();
	readyToShowContent = true;
	loadContentQueue("home.html");
}

function centerIcon(event) {
	event.preventDefault();
	
	var href = event.data.href;
	activeMenu = event.data.elementID.substring(2);
	body.attr({id : activeMenu});
	$.log(activeMenu, "-> ActiveMenu");

	var moveX;
	var hitArea = $(event.currentTarget);
	var hitAreaOffset = hitArea.offset();
	var hitAreaLeft = hitAreaOffset.left + hitArea.width()/2;
	var windowtWidth = $(window).width();
	
	
	$.log(hitArea, "HitArea");

	if (hitAreaLeft < windowtWidth/2-30) {
		moveX = windowtWidth/2 - hitAreaLeft - 30;
	} else {
		moveX = (hitAreaLeft - windowtWidth/2 + 30) *-1;	
	}
		
	if (moveX != 0) {
/* 		iconIsAnimate = true; */
		var speed = Math.sqrt(Math.abs(moveX*1000));

		menu.animate({left:"+="+moveX},speed, function() {
/* 			iconIsAnimate = false; */
			$('#arm img').fadeIn(500, function() {
				fadeOutMenuQueue();
				loadContentQueue(href);
			})
		});
	} else {
 		fadeOutMenuQueue();
		loadContentQueue(href);
	}
}


	
/** Actions **/	

function fadeInMenu() {
	menu.fadeIn(1000);
}

function fadeInMenuSmall() {
	menu_small.fadeIn(3000);
}

function fadeOutMenu() {	
	menu.fadeOut("slow");
}

function fadeOutMenuSmall() {	
	menu_small.fadeOut(400);
}

function fadeInBlack(time) {
	black.css({"opacity" : "0.7"}).fadeIn(time);
}

function fadeOutBlack() {
	black.fadeOut("slow");
}

function fadeOutContent() {
	content.fadeOut("slow");
}

function fadeOutArm() {
	$('#arm img').fadeOut("slow");
}


var readyToShowContent = false;
function fadeInContent() {
/* 	$.log(readyToShowContent, "-> ReadyToShow"); */
	if (readyToShowContent) {
		content.fadeIn("slow");
		readyToShowContent = false;
	} else {
		readyToShowContent = true;
	}
}


/* QUEUES */

function loadContentQueue(data) {
	var href;
	if (typeof data == "object") {
		var event = data;
		data.preventDefault();
		href = event.data.href;
		var activeMenu = event.data.elementID.substring(2);
		body.attr({id : activeMenu});
		readyToShowContent = true;
		$.log(href, "Href");
	} else {
		href = data;
	}
	theQueue.queue("loadContent", function(next) {
	$.log(content, "-> Content");
		content.fadeOut("slow", function() {
			wrapper.addClass("loading");
			content.load(href, function() {
				wrapper.removeClass("loading");
				fadeInContent();
			});			
		});
		next();	
	});	
	
	theQueue.queue("loadContent", function(next) {

	
	});	
	
	
	theQueue.dequeue("loadContent");
}

// Queue for fading out menu
function fadeOutMenuQueue() {
	theQueue.queue("fadeOutMenu", function(next) {
		$.log(theQueue,"-> Queue");										
		fadeOutMenu();
		fadeOutArm();
		next();
	});	
		
	theQueue.queue("fadeOutMenu", function(next) {
		fadeInBlack(1000);
		next();
	});
	
	theQueue.delay(1000, "fadeOutMenu");
	
	theQueue.queue("fadeOutMenu", function(next) {
		menu_container.slideUp(400);
		fadeInMenuSmall();
		next();
	});
	
	theQueue.delay(400, "fadeOutMenu");
	
	theQueue.queue("fadeOutMenu", function(next) {
		fadeInContent();
	});	
	
	theQueue.dequeue("fadeOutMenu");
}


// Queue for fading in menu
function fadeInMenuQueue() {	
	theQueue.queue("fadeInMenu", function(next) {
		fadeOutMenuSmall();
		next();
	});
	
	theQueue.queue("fadeInMenu", function(next) {
		fadeOutBlack();
		next();
	});		

	theQueue.queue("fadeInMenu", function(next) {
 		menu_container.slideDown(400);
 		body.css({"overflow-y" : "hidden"});
 		fadeOutContent();
		next();
	});

	theQueue.delay(400, "fadeInMenu");
	
	theQueue.queue("fadeInMenu", function(next) {
		fadeInMenu();
		next();
	});	
	
	theQueue.delay(400, "fadeInMenu");	

	theQueue.queue("fadeInMenu", function(next) {
 		body.css({"overflow-y" : "visible"});
	});	
	
	theQueue.dequeue("fadeInMenu");
}




(function($){
	$.fn.idle = function(time) { 
      var o = $(this); 
      o.queue(function()
      { 
         setTimeout(function() { 
            o.dequeue();
	     }, time);
      });
  };
})(jQuery);
