// JavaScript Document


function tickInit() {

	placeElements();
	window.onresize = placeElements;
	if (editmode == '') {
// 		$('#topmenu #menuitem_175 a, #topmenu #menuitem_112 a').addClass('noAjax');
		// Cufon('#contenttitle');
	// 	Cufon('#content');

	}


	processPictureLinks();
	attachKeyHandlers();

	


	
	
	if (editmode == '') {
		$('#topmenu a.linklevel3').each(function(){
			$(this).append('<span class="hover">'+$(this).text()+'</span>');
		});
		ajaxContent();
		//Cufon('#topmenu span');
	$('#topmenu #menuitem_'+projectid+' ul.menulevel2:first').prepend('<li id="playControl"><a class="noAjax" href="#play"><span class="stopped">resume slideshow</span><span class="playing">stop slideshow</span></a></li>');

	}


	if ($('body').hasClass('home') && (editmode=='')) playIntro();
	createProjectNavigation();



}
var projectPlayerInterval;

function stopPlaying() {
	if (playing) {
		$('#playControl a').addClass('stopped').attr('title','start PixelPoolShow');
		playing = false;
		clearInterval(projectPlayerInterval);
		randomProjects = null;
	}
}

function projectPlayer(first) {

	if (playing) {
		$('#playControl a').removeClass('stopped').attr('title','stop PixelPoolShow');
		if (!randomProjects) {
			prepareProjects();
			nextProject(first);
		}
		else {
			nextProjectPicture();
		}
	}
}



function placeElements() {
	var height = 360+(Math.round(($('#srolldiv').height()-360)/20))*10;
	var left = (Math.round(($('#srolldiv').width()-700)/20))*10;
	$('#wrapper').css('height',height+'px').css('margin','0').css('marginLeft',left+'px').show();
	$('#logo').css('left',(left+700-70)+'px').css('top',(height-60-18)+'px').show();
}

var whitener;

function processPictureLinks() {
	// $('#contentcontainer').css('backgroundImage','url('+$('#projectImages li:first a').attr('href')+')');
	if (!document.getElementById('whitener')) {
		whitener = document.createElement('div');
		whitener.id='whitener';
		if (document.getElementById('contentcontainer')) document.getElementById('contentcontainer').appendChild(whitener);

	}
	$('#projectImages li a').click(function(){
		var a = this;
		$(whitener).stop(true,true).css('backgroundImage',$('#contentcontainer').css('backgroundImage'));
		$(whitener).css('left','0px').fadeIn(200,function(){
			$('#contentcontainer').css('backgroundImage','url('+escape($(a).attr('href'))+')');
			$('#projectImages li a.active').removeClass('active');
			$(a).addClass('active');
			$(whitener).css('left','0px').fadeOut(2000,function(){
				$(whitener).css('left','-4000px');
			});
		});

		return false;
	}).mouseup(function(){stopPlaying();});
	if (!playing) $('#projectImages li:first a').click();
	else {
		prepareProjectPictures();
		nextProjectPicture();
	}
}

function ajaxContent() {

	$('#topmenu a:not(".noAjax")').mouseup(function(){stopPlaying();}).click(function(){


		if ($(this).hasClass('linklevel2')) {
			var link = this;
			$('#topmenu ul.jquery_visible').removeClass('jquery_visible').slideUp(300,function(){

				$('ul.menulevel3',link.parentNode).slideDown(300, function(){
					$(this).addClass('jquery_visible')
				});
			});




			return false;
		}


		var a = this;

		// alert($(this).parents('li.menuitemlevel1').attr('id') + ':' + projectpage);

		$('body').addClass('waiting');

		$.get($(this).attr('href')+'&window=content',function(data){


			if ($(a).parents('li.menuitemlevel1').attr('id')=='menuitem_'+projectid) {
				projectpage=true;
				$('#wrapper').addClass('projectpage');
				$('body').removeClass('contentpage');
			}
			else {
				projectpage=false;
				$('#wrapper').removeClass('projectpage');
				$('body').addClass('contentpage');
			}

			if (projectpage) {
				$(whitener).css('backgroundImage',$('#contentcontainer').css('backgroundImage'));
				$(whitener).css('left','0px').fadeIn('slow',function(){

					document.getElementById('contentcontainer').innerHTML=data;

					if ($(a).hasClass('linklevel3')) {
						$('#topmenu a.currentlink').removeClass('currentlink');
						$(a).addClass('currentlink');
						var shower = $(a).parent().parent();

						if (!$(shower).hasClass('jquery_visible')) {
							if ($('#topmenu ul.jquery_visible').size()===0) $(shower).addClass('jquery_visible');
							$('#topmenu ul.jquery_visible').removeClass('jquery_visible').slideUp(300,function(){
								$(shower).slideDown(300, function(){
									$(shower).addClass('jquery_visible');
								});
							});

						}
						else {
							$(shower).slideDown(300, function(){
								$(shower).addClass('jquery_visible');
							});
						}
					}


					// ajaxContent();
					// $('#topmenu ul.menulevel2 li.currenttree ul.menulevel3').addClass('jquery_visible');
					processPictureLinks();
					$('body').removeClass('waiting');
					$(whitener).css('left','0px').fadeOut('slow',function(){
						$(whitener).css('left','-4000px');
					});
					//Cufon('#contenttitle');
					//Cufon('#content');
					// Cufon('#topmenu span');

				});
			}
			else {
				document.getElementById('contentcontainer').innerHTML=data;
				// var scripts = document.getElementById('contentcontainer').getElementsByTagName('script');

				$('#linked script').each(function() {
					eval($(this).text());
				});

				$('body').removeClass('waiting');
					//Cufon('#contenttitle');
					// Cufon('#content');
				// ajaxContent();
				processPictureLinks();
			}


		});
		var url = $(this).attr('href').split('?');
		url = url[1];


		// this.href='#_' + url;
		return false;
	});
}






