var slider, slideSpeed = 1000, si = 0, osi = -1, slide = $("#slider-container .slider-item"), sControl = $("#slider-nav li"), sliding = false, timer,
pause = false;


/////////////////
/*Slider Object*/
/////////////////

var slider = 
{
	prev : function ()
	{
		this.clear_timer();
		this.set_active();
		slider.set_timer();
		$(slide[si]).animate({ top: "-400px" }, 0).show();
		$(slide[si]).animate({ top: 0 }, slideSpeed);
		$(slide[osi]).animate({ top: "400px" }, slideSpeed, function ()
		{
			$(this).hide();
			sliding = false;
		});
	},
	
	next : function ()
	{
		this.clear_timer();
		this.set_active();
		slider.set_timer();
		$(slide[si]).animate({ top: "400px" }, 0).show();
		$(slide[si]).animate({ top: 0 }, slideSpeed);
		$(slide[osi]).animate({ top: "-400px" }, slideSpeed, function ()
		{
			$(this).hide();
			sliding = false;
		});
	},
	
	set_active : function ()
	{
		sControl.removeClass("active");
		$(sControl[si]).addClass("active");
	},
	
	set_timer : function ()
	{
		timer = setTimeout(function ()
		{
			if (pause) return false;
			osi = si;
			((parseFloat(si) + 1) == slide.length) ? si = 0 : si = parseFloat(si) + 1;
			slider.next();
		}, 5000);
	},
	
	clear_timer : function ()
	{
		clearTimeout(timer);
	},
	
	trigger : function (ev, obj)
	{
		ev.preventDefault();
		if (sliding == true) return false;
		sliding = true;
		osi = si;
		si = $(obj).parent().index();
		if (osi == si) 
		{
			sliding = false;
			return false;
		}
		(osi < si) ? this.next() : this.prev();
	}
}



////////////////
/*Cufon Object*/
////////////////

var cufon = 
{
	init : function ()
	{
		Cufon.replace("h1", { hover: true });
		Cufon.replace(".h1", { hover: true });
		Cufon.replace("h2", { hover: true });
		Cufon.replace(".btn", { hover: true });
		Cufon.replace(".mssans", { hover: true });
		Cufon.replace(".highlight", { hover: true });
	}
}


////////////
/*Lightbox*/
////////////

var lightbox =
{
	oldIndex : 0,
	index : false,
	element : $("#detail-images a"),
	img : false,
	holder : $("#lb"),
	animationSpeed : 500,
	
	init : function (ev, obj)
	{
		ev.preventDefault();
		$("body").append('<div id="lb-container"><div id="lb"><a class="lb-close"></a><div id="lb-holder"></div><div id="lb-control"><a class="lb-prev"></a><a class="lb-next"></a></a></div></div>');
		$.each(this.element, function (i)
		{
			$("#lb-holder").append('<img src="'+$(lightbox.element[i]).attr("href")+'" alt="">');
		});
		$("#lb-container").fadeIn(1000);
		$("#lb-holder img").hide();
		this.index = $(obj).index();
		this.check_if_loaded();
		/*$("#lb").hover(function ()
		{
			$("#lb-control").fadeIn(500);
		}, function ()
		{
			$("#lb-control").fadeOut(1500);
		});*/
	},
	
	check_if_loaded : function ()
	{
		this.img = new Image();
		$(this.img).load(function ()
		{
			lightbox.resize_holder();
		}).attr("src", $(this.element[this.index]).attr("href"));
	},
	
	resize_holder : function ()
	{
		$("#lb-holder img:eq("+this.oldIndex+")").fadeOut(this.animationSpeed, function ()
		{
			$("#lb").animate(
			{
				width: lightbox.img.width,
				height: lightbox.img.height,
				marginLeft: "-" + ((lightbox.img.width / 2) + 25),
				marginTop: "-" + ((lightbox.img.height / 2) + 25),
			}, 1000, function ()
			{
				$("#lb-holder img:eq("+lightbox.index+")").fadeIn(lightbox.animationSpeed);
			});
		});
	},
	
	close : function ()
	{
		$("#lb-container").fadeOut(this.animationSpeed, function ()
		{
			$(this).remove();
		});
	},
	
	next : function ()
	{
		this.oldIndex = this.index;
		((this.index + 1) != this.element.length) ? this.index = this.oldIndex + 1 : this.index = 0;
		this.check_if_loaded();
	},
	
	prev : function ()
	{
		this.oldIndex = this.index;
		(this.index == 0) ? this.index = this.element.length - 1 : this.index = this.oldIndex - 1;
		this.check_if_loaded();
	}
}


////////////////////////
/*Misc Functies Object*/
////////////////////////

var misc = 
{
	scrollTo : function (ev, obj)
	{	
		ev.preventDefault();
		var scrollSpeed = 800;
		var id = $(obj).attr("href");
		$("html, body").animate({
			scrollTop: $(id).offset().top
		}, scrollSpeed);
	},
	
	equil_height : function (obj, find)
	{
		var arr = $(obj).find(find);
		$.each(arr, function (i)
		{
			if (i % 2 == 1) 
			{
				if ($(arr[i]).height() > $(arr[i]).prev().height()) 
				{ 
					$(arr[i]).prev().css("height", $(arr[i]).height());
					$(arr[i]).css("height", $(arr[i]).height());
				}
				else
				{
					$(arr[i]).css("height", $(arr[i]).prev().height());
					$(arr[i]).prev().css("height", $(arr[i]).prev().height());
				}
			}
		});
	}
}


////////////
/*Triggers*/
////////////

$("#slider-nav, #slider-container").hover(function ()
{
	pause = true;
	slider.clear_timer();
}, function ()
{
	slider.set_timer();
	pause = false;
});

$("#detail-images a").click(function (ev) { lightbox.init(ev, this); });
$("#lb-container").live("click", function (ev) { lightbox.close(ev, this); });
$(".lb-close").live("click", function (ev) { lightbox.close(ev, this); });
$(".lb-next").live("click", function (ev) { lightbox.next(ev, this); });
$(".lb-prev").live("click", function (ev) { lightbox.prev(ev, this); });


sControl.find("a").click(function (ev) { slider.trigger(ev, this); });
$(".back-to-top").click(function (ev) { misc.scrollTo(ev, this); });
$("#article-anchor a").click(function (ev) { misc.scrollTo(ev, this); });

///////////////
/*Dom geladen*/
///////////////

$(document).ready(function()
{
	cufon.init();
	$.each($("#categorie li"), function (i) { if (i % 4 == 3) $(this).addClass("last"); });
	//$("#sub-nav").find(".active").parent().removeClass("close");
	//$("#sub-nav").find(".active").parent().parent().parent().removeClass("close");
});


$(window).load(function ()
{
	misc.equil_height("#vertegenwoordigers", "li");
	slider.next();
	
	$("#sub-nav").find(".active").each(function(i)
	{
		var new_height = $(this).find("a").outerHeight(true);
		$(this).find("span").css({ height: new_height });
	});
});
