var curr;
var fadeDescription = 500;
var url = "/producten/trappen/"; // Verander hier de url waar de slideshow op de homepagina moet verwijzen. Bv. : "url = '/producten/'", dan zal bij de slide "Modern", worden gelinkt naar "/producten/modern". Vergeet een slash op het einde dus niet!

$(document).ready( function() {
	
	Cufon.replace("#bigMenu ul li a, #smallMenu ul li a, #subMenu ul li a, h1, h2, #teaser .categories ul li a, .interactionblock span, .simplePagerNav li a, .submit, .beerbaumTeaser .overlayTeaser", { fontFamily: 'Frutiger 57 Condensed', hover:true });
	
	// Standaard pagina/nieuws detail --> Slideshow
	if($(".beerbaumTeaser").length){
		$('.beerbaumTeaser').hover(function(){
			Cufon.replace('.beerbaumTeaser h2', { fontFamily: 'Frutiger 57 Condensed', hover:true });
			$('.overlayTeaser').animate({bottom: '0px'},250);
		}, function(){
			Cufon.replace('.beerbaumTeaser h2', { fontFamily: 'Frutiger 57 Condensed', hover:true, color: '#fff' });
			$('.overlayTeaser').animate({bottom: '-45px'},250);
		});	
	}
	
	// Interactionblocks
	update = false;
	
	$('.interactionblock, .item.medium img, .item.small, .item.big, .column').hover(function(){
		Cufon.replace('.interactionblock:visible span, .item.medium h2, .item.small:visible h2, .item.big h2, .column h2', { fontFamily: 'Frutiger 57 Condensed' });
		update=true;
	});
	
	$("body").mousemove(function(e){
		if(update==true){
			Cufon.replace('.interactionblock:visible span, .item.medium h2, .item.small:visible h2, .item.big h2, .column h2', { fontFamily: 'Frutiger 57 Condensed' });
		}
		update=false;
	});
	
	// Item blocks
	$('.item.medium img, .item.medium .description').hover(function(){
		$(this).parent('.item.medium').children('.description').css('background', '#ffffff');
	}, function(){
		$(this).parent('.item.medium').children('.description').css('background', 'url("/~siteDesign/site/~img/itemDescriptionBg.png") repeat');
	});
	
	// Home --> Teaser
	if($(".slideshow").length){
		
		// Build up the array for category navigation
		var categories = [];
		var x = 0;
		$(".slideshow .slides li a").each(function(){
			if($(this).attr("rel") != ''){
				categories[x] = ".slideshow .categories ul li a[rel='" + $(this).attr("rel") + "']";
			} else {
				categories[x] = null;
			}
			x++;
		});
		
		$(".slideshow").jCarouselLite({
			circular: true,
			btnNext: "#next",
			btnPrev: "#prev",
			btnGo: categories,
			auto: 3000,
			speed: 750,
			pauseOnHover: true,
			beforeStart: function(a){
				
				if(a.first().children("a").children(".description").length){
					if ($.browser.msie && $.browser.version.substr(0, 1) < 9) {
						a.first().children("a").children(".description").css('display', 'none');
					}else {
						a.first().children("a").children(".description").fadeOut(fadeDescription);}
					
				}
				
			},
			afterEnd: function(a){
				// Fade in description
				//a.first().children("a").children(".description").fadeIn(fadeDescription);
				//a.first().children("a").children(".description").css('display', 'block');
				
				if ($.browser.msie && $.browser.version.substr(0, 1) < 9) {
						a.first().children("a").children(".description").css('display', 'block');
					}else {
						a.first().children("a").children(".description").fadeIn(fadeDescription);
				}
				
				// Remove old active
				$('.slideshow .categories ul li.active').removeClass("active");
				//$('.slideshow .categories ul li a[rel=""]').attr("href", "#");
				
				// Set new active
				
				rel = a.first().children("a").attr("rel");
				if(rel != ""){
					$('.slideshow .categories ul li a[rel="' + rel + '"]').parent("li").addClass("active");
					$('.slideshow .categories ul li a[rel=""]').attr("href", url + rel);
				}

				Cufon.replace("#teaser .categories ul li a, .description h2", { fontFamily: 'Frutiger 57 Condensed', hover:true });
			}
		});
		
		resizeOverlays();
		
	}
	
	// Standaard pagina/nieuws detail --> Slideshow
	if($(".zoomSlideshow .zoomContent").length){
		initSlideshow();
		
	}
	
/*
	// Nieuws detail --> Dropdown
	if($("#selectSlides").length){
		$("#selectSlides select").styledSelect({
			zIndexApply: true,
			zIndexStart: 20
		}); // See source code for edits made [line >90]
	}
*/
	
	// Product decoroverzicht --> Paging
	if($(".paging").length){
		$(".oneColumn").quickPager({
			pageSize: 8, // If you change this: don't forget to change the height of .oneColumn.paging in the CSS
			pagerLocation: "breadcrumbs",
			speed: 500
		});
		Cufon.replace(".simplePagerNav li a", { fontFamily: 'Frutiger 57 Condensed', hover:true });
	}
	
	$.preloadCssImages();
	
/*
	if ($('#onePhoto').length) {
		// Zoom function
		Shadowbox.init({
			skipSetup: false,
			players: "img",
			displayNav: true,
			handleUnsupported: "remove",
			autoplayMovies: true,
			overlayColor: '#576368',
			overlayOpacity: 0.9,
			troubleElements: ["select", "object", "embed"],
			onFinish: function(b){
				toggleLoupe();
			}
		});
	}
*/

});


