// JavaScript Document

var slideID;

jQuery.fn.correctPNG = function( settings ) {
   for(var i=0; i<document.images.length; i++)
	  {
	  var img = document.images[i]
	  var imgName = img.src.toUpperCase()
	  if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
		 {
		 var imgID = (img.id) ? "id='" + img.id + "' " : ""
		 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		 var imgStyle = "display:inline-block;" + img.style.cssText 
		 if (img.align == "left") imgStyle = "float:left;" + imgStyle
		 if (img.align == "right") imgStyle = "float:right;" + imgStyle
		 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle        
		 var strNewHTML = "<span " + imgID + imgClass + imgTitle
		 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
		 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
		 img.outerHTML = strNewHTML
		 i = i-1
		 }
	  }
   }
 $('img.fix').correctPNG();

	
function loadPopup(main, url_title) {
	// verwijder huidige popup indien reeds aanwezig
	removePopup();
	
	// Toon 'loading'-tekst
	$('<div id="loading" class="curved opac"><p class="no-opac">Bezig met laden. Even geduld a.u.b.</p></div>').appendTo(".content .wrapper");
	
	$.ajax({
		url: '/ajax/index/' + main + '/' + url_title,
		data: {},
		success: function (data) {
			// 'loading'-tekst verwijderen
			$("#loading").remove();
			
			// inhoud toevoegen
			$(".content .wrapper").append(data);
		  
		  	Cufon.refresh();
			
			initPopup();			
		}
	});
}

function initPopup() {
	// sluit knop
	$(".content:not(.contact) .wrapper .close-btn a").bind('click', function (e) {
		e.preventDefault();
		removePopup();
	});
	
	// meer info bullets
	$(".more-text a").bind('click', function (e) {
		e.preventDefault();
		var $me = $(e.target).is("a") ? $(e.target) : $(e.target).parents('a');
		var $li = $me.parent();
		var $ul = $li.parents('ul');
		var li_index = $("li", $ul).index($li);
		var top_px = (-270 * li_index);
		if ( ! $li.hasClass("active")) {
			$("li.active", $ul).removeClass("active");
			$("li", $ul).eq(li_index).addClass("active");
			$(".scroll ul").animate({ top: top_px }, 500);
		}
	});
	
	if ($(".container .photos li").size() > 1)
		slideID = setInterval( "slideSwitch()", 5000 );
}

function removePopup() {
	clearInterval(slideID);
	$(".container").remove();
	$(".close-btn").remove();
}


function slideSwitch() {
	var $active = $('.photos li.active');
    var $next = $active.next().length ? $active.next() : $('.photos li:first');
    $active.addClass('last-active');
    $next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() { $active.removeClass('active last-active'); });
}

function landingSlideSwitch() {
	var $active = $('li.visible .photos li.active');
    var $next = $active.next().length ? $active.next() : $('li.visible .photos li:first');
    $active.addClass('last-active');
    $next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() { $active.removeClass('active last-active'); });
}

var hasAnim = [];

function addAnim(id) {
	if ( ! checkAnim(id)) hasAnim.push(id);
}

function removeAnim(id) {
	for (var i=0, ilen = hasAnim.length; i<ilen; i++) {
		if (hasAnim[i] == id) {
			hasAnim.splice(i,1);
			break;
		}
	}
}

function checkAnim(id) {
	var found = false;
	for (var i=0, ilen = hasAnim.length; i<ilen; i++) {
		if (hasAnim[i] == id) {
			found = true;
			break;
		}
	}
	return found;
}

function selectPhoto(index) {
	var $curr = $("#overlay li:visible");
	var curr_index = $("#overlay li").index($curr);
	var $next = $("#overlay li").eq(index);
	var $curr_txt = $("#overlay li:visible");
	var $next_txt = $("#overlay li").eq(index);
	if ($next.length == 0) {
		$next = $("#overlay li:first");
		$next_txt = $("#overlay li:first");
	}
	$curr.fadeOut(250, function () { updateControls(); $next.fadeIn(400); });
	$curr_txt.fadeOut(250, function () { $next_txt.fadeIn(400); });
	
}

function updateControls() {
	var $curr = $("#overlay li:visible");
	var curr_index = $("#overlay li").index($curr);
	var $li = $("#overlay .photo li");
	
	// previous
	if (curr_index == 0) {
		$("#overlay .prev").hide();
	} else {
		$("#overlay .prev").show();
	}
	
	// next
	if (curr_index == ($li.size()-1)) {
		$("#overlay .next").hide();
	} else {
		$("#overlay .next").show();
	}
}