function attachKeyHandlers() {
	if (editmode!='') return ;

	$(document).keyup(function(event){


		switch(event.keyCode) {
	   		case 37: // previous
	   			if (!projectpage) break;
	   			stopPlaying();
	   			prevProjectPictureClick();
	   			break;
	   		case 39: //next
	   			if (!projectpage) break;
	   			stopPlaying();
	   			nextProjectPictureClick();
	   			break;
	   		case 38: // previous project
	   			if (!projectpage) break;
	   			stopPlaying();
	   			prevProjectClick();
	   			break;
	   		case 40: // next project
	   			if (!projectpage) break;
	   			stopPlaying();
	   			nextProjectClick();
	   			break;

	   	}
	});

}
function nextProjectPictureClick() {
	var index = $('#projectImages li').index($('a.active').parent());
	if (index<($('#projectImages li').size()-1)) $('#projectImages li a.active').parent().next().children('a').click();
	else $('#projectImages li:first a').click();
}

function prevProjectPictureClick() {
	var index = $('#projectImages li').index($('a.active').parent());
	if (index>0) $('#projectImages li a.active').parent().prev().children('a').click();
	else $('#projectImages li:last a').click();
}



function prevProjectClick() {
	var index = $('#topmenu #menuitem_'+projectid+' ul a:not(.linklevel2)').index($('a.currentlink'));
	if (index>0) $('#topmenu #menuitem_'+projectid+' ul a:not(.linklevel2):eq('+(index-1)+')').click();
	else $('#topmenu #menuitem_'+projectid+' ul a:last').click();
}

function nextProjectClick() {
	var index = $('#topmenu #menuitem_'+projectid+' ul a:not(.linklevel2)').index($('a.currentlink'));
	if (index<( $('#topmenu #menuitem_'+projectid+' ul a:not(.linklevel2)').size()-1)) $('#topmenu #menuitem_'+projectid+' ul a:not(.linklevel2):eq('+(index+1)+')').click();
	else $('#topmenu #menuitem_'+projectid+' ul a:first').click();
}

var picturesPerView = 1;
var playing = false;
var randomProjects;
var randomProjectPictures;

function randomizeArray(array) {
  var i = array.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = array[i];
     var tempj = array[j];
     array[i] = tempj;
     array[j] = tempi;
   }
   return array;
}


function prepareProjects() {
	var projects = $('#topmenu #menuitem_'+projectid+' a.linklevel3').size();
	randomProjects=new Array();
	for(var k=0; k<projects; k++) {
		randomProjects[k]=k;
	}
	randomProjects = randomizeArray(randomProjects);

}
function prepareProjectPictures() {
	var pictures = $('#projectImages li').size();
	if (pictures>0) {
		randomProjectPictures=new Array();
		for(var k=0; (k<pictures); k++) {
			randomProjectPictures[k]=k;
		}
		randomProjectPictures = randomizeArray(randomProjectPictures);
		randomProjectPictures = randomProjectPictures.slice(0,picturesPerView);
	}

}


function removeFromArrayByValue(array,val) {
	for (var k=0; k<array.length; k++) {
		if (array[k]===val)array.splice(k,1);
	}
	return array;
}

function nextProject(first) {
	randomProjectPictures = null;
	if (randomProjects.length>0){
		if (first) {
			var projectIndex = 0;
			randomProjects = removeFromArrayByValue(randomProjects,0)
		}
		else {
			var projectIndex = randomProjects.pop();
		}

		$('#topmenu #menuitem_'+projectid+' a.linklevel3:eq('+projectIndex+')').click();
	}
	else stopPlaying();
}

function nextProjectPicture() {

	if (randomProjectPictures && randomProjectPictures.length>0) {
		var pictureIndex = randomProjectPictures.pop();
		$('#projectImages li:eq('+pictureIndex+') a').click();

	}
	else nextProject();
}

function playIntro() {
	var div = document.createElement('div');
	div.id = 'intro';
	document.getElementById('wrapper').appendChild(div);
	playing=true;
	projectPlayer(true);

	$('#intro').click(function(){
		$(this).remove();
	})

	setTimeout(function(){
		$('#intro').animate({opacity: '0'},500,'',function(){
		$(this).remove();
		$('body').removeClass('home');
		projectPlayerInterval = setInterval('projectPlayer()',9000);
	});

	},5000);
}

function createProjectNavigation() {
	var ul = document.createElement('ul');
	ul.id = 'projectNavigation';
	var str = '<li><a class="prev" href="#_prev_project" title="vorig projectbeeld">vorig projectbeeld</a></li>';
	str += '<li><a class="play stopped" href="#_toggle_play" title="stop">play/stop</a></li>';
	str += '<li><a class="next" href="#_next_project" title="volgend projectbeeld">volgende projectbeeld</a></li>';
	ul.innerHTML = str;
	if (document.getElementById('wrapper')) document.getElementById('wrapper').appendChild(ul);
	else return;
	$('#projectNavigation a.prev').click(function(){
		stopPlaying();
		prevProjectPictureClick();

	});
	$('#playControl a').click(function(){
		if (!playing) {
			$(this).removeClass('stopped');
			playing=true;
			this.clicked=true;
			projectPlayer();
			projectPlayerInterval = setInterval('projectPlayer()',9000);
		}
		else {
			stopPlaying();
		}

	});
	$('#projectNavigation a.next').click(function(){
		stopPlaying();
		nextProjectPictureClick();


	});
}




$(document).ready(function() {

	$('html').addClass('js-enabled');


});

