
/**
 * 
 * @param url
 * @return void
 */
function activateQuote(url, element) {
	if ($('a[rel=quote]:visible').length === 0) { // Zijn er geen links naar quots?
		return;
	}
	if (element === undefined) {
		activateQuote.element = $('a[rel=quote]:visible').first()[0];
	} else {
		activateQuote.element = element;
	}
	$('#quote').load(url + ' p.handwriting', function () {
		var index = activateQuote.getIndex();
		$('#prevQuote').show();
		$('#nextQuote').show();
		if (index === 0) {
			$('#prevQuote').hide();
		}
		if (index == activateQuote.quotes().length - 1) {
			$('#nextQuote').hide();
		}
		sIFR.replaceElement(named({sSelector: "p.handwriting", sFlashSrc: "angelina.swf", sColor: "#ffffff", sLinkColor: "#000000", sBgColor: "#0075BF"}));
		$('.sIFR-alternate').hide();
	});
}
activateQuote.quotes = function () {
	return $('a[rel=quote]:visible');
};

// 
activateQuote.getIndex = function (element) {
	var currentIndex = 0;
	this.quotes().each(function (index) {
		if (this === activateQuote.element) {
			currentIndex = index;
		}
	});
	return currentIndex;
};
activateQuote.next = function () {
	var element = this.quotes()[this.getIndex() + 1];
	if (element !== undefined) {
		activateQuote(element.href, element);
	}
};

activateQuote.previous = function () {
	var element = this.quotes()[this.getIndex() - 1];
	if (element !== undefined) {
		activateQuote(element.href, element);
	}
};

/**
 * Het active block tonen (vergroten en infaden)
 */
function showActiveBlock() {
	// Eerst het actieve blok vergroten
	$('.active-block .contents').animate({ height: 'show', width: 'show'}, 600, 'swing',
		// Daarna de contents infaden
		function () {
			if ($.browser.webkit)  {
				$(this).parent().width(430); // WebKit fix
			}
			$(this).animate({
				opacity: 1 
			}, 400, 'swing', function () {
				activateQuote('quote/first.html');
			});
		}
	);
}


/**
 * referentie popups
 */
function blockReady() {


	$('a.popup').fancybox({
		transitionOut: 'none',
		//transitionIn: 'elastic',
		padding: 0,
		autoDimensions: false,
		width: 750,
		height: 'auto',
		onStart: function () {
			$('#fancybox-wrap').removeClass('quote');
		},
		onComplete: function (currentArray, currentIndex, currentOpts) {
			if (currentArray.length == 1) {
				return;
			}
			if (currentIndex == 0) { // eerste?
				$('#fancybox-right').css('width', '100%');
			} else {
				$('#fancybox-right').css('width', '50%');
			}
			if (currentIndex == currentArray.length - 1) { // laatste?
				$('#fancybox-left').css('width', '100%');
			} else {
				$('#fancybox-left').css('width', '50%');
			}
		}		
	});
	

	
	/**
	 * In en uitklappen op de Werkgebieden & Diensten paginas
	 */
	$('div.collapse h3').next('div').hide();
	$('div.collapse h3').toggle(function () {
		$(this).next('div:hidden').slideDown('fast'); //.siblings('ul:visible').slideUp('fast');
		$(this).addClass('expanded');
	}, function () {
		$(this).next('div:visible').slideUp('fast');
		$(this).removeClass('expanded');
	});
	
	/**
	 * Quotes
	 */
	$('a[rel=quote]').click(function () {
		activateQuote(this.href, this);		
		return false;
	});
	$('#nextQuote').click(function () {
		activateQuote.next();
	});
	$('#prevQuote').click(function () {
		activateQuote.previous();
	});
}

/**
 * Event #state change.
 * @param string hash  De string achter de # in de url
 */
$.History.bind(function (hash) {
	if (hash == 'disclaimer') {
		$('a[href=disclaimer.html]').click();
		return;
	}
	// class "active-block" verplaatsten van de vorige naar de nieuwe
	var previousBlockContents = $('.active-block .contents'); // Vorige block(en) in een var stoppen
	$('.active-block').removeClass('active-block');
	$('div#' + hash).addClass('active-block');
	
	//if ($('.active-block .contents').hasClass('loaded') === false) { // Moet het content block nog ingeladen worden?
		$.getJSON('contents/' + hash + '.json', function (result) {
			var contentBlock  = $('div#' + result.id + ' .contents');
			contentBlock.html(result.contents);
			contentBlock.addClass('loaded');
			blockReady();
			if (contentBlock.hasClass('waiting')) { // Was de verberg animatie eerder klaar dan het laden? 
				contentBlock.removeClass('waiting');
				// Dan moet de show animatie gestart worden.
				showActiveBlock();
			}
		});
//	}
	
	/**
	 * Het animeren van de content blokken
	 */	
	// Eerst de contents uitfaden
	previousBlockContents.animate({opacity: 0}, 600, 'swing', 
		function () {
			// Daarna verkleinen/verbergen
			$(this).animate({height: 'hide', width: 'hide'}, 600, 'swing', function () {
				if ($.browser.webkit)  {
					$(this).parent().width(200); // WebKit fix
				}
				if ($('.active-block .contents').hasClass('loaded')) { // Is de contents al ingeladen? 
					showActiveBlock();
				} else {
					// De show animate pas starten als de content geladen is.
					$('.active-block .contents').addClass('waiting'); 
				}
			});
		}
	);		
});


$(document).ready(function () {
	
	
	blockReady();
	activateQuote('quote/first.html');

	/**
	 * Vang de kliks op de content-header op.
	 */
	$('.content-header').click(function () {
		var blockId = $(this).parent().attr('id');
		$.History.setHash(blockId); // Stel de hash in (veroorzaak een animatie)
		return false;
	});
	
	/**
	 * De nieuws ticker / marqee
	 */
	$('marquee').marquee('marquee');
	

});