$(window).resize(function(){
	resizeOverlays();
	
	// Enable/disable Loupe if Shadowbox is resized
	if($("#sb-player").length){
		//console.log(1);
		toggleLoupe();
	}
});

function resizeOverlays(){
	// Width of overlays
	$(".overlay").css("width", ($(window).width() - 960)/2);
}

function toggleLoupe(){
				
	// Create new JS image to compare real and client image size
	var image = new Image();
   	image.src = $("#sb-player").attr("src");
				
	// Only enable Loupe if image is resized
	if($("#sb-player").height() != image.height){
		$("#sb-player").loupe({
			width: 203,
			height: 203,
			loupe: 'loupe'
		});
	} else {
		$('.loupe').remove();
	}
	
}

function initSlideshow(){
	
	// Set initial link for zoom button
	$(".zoomSlideshow .zoomContent .zoomNavigation #zoom").attr('href', $(".zoomSlideshow .zoomContent ul li").first().children("a").attr("href"));
		
/*
	// Initiate slider
	$(".zoomSlideshow .zoomContent").jCarouselLite({
		btnNext: "#next",
		btnPrev: "#prev",
		auto: 2000,
		speed: 750,
		pauseOnHover: true,
		afterEnd: function(a){
			$(".zoomSlideshow .zoomContent .zoomNavigation #zoom").attr('href', a.first().children("a").attr("href"));
			Shadowbox.setup();
		}
	});
*/

	if($(".zoomSlideshow .zoomContent ul li").length > 1){
		
		// Build up the array for category navigation
		var selectValues = [];
		var x = 0;
		$("#selectSlides option").each(function(){
			if($(this).attr("id") != 'none'){
				selectValues[x] = "#selectSlides option#" + $(this).attr("id") + "";
			} else {
				selectValues[x] = null;
			}
			x++;
		});
		
		// Nieuws detail --> Dropdown
		if($("#selectSlides").length){
			$("#selectSlides select").styledSelect({
				zIndexApply: true,
				zIndexStart: 20
			}); // See source code for edits made [line >90]
		}
		
		$("#options").trigger('change');
			
		// Initiate slider
		$(".zoomSlideshow .zoomContent").jCarouselLite({
			btnNext: "#next",
			btnPrev: "#prev",
			btnGo: selectValues,
			auto: 4000,
			speed: 750,
			circular: true,
			visible: 1,
			pauseOnHover: true,
			afterEnd: function(a){
				$(".zoomSlideshow .zoomContent .zoomNavigation #zoom").attr('href', a.first().children("a").attr("href"));
				$("#options option").attr("selected", false);
				$("#options option:nth-child(" + a.first().children("a").attr("rel").replace("shadowbox ", "") + ")").attr("selected", "selected");
				$("#options").trigger('change');
				Shadowbox.setup();
			}
		});
		
	} else {
		$(".zoomNavigation #prev, .zoomNavigation #next, #selectSlides").css("display", "none");
	}
		
		
	// Zoom function
	Shadowbox.init({
		skipSetup: false,
		players: "img,html",
		displayNav: true,
		handleUnsupported: "remove",
		autoplayMovies: true,
		overlayColor: '#576368',
		overlayOpacity: 0.9,
		troubleElements: ["select", "object", "embed"],
		onFinish: function(b){
			//check if mobile disable loupe
			var deviceAgent = navigator.userAgent.toLowerCase();
			var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
			if (agentID) {
		 
			}else{
				toggleLoupe();
			}		
		}
	});
	
	
	
}


