var site = {
	/*
    *	Variables
    ------------------------------------------------------------*/
	_this : "",
	
	site_width : 960,
	content_height : "",
	applying_for : "",
	
	page : {
		main : "",
		sub : "",
		item_id : "",
		full_path : "",
		is_loading : "false"
	},
	
	
	/********************************************************************************************************
	*
	*	-- FUNCTIONS --
	*
	*********************************************************************************************************/

	/*
    *	Initialize navigation object
    ------------------------------------------------------------*/
	init : function() {
		// Set _this
		_this = this;

		// Get content height
		_this.get_content_height();
		
		// Position elements
		_this.position_header();
		_this.position_content();
		//_this.position_iframe();
		_this.position_menu();
		_this.position_background();
		
		// If hash is set, load page
		if(window.location.hash != "" && window.location.hash != "#")
		{
			_this.load_page(window.location.hash.replace("#", ""));
			_this.hide_background();
		}
	},
	
	/*
    *	Position header
    ------------------------------------------------------------*/
    position_header : function()
    {
    	var doc_width = $(document).width();
		var site_width = _this.site_width;
		var left = (doc_width - site_width) / 2;
		
		// Set values
		$("#header")
		.css("left", left+"px")
		.css("top", 20+"px");
    },
	
	/*
    *	Position content box
    ------------------------------------------------------------*/
	position_content : function() {
		// Calculate left
		var doc_width = $(document).width();
		var site_width = _this.site_width;
		var left = (doc_width - site_width) / 2;
		
		// Get header height
		var header_height = parseInt(($("#header").css("height")).replace("px", ""));
		var header_height = header_height + 40;
		
		// Set values
		$("#content")
		.css("left", left+"px")
		.css("top", header_height+"px")
		.css("height", _this.content_height+"px");
	},
	
	/*
    *	Position content box
    ------------------------------------------------------------*/
    position_iframe : function() {
    	// Calculate left
		var doc_width = $(document).width();
		var site_width = _this.site_width;
		var left = (doc_width - site_width) / 2;
		
		// Get header height
		var header_height = parseInt(($("#header").css("height")).replace("px", ""));
		var header_height = header_height + 40;
		
    	// Set values
		$("#iframe")
		.css("left", left+"px")
		.css("top", header_height+"px")
		.css("height", _this.content_height+"px");
    },
	
	/*
    *	Position background
    ------------------------------------------------------------*/
	position_background : function() {
		if($.browser.msie && $.browser.version == "7.0")
		{
		
		}
		else
		{
    		// Set width and height of background - 100%
			$("#background").css("width", $(document).width()+"px");
			$("#background").css("height", $(document).height()+"px");
			$("#background").css("display", "block");
		}
	},
	
	/*
    *	Position menu
    ------------------------------------------------------------*/
	position_menu : function() {
		var doc_width = $(document).width();
		if ($.browser.msie) 
		{
			var menu_width = 965;
		}
		else
		{
			var menu_width = parseInt(($("#menu").css("width")).replace("px", ""));
		}
		var left = (doc_width - menu_width) / 2;
		
		
		$("#menu").css("left", left+"px");
		
		// IE7
		if($.browser.msie && $.browser.version == "7.0")
		{
			var top = _this.content_height + $("#header").height() + 60;
			$("#menu").css("left", left+"px").css("top", top+"px");
		}
	},
	
	/*
    *	Get content height
    ------------------------------------------------------------*/
	get_content_height : function() {
		// Get header height
		var header_height = parseInt(($("#header").css("height")).replace("px", ""));
		var header_height = header_height + 40;
		
		// Get menu height
		var menu_height = 55;
		
		// Calculate height
		var doc_height = $(document).height();
		_this.content_height = doc_height - header_height - menu_height - 20;
	},
	
	show_background : function() {
		$("#background").css("display", "block");
	},
	
	hide_background : function() {
		$("#background").css("display", "none");
	},
	
	/*
    *	Load a page
    ------------------------------------------------------------*/
	load_page : function(page) {
		
		// Update page variables
		_this.set_page_vars(page);
		
		// Flag that a page is loading
		_this.page.is_loading = "true";
		
		// Show loading
		_this.show_loading();
				
		$.ajax({
			type: "GET",
			url: "views/"+_this.page.main+"_view.php",
			dataType: "html",
			error: function(){
				alert("Unable to load page! (main)");
			},
			success: function(new_page){
				//title = $("a[href='"+doc+"']").attr("title");
				
				_this.page.full_path = page;
				
				// Set column height
				var height = _this.content_height - 41;
				var new_page = new_page.replace(/<div class="column">/g, '<div class="column" style="height:'+height+'px;">');
				// Set scroll-pane height
				var new_page = new_page.replace(/<div class="scroll-pane">/g, '<div class="scroll-pane" style="height:'+(height-35)+'px;">');
				
				// Hide loading
				_this.hide_loading();
				
				// Append content
				$("#content").html(new_page);
				
				// If set, load sub page
				if(_this.page.sub != "")
				{
					_this.load_sub_page("."+_this.page.main+"_content > .scroll-pane");
					
					$(".menu > a").removeClass("selected");
					$(".menu a[href="+_this.page.full_path+"]").addClass("selected");
				}
				else
				{
					// Init scroll-pane
					$(".scroll-pane").jScrollPane();
				}
				
				//document.title = _this.config.doc_title+" / "+title;
				window.location.hash = page;
				
				// Page is done loading
				_this.page.is_loading = "false";
			}
		});
	},
	
	/*
    *	Load a sub-page
    ------------------------------------------------------------*/
	load_sub_page : function(target) {
		// Flag that a page is loading
		_this.page.is_loading = "true";
		
		// Show loading
		$(".loading").css("display", "block");
		
		// Empty div
		$(target).empty();
		
		if(_this.page.main == "vacatures")
		{
			$("#content > #vacatures_wrapper > .column > .vacature_content > .scroll-pane").empty();
		}
		
		// swithcrule
		switch(_this.page.main)
		{
			case("vacatures"):
				if(_this.page.item_id == "")
				{
					// Set category name
					var category_name = $(".menu > a[href="+_this.page.full_path+"]").text();
					$(".vacature_content > h1 > .text").empty().append(category_name);

					var request_url = "views/vacatures/vacatures_list_view.php?category="+_this.page.sub;
				}
				else
				{
					// Set category name
					//vacancy_name = $(".vacancy_item > a[href="+_this.page.full_path+"]").text();
					$(".vacature_content > h1 > .text").empty().append("Vacature");

					var request_url = "views/vacatures/vacature_view.php?name="+_this.page.item_id;
				}
			break;
			
			case("news"):
				// Set news name
				var news_name = $(".menu > a[href="+_this.page.full_path+"]").text();
				$(".news_content > h1 > .text").empty().append(news_name);

				var request_url = "views/news/news_item_view.php?item="+_this.page.sub;
			break;
			
			default:
				var request_url = "views/"+_this.page.main+"/"+_this.page.sub+"_view.php";
		}

		$.ajax({
			type: "GET",
			url: request_url,
			dataType: "html",
			error: function(){
				alert("Unable to load page! (sub)");
			},
			success: function(new_sub_page){
				// Show loading
				$(".loading").css("display", "none");

				// Add content
				$(target).html(new_sub_page);
				
				// Needed on vacatures to auto-load content 
				// on page refresh for some fucking reason!
				if(_this.page.main == "vacatures")
				{
					$("#content > #vacatures_wrapper > .column > .vacature_content > .scroll-pane").html(new_sub_page);
				}
				
				if(_this.page.main == "news")
				{
					$("#content > #news_wrapper > .column > .news_content > .scroll-pane").html(new_sub_page);
				}
					
				// Init scroll-pane
				$(".scroll-pane").jScrollPane();
				
				// Set hash
				window.location.hash = _this.page.full_path;
				
				// Page is done loading
				_this.page.is_loading = "false";
			}
		});

	},
	
	/*
    *	Set page variables
    ------------------------------------------------------------*/
    set_page_vars : function(page) {
    	// Update current page variables
		exp = page.split("/");
		
		_this.page.full_path = page;
		
		switch(exp.length)
		{
			// Single page
			case(1):
				_this.page.main = exp[0];	// main
				_this.page.sub = "";		// sub
				_this.page.item_id = "";	// item_id
			break;
			
			// Sub page
			case(2):
				_this.page.main = exp[0];	// main
				_this.page.sub = exp[1];	// sub
				_this.page.item_id = "";	// item_id
			break;
			
			// Sub page with item_id	
			case(3):
				_this.page.main = exp[0];	// main
				_this.page.sub = exp[1];	// sub
				_this.page.item_id = exp[2];	// item_id
			break;
			
			default:
				// nothing
		}
    },
	
	/*
    *	Show loading for main content
    ------------------------------------------------------------*/
	show_loading : function() {		
		var top = $("#content").css("top").replace("px", "") - 20;
		var left = _this.site_width + (($(document).width() - _this.site_width) / 2) - $("#loading").width();
		
		$("#loading").css("top", top+"px").css("left", left+"px").css("display", "block");
	},
	
	/*
    *	Hide loading for main content
    ------------------------------------------------------------*/
	hide_loading : function() {
		$("#loading").css("display", "none");
	},
	
	
	/********************************************************************************************************
	*
	*	-- Vacatures functions --
	*
	*********************************************************************************************************/
	
	load_vacancy_category : function() {
		$.ajax({
			type: "GET",
			url: "views/vacatures/vacatures_list_view.php?category="+_this.page.sub,
			dataType: "html",
			error: function(){
				alert("Unable to load page!");
			},
			success: function(new_sub_page){
				// Show loading
				$(".menu > .loading").css("display", "none");
				
				// Add content
				$(target).html(new_sub_page);
				
				// Init scroll-pane
				$(".scroll-pane").jScrollPane();
				
				// Set hash
				window.location.hash = _this.page.full_path;
			}
		});
	}
};