$(document).ready(function () {
	/*
	/************************************************************
	/*	verbergen gras sub-nav indien homepage & geen flash		*
	/************************************************************
	/*/
	if (seg1 == "home" && swfobject.hasFlashPlayerVersion("9.0.0")) {
		$(".nav-sub").css("display", "none");
	}
	
	/*
	/************************************************************
	/*	Landing page foto slideshow								*
	/************************************************************
	/*/
	if ($(".callout-big li.visible .photos li").size() > 1)
		slideID = setInterval( "landingSlideSwitch()", 5000 );
		
	// More promotions - navigation
	if ($("ul.promotions li.promotion").size() > 1) {
		$("ul.promotions li.promotion").each(function (n) {
			if (n == 0) {
				$('<li class="active"><a href="#">.</a></li>').appendTo($('.more-text ul'));
			} else {
				$('<li><a href="#">.</a></li>').appendTo($('.more-text ul'));
			}
			
		});
		
		$('.more-text ul').addClass('clearfix');
		
		// meer info bullets
		$(".more-text a").bind('click', function (e) {
			e.preventDefault();
			var $me = $(e.target).is("a") ? $(e.target) : $(e.target).parents('a');
			var $li = $me.parent();
			var $ul = $li.parents('ul');
			var li_index = $("li", $ul).index($li);
			var top_px = (-230 * li_index);
			if ( ! $li.hasClass("active")) {
				$("li.active", $ul).removeClass("active");
				$("li", $ul).eq(li_index).addClass("active");
				$("li.promotion.visible").removeClass("visible");
				$("li.promotion:eq("+li_index+")").addClass("visible");
				$(".scroll ul.promotions").animate({ top: top_px }, 500);
			}
		});
	}
	
	
	/*
	/************************************************************
	/*	Dynamisch ophalen van inhoud en tonen in overlay		*
	/************************************************************
	/*/
	
	$("a[rel]").each(function (n) {
		var me = $(this);
		
		me.bind('click', function (e) {
			var link_args = $(this).attr("href").split("/");
			e.preventDefault();
			$("a[rel]").removeClass("active");
			$(this).addClass("active");
			loadPopup(link_args[1], link_args[2]);
		});
	});
	
	/*
	/************************************************************
	/*	Dynamisch ophalen van inhoud en tonen in overlay		*
	/************************************************************
	/*/
	
	var version = parseInt($.browser.version, 10);
	
	//alert ("browser: " + $.browser.msie + ", versie: " + version);
	
	/*if ($.browser.msie && version < 8) {
		var me = $(".nav-sub li");
		$(".top", me).animate({height:108}, 0);
		$(".nav-sub").animate({height:164}, 0);
		
	} else {*/
		$(".fotoboek .nav-sub li a").bind('mouseenter', function (e) {
			var me = $(e.target).parents('li');
			var id = me.attr("id");
			if ( ! checkAnim(id)) {
				$(".top", me).animate({height:108}, 500);
				$(".nav-sub").animate({height:164}, 0);
				addAnim(id);
			}
		});
		$(".fotoboek .nav-sub li a").bind('mouseleave', function (e) {
			var me = $(e.currentTarget).parents('li');
			var id = me.attr("id");
			if (checkAnim(id)) {
				$(".top", me).clearQueue();
				$(".top", me).animate({height:8}, 500, function () { 
					var me = $(this).parents('li');
					removeAnim(me.attr('id'));
					if (hasAnim.length == 0) {
						$(".nav-sub").clearQueue();
						$(".nav-sub").animate({height:80}, 0);
					}
				});
			}
		});
	//}
	
	// init popup indien rechtstreeks deep-link
	initPopup();
	
	/*
	/************************************************************
	/*	Fotoalbum triggers										*
	/************************************************************
	/*/
	
	$(".fotoboek-link").overlay({
		mask: {
			color: '#000000',
			loadSpeed: 200,
			opacity: 0.8
		},
		target: '#overlay',
		closeOnClick: false,
		onBeforeLoad: function () {
			var wrap = this.getOverlay().find(".photo");
			wrap.html('');
			$("#overlay .next").hide();
			$("#overlay .prev").hide();
			
			// load the page specified in the trigger
			wrap.load(this.getTrigger().attr("href"), function (response, status, xhr) {
				wrap.html($(".photo", $(response)).html());
				
				$("#overlay .next a").bind('click', function (e) {
					e.preventDefault();
					$(this).blur();
					var $li = $("#overlay .photo li")
					var $curr = $("#overlay li:visible");
					var index = $li.index($curr);
					index++;
					if (index >= $li.size()) index = 0;
					selectPhoto(index);
				});
			
				$("#overlay .prev a").bind('click', function (e) {
					e.preventDefault();
					$(this).blur();
					var $li = $("#overlay li")
					var $curr = $("#overlay li:visible");
					var index = $li.index($curr);
					index--;
					if (index < 0) index = $li.size()-1;
					selectPhoto(index);
				});
			
				updateControls();
			});
		}			
	});
	
	/*
	/************************************************************
	/*	Inschuiven van subnavigatie								*
	/************************************************************
	/*/
	
	if (seg1 != "home" || ! swfobject.hasFlashPlayerVersion("9.0.0")) {
		
		varMax = $(".nav-sub ul li").size();
		varID = setInterval(varName, 250);

		$(".nav-sub ul li").bind('mouseenter', function (e) { 
			var $li = $(e.target).is('li') ? $(e.target) : $(e.target).parents('li');
			$li.animate({ paddingTop:17 }, { queue:false, duration:250 }) 
		});
		$(".nav-sub ul li").bind('mouseleave', function (e) { 
			var $li = $(e.target).is('li') ? $(e.target) : $(e.target).parents('li');
			$li.clearQueue().animate({ paddingTop:22 }, { queue:false, duration:200 }) 
		});
	
		//$(".nav-sub ul").animate({ bottom:0 }, 1000, function () { Cufon.refresh(); });
		$(".fotoboek .paginate .next, .fotoboek .paginate .prev").animate({ bottom:15 }, 1000);

	}
	
});

var varCounter = 0;
var varMax;
var varID;
var varName = function() {
	if(varCounter < varMax) {
		//console.log(varCounter);
		$(".nav-sub li:eq(" + varCounter + ")").animate({ paddingTop:22 }, { queue:false, duration:750 });
		varCounter++;
	} else {
		clearInterval(varID);
	}
};


$(window).load(function () {
	
	//
	
});
