var myScroll;
var imagesAmount = 0;
var imagesLoaded = 0;
var imagesWidth = 0;
var firstLoad = true;

$(document).ready(onReady);

function onReady() {
	addResizeListener();
	SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
}
	
function makeLayout(){
	imagesAmount = 0;
	imagesLoaded = 0;
	imagesWidth = 0;
	positionFooter();
	if($("#scroller").length) {  
		$("#logo").addClass("invisible");
		$("#thelist > li > img").each(
			function(){
				!$(this).hide();
				if(!$(this).width()){
					$(this).load(
						function(){
							imagesWidth += $(this).width() + 10;
							imagesLoaded++;
							checkIfAllImagesLoaded($(this));
						}
					);
				} else {
					imagesWidth += $(this).width() + 10;
					imagesLoaded++;
					checkIfAllImagesLoaded($(this));
				}
			}
		);
	} else {
		$("#logo").removeClass("invisible");
	}
	if($("#menu").length) { 
		$("#menu_container").hide();
		window.setTimeout('window.parent.adjustMenu()', 100);	
	}
	if($("#textContent").length) { 
		var pos = $("#info").offset().left;
		$("#header").css("left", pos - $("#logo").offset().left - 7);
		$("#header").css("text-align", "left");
		$("#textContent").css("margin", "0 0 0 "+pos+"px");
	}
	positionHeader();
}

function checkIfAllImagesLoaded(img){
	$("#scroller").width(imagesWidth + 10);
	img.show();
}

function handleChange(){
	makeLayout();
}

function adjustMenu(){
	$("#menu_container").show();
	var menuHeight = $("#menu > .right").height();
	var stylingHeight = $("#menu > .styling").height() + 44;
	if($("#menu > .film").length){
		$("#menu > .styling").height(stylingHeight);
		$("#menu > .styling").css("top", menuHeight - stylingHeight);
		$("#menu > .film").css("top", -menuHeight);
	} else {
		
		$("#menu > .left").height(menuHeight);
		console.log( "no film : ");
	}
	$("#menu").css("height",menuHeight+"px");
	$("#menu_container").css("height",menuHeight+"px");
	$("#menu_container").css("top","50%");	
	var target = menuHeight/-1;
	var target = -180;
	$("#menu_container").css("margin-top",target+"px");	
	positionHeader();
}

function positionHeader(){
	if($("#header").length){
		$("#header").css("margin-top", 0);
		var topAreaHeight;
		if($("#wrapper").length){
			topAreaHeight = $("#content").height() / 2 - 200;	
		}
		if($("#menu_container").length){
			topAreaHeight = ($("#menu_container").offset().top);
		}
		var target = topAreaHeight /2 - $("#header").height()/2;
		$("#header").css("margin-top", target);
		$("#header").delay(200).show(0);
	}
}

function positionFooter(){
	if($("#footer").length){
		$("#footer").show();
		dotFooter();
		$("#footer").css("margin-top", 0);
		var screenHeight = $("#content").height();
		var bottomAreaHeight = (screenHeight / 2) - 215;	
		var target = ((screenHeight / 2) + 200);
		target = target + bottomAreaHeight/2;
		target = target - $("#footer").height()/2;
		if($("#footer").offset().top != target){
			$("#footer").css("margin-top", target);
		}
	}		
}

function dotFooter(){
	var lastOffset = -1;
	$("#footer > ul > li").each(
		function(){
			if(lastOffset == $(this).offset().top){
				lastOffset = $(this).offset().top;
				$(this).addClass("middot");
			} else {
				lastOffset = $(this).offset().top;
			}
		}
	);
}

function addResizeListener(){
	$(window).resize(function(e){
		onResize();
	});
}

function onResize(){
	positionFooter();
	positionHeader();
}
