$(document).ready(function () {
	
	
	/**
	 * Menu
	 */
	
	if (active_menu === true) {
	
		// Fetch the active
		var active = $("#menu li.active");
		var selected = false;
	
		// Set selected if active
		if (active.length == 1)
			selected = active;
		
		// Loop through every menu item
		$("#menu li.button").each (function () {
			
			// Mouse enter function
			$(this).mouseenter (function () {
				
				// Show the active item
				display_nav (this);
			});
		});
		
		// Register hide function
		$("#navigation").mouseleave(function () {
			
			// Check for selected
			if ($("a", active).attr("class") != $("a", selected).attr("class")) {
				
				// Hide menu
				$("#nav").stop().animate({top: '-122px'}, {duration: 300, complete: function () {
					
					// Check for active
					if (active.length != 0) {
						
						// Execute function
						display_nav (active, true);
						
						// Set selected
						selected = active;
					}
				}});
			}
		});
	}
	
	/**
	 * Slider navigation
	 */
	
	// Fetch some settings
	var items = $("#content .item, #content ul.resultlist, #content .block");
	var margin = parseInt($("#content").css("marginLeft"));
	
	// Previous
	$("a#previous").unbind('click').click(function () {
		
		var margin = parseInt($("#content").css("marginLeft"));
		
		// Check for margin
		if (margin < -15) {
				
			// Animate
			margin = margin + xoffset;
			$("#content").animate({marginLeft: margin}, duration, easing);
		}
	});
	
	// Next
	$("a#next").unbind('click').click(function () {
		
		var margin = parseInt($("#content").css("marginLeft"));
		
		// Calculate the max margin
		var max = 0 - ((xoffset * items.length) - (xoffset * 1.5));
		
		// Check for margin
		if (margin > max) {
			
			// Animate
			margin = margin - xoffset;
			
			$("#content").animate({marginLeft: margin}, duration, easing);
		}
	});
	
	/**
	 * Lightbox
	 */
	
	// Add HTML to page
	var html = ''
	+ '<div id="lightbox" class="lightbox">'
		+ '<div id="light_loading"></div>'
		+ '<div id="light_outer">'
			+ '<div id="light_inner"></div>'
			+ '<div id="light_controls">'
				+ '<div id="light_close"></div>'
				+ '<div id="light_larger"></div>'
				+ '<div id="light_prev"></div>'
				+ '<div id="light_next"></div>'
			+ '</div>'
		+ '</div>'
		+ '<div id="light_overlay"></div>'
	+ '</div>';
	
	// Append to body
	$(html).appendTo("body");
	
	// Register click functions
	$("#light_overlay").click(function () { $("#lightbox").hide(); });
	
	/**
	 * Navigation
	 */
	
	// Function to display menu navigation
	function display_nav (elem, original) {
		
		// Check if there is already a selected element.
		if (selected == false || $("a", elem).attr("class") != $("a", selected).attr("class")) {
			
			// Get the content by a post
			$.post("data/navigation.php", {page: $("a", elem).attr("class"), sections: $("span#sections").html(), head: $("span#url").html(), language: $("span#language").html()}, function (data, status) {
								
				// Check for data
				if (data != "0") {
					
					// Check for active
					if (active == elem && original != true) {
						
						// Set content
						$("#nav .content").html(data);
						
						// Animate
						$("#nav").css({top: '18px'});
						
					} else {
						
						// Hide the first
						$("#nav").stop().animate({top: '-122px'}, {duration: 700, complete: function () {
							
							// Load content
							$("#nav .content").html(data);
							
							// Show navigation bar
							$("#nav").animate({top: '18px'}, 700);
						}});
					}
					
				} else {
					
					// Hide nav
					$("#nav").stop().animate({top: '-122px'}, {duration: 700});
				}
					
				// Set selected
				selected = elem;
			});
		}
	};
	
});

function loadLightbox (file, element, type) {
		
	// Set element
	element = $("#" + element);
	
	// Load the content
	$.post(file, {"pid": $(element).attr("id")}, function (data, success) {
		
		// Add the data to the lightbox
		$("#light_inner").html(data);
		
		// Show the lightbox
		$('#lightbox').show();
		
		// Render the h1
		$(".lightbox h1").render(type);
	});
	
	// Unbind
	$("#light_larger, #light_close, #light_prev, #light_next").unbind("click");
	$("#light_larger, #light_prev, #light_next").hide();
	
	$("#small").click(function () { alert ("a"); });
	
	if ($("#small").length == 1) {
		
		// Register functions
		$("#light_larger").show().click(function () {
			
			// Fetch sources
			var small_src = $("#lightbox #small").attr("src");
			var small_rel = $("#lightbox #small").attr("rel");
			
			var large_src = $("#lightbox #large").attr("src");
			var large_rel = $("#lightbox #large").attr("rel");
			
			$("#lightbox #large").attr({"src": small_rel, "rel": small_src});
			$("#lightbox #small").attr({"src": large_rel, "rel": large_src});
		});
	}

	$("#light_close").click(function () { $("#lightbox").hide(); });
	
	if (typeof ($(element).prev().attr("id")) != "undefined") {
		
		// Show
		$("#light_prev").show();
		
		// Click
		$("#light_prev").click(function () { loadLightbox(file, $(element).prev().attr("id"), type); });
	}
	
	if (typeof ($(element).next().attr("id")) != "undefined") {
		
		// Show
		$("#light_next").show();
		
		// Click
		$("#light_next").click(function () { loadLightbox(file, $(element).next().attr("id"), type); });
	}
}

function setContentWidth() {
	
	var totalWidth = 0;
	
	$(".item").each( function () {
		totalWidth += parseInt($(this).width()) + 10;
	});
	
	if (parseInt($(window).width()) < totalWidth) { 
		$("#slider-container").css("display", "block");
	} else if (parseInt($(window).width()) > totalWidth) { 
		$("#slider-container").css("display", "none");
	}
	
	$("#content-scroll").width(parseInt($(window).width()) - 30 - 100);
	$("#content-holder").width(totalWidth);
}

/*
 * Javascript slider
 */	

// Click
function handleSliderChange(e, ui) {
	var maxScroll = parseInt($("#content-holder").width() - $("#content-scroll").width());
	var left = parseInt(ui.value * (maxScroll / 100));
	$('#content-scroll').animate({"scrollLeft": left}, 1000);
}

// Slide
function handleSliderSlide(e, ui) {
	var maxScroll = parseInt($("#content-holder").width() - $("#content-scroll").width());
	var left = parseInt(ui.value * (maxScroll / 100));
	$("#content-scroll").attr({"scrollLeft": left });
}
