$.fn.toSlot = function(d){
	$(this).each(function(){
		var max = $(this).find('li:last').index();
		var random=Math.random()*(max - 1);
		randomElement = $(this).find('li').removeClass('selected').eq(Math.ceil(random)).addClass('selected');
		var elementHeight = ($(this).parents('.matrix_cont').innerHeight() - $(this).find('li').outerHeight()) / 2;
		offsetTop = randomElement.position().top - elementHeight;
		$(this).animate({'top': + (offsetTop*-1) + 'px'}, 200)
	})
}


$(function(){

	// random image
	bgImageTotal=3;
	randomNumber = Math.round(Math.random()*(bgImageTotal-1))+1;
	imgPath = ('/site/gfx/bg_text_0'+randomNumber+'.gif');

	$('body').css('background-image', ('url("'+imgPath+'")'));
	
	randomNumber = ++randomNumber;
	imgPath2 = ('/site/gfx/bg_text_0'+randomNumber+'.gif');

	$('#tab2 .bg').css('background-image', ('url("'+imgPath2+'")'));
	
    //Load speelhal ascynchronosly
	$('#tab1 div.content').load("/site/ajax/speelhal.php", function(response, status, xhr) {
		tabHeight();
		if (status == "error") {
			var msg = "Sorry but there was an error: ";
			alert(msg);
		} else {
			// matrix
			$('.handle').click(function(){
				$(this).prev().find('.matrix').toSlot();
				return false;
			})
			$('.matrix li:not(.selected)').click(function(){
				var elementHeight = ($(this).parents('.matrix_cont').innerHeight() - $(this).outerHeight()) / 2;
				offsetTop = $(this).position().top - elementHeight;
				$(this).parent().animate({'top': + (offsetTop*-1) + 'px'}, 200)
				$(this).addClass('selected').siblings().removeClass('selected');
		
			})
			$('#blurbmatrix p').expander({
				slicePoint: 200,
				expandText: 'lees meer',
				userCollapseText: 'lees minder'
			});
			$('#blurbmatrix .read-more a, .re-collapse a').click(function(){
				tabHeight();
			})
			// temp
			$('table tr').each(function(){
				$('.cont').append('<li>' + $(this).find('span:eq(2)').text() + '</li>\n')
			})
			// split
			$('#textometer .analyze').click(function(){
				$.ajax({
					type:"POST",
					url:"/site/ajax/split.php",
					data: "text=" + $('#textometer textarea').val(),
					success: function(msg){
						var	height = $('#textometer textarea').outerHeight() - 40;
						if(msg < 20){
							classVal = 'val_0';
						} else if(msg > 20 && msg < 40){
							classVal = 'val_20';
						} else if(msg > 40 && msg < 60){
							classVal = 'val_40';
						} else if(msg > 60 && msg < 80){
							classVal = 'val_60';
						} else {
							classVal = 'val_80';
						}
						$('.analyze').fadeOut(100, function(){
							$(this).next().fadeIn(100);
						});
						$('.overlay').css({'height' : height}).slideDown(200);
						$('#' + classVal).show();
						//console.log(msg)
						$('#textometer').addClass('result');
					}
				})
				return false;
			})
			$('.retry').click(function(){
				$('.overlay').slideUp(200).parent().removeClass('result');
				$('.overlay > div').hide();
				$(this).fadeOut(100, function(){
					$(this).prev().fadeIn(100);
				});
			})
		}  
	}); 	
	
	$('.tab.closed').live('mouseenter', function(){
		$(this).css('cursor','pointer').stop().animate({left:-770}, 100)
	}).live('mouseleave', function(){
		$(this).css('cursor','default').stop().animate({left:-790}, 100);
	});
	
	$('.tab.closed').live('click', function(){
		$(this).css('cursor','default').animate({left:0}, 200).removeClass('closed');
		$.address.value($(this).attr('id'));
		$('#clickbg').addClass('behind');
		//$('.tab').unbind('mouseenter mouseleave');
	});
	
	$('.toggle_tab').live('click', function(event){
		
		event.preventDefault();
			
		if($('.tab').is('.closed'))
		{
			$('.tab').animate({left:0}, 200).removeClass('closed').css('cursor','default');
			$('#clickbg').addClass('behind');
			$.address.value($('.tab').attr('id'));
		
		}else
		{
			$('.tab').animate({left:-790}, 200, function(){
				$('.tab').addClass('closed');
				$('#clickbg').removeClass('behind');
				$.address.value($('.tab').prev().attr('id'));
			});
		}
	});
	
	$('#clickbg.behind').live('click', function(){
	
		if($('.tab').not('.closed'))
		{
			$('.toggle_tab').trigger('click');
		}
		
	})

	// meer nieuws berichten
	$('.more_toggle, .less_toggle').click(function(){
		$('.more, .more_toggle').toggle();
		tabHeight();
		return false;
	})
	// form submit
	$('form a').click(function(){
		$(this).closest('form').submit();
	})
	
	$('#center').localScroll({
		duration:300
	});
	
	$.address.init(function(event){
		var hash = event.value.replace('/', '#')
		if(hash != '#'){
			openTab(hash);
		} else {
			var url = window.location.toString().search(/introductie/i);
			if(url != -1){
				openTab('#tab0')
			}
		}	
		tabHeight();
	})
	$(window).resize(function(){
		tabHeight();
	})

})

// height tabs
function tabHeight(){
	$('section').each(function(){
		var openTab = $('section:not(.closed, .behind) .content').outerHeight();
		if(openTab <= $(window).height()){
			var height = $(window).height();
			//debug.log('tab <= window height. Tab: '+openTab+' window: '+height);			
		} else {
			var height = openTab;
			//debug.log('tab > window height. Tab: '+height);						
		}
		$(this).height(height);
		
		//debug.log(height);
	})
}

function openTab(id){
	
	$(id).removeClass('closed');
	$('#clickbg').addClass('behind');

}
