$(document).ready(
	function()
	{
		if ($.browser.version.charAt(0) < 7 && $.browser.msie){$("#mainnav li, #subnav li").hover(function(){$(this).addClass('hover');},function(){$(this).removeClass('hover');});}
		
        //doCollectie();
        imageRotator(-1);
        doLogos();
        //doLogoRotater();
        showModal();
        
        /*$("#content-rotator").bxSlider(
        	{
        		ticker:true,
        		tickerSpeed:800,
        		displaySlideQty:4
        	}
        )*/
	}
);


/*****************************
 * Collectie
 *****************************/
function doCollectie()
{
    //Bij gekozen subitem, menu wel weergeven.
    if($('.subnav li').hasClass('active'))
    {
        $('.collectie').parent().addClass('active');
        $('.subnav').css('visibility','visible');
        //makeLinks();
    }
    
    
    $('.collectie').click(function(){
        if($('.subnav').css('visibility') == 'visible')
        {
            $('.subnav').css('visibility','hidden');
           // repairLinks();
        }
        else
        {
            $('.subnav').css('visibility','visible'); 
          //  makeLinks();
        }
    });
}

function makeLinks()
{
	$("#layout-thumbs li a").removeClass("showmodal").each (
		function()
		{
			$(this).data("link", $(this).attr("href"));
		}
	)
	
	var iIndex = 1;
	
	$('.subnav li a:lt(3)').each(
		function()
		{
			$("#layout-thumbs li:eq("+iIndex+") a").unbind("click").attr("href", $(this).attr("href"));		
			
			iIndex++;
		}
	)
}

function repairLinks()
{
	showModal();
	$("#layout-thumbs li a").addClass("showmodal").each (
		function()
		{
			$(this).attr("href", $(this).data("link"));
		}
	)
}

/*****************************
 * Image rotator
 *****************************/
function imageRotator(iIndex)
{
    if (iIndex >= 0)
    {
        $(".content-image:nth-child(" + iIndex +")").fadeOut(1500);
        iIndex++;
        if (iIndex >= ($(".content-image").size() + 1))
        {
            iIndex = 1;
        }
    }else{
        iIndex = 1;
    }
        
    $(".content-image:nth-child(" + iIndex +")").fadeIn(1500);
    $(".content-image:nth-child(" + iIndex +")").css("display","block");
    
    if ($(".content-image").size() != 1)
    {
        setTimeout("imageRotator("+iIndex+")", 5000);    
    }
}


function doLogoRotater()
{
	var aantal = $("ul#content-rotator li").length;
	var breedte = 150;
	var delta = aantal * breedte;
	var tijd = 2000 * aantal;
	
	for (var i = 0; i < aantal; i++)
	{
		$("ul#content-rotator li:eq("+i+")").clone().appendTo("ul#content-rotator");
	}
	
	$("ul#content-rotator").animate({
		left: "-"+delta+"px"
	},
	tijd,
	'linear',
	function()
	{
//	alert('klaat');
		//doSwitch();
	});		
}

/*****************************
 * Animate logos.
 *****************************/
function doLogos()
{
	$("ul#content-rotator").animate({
		left: "-150px"
	},
	2000,
	'linear',
	function()
	{
		$("ul#content-rotator li:first").appendTo("ul#content-rotator");
		$("ul#content-rotator").css("left", "0px");
		
		doLogos();
	});	
}

function replaceLogos()
{
    $("ul#content-rotator li").each(
        function()
		{
			var doReplace = true;
            $(this).css("text-align", "center");
			
            
			$(this).find("img").load(
					function()
					{           
						
                        var img = $(this);
						doLogoReplace(img);  
                        doReplace == false;
					}
			);	
            
            if(doReplace == true)
            {
                img = $(this).find("img");
                doLogoReplace(img);    
            }
            	
            			
		}
	);
}

function doLogoReplace(img)
{
	var height = img.height();
	
	//console.log(height);
    //alert(Math.round(((200-height)/2)-10));
    var top = Math.round(((200-height)/2)-10);
    
	img.css("margin-top", top+"px");
}

function showModal()
{
	$(".showmodal").unbind("click");
    $(".showmodal").click(function(e)
    {
	    e.preventDefault();

   		$("#modal-content *").hide();    
   		
    	if ($(this).hasClass('img'))
    	{
			$("#modal-content img").attr("src", $(this).attr("href"));
			$("#modal-content img").show();
    	}
    	else if ($(this).hasClass('leesmeer'))
    	{
			$("#modal-content div.leesmeer").html($("#leesmeer").html());
			$("#modal-content div.leesmeer").show();
    	}
    	else if ($(this).hasClass('leesmeer_menu1'))
    	{
			$("#modal-content div.leesmeer").html($("#leesmeer_menu1").html());
			$("#modal-content div.leesmeer").show();
    	}
    	else if ($(this).hasClass('leesmeer_menu2'))
    	{
			$("#modal-content div.leesmeer").html($("#leesmeer_menu2").html());
			$("#modal-content div.leesmeer").show();
    	}    	
    	else if ($(this).hasClass('leesmeer_form'))
    	{
			$("#modal-content div.leesmeer").html($("#leesmeer_form").html());
			$("#modal-content div.leesmeer").show();
    	}                

        var iWidth          = $(document).width();
        var iHeight         = $(document).height();
        var iLeftModal      = (iWidth / 2) - 300;

        $("#modal-bg").height(iHeight).width(iWidth).show();
        $("#modal").css("left", iLeftModal).show();
        centerModal();
    });
    
    $("#modal-close").click(function(){
        $("#modal").hide();
        $("#modal-bg").hide();
        return false;
    });
} 

function centerModal()
{        
    $("#modal").css("position", "fixed");
    $("#modal").css("top", 20);
    var topPos = $("#modal").offset().top;
    $("#modal").css("top", topPos);
    $("#modal").css("position", "absolute");    
}


