var loggedIn = false;
function showDetails(obj,id,hideText,showText){
	//alert($(obj).text());
	if($(obj).text() == showText)
		$(obj).text(hideText);
	else
		$(obj).text(showText);
		
	$('#' + id).slideToggle(500);

}


function bindTopMenuElements(){

	
	$('#navigation').find('> ul > li').each(function(){
	
		$(this).find('a').bind('mouseenter',function(e){

			//Hide other submenus
			hideAllMenus();
			//Show submenu
				//Fetch submenuId from rel attribute on anchor
				menuToShow = $(this).attr('rel');
				//alert("Show " + menuToShow);
				//showMenu(menuToShow);
				////$('#menu_child_' + menuToShow).fadeIn();
				showSubMenu($('#menu_child_' + menuToShow));
			
			
			//Timeout om menu te hiden weer afzetten
			window.clearTimeout(window.menuTimeout);	
			
			//Alles op inactive plaatsen
			$('#navigation').find('> ul > li').each(function(){
				$(this).find('a').removeClass('active');		
			});
			
			//Juiste op active plaatsen
			$(this).toggleClass('active');
		});
		
		$(this).find('a').bind('mouseout',function(e){
		
			//Clear timer
			window.clearTimeout(window.menuTimeout);
			
			//Start timer voor hiden van menu
			window.menuTimeout = window.setTimeout(hideInactiveMenus,4000);
			
		});
		
	});
	
}

function showSubMenu(menu){
	menu.show();
	menu.find('li').each(function(){
	
		$(this).find('a').bind('mouseenter',function(e){
			//Timeout om menu te hiden weer afzetten
			window.clearTimeout(window.menuTimeout);	
		});	
	
		$(this).find('a').bind('mouseout',function(e){
		
			//Clear timer
			window.clearTimeout(window.menuTimeout);
			
			//Start timer voor hiden van menu
			window.menuTimeout = window.setTimeout(hideInactiveMenus,4000);
			
		});
	});
}

function hideAllMenus(){
	$('#navigation').find('.sub').find('ul').each(function(){
		$(this).hide();
	});
}
function hideInactiveMenus(){
	
	//Set all classes active on a in main menu to normal
	$('#navigation').find('> ul > li').each(function(){
		$(this).find('a.active').toggleClass('active');
	});
		
	//Hide inactive submenus
	hideAllMenus();
	
	//Show active menu (if any)
	$('#navigation').find('.sub').find('ul.active').each(function(){
		//$(this).show();
		$(this).fadeIn();
	});
	
}


function bindNewsHeadlines(){
	$('#newsHomepageHeadlines').find('ul.headlines > li').each(function(){
	
		$(this).find('a').bind('mouseenter',function(e){
			
			window.clearInterval(window.intervalId);
			
			showHeadline.call($(this));
			
			});
		/*
		$(this).find('a').bind('mouseleave',function(e){
			alert('boe');		
		});
		*/
	
	});
}

function animateHeadlines(){
	//Start currentHeadline at 1 because 0 is already shown onload
	window.currentHeadline = 1;
	window.nofArticles = $('#newsHomepageHeadlines').find('div.article_display > div').length;
	window.intervalId = window.setInterval(nextHeadline,6000);
}
function nextHeadline(){

	showHeadline.call($('#newsHomepageHeadlines').find('ul.headlines > li > a').get(window.currentHeadline));
	
	if(window.currentHeadline == window.nofArticles - 1 ){
		window.currentHeadline = 0;
	}else{
		window.currentHeadline = window.currentHeadline + 1;
	}
}
function showHeadline(){
	
	//Fetch articleId from rel attribute on anchor
	articleToShow = $(this).attr('rel');

	//Unhighlight all anchors
	$('#newsHomepageHeadlines').find('ul.headlines > li > a').each(function(){
		$(this).removeClass('selected');
	});

	//Highlight the anchor in question
	$(this).toggleClass('selected');

	
	//Hide all articles before showing the right one	
	$('#newsHomepageHeadlines').find('div.article_display > div').each(function(){
		$(this).hide();		
	});
	
	//Show the correct article
	$('#' + articleToShow).show();
	
}


function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
		thisfield.value = "";
	}
}

function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
		thisfield.value = defaulttext;
	}
}

