var CURIMAGE = 0;
var rotater = true;



$(document).ready(
	function()
	{
        //Acties      
        if($("#actie li").length > 1){
            changeItem("home","actie");
            bindNieuwsHome("home","actie");
            rotater = setTimeout("rotate('home','actie')", 5000);
        }else{
            changeItem("home","actie");
            $("#actie").css('display','none');             
        }

        //Nieuws 
        if($("#nieuws li").length > 1){
    		changeItem("home","nieuws");
            bindNieuwsHome("home","nieuws");
    		rotater = setTimeout("rotate('home','nieuws')", 5000);
        }else{
            changeItem("home","nieuws");
            $("#nieuws").css('display','none');
        }
        
        //Aanbiedingen
        if($("#aanbieding li").length > 1){
            changeItem("content","aanbieding");
            bindNieuwsHome("content","aanbieding");
    		rotater = setTimeout("rotate('content','aanbieding')", 5000);
        }else{
            changeItem("content","aanbieding");
            $("#aanbieding").css('display','none');
        }
        
        bindBusinessLeden();
	}
);


function bindNieuwsHome(locatie, item)
{        
    $("#" + item + " li").hover(
		function()
		{
			clearTimeout(rotater);
			rotater = setTimeout("rotate('" + locatie + "','" + item + "')", 5000);	

			$("#" + item + " li.active").removeClass("active");

			$(this).addClass("active");
			
            changeItem(locatie, item);
		}
	);
}



function rotate(locatie, item)
{
        var active    = $("#" + item + " li.active");
		var next      = active.next();

		if (next.length == 0)
		{
			next = $("#" + item + " li:first");
		}

		$("#" + item + " li.active").removeClass("active");
		next.addClass("active");

        changeItem(locatie, item);

		rotater = setTimeout("rotate('" + locatie + "','" + item + "')", 5000);
}

function changeItem(locatie, item)
{
    $(".block-"+ locatie + "-image-" + item + " a img").attr("src", $("#" + item + " li.active a img").attr("src"));
	$(".block-"+ locatie + "-text-" + item + " a.text-title-aanbieding").text($("#" + item + " li.active a span.titel").text());
    $(".block-"+ locatie + "-text-" + item + " p span").text($("#" + item + " li.active a span.omschrijving").text());
            
    $(".block-"+ locatie + "-image-" + item + " a").attr("href", $("#" + item + " li.active a").attr("href"));
    $(".block-"+ locatie + "-text-" + item + " a.text-title").attr("href", $("#" + item + " li.active a").attr("href"));
    $(".block-"+ locatie + "-text-" + item + " a.text-title").text($("#" + item + " li.active span.titel").text());
	$(".block-"+ locatie + "-text-" + item + " p.lees-meer a").attr("href", $("#" + item + " li.active a").attr("href"));

}


function bindBusinessLeden()
{
    $(".business-popup").click(function(e){
        e.preventDefault();
        selector = "#modal-" + $(this).attr("id"); 
        lidID = $(this).attr("rel"); 
        
        $("#modal-bg").css("width",$(document).width()).css("height",$(document).height());
        $("#modal-bg").fadeIn(750);
        
        //Centreren contactID toevoegen en infaden.
        centerModal(selector);
        $(".contactID").attr("value",lidID);
        $(selector).fadeIn(750);
    });
    
    $(".modal-close, .modal-close2").click(function(e){
        e.preventDefault();
        
        $("#modal-bg").fadeOut(500);
		$(".modal-content-wrapper").fadeOut(500);

    }); 
    
}

function centerModal(modal)
{        
    var topPos  = $(modal).offset().top;
    var leftPos = ($(document).width() / 2) - ($(modal).width() / 2);
    
    $(modal).css("left", leftPos);
    topPos += 50;
    $(modal).css("top", topPos);
    $(modal).css("position", "absolute");    
}
