var looping;
var iIndex = 0;
var iBlok = 0;


$(document).ready(
	function()
	{
//		$("img").data("src", $(this).attr("src"));
//		$("img").attr("src", "/gfx/css/ocursor.gif");

		/*$(".nieuwsHorizontaal div img").each(
			function()
			{
				$(this).data("src", $(this).attr("src"));	
				$(this).attr("src", "/gfx/css/ocursor.gif");
			}
		)*/


        $("#content-bloken div img:first").show();				

		$(window).load(
			function()
			{
		        if($("body").attr("id") == 'home')
		        {    
					bindHome();
		        }
		    }
		);
		
		
		$("#mainnav a[rel=presentatie]").addClass("rood");
		$("#mainnav a[rel=event]").addClass("groen");
		$("#mainnav a[rel=multimedia]").addClass("blauw");	
		
		
		$("#flash-home").flash(
			{
				swf: '/showfile/'+$("#flash-home").attr('rel')+'/home.swf?ID='+$("#flash-home").attr('rel'),
				width: 870,
				wmode: 'transparent'
			}
		);
		
   }
);

$(document).ready(
    function()
    {
        bindThumbs();
		bindModal();
        
        bindLinkNav();
	//	bindModalThumbs();

		switch($("body").attr("id"))
		{

			case 'nieuws':		makeScrollingVert();
			case 'home':		bindNieuws();
								valign();
								break;
			case 'producten':	bindProducten();
								break;
            case 'diensten':    bindDiensten();                        
                                break;
			case 'klanten':		bindKlanten();
								makeScrolling();
								break;
            case 'fidato':      bindFidato();
					            makeScrolling();
                                break;
			case 'zoeken':      bindZoeken();
                                break;
		}
    }
);


function valign()
{
	$(".news-list-title img").each(
		function()
		{
			$(this).show();

			var height = $(this).height();
			var top = (55 - height) / 2;
			
			$(this).css("margin-top", top+"px");
		}
	);
}

function makeScrolling()
{
	$("#content-bloken").customScroller({
			speed: 6,
			vertical: 0,
			horizontal: 1
		});
}

function makeScrollingVert()
{
/*	$("#content-bloken").customScroller({
			speed: 6,
			vertical: 1,
			horizontal: 0
		});*/
}

function bindHome()
{       
	$("div#content-bloken div a img").hide();
	$("div#content-bloken div").show();

	setTimeout("showBlok(0)", 500);
	setTimeout("showBlok(1)", 1000);
	setTimeout("showBlok(2)", 1500);
  	
}


function showBlok(iNr)
{
	$("div#content-bloken div:eq("+iNr+") a img:eq(0)").fadeIn(600);
	
	if (iNr == 2)
	{
		looping = setInterval(function(){loopimages()}, 500);
	}
}

function loopimages()
{
	iNext = iIndex + 1;
		
	if ($("div#content-bloken div:eq("+iBlok+") a img:eq("+iNext+")").length == 0)
	{
		iBlok++;
		iIndex = 0;
		iNext = 1;
	}
	
	if ($("div#content-bloken div:eq("+iBlok+")").length == 0)
	{
		clearInterval(looping);
		
		$("#content-bloken div").hover(
			function()
			{
				var nr = $(this).find("img").length;
				nr = nr - 2;
				
				nr = Math.round(Math.random()*nr);
						
				$(this).find("img:not(:last)").hide();
				$(this).find("img:eq("+nr+")").show();
				
				
				
				$(this).find("img:last").animate({opacity: 0.4}, 300);
			},
			function()
			{
				$(this).find("img:last").animate({opacity: 1}, 300);			
			}
		);
	}
	else
	{
	//	$("div#content-bloken div:eq("+iBlok+") a img:eq("+iIndex+")").fadeOut(400);                
        $("div#content-bloken div:eq("+iBlok+") a img:eq("+iNext+")").fadeIn(500);
        
        //$("div#content-bloken div:eq("+iBlok+") a img:eq("+iIndex+")").hide();
		//$("div#content-bloken div:eq("+iBlok+") a img:eq("+iNext+")").show();
		
		iIndex++;
	}

}