function changeTab(thistab, thisheader, othertab1, otherheader1, othertab2, otherheader2){
	document.getElementById(thistab).style.display = 'block';
	document.getElementById(othertab1).style.display = 'none';
	document.getElementById(othertab2).style.display = 'none';
	document.getElementById(thisheader).className = 'selected';
	document.getElementById(otherheader1).className = '';
	document.getElementById(otherheader2).className = '';
}

function refreshMemberStatus(url){
	if (typeof url == "object"){
		url = url.href;
	}
	
	$.ajax({ 
		dataType: "json",
		url: url,
			success: function(data){
				if (data.html){
					$("#user_info").html(data.html);
				}
				if (typeof data.loggedIn == "boolean"){
					loggedIn = data.loggedIn;
				}
				
				var obj = null;
				var checked = true;
				var disabled = false;
				
				obj = $("#newsletter_email");
				if (obj.length == 1){
					disabled = false;
					if (typeof data.email == "string"){
						obj.val(data.email);
						disabled = true;
					}
					else {
						obj.val("");
						obj.get(0).onblur();
					}
					obj.get(0).disabled = disabled; 
				}
				
				obj = $("#newsletter_nieuwsoverzicht");
				if (obj.length == 1){
					checked = true;
					disabled = false;
					if (typeof data.wantDailyMailing == "string" || data.wantDailyMailing === null){
						data.wantDailyMailing = data.wantDailyMailing == "1" ? true: false;
						if (data.wantDailyMailing){
							checked = data.wantDailyMailing;
							disabled = true;
						}
						else {
							checked = true;
						}
					}
					obj.get(0).checked = checked; 
					obj.get(0).disabled = disabled; 
				}
				
				obj = $("#newsletter_achtergrond");
				if (obj.length == 1){
					checked = true;
					disabled = false;
					if (typeof data.wantWeeklyMailing == "string" || data.wantWeeklyMailing === null){
						data.wantWeeklyMailing = data.wantWeeklyMailing == "1" ? true: false;
						if (data.wantWeeklyMailing){
							checked = data.wantWeeklyMailing;
							disabled = true;
						}
						else {
							checked = true;
						}
					}
					obj.get(0).checked = checked;
					obj.get(0).disabled = disabled; 
				}
			}
		});
}

function newsletterSubscribe(urlRegister, urlProfile, urlSetNewsletter){
	var url = urlRegister;
	
	var email = $('#newsletter_email').val();
	
	if (email.indexOf(' ') >= 0 || email.indexOf('@') < 0) {
		email = '';
	}

	var suffix = '?height=500&width=600&forward=profile&KeepThis=true&TB_iframe=true';
	if (email !== '') {
		suffix = '?email='+email+'&height=500&width=600&forward=profile&KeepThis=true&TB_iframe=true';
	}
	
	var obj = null;
	if (loggedIn){
		var set = 0;
		obj = $("#newsletter_nieuwsoverzicht");
		if (obj.length == 1){
			obj = obj.get(0);
			if (obj.checked && !obj.disabled){
				suffix += "&wantDailyMailing=1";
				set++;
			}
		}
		obj = $("#newsletter_achtergrond");
		if (obj.length == 1){
			obj = obj.get(0);
			if (obj.checked && !obj.disabled){
				suffix += "&wantWeeklyMailing=1";
				set++;
			}
		}
		if (set == 0){		
			url = urlProfile;
		}
		else {
			url = urlSetNewsletter;
		}
	}
	tb_show('', url + suffix, false);
}

var synchronizedRegisterNewsLetter = false;

function syncRegisterNewsletter(){
	if (!synchronizedRegisterNewsLetter){
		var obj = null;
		var objReg = null;
		obj = $("#newsletter_email");
		objReg = $("#register_email");
		if (obj.length == 1 && objReg.length == 1){
			var email = obj.get(0);
			if (email.title != email.value){
				objReg.get(0).value = email.value;
			}
		}
		obj = $("#newsletter_nieuwsoverzicht");
		objReg = $("#register_dailyMailing");
		if (obj.length == 1 && objReg.length == 1){
			objReg.get(0).checked = obj.get(0).checked;
		}
		obj = $("#newsletter_achtergrond");
		objReg = $("#register_weeklyMailing");
		if (obj.length == 1 && objReg.length == 1){
			objReg.get(0).checked = obj.get(0).checked;	
		}
		synchronizedRegisterNewsLetter = true;
	}
}
