﻿jQuery.fn.infoPopup = function(oParams) {
	var text 	= $(this).attr("title");

	if(oParams.pos == "L"){
		var div		= $("div.popup_infoL[@rel="+oParams.type+"]");
	} else {
		var div		= $("div.popup_info[@rel="+oParams.type+"]");
	}

	var cDiv	= div.find("div");
	var item	= $(this);
	var typeID	= oParams.typeID;

	item.removeAttr("title");

	item.hover(function() {
		cDiv.html(text);
		div.appendTo(item.parent());
		div.show();
	}, function() {
		div.hide();
		div.insertBefore($("#"+typeID));
	});
};

jQuery.fn.extend({

	initInfoPopups: function(pos) {
		if(!pos){
			pos="";
		}

		var thumbs 	= $(this).find("label.error img");
		var type	= $(this).attr("id")+"Display";
		var typeID	= $(this).attr("id");
		thumbs.each( function(i) {
			if($(this).attr("title")){
				$(this).infoPopup({"type":type,"typeID":typeID,"pos":pos});
			}
		});
	}

});

var featureCarousel = new Array;

function changeCarousel(id,year) {
	var count = 0;
	$('.images').css('height','300px');
	$('#carouselYear').html(year);
	$('#featureCarouselContainer .pagenavigation').find('a:visible').each(function(){
		count++;
		if ( $(this).attr('id') == 'change'+id ) {
			if ( count == 1 ) {
				$('#change'+(id+2)).hide();
				if ( $('#change'+(id-1)).attr('id') ) {
					$('#change'+(id-1)).show();
				}
				else {
					$.ajax({
						type: "POST",
						dataType: "json",
						url: "/site/dromen/ajax/getCarousel.php",
						data: '&month='+id+'&direction=l',
						success: function(json) {
							if ( json.error ) {
								alert(json.error);
							}
							else {
								$('#featureCarouselContainer .pagenavigation').prepend(json.link);
								$('#featureCarouselContainer h1').after(json.div);
								$('#change'+(id-1)).show();
							}
						},
						error: function(oRequest, sErrType) {
							alert('Er heeft zich een fout voorgedaan bij het ophalen van de gegevens');
						}
					});
				}
			}
			else if ( count == 3 ) {
				$('#change'+(id-2)).hide();
				if ( $('#change'+(id+1)).attr('id') ) {
					$('#change'+(id+1)).show();
				}
				else {
					$.ajax({
						type: "POST",
						dataType: "json",
						url: "/site/dromen/ajax/getCarousel.php",
						data: '&month='+id+'&direction=r',
						success: function(json) {
							if ( json.error ) {
								alert(json.error);
							}
							else {
								$('#featureCarouselContainer .pagenavigation').append(json.link);
								$('#featureCarouselContainer').append(json.div);
								$('#change'+(id+1)).show();
							}
						},
						error: function(oRequest, sErrType) {
							alert('Er heeft zich een fout voorgedaan bij het ophalen van de gegevens');
						}
					});
				}
			}
		}
	});
	$("div.imageflow").each(function(){
		$(this).fadeOut(0);
	});
	$(".pagenav-num").removeClass('pagenav-selected')
	$(".nav"+id).addClass('pagenav-selected')
	$("div#featureCarousel"+id).fadeIn(0);

	if (!featureCarousel[id.toString()] ) {
		featureCarousel[id.toString()] = new ImageFlow();
		featureCarousel[id.toString()].init({ 	ImageFlowID: 'featureCarousel'+id.toString(),
								reflections: false,
								reflectionP: 0.4,
								circular: true,
								captions: false,
								slideshow: true,
								xStep: 125,
								imageCursor: 'pointer',
								slider: false,
								onClick: function() { document.location = $(this).attr('longdesc'); }});
	}


}



function TickerTape(run) {

	if (run == 1) {
		//$("#banner a").fadeOut(500);
		//$('#header div[id^=inheader]').fadeOut(500);
	}

	var lastTicker = currentTicker;

	if (currentTicker == tickers) {
		currentTicker = 1;
	} else {
		currentTicker++;
	}

	$("#ticker"+lastTicker).fadeOut(500, function(){
		$("#ticker"+currentTicker).fadeIn(500);
	});
	$('#inheader'+lastTicker).fadeOut(500, function(){
		$('#inheader'+currentTicker).fadeIn(500);
	});


	setTimeout("TickerTape()", 10000);

}

var currentTicker = 1;
var tickers = $("#banner a").size();

$(document).ready(function() {
	$('img[src$=".png"]').ifixpng();

	//TickerTape(1);
	setTimeout("TickerTape(2)", 10000);

	if (typeof jQuery.fn.jdMenu == 'function') {
		$('ul.sitemenu').jdMenu({
			disableLinks: false
		});

		$('ul.sitemenu a').hover(
			function () {
				$(this).data('title', $(this).attr("title") );
				$(this).removeAttr("title");
			},
			function () {
				$(this).attr("title", $(this).data('title') );
				$(this).removeData("title");
			}
		);
	}

	for ( var i = 2; i <= tickers; i++ )
		$('#inheader'+i).hide();

	//submit form
	$('.submit').click(function(){
		$(this).parents('form').submit();
	});

	$('span.tab').click(function(){
		$("span[rel='"+$(this).attr("rel")+"']").removeClass('active');
		$(this).addClass('active');
		$("div."+$(this).attr("rel")).fadeOut(0);
		$("div#container"+this.id).fadeIn(0);
	});

	$("span[rel*='group']").each(function(){
		$("span[rel='"+$(this).attr('rel')+"']:first").trigger('click');
	});

	//toggleval
	$("input.toggleInput").toggleVal({
		populateFrom:	"labelAndEmpty",
		removeLabels:	true,
		focusClass:		"hasFocus",
		changedClass:	"isChanged"
	});

	//quick search
	$("form#searchContent, form#loginForm, form#lost_pw").submit(function(){
		$(this).find(".toggleInput").each(function() {
			if($(this).val() == $(this).data("defText")) {
				$(this).val("");
			}
		});
	});
	
	if ( typeof(starting_year) != 'undefined' )
		changeCarousel(1,starting_year);

});
