var isIE = /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
var isiPad = navigator.userAgent.match(/iPad/i) != null;

if(!isiPad)
{
    if(!navigator.userAgent.match(/Chrome/i))
    {
        // Weird bug where Cufon doesn't show the text of the submenu's in Chrome:
        Cufon.replace("#top li a, #content h1, #content h2, #content h3");
    }
    Cufon.replace("#relatie div.categorie li a", {hover: true});
    Cufon.replace("#content div.sub a", { hover: true});
}


var currentSlide = 1;
var totalSlides;
var intervalID = -1;
var slideOffset;
var imageWidth;
var totalWidth;


$(function(){
	// Dropdowns:
	$("#top li").hover(function(){
		$("ul", this).show();
		$("ul ul", this).hide();
	}, function(){
		$("ul", this).hide();
	});
	
	// Sliders:
	$("#sliderNavigation").html('');
	totalSlides = 0;
	$("div.slide").each(function(){
		totalSlides++;
		$(this).attr("id", "slide"+totalSlides);
		$("#sliderNavigation").append('<a href="#" rel="slide'+totalSlides+'">'+totalSlides+'</a>');
	});

	// Breedte instellen van de slides-container:
	$("#slideContainer").width(totalSlides * 900);
	$("#sliderNavigation a[rel=slide1]").addClass("active");

    if(!isiPad)
    {
        // Voor non-iPad:
        $("#sliderNavigation a").click(function(){
            if(intervalID != -1) {
                clearInterval(intervalID);
            }
            currentSlide = $(this).attr("rel").replace('slide', '') - 1;
            $("#slideContainer").stop().animate({marginLeft: -900 * currentSlide}, 1000);
            $("#sliderNavigation a").removeClass("active");
            $(this).addClass("active");
            intervalID = setInterval("nextSlide();", 10000);
            return false;
        });
        intervalID = setInterval("nextSlide();", 10000);
        $("a.prev").click(function(){
            if(intervalID != -1) {
                clearInterval(intervalID);
            }
            currentSlide -= 2;
            if(currentSlide < 0) {
                currentSlide = totalSlides - 1;
            }
            nextSlide();
            intervalID = setInterval("nextSlide();", 10000);
            return false;
        });
        $("a.next").click(function(){
            if(intervalID != -1) {
                clearInterval(intervalID);
            }
            nextSlide();
            intervalID = setInterval("nextSlide();", 10000);
            return false;
        });
    }

	// Sliders 2:
	totalWidth = 0;
	var totalImages = $("#slideContainer2 img").length;
	var currentImageLoaded = 0;
	slideOffset = 0;

    // Totale breedte berekenen:
	$("#slideContainer2 img").each(function(){
        totalWidth += parseInt($(this).attr("width"));
        totalWidth += 5;
	});
    totalWidth -= 5;
    if(totalWidth > 852)
    {
        $("#slideContainer2").css({width: totalWidth});
        if(!isiPad)
        {
            $("#collectie2 a.next, #collectie2 a.prev").show();
        }
    } else {
        $("#collectie2 a.next, #collectie2 a.prev").hide();
    }
	$("#slideContainer2 a[rel=fancybox]").fancybox();

    if(!isiPad)
        {
        $("#collectie2 a.next").click(function(){
            moveSlides(-200);
            return false;
        });
        $("#collectie2 a.prev").click(function(){
            moveSlides(200);
            return false;
        });

        $("#slideContainer2 a").each(function(){
            this.offset = 0;
        });
    }

	// En nog een slideshow, maar dan op home:
	$("#homeSlides #slides2 img").not(":first").hide();
	$("#homeSlides #thumbs img").each(function(){
		$(this).click(function(){
			var rel = $(this).attr("rel");
			$("#homeSlides #slides2 img").not("[rel=" + rel + "]").fadeOut();
			$("#homeSlides #slides2 img[rel=" + rel + "]").fadeIn();
			$("#homeSlides #thumbs img").removeClass("active");
			$(this).addClass("active");
		});
	});
	
	$("a[rel=fancybox]").fancybox({type: 'image', cyclic: true, showCloseButton: true});
});

function showFancyBox(link)
{
	// $.fancybox({href: link, type: 'image', cyclic: true, showCloseButton: false});
	// console.log(link);
	$("a[href='"+link+"']").click();
}

function moveSlides(amount)
{
	slideOffset += amount;
	
	if(slideOffset > 0) { slideOffset = 0; }
	if(slideOffset < -totalWidth + 852) { slideOffset = -totalWidth + 852; }
	
	/*
	var firstSlideWidth = $("#slideContainer2 a:first").width();
	if(slideOffset < -firstSlideWidth + amount)
	{
		// Plaats de eerste image achteraan:
		var image = $("#slideContainer2 > *:first").detach();
		image.appendTo("#slideContainer2");
		slideOffset += firstSlideWidth;
	}
	
	var lastSlideWidth = $("#slideContainer2 a:last").width();
	if(slideOffset > 0)
	{
		// Plaats de laatste image vooraan:
		var image = $("#slideContainer2 > *:last").detach();
		image.prependTo("#slideContainer2");
		slideOffset -= lastSlideWidth;
	}
	*/
	
	// $("#slideContainer2").css({marginLeft: slideOffset - amount});
	$("#slideContainer2").stop().animate({marginLeft: slideOffset});
	
/*	
	$("#slideContainer2 a").each(function(){
		// var offset = parseInt($(this).position().left) + amount;
		this.offset += amount;
		$(this).stop().animate({left: this.offset}, function(){
			if($(this).position().left + $(this).width() < 25)
			{
				
				// Achteraan plaatsen:				
				this.offset = totalWidth;
				$(this).css({left: this.offset, top: 10});
			}
		
		// 25 = ok
		});
	});
*/
}

function nextSlide()
{
	currentSlide++;
	if(currentSlide > totalSlides) { currentSlide = 1; }
	$("#slideContainer").stop().animate({marginLeft: -900 * (currentSlide - 1)}, 1000);
	$("#sliderNavigation a").removeClass("active");
	$("#sliderNavigation a[rel=slide"+currentSlide+"]").addClass("active");
}