function bindKlanten()
{
    var aantal = $("ul#thumbs li").length;
	var breedte = Math.round((aantal * 117)/2);	
	
	var iBreedte = aantal * 251;
	$("ul#thumbs").css("width", iBreedte+"px");
        
    $("div#content-bloken li.nieuwsHorizontaal a").click(
		function()
		{			
/* Gegevens ophalen van de klant */		
//			if ($(this).parent().find("div").length == 0)
//			{		
				var current = $(this);

				$.get(	$(this).attr("href"),
						function(html)
						{
							current.parent().find("div:first").html(html);
							
							var iWidth = $(document).width();
							var iHeight = $(document).height();
							
							var iLeftModal;
							var iTopModal;
							var inhoud = current.parent().find("div:first");
							
							iLeftModal = Math.round((iWidth / 2) - 414);
							iTopModal = 20;			            
				            
				            $("#modal").attr("class", "");
							$("#modal").addClass("klanten");
							
						     // Modal weergeven
						    $("#modal div.modal-content h2").text(inhoud.find("h2").text()); 
						    $("#modal div.modal-content .printklant").attr('href', "/printklant.php?paginaID="+inhoud.find(".paginaID").text()); 
				            $("#modal div.modal-content div.inhoud").html(inhoud.find("div.inhoud").html());
							$("#modal div.modal-content img.logo").attr("src", inhoud.find("img.logo").attr("src"));
						
				        	$("#modal div.modal-content .modal-right .fotos").html(inhoud.find(".fotos").html());
				            $("#modal div.modal-content .modal-right .afbeelding").html("");
				
				            $("#modal div.modal-content .modal-right .afbeelding").html(inhoud.find(".fotos:first .thumbsKlantMedia").html());
							Cufon.replace('#modal div.modal-content h2');
							
							$("#modal-shadow").height(iHeight).width(iWidth).show();
							$("#modal").css("top", iTopModal).css("left", iLeftModal).show();                
							
				            
				    
				            return false;
						}
				);
	//		}
	
			return false;
		}
	);
    
    $('.thumbsKlantMedia').live('click', function() {
        //$("#modal div.modal-content .modal-right .afbeelding .foto").attr("src",this.src);
        $("#modal div.modal-content .modal-right .afbeelding").html($(this).html());
    });
}

function bindProducten()
{
	var aantal = $("ul#thumbs li").length;
	var breedte = Math.round((aantal * 117)/2);
	
//	$("ul#thumbs").css("width", breedte+"px");
	
	$("div#content-bloken li.nieuws a").click(
			function()
			{			
				var iWidth = $(document).width();
				var iHeight = $(document).height();
				
				var iLeftModal;
				var iTopModal;
				var inhoud = $(this).parent().find("div:first");
				
				iLeftModal = Math.round((iWidth / 2) - 414);
				iTopModal = 20;

				$("#modal").attr("class", "");
				$("#modal").addClass("producten");
				
                // Modal weergeven
				$("#modal div.modal-content h2").text(inhoud.find("h2").text());
				$("#modal div.modal-content div.inhoud").html(inhoud.find("div.inhoud").html());
				
				Cufon.replace('#modal div.modal-content h2');
				
				$("#modal-shadow").height(iHeight).width(iWidth).show();
				$("#modal").css("top", iTopModal).css("left", iLeftModal).show();

				return false;
			}
		);
	
	$("div#modal li.nieuws a").click(
			function()
			{			
				var iWidth = $(document).width();
				var iHeight = $(document).height();
				
				var iLeftModal;
				var iTopModal;
				var inhoud = $(this).parent().find("div:first");
				
				iLeftModal = Math.round((iWidth / 2) - 414);
				iTopModal = 20;
				
                // Modal weergeven
				$("#modal div.modal-content h2").text(inhoud.find("h2").text());
				$("#modal div.modal-content div.inhoud").html(inhoud.find("div.inhoud").html());
				
				Cufon.replace('#modal div.modal-content h2');
				
				return false;
			}
		);
	
		$("div.modal-search input[name=naam], div.search input[name=naam]").keyup(
			function()
			{
				
				var sZoek = $(this).val();
				var sCat = $("div.modal-search select[name=categorie]").val();
				
				if (sCat == '')
				{
					sCat = $("div.search select[name=categorie]").val();
				}
				
				search(sZoek, sCat);
			}
		);
		
		$("div.modal-search select[name=categorie], div.search select[name=categorie]").change(
				function()
				{					
					var sCat = $(this).val();
					var sZoek = $("div.modal-search input[name=naam]").val();
					
					if (sZoek == '')
					{
						sZoek = $("div.search input[name=naam]").val();
					}
					
					search(sZoek, sCat);
				}
			);
}

function search(sZoek, sCat)
{
	if (sCat != '')
	{
		$("li.nieuws").each(
				function()
				{			
					if ($(this).find("div .categorie:icontains("+sCat+")").length > 0)
					{
						if ($(this).find("div h2:icontains("+sZoek+")").length > 0)
						{
							$(this).show();
						}
						else
						{
							$(this).hide();
						}
					}
					else
					{
						$(this).hide();
					}
				}
		);
	}
	else if (sZoek != '')
	{
		$("li.nieuws").each(
				function()
				{			
					if ($(this).find("div h2:icontains("+sZoek+")").length > 0)
					{
						$(this).show();
					}
					else
					{
						$(this).hide();
					}
				}
		);
	}
	else
	{
		$("li.nieuws").show();
	}
	
	$("div.modal-search input[name=naam], div.search input[name=naam]").val(sZoek);
}

function bindNieuws()
{
	$("div#content-bloken li.nieuws a, div.nieuws-home-item a").click(
			function()
			{			
				var iWidth = $(document).width();
				var iHeight = $(document).height();
				
				var iLeftModal;
				var iTopModal;
				var inhoud = $(this).parent().find("div.verborgen");
				
				iLeftModal = Math.round((iWidth / 2) - 414);
				iTopModal = 20;

				$("#modal").attr("class", "");
				$("#modal").addClass("nieuws");
				
			// Modal weergeven                
				$("#modal div.modal-content h2").text(inhoud.find("h2").text());
				$("#modal div.modal-content h3").text(inhoud.find("h3").text());
				
				$("#modal div.modal-content div.intro").html(inhoud.find("div.intro").html());
				$("#modal div.modal-content div.inhoud").html(inhoud.find("div.inhoud").html());
				
				Cufon.replace('#modal div.modal-content h2');
				
				$("#modal-shadow").height(iHeight).width(iWidth).show();
				$("#modal").css("top", iTopModal).css("left", iLeftModal).show();

				return false;
			}
		);
	
	$("div#modal li.nieuws a").click(
			function()
			{			
				var iWidth = $(document).width();
				var iHeight = $(document).height();
				
				var iLeftModal;
				var iTopModal;
				var inhoud = $(this).parent().find("div:first");
				
				iLeftModal = Math.round((iWidth / 2) - 414);
				iTopModal = 20;
				
			// Modal weergeven
				$("#modal div.modal-content h2").text(inhoud.find("h2").text());
				$("#modal div.modal-content div.intro").html(inhoud.find("div.intro").html());
				$("#modal div.modal-content div.inhoud").html(inhoud.find("div.inhoud").html());
				
				Cufon.replace('#modal div.modal-content h2');
				
				return false;
			}
		);
}

function bindFidato()
{
    var aantal = $("ul#thumbs li").length;
	var breedte = Math.round((aantal * 117)/2);
	
	var iBreedte = aantal * 251;
	$("ul#thumbs").css("width", iBreedte+"px");	
	
	//$("ul#thumbs").css("width", breedte+"px");
	
	$("div#content-bloken li.nieuwsHorizontaal a").click(
    
			function()
			{			
				var iWidth = $(document).width();
				var iHeight = $(document).height();
				
				var iLeftModal;
				var iTopModal;
				var inhoud = $(this).parent().find("div:first");
				
				iLeftModal = Math.round((iWidth / 2) - 414);
				iTopModal = 20;
                
                $("#modal").attr("class", "");
				$("#modal").addClass("fidato");
				
			     // Modal weergeven
			    $("#modal div.modal-content h2").text(inhoud.find("h2").text()); 
                $("#modal div.modal-content div.inhoud").html(inhoud.find("div.inhoud").html());
				$("#modal div.modal-content img.logo").attr("src", inhoud.find("img.logo").attr("src"));
				$("#modal div.modal-content img.foto").attr("src", inhoud.find("img.foto").attr("src"));
			
				
				Cufon.replace('#modal div.modal-content h2');
				
				$("#modal-shadow").height(iHeight).width(iWidth).show();
				$("#modal").css("top", iTopModal).css("left", iLeftModal).show();

				return false;
			}
		);
	
	$("div#modal li.nieuws a").click(
			function()
			{			
				var iWidth = $(document).width();
				var iHeight = $(document).height();
				
				var iLeftModal;
				var iTopModal;
				var inhoud = $(this).parent().find("div:first");
				
				iLeftModal = Math.round((iWidth / 2) - 414);
				iTopModal = 20;
				
			// Modal weergeven
				$("#modal div.modal-content h2").text(inhoud.find("h2").text());
				$("#modal div.modal-content div.intro").html(inhoud.find("div.intro").html());
				$("#modal div.modal-content div.inhoud").html(inhoud.find("div.inhoud").html());

				Cufon.replace('#modal div.modal-content h2');
				
				return false;
			}
		);
}

function bindDiensten()
{
	$("div#content-bloken ul li.nieuws a").click(
			function()
			{			
				var iWidth = $(document).width();
				var iHeight = $(document).height();
				
				var iLeftModal;
				var iTopModal;
				var inhoud = $(this).parent().find("div.verborgen");
				
				iLeftModal = Math.round((iWidth / 2) - 414);
				iTopModal = 20;

				$("#modal").attr("class", "");
				$("#modal").addClass("nieuws");
				
			// Modal weergeven                
				$("#modal div.modal-content h2").text(inhoud.find("h2").text());
				
				$("#modal div.modal-content div.intro").html(inhoud.find("div.intro").html());
				$("#modal div.modal-content div.inhoud").html(inhoud.find("div.inhoud").html());
				
				Cufon.replace('#modal div.modal-content h2');
				
				$("#modal-shadow").height(iHeight).width(iWidth).show();
				$("#modal").css("top", iTopModal).css("left", iLeftModal).show();

				return false;
			}
		);
	
	$("div#modal li.nieuws a").click(
			function()
			{			
				var iWidth = $(document).width();
				var iHeight = $(document).height();
				
				var iLeftModal;
				var iTopModal;
				var inhoud = $(this).parent().find("div:first");
				
				iLeftModal = Math.round((iWidth / 2) - 414);
				iTopModal = 20;
				
			// Modal weergeven
				$("#modal div.modal-content h2").text(inhoud.find("h2").text());
				$("#modal div.modal-content div.intro").html(inhoud.find("div.intro").html());
				$("#modal div.modal-content div.inhoud").html(inhoud.find("div.inhoud").html());
				
				Cufon.replace('#modal div.modal-content h2');
				
				return false;
			}
		);
}

function bindZoeken()
{
	$("div#content-bloken #thumbs li.nieuws a").click(
			function()
			{			
				if ($(this).parent().hasClass('project'))
				{
					$("#modal").addClass('klanten');
					$("#modal .modal-nieuws").hide();
					$("#modal .modal-klanten").show();
	
					var current = $(this);

	
	
					$.get(	$(this).attr("href"),
							function(html)
							{
								current.parent().find("div.projectinhoud").html(html);
								
								var iWidth = $(document).width();
								var iHeight = $(document).height();
								
								var iLeftModal;
								var iTopModal;
								var inhoud = current.parent().find("div.projectinhoud");
								
								iLeftModal = Math.round((iWidth / 2) - 414);
								iTopModal = 20;			            
					            
					            $("#modal").attr("class", "");
								$("#modal").addClass("klanten");
								
							     // Modal weergeven
							    $("#modal div.modal-content h2").text(inhoud.find("h2").text()); 
							    $("#modal div.modal-content .printklant").attr('href', "/printklant.php?paginaID="+inhoud.find(".paginaID").text()); 
					            $("#modal div.modal-content div.inhoud").html(inhoud.find("div.inhoud").html());
								$("#modal div.modal-content img.logo").attr("src", inhoud.find("img.logo").attr("src"));
							
					        	$("#modal div.modal-content .modal-right .fotos").html(inhoud.find(".fotos").html());
					            $("#modal div.modal-content .modal-right .afbeelding").html("");
					
					            $("#modal div.modal-content .modal-right .afbeelding").html(inhoud.find(".fotos:first .thumbsKlantMedia").html());
								Cufon.replace('#modal div.modal-content h2');
								
								$("#modal-shadow").height(iHeight).width(iWidth).show();
								$("#modal").css("top", iTopModal).css("left", iLeftModal).show();                
								
					            
					    
					            return false;
							}
					);	
					
					return false;			
				}
				else if ($(this).parent().hasClass('pagina'))
				{
					return true;
				}
				else
				{
					$("#modal").removeClass('klanten');
					$("#modal .modal-nieuws").show();
					$("#modal .modal-klanten").hide();
				
					var iWidth = $(document).width();
					var iHeight = $(document).height();
					
					var iLeftModal;
					var iTopModal;
					var inhoud = $(this).parent().find("div.verborgen");
					
					iLeftModal = Math.round((iWidth / 2) - 414);
					iTopModal = 20;
	
					$("#modal").attr("class", "");
					$("#modal").addClass("nieuws");
					
				// Modal weergeven                
					$("#modal div.modal-content h2").text(inhoud.find("h2").text());
					$("#modal div.modal-content div.intro").html(inhoud.find("div.intro").html());
					$("#modal div.modal-content div.inhoud").html(inhoud.find("div.inhoud").html());
					$("#modal div.modal-content img.foto").attr("src", inhoud.find("img.foto").attr("src"));
					
					Cufon.replace('#modal div.modal-content h2');
					
					$("#modal-shadow").height(iHeight).width(iWidth).show();
					$("#modal").css("top", iTopModal).css("left", iLeftModal).show();
	
					return false;
				}
			}
		);
	
	$("div#modal li.nieuws a").click(
			function()
			{			
				var iWidth = $(document).width();
				var iHeight = $(document).height();
				
				var iLeftModal;
				var iTopModal;
				var inhoud = $(this).parent().find("div:first");
				
				iLeftModal = Math.round((iWidth / 2) - 414);
				iTopModal = 20;
				
			// Modal weergeven
				$("#modal div.modal-content h2").text(inhoud.find("h2").text());
				$("#modal div.modal-content div.intro").html(inhoud.find("div.intro").html());
				$("#modal div.modal-content div.inhoud").html(inhoud.find("div.inhoud").html());
				$("#modal div.modal-content img.foto").attr("src", inhoud.find("img.foto").attr("src"));
				
				Cufon.replace('#modal div.modal-content h2');
				
				return false;
			}
		);
		
    $('.thumbsKlantMedia').live('click', function() {
        //$("#modal div.modal-content .modal-right .afbeelding .foto").attr("src",this.src);
        $("#modal div.modal-content .modal-right .afbeelding").html($(this).html());
    });
}
                             

function bindModal()
{
	$("#modal .close").click(
			function()
			{
					$("#modal").hide();
					$("#modal-shadow").hide();
			}
	);
    
}

