var initVar = false
var baseUrl = "http://" + top.location.host.toString();

$(document).ready(init);
$(window).load(init);


function init()
{
  if(true == initVar)
    return;
  initVar = true;
  $(".archive.menu a").each(function(c,e){
  	$(e).html('Archiv '+$(e).html());
  });
  
  initGallery();

	$("#popular a > img").each(function(c,e){
		var cur = $(e);
		if(cur.height() < 90)
			fixHeight(cur);
	});

}

function initGallery()
{
	if($("#article").length != 1 || $("#article .ngg-galleryoverview").length != 1 )
	{
		$("#gallery").remove();
		return;
	}
	$("#article .ngg-galleryoverview").clone().appendTo("#gallery");
	$("#article .ngg-galleryoverview").remove();
	
	var container = $("#gallery .ngg-galleryoverview");
	var imgCount = container.children(".ngg-gallery-thumbnail-box").length;
	var pageCount = Math.ceil(imgCount/2);
	
	container.width(pageCount*780);
	
	for(var i = 0; i < pageCount; i++)
	{
		if(pageCount < 2)
			return;
		var entry = $(document.createElement('div'));
		if(i == 0)
			entry.addClass("active");
		entry.attr("page",i);
		entry.addClass("control");
		entry.html(i+1);
		entry.click(clickGalleryControl);
		entry.appendTo("#galleryControl");
	}
}


function fixHeight(cur)
{
	var curHeight = cur.height();
	var factor = 90/curHeight;
	cur.width(cur.width()*factor);
	cur.height(90);
	cur.parent().css("margin-left",-(cur.width()/4));
}

function clickGalleryControl()
{
	$("#galleryControl .control").removeClass("active");
	setGallery($(this).attr("page"));
	$(this).addClass("active");
}

function setGallery(page)
{
	$("#gallery .ngg-galleryoverview").animate({"left":(-(page*780))});
}

