$(function(){
	$('.list h3').click(function(){
		if($(this).next().is(':visible')){
			$(this).next().slideToggle(200).parent().toggleClass('open');
		} else {
			$('.content:visible').slideUp().parent().toggleClass('open');
			$(this).next().slideToggle(200).parent().toggleClass('open');
		}
	})
	$('.disclaimer, .contact').colorbox({
		initialHeight: '100',
		initialWidth: '400',
		transition: 'none',
		width: '400'
	});
	$('#links .add, article.center a').colorbox({
		inline:true, 
		href:"#form_add",
		transition: 'none',
		title: false
	})
	$('article .email_friends').colorbox({
		inline:true, 
		href:"#form_friends",
		transition: 'none',
		title: false
	})
	// track analytics
	$("article.last li a").not('.voorbeeld').click(function(){
 		pageTracker._trackPageview('/outgoing/'+ $(this).attr('href'));
	});
	var height = 0;
	var i = 0
	$('#trio article').each(function(){
		if($(this).height() >= height){
			height = $(this).height()
		}
		if(i == $('#trio article:last').index()){
			$('#trio article').height(height);			
		}
		i++
	})
	$('#confirmation').slideDown(600).delay(5000).slideUp(1000);
	$('.hash_links').localScroll({duration: 700});
	$('#links #more_less').toggle(function(){
		$(this).parents('#links').addClass('shown');
		$('.more:first').addClass('less').next().removeClass('less');
		return false
	}, function(){
		$('.more:last').addClass('less').prev().removeClass('less');
		$(this).parents('#links').removeClass('shown');
	})
		$('article.last a.voorbeeld:last').click(function(){
		$('#ie6_overlay_bg, #ie6_overlay').fadeIn(100);
		$('body, html').css({'overflow': 'hidden', 'height': '100%', 'width': '100%'})
		$('#ie6_overlay').after('<p class="close_overlay">sluit voorbeeld</p>')
	})
	$('.close_overlay').live('click', function(){
		$('#ie6_overlay_bg, #ie6_overlay').fadeOut(100);
		$('body, html').removeAttr('style');
		$('.close_overlay').remove();
		$.scrollTo('#wat_nu', 300);
		return false;
	})

	$('article.last a.voorbeeld:first').click(function(){
		$('#ie6_banner').slideDown(100);
		$.scrollTo('#top', 300);
		$('#ie6_banner').after('<p class="close_down">sluit voorbeeld en ga terug naar beneden</p>')
		return false;
	})
	$('.close_down').live('click', function(){
		$.scrollTo('#wat_nu', 300);
		$('#ie6_banner').slideUp(100);
		$('.close_down').remove();
	})


	$('#add_submit').bind({
		click: function() {
		signIn();
		return false;
		}
	});
	$('#friends_submit').bind({
		click: function() {
		sendIn();
		return false;
		}
	});
	
    $("#add_form").keypress(function(e) {
	  if (e.which == 13) {
	    return false;
	  }
	});

	
	$("#friends_form").keypress(function(e) {
	  if (e.which == 13) {
	    return false;
	  }
	});
	
	
})




function signIn()
{
	var error = false;
	
	var $inputs = $('#add_form :input');
    var values = {};
    $inputs.each(function() {
        values[this.name] = $(this).val();
       
    });
	if(values['naam'].length < 2) {
		error = true;
		toggleErrorLabel('label_naam', 'on');
	}else
	{
		toggleErrorLabel('label_naam');
	}
	if(values['url'].length < 2) {
		error = true;
		toggleErrorLabel('label_url', 'on');
	}else
	{
		toggleErrorLabel('label_url');
	}
    if(!isValidEmail(values['email']) || values['email'].length < 2){
    	error = true;
    	toggleErrorLabel('label_email', 'on');
    }else{
    	toggleErrorLabel('label_email');
    }
    if(!error)
    {
    	$('#form_add').css({'background': 'url(/site/gfx/loading.gif) no-repeat center center', 'height': '100%'}).empty();
	
		$.ajax({
		type: 'POST',
		url: '/site/ajax/entry.php',
		data: (values),
		dataType: 'html',
		async: false,
		success: function(html){
			$('#form_add').css({'background': '#fff'}).html(html);
			$('#form_add').animate({'opacity': 1},5000, function(){
				$.fn.colorbox.close()
			})
		}
		});
    }
}

function sendIn()
{
	var error = false;
	var errorfriends = false;
	
	var $inputs = $('#friends_form :input');
    var values = {};
    $inputs.each(function() {
        values[this.name] = $(this).val();
       
    });
	if(values['naam_vriend'].length < 2) {
		error = true;
		toggleErrorLabel('label_naam_vriend', 'on');
	}else
	{
		toggleErrorLabel('label_naam_vriend');
	}
    if(!isValidEmail(values['email_vriend']) || values['email_vriend'].length < 2){
    	error = true;
    	toggleErrorLabel('label_email_vriend', 'on');
    }else{
    	toggleErrorLabel('label_email_vriend');
    }
    var emails = values['email_vrienden'].split(',');

	for ( var i in emails )
	{
    	//alert( emails[i] );
    	if(!isValidEmail(emails[i])){
    		error = true;
    		errorfriends = true;
    		toggleErrorLabel('label_email_vrienden', 'on');
   		}
	}
	if(!errorfriends)
	{
		toggleErrorLabel('label_email_vrienden');
	}
    
    if(!error)
    {
    	$('#form_friends').css({'background': 'url(/site/gfx/loading.gif) no-repeat center center', 'height': '100%'}).empty();
	
		$.ajax({
		type: 'POST',
		url: '/site/ajax/friends.php',
		data: (values),
		dataType: 'html',
		async: false,
		success: function(html){
			$('#form_friends').css({'background': '#fff'}).html(html);
			$('#form_friends').animate({'opacity': 1},5000, function(){
				$.fn.colorbox.close()
			})
		}
		});
    }
}

function toggleErrorLabel(label,state) {
	if(state == 'on') {
		document.getElementById(label).className = "errorlabel";
	}else{
		document.getElementById(label).className = "normallabel";
	}
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
 
}