$(function() {
	/* correct menu for safari */
	if ($.browser.safari) {
		$("#header_menu_wrapper li").css("text-indent", "-6px");
		$("#header_categories li:not(:first, :nth-child(6))").css("text-indent", "-6px");
		$("#teaser_grid span.category li").css("margin-left", "14px");
		$("#header_menu_wrapper div.menu").css("margin-left", "8px")
	}
	
	/* remove margin and bullet-point from element at the start of the second line -- change number in nth-child when you change category order + change in next function aswell */
	$("#header_categories ul li:nth-child(6)").css({'list-style-type' : 'none', 'margin-left' : '0'});
	
	if ($.browser.msie) {
		/* change here too */
		$("#header_categories ul li:nth-child(6)").addClass("fix_ie_4th").before("<br />");
	}
	
	var header_categories = $("#header_categories");
	$("#header_menu_wrapper .menu li:first").bind("mouseenter", function () {
			header_categories.slideDown(100);
		});
		
	$("#header_menu_wrapper h1").bind("mouseenter", function () {
		header_categories.slideUp(100);
	}).next(".menu li:not(:first)").bind("mouseenter", function () {
		header_categories.slideUp(100);
	});
		
	$("#header_mouseover").bind("mouseleave", function () {
		header_categories.slideUp(100);
	});	
		
	initImageBrowser();
	
	/* remove margins from elements to the left, currently multiples of 6 */
	$("#teaser_grid .posts_teaser:nth-child(6n+1)").css("margin-left", "0px");
	
	$("#teaser_grid div.posts_thumbnail").hover(
		function () {
			$(this).find("img").animate({opacity: 0.5}, 400);
		}, function () {
			$(this).find("img").animate({opacity: 1}, 200);
		});
});

function initImageBrowser () {
	var allImageBrowser = $("#content_entry .ngg-imagebrowser");
	
	allImageBrowser.each(function () {
		
		var thisNav = $(this).find(".ngg-imagebrowser-nav a");
		/* stupid safari hack */
		thisNav.css("display", "none");
		var thisCounter = $(this).find(".counter");
		
		if(!$.browser.msie){
			
			$(this).bind("mouseenter", function (e) {
				thisNav.fadeIn("slow");
				thisCounter.fadeIn("slow");
			});
			
			$(this).bind("mouseleave", function (e) {
				thisNav.fadeOut("slow");
				thisCounter.fadeOut("slow");
			});
			
			thisNav.parent().hover(function () {
				thisNav.fadeIn("slow");
				thisCounter.fadeIn("slow");
			}, function () {
				thisNav.fadeOut("slow");
				thisCounter.fadeOut("slow");
			});

		}else{
			$(this).bind("mouseenter", function (e) {
				thisNav.css("display", "block");
				thisCounter.css("display", "block");
			});
			
			$(this).bind("mouseleave", function (e) {
				thisNav.css("display", "none");
				thisCounter.css("display", "none");
			});
		}
		
	});
	
	allImageBrowser.find(".ngg-browser-prev").bind("click", function (e) {
		e.preventDefault();
		imageBrowserShowPrev($(this));
	});
	
	allImageBrowser.find(".ngg-browser-next").bind("click", function (e) {
		e.preventDefault();
		imageBrowserShowNext($(this));
	});	
}

function imageBrowserShowNext(obj) {
	var curImageBrowser = $("#"+ obj.attr("rel"));	
	var currentPic = curImageBrowser.find(".pic.visible");
	if (!currentPic.hasClass("last")){
			var nextPic = currentPic.next();
		}else{
			var nextPic = curImageBrowser.find(".pic.first");
	}

	currentPic.fadeOut("slow", function () {
		nextPic.fadeIn("slow").addClass("visible");
		$(this).removeClass("visible");
		curImageBrowser.find(".counter").html(nextPic.attr("rel"));
	});
	
}

function imageBrowserShowPrev(obj) {
	var curImageBrowser = $("#"+ obj.attr("rel"));	
	var currentPic = curImageBrowser.find(".pic.visible");
	if (!currentPic.hasClass("first")){
			var nextPic = currentPic.prev();
		}else{
			var nextPic = curImageBrowser.find(".pic.last");
	}

	currentPic.fadeOut("slow", function () {
		nextPic.fadeIn("slow").addClass("visible");
		$(this).removeClass("visible");
		curImageBrowser.find(".counter").html(nextPic.attr("rel"));
	});
}