$(document).ready(function(){
	$('.back').css({
		cursor:'pointer'
	});
	$('.back').click(function(){
		history.back();
	});
	$('.slideshow').each(function(){
		var data = eval('('+$(this).attr('ssData')+')');
		$('.ssImage').css({visibility:'visible'});
		$(this).cycle(data);
		$(this).height(data.height);
		$(this).width(data.width);
	});

	var slider = {};
	slider.width = 689;
	slider.index = 0;
	slider.id = 'tableHomeNews';
	slider.moving = false;

	function next(elem){
		do{
			elem = elem.nextSibling;
		} while (elem && elem.nodeType != 1);
		return elem;
	}

	function prev(elem){
		do{
			elem = elem.previousSibling;
		} while (elem && elem.nodeType != 1);
		return elem;
	}

	function first(elem){
		elem = elem.firstChild;
		return elem && elem.nodeType != 1 ? next(elem) : elem;
	}

	function last(elem){
		elem = elem.lastChild;
		return elem && elem.nodeType != 1 ? prev(elem) : elem;
	}

	$("#right").click(function(){ 
		if(!slider.moving){
			var el = document.getElementById(slider.id);
			el = first(first(el));
			var td = first(el).cloneNode(true);
			el.appendChild(td);
			slider.moving = true;
		
			$("#"+slider.id).animate({left: -slider.width}, 500, 'swing', function(){
				var table = document.getElementById(slider.id);
				var el = first(first(table));
				var td = first(el);
				td.parentNode.removeChild(td);
				table.style.left = 0;
				slider.moving = false;
			});
		}
	});	
	$("#left").click(function(){
		if(!slider.moving){
			var table = document.getElementById(slider.id);
			el = first(first(table));
			var td = last(el).cloneNode(true);
			var f = first(el);
			el.insertBefore(td, f);
			table.style.left = '-'+slider.width+'px';
			slider.moving = true;
			$("#"+slider.id).animate({left: 0}, 500, 'swing', function(){
				var table = document.getElementById(slider.id);
				var el = first(first(table));
				var td = last(el);
				td.parentNode.removeChild(td);
				slider.moving = false;
			});
		}
	});
	$(".moveRight, .moveLeft").mouseover(function() {
		$(this).toggleClass('moveHover');
	});
	$(".moveRight, .moveLeft").mouseout(function() {
		$(this).toggleClass('moveHover');
	});
	
	function slideReferenties() {
		if(!slider.moving){
			var el = document.getElementById(slider.id);
			el = first(first(el));
			var td = first(el).cloneNode(true);
			el.appendChild(td);
			slider.moving = true;		
			$("#"+slider.id).animate({left: -slider.width}, 600, 'swing', function(){
				var table = document.getElementById(slider.id);
				var el = first(first(table));
				var td = first(el);
				td.parentNode.removeChild(td);
				table.style.left = 0;
				slider.moving = false;
				setTimeout(function() {
					slideReferenties()
				}, 4000);
			});
		}
	}
	setTimeout(function() {
		slideReferenties()
	}, 4000);

	$('.inputText, .inputTextarea').focus(function() {
		var id = $(this).attr('id');
		var value = $(this).val();
		if(value == id) {
			$(this).val('');
		}
	});
	$('.inputText, .inputTextarea').blur(function() {
		var id = $(this).attr('id');
		var value = $(this).val();
		if(value == '') {
			$(this).val(id);
		}
	});
	$('.submit').mouseover(function() {
		$(this).toggleClass('submitHover');
	});
	$('.submit').mouseout(function() {
		$(this).toggleClass('submitHover');
	});

	


	var refs = {};
	refs.width = 177;
	refs.index = 0;
	refs.id = 'photoShow';
	refs.moving = false;

	$("#volgende").click(function(){ 
		if(!refs.moving){
			var el = document.getElementById(refs.id);
			el = first(first(el));
			var td = first(el).cloneNode(true);
			el.appendChild(td);
			refs.moving = true;
			$("#"+refs.id).animate({left: -refs.width}, 500, 'swing', function(){
				var table = document.getElementById(refs.id);
				var el = first(first(table));
				var td = first(el);
				td.parentNode.removeChild(td);
				table.style.left = 0;
				refs.moving = false;
				addFB();
			});
		}
	});
	$("#vorige").click(function(){
		if(!refs.moving){
			var table = document.getElementById(refs.id);
			el = first(first(table));
			var td = last(el).cloneNode(true);
			var f = first(el);
			el.insertBefore(td, f);
			table.style.left = '-'+refs.width+'px';
			refs.moving = true;
			$("#"+refs.id).animate({left: 0}, 500, 'swing', function(){
				var table = document.getElementById(refs.id);
				var el = first(first(table));
				var td = last(el);
				td.parentNode.removeChild(td);
				refs.moving = false;
				addFB();
			});
		}
	});
	function addFB(){
		$('.pagePhotoImg').click(function(){
			fb.start(document.getElementById($(this).attr('id')));
			return false;
		});
	}
	addFB();
});
