var statics = {
	BODY 				: $("#body"),
	CONTAINER 			: $(".containerMovie"),
	HEADER 				: $("#header"),
	AMOUNT_DIRECTORS 	: 8
};

var floats = {
	
	divWidth : 0.0,
	numCols  : 0.0,
	margin   : 0.0
	
}

var rendering = {
	
	divWidth : (statics.BODY.width()),
	numCols  : (floats.divWidth / (239+5)),
	margin   : (((floats.divWidth - (Math.floor(floats.numCols) * 239)) / (floats.numCols)) / 2),
	
	/**
	 * Initializes window event listeners.
	 *
	 */
	init : function() {
		
		// bind rendering to resize
		$(window).bind("resize",rendering.resizeHandler);
		rendering.resizeHandler($(window));
		
	},
	
	/**
	 * Handles resize event from window-object
	 *
	 * @param sender	Window objects
	 */
	resizeHandler : function(sender) {
		
		floats.divWidth 	= statics.BODY.width();
		floats.numCols 		= (floats.divWidth / (239+5));
		floats.margin 		= ((floats.divWidth - (Math.floor(floats.numCols) * 239)) / (floats.numCols)) / 2;
		
		//-------------- aligned header -------------------
		//statics.CONTAINER.css("margin",      '5px ' + floats.margin + 'px');
		//statics.HEADER.css   ("width",       ((statics.BODY.width()-floats.margin*3)+floats.margin/floats.numCols) + "px");
		//statics.HEADER.css   ("margin-left", floats.margin + "px");
		//-------------------------------------------------
		
		//-------------- aligned content ------------------
		statics.BODY.css("padding-left",Math.floor(floats.margin) + "px");
		statics.CONTAINER.css("margin",'5px ' + Math.floor(floats.margin) + 'px');
		//-------------------------------------------------
	}
	
};


var directors = {
	
	'#Filip_Nilsson'	: '#dir1',
	'#Johan_Tappert'	: '#dir2',
	'#Tobias_Bergman'	: '#dir3',
	'#Ruben_Ostlund'	: '#dir4',
	'#Andreas_Nilsson'	: '#dir5',
	'#Pekka_Hara'		: '#dir7',
	'#Motionblur'		: '#dir6'

};


var folke = {
	
	init : function() {
		
		// Jquery PNG fix for IE
		// $(document).pngFix();
		
		// goes through the menu, and adds different classes on each li a
		/* $("#mainMenu li a").each(function (j) {
			j = j+1;
			$(this).addClass("color"+j);
		});*/	
		for(var j = 1;j < statics.AMOUNT_DIRECTORS; j++){
			$("#dir" + j).addClass("color"+j);
		}
		
				
		// Attach events and classes to menu items.
		for(var i = 1;i < statics.AMOUNT_DIRECTORS; i++){
			$("#dir" + i).addClass("active");
			$("#dir" + i).bind("click",folke.clearFilterExcept);
		}
		
		// Menu graphics (css) toggles
		$("#mainMenu li a").toggle(function(){
			$(this).addClass("active");
			}, function () {
			$(this).removeClass("active");
		});
	
		// Movie container
		// Hide all the titles
		$(".containerMovie h2").hide();
		// Mouse over effect
		$(".containerMovie").mouseover(function(){
												
		  $("h2:first",this).show();
		  $(".title",this).addClass("active");
		  
		}).mouseout(function(){
			
		 $("h2:first",this).hide();
		 $(".title",this).removeClass("active");
		 
		});
		
		
		
		// Generates overlay for movies
		$(function() {
			// if the function argument is given to overlay, it is assumed to be the onBeforeLoad event listener
			$("a[rel]").overlay(function() {
		 		
				// grab wrapper element inside content
				var wrap = this.getContent().find("div.wrap");
		 
				// load only for the first time it is opened
				wrap.load(this.getTrigger().attr("href"));

			});
			
		});
		
		
		// Check if anchor is set for directors
		if (self.document.location.hash != '') {
			
			var anchorValue = self.document.location.hash;
			
			for ( keyVar in directors ) {
			   // alert(directors[keyVar]);
			   if (keyVar == anchorValue) {
					$(directors[keyVar]) .click();  
			   }
			}
			
		}

				
	}, // end init function
	
	store:0,
	temp:-1,
	clearFilterExcept: function(evt) {
		evt.stopImmediatePropagation();
		var index = Number($(this)[0].id.substr(3));
		folke.store = index;
		for(var i = 1;i < statics.AMOUNT_DIRECTORS; i++){
			//$("#dir" + i).unbind("click",folke.clearFilterExcept);
			//$("#dir" + i).bind("click",folke.toggleMenuItem);
			if(i != index){
				$(String(".dir" + i)).hide("slow");
				$("#dir" + i).removeClass("active");
				// console.log("not active index: " + i);
			}else{
				$("#dir" + i).addClass("active");
				$(String(".dir" + i)).show("slow");
				// console.log("active index: " + i);
			}
			
		}
		
	},
	
	// menu item click
	toggleMenuItem : function (sender) {
		// console.log('sender: '+sender);
		var index = Number($(this)[0].id.substr(3));
		if(index == folke.store){
			$("#dir" + index).removeClass("active");
			folke.temp = folke.store;
			folke.store = -1;
		}else if(index == folke.temp){
			$("#dir" + index).addClass("active");
			folke.store = folke.temp;
			folke.temp = -1;
		}
		$(".dir" + index).toggle("slow");
	}

}; // end folke


// Firebug tracing
function logObject(obj){
	for(var param in obj){
		console.log(param + ": " + obj[param]);
	}
}


// When Document ready
$(document).ready(function() {
	folke.init();
	rendering.init();
});
