var CURIMAGE = 0;

$(document).ready(
	function()
	{
		bindProjecten();
		
		if ($.browser.version.charAt(0) < 7 && $.browser.msie){$("#mainnav li, #servicelinks li").hover(function(){$(this).addClass('hover');},function(){$(this).removeClass('hover');});}
		if ($.browser.version.charAt(0) < 7 && $.browser.msie){$("#mainnav li.home").hover(function(){$(this).addClass('hover-home');},function(){$(this).removeClass('hover-home');});}
		
        rotatingHeader();
        rotatingHeaderHover();
        
        /*
        if ($("body").attr("id") == 'home')
		{
			setTimeout("bindHome()", 5000);
		}
        */
	}
);

function bindHome()
{
	$("#content-container .home"+CURIMAGE).css('z-index', 10).fadeOut(2000);
	CURIMAGE++;
	if (CURIMAGE > 4)
	{
		CURIMAGE = 0;
	}
	$("#content-container .home"+CURIMAGE).css('z-index', 20).fadeIn(3000);

	setTimeout("bindHome()", 6000);
}

function bindProjecten()
{
	var iWidth = $(document).width();
	var iHeight = $(document).height();
	
	var iLeftModal;
	var iTopModal;
	
	iLeftModal = Math.round((iWidth / 2) - 310);
	iTopModal = 20;
	
	$(".project-overview a, .nieuwsimg").click(
		function()
		{
			var div = $(this).parent();
			var ul = div.find("ul");
			
			// Eerst even controleren of er foto's bij het project zitten
			
			if (ul.find("li").length > 0)
			{
			// De titel van het project overnemen naar de modal
				var titel = div.find(".news-overview-title").text();	
				$("#modal #modal-title h2").text(titel);
				
			// De lijst met thumbnails overnemen naar de modal
				$("#modal #modal-thumbs ul").empty().append(ul.html());
				
			// De eerste foto uit de lijst groot weergeven
				var first = $("#modal #modal-thumbs ul li:first").addClass('active');
				showModalFoto();
	
			// Thumbnails clickbaar maken
				bindThumbs();
			// Modal weergeven
				$("#modal-shadow").height(iHeight).width(iWidth).show();
				$("#modal").css("top", iTopModal).css("left", iLeftModal).show();
			}
			return false;
		}
	);
	
	bindModal();
	bindModalPrevNext();

}

function bindThumbs()
{
	$("#modal #modal-thumbs ul a").click(
			function()
			{
				$("#modal #modal-thumbs ul li.active").removeClass("active");
				$(this).parent().addClass('active');
				
				showModalFoto();			
				
				return false;
			}
	);
}

function bindModalPrevNext()
{
	$("#modal .prev").click(
		function()
		{
			var cur = $("#modal #modal-thumbs ul li.active");
			var nxt = cur.prev("li");
			
			if (nxt.length == 0)
			{
				nxt = $("#modal #modal-thumbs ul li:last");
			}
			
			cur.removeClass('active');
			nxt.addClass('active');
			
			showModalFoto();
			
			return false;
		}
	);
	
	$("#modal .next").click(
			function()
			{
				var cur = $("#modal #modal-thumbs ul li.active");
				var nxt = cur.next("li")
				
				if (nxt.length == 0)
				{
					nxt = $("#modal #modal-thumbs ul li:first");
				}
				
				cur.removeClass('active');
				nxt.addClass('active');
				
				showModalFoto();
				
				return false;
			}
		);
	
}

function showModalFoto()
{
	var src = $("#modal #modal-thumbs ul li.active a").attr("href");
	var img = $("<img>");
	
	img.attr("src", src);
	
	$("#modal #modal-content").empty().append(img);
}

function bindModal()
{
	$("#modal .modal-close").click(
			function()
			{
					$("#modal").hide();
					$("#modal-shadow").hide();
			}
	);
}

























/* Rotate header
**************************/
function rotatingHeader()
{
    if($("#carrousel-thumbs li.active").length == 0)
    {
        var iIndex = 0;
    }else{
        var iIndex = $("#carrousel-thumbs li.active").attr("id").replace("thumbsID","");
        
        if (iIndex >= 0)
        {
            iIndex++;
 
            if (iIndex >= $("#carrousel-thumbs li").size())
            {
               var iIndex = 0;
            }
        }
    }
      
    wisselHeader(iIndex);
}

function rotatingHeaderHover()
{
	$("#carrousel-thumbs li").click(
		function()
		{
            curID   = $("#carrousel-thumbs li.active").attr("id").replace("thumbsID","");
            id      = $(this).attr("id").replace("thumbsID","");
            if(curID != id)
            {
                clearTimeout(headerTimer);
                wisselHeader(id);              
            }
		}
	);
}

function wisselHeader(iIndex)
{          
    console.log($("#bigImage"+iIndex).attr("alt"));
    $(".header-slogan").fadeOut(500,function(){
        $(".header-slogan").html($("#bigImage"+iIndex).attr("alt"));
    });
    
    
    $(".image-content").fadeOut(1000);
  
    $("#bigImage"+iIndex).fadeIn(1000);

    
    
    $(".header-slogan").fadeIn(500);
    $("#home"+iIndex).css('display','block');
    
    $("#carrousel-thumbs li.active").removeClass("active");
    $("#thumbsID"+iIndex).addClass('active');
    
    
            
    headerTimer = setTimeout("rotatingHeader()", 5000);	
 
}