function bindThumbs()
{
    var aantal      = $("ul#thumbs li").length;   

    var breedte     = 200;
	var totallength = aantal * 117;
	var maxLeft     = (totallength + breedte) * -1;
	
    var hoogte      = 117;
    var totalTop    = aantal * 117;
    var maxTop      = (totalTop - hoogte) * -1;	
        	
	$("div#content-bloken div.pijlrechts a").click(
        function()
        {
            var pos = $("ul#thumbs").css("left");
                        
            pos = parseInt(pos.replace('px',''));
            if(isNaN(pos))
            {
                pos = '0';
                pos = parseInt(pos.replace('px',''));
            }
                        
            pos -= breedte;
            if(pos <= maxLeft)
            {
                nwPos = maxLeft;
            }else{
                nwPos = pos;
            }
            
            $("ul#thumbs").animate(
					{
						left: nwPos+"px"
					},
					{queue: false, duration: 500}
			);
        }
	);
	
	$("div#content-bloken div.pijllinks a").click(
        function()
        {
            var pos = $("ul#thumbs").css("left");
            pos = parseInt(pos.replace('px',''));
            
            
            pos += breedte;
            if(pos >= 0)
            {
                nwPos = 0;
            }else{
                nwPos = pos;
            }
            
            $("ul#thumbs").animate(
					{
						left: nwPos+"px"
					},
					{queue: false, duration: 500}
			);
        }
	);
    
    $("div.pijlboven a").click(
			function()
            {
                var pos = $("ul#thumbs").css("top");
                pos = parseInt(pos.replace('px',''));
                pos += hoogte;
                
                if(pos >0)
                {
                    nwPos = 0;
                }else{
                    nwPos = pos;
                }
                
                $("ul#thumbs").animate(
						{
							top: nwPos+"px"
						},
						{queue: false, duration: 500}
				);
				
				return false;
            }
	);
    
    $("div.pijlonder a").click(
		function()
        {
            var pos = $("ul#thumbs").css("top");
            
            var totaal = $("ul#thumbs li").length * 76;
            
            pos = parseInt(pos.replace('px',''));
            if(isNaN(pos))
            {
                pos = '0';
                pos = parseInt(pos.replace('px',''));
            }
            
            pos -= hoogte;
            
            if(pos <= maxTop)
            {
                nwPos = maxTop;
            }else{
                nwPos = pos;
            }


			var rest = nwPos + totaal;                

            if (rest > 0)
            {
	            $("ul#thumbs").animate(
						{
							top: nwPos+"px"
						},
						{queue: false, duration: 500}
				);
			}
			
			return false;
        }
	);
    
}

function bindModalThumbs()
{
	var aantal = $("ul#modal-thumbs li").length;
	var totallength = aantal * 117;
	var maxLeft = (totallength - (6 * 117)) * -1;
			
	$("div#modal div.pijlrechts a").click(
			function()
			{
				var pos = $("ul#modal-thumbs").position();
				
				if (pos.left <= maxLeft)
				{
					left = 0;
				}
				else
				{
					left = pos.left - 117;
				}
			
				$("ul#modal-thumbs").animate(
						{
							left: left+"px"
						},
						{queue: false, duration: 500}
				);
			}
	);
	
	$("div#modal div.pijllinks a").click(
			function()
			{
				var pos = $("ul#modal-thumbs").position();
				
				if (pos.left > 0)
				{
					left = maxLeft;
				}
				else
				{
					left = pos.left + 117;
				}
			
				$("ul#modal-thumbs").animate(
						{
							left: left+"px"
						},
						{queue: false, duration: 500}
				);
			}
	);
}

function bindLinkNav()
{
    $("#linksnav li a").hover(
        function () {
            $(this).addClass("active");
        },
        function () {
            $(this).removeClass("active");
        }
    );
    
    //$("li.fade").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);});
}


$.expr[':'].icontains = function(obj, index, meta, stack){
	return (obj.textContent || obj.innerText || jQuery(obj).text() || '').toLowerCase().indexOf(meta[3].toLowerCase()) >= 0;
	};
