/*
 * Realityweb javascript functions
 * http://www.realityweb.nl
 *
 * Copyright 2010, Realityweb
 * Written by Bob Olde Hampsink
 */
 
function goToPage(page) {
	$('#news li').hide();
	$('#news li').eq((page*3)).show();
	$('#news li').eq((page*3)+1).show();
	$('#news li').eq((page*3)+2).show();
}

$(function() {

	$('ul.toplevel > li').each(function(index) {
		if($(this).children('ul').size()) {
			$(this).children('a').click(function(e) {
				e.preventDefault();
			});
			$(this).mouseleave(function() {
				$(this).children('a').removeClass('hover');
				$(this).children('ul').hide('blind');
			});
			$(this).bind('mouseenter click', function() {
				$('ul.sublevel:visible').not($(this).children('ul')).hide();
				$(this).children('a').addClass('hover');
				$(this).children('ul').show('blind');
			});
		}
	});
	
	if($('#news li').size() > 3) {
		$('.volgende').css('display', 'block !important');
	}
	
	$('#nav a').click(function() {
		$('#nav a').removeClass('active');
		$(this).addClass('active');
		var page = parseInt($(this).text()) - 1;
		goToPage(page);
	});
	
	$('#nav li:last a').click(function() {
		$('.volgende').css('display', 'none !important');
		$('.vorige').css('display', 'block !important');
	});
	
	$('#nav li:first a').click(function() {
		$('.vorige').css('display', 'none !important');
		$('.volgende').css('display', 'block !important');
	});
	
	$('.volgende').click(function() {
		$('.vorige').css('display', 'block !important');
		var page = parseInt($('#nav a.active').text());
		if((page*3) < $('#news li').size()) {
			$('#nav a.active').parent('li').next('li').children('a').addClass('active');
			$('#nav a.active:eq(0)').removeClass('active');
			goToPage(page);
		}
		if(((page+1)*3) >= $('#news li').size()) {
			$(this).css('display', 'none !important');
		}
	});
	
	$('.vorige').click(function() {
		$('.volgende').css('display', 'block !important');
		var page = parseInt($('#nav a.active').text()) - 2;
		if(page == 0) {
			$(this).css('display', 'none !important');
		}	
		if(page >= 0) {
			$('#nav a.active').parent('li').prev('li').children('a').addClass('active');
			$('#nav a.active:eq(1)').removeClass('active');	
			goToPage(page);
		}
	});
	
	$('#slider').nivoSlider();
	
	$.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=pakhuisopslag&count=3&callback=?', function(data) {
		for(var i=0; i<data.length; i++) {
			tweet = data[i].text;
			if(data[i].text.length > 60) {
				tweet = data[i].text.substr(0, 60) + '...';
			}
			$('#tweets').append('<li><a href="http://www.twitter.com/pakhuisopslag/status/' + data[i].id_str + '" target="_blank">' + tweet + '</a></li>');
		}				
	});
	
	$('#laatbellen a').click(function() {
		$a = $(this);
		$.post(window.location.href, {
			call: 1,
			naam: $a.prevAll('input').eq(1).val(),
			telnr: $a.prevAll('input').eq(0).val()
		}, function(transport) {
			if(transport == '100') {
				$a.prevAll(':not(h2)').remove();
				$a.remove();
				$('#laatbellen h2').after('<p>Bedankt voor het invullen. We nemen zo spoedig mogelijk contact met u op!</p>');
			} else {
				if(!$('#laatbellen p').size()) {
					$('#laatbellen h2').after('<p>U heeft &eacute;&eacute;n of meerdere verplichte velden niet (correct) ingevuld!</p>');
				}
			}
		});
	});
	
	$('#vertelverder a').click(function() {
		$a = $(this);
		$.post(window.location.href, {
			tell: 1,
			naam: $a.prevAll('input').eq(1).val(),
			email: $a.prevAll('input').eq(0).val()
		}, function(transport) {
			if(transport == '100') {
				$a.prevAll(':not(h2)').remove();
				$a.remove();
				$('#vertelverder h2').after('<p>Bedankt voor het verder vertellen! Uw vriend of kennis heeft een mail ontvangen.</p>');
			} else {
				if(!$('#vertelverder p').size()) {
					$('#vertelverder h2').after('<p>U heeft &eacute;&eacute;n of meerdere verplichte velden niet (correct) ingevuld!</p>');
				}
			}
		});		
	});

});
