// JavaScript Document


function tickInit() {

	placeElements();
	window.onresize = placeElements;

	processPictureLinks();
	attachKeyHandlers();
	if (editmode == '') ajaxContent();


	if ($('body').hasClass('home') && (editmode=='')) playIntro();
	createProjectNavigation();
	if (editmode) processFilesButton();
	if (editmode=='') createContentEvent();

	topmenuFunctions();





}
var projectPlayerInterval;


function topmenuFunctions() {

	$('#topmenu ul.menulevel2').hover(function(){
	},function(){
		$('ul.currentMenu').removeClass('currentMenu').hide('slow');
	});
	$('#topmenu a.linklevel1').hover(function(){

		var a = this;
		$('#topmenu ul.currentMenu').each(function(){if ($(a).attr('href')!=$(this).prev().attr('href'))$(this).removeClass('currentMenu').hide('slow');});
		$('ul.menulevel2',$(this).parent()).stop(true, true).show('slow').addClass('currentMenu');

	},function(){});
}

function createContentEvent() {
	if (projectpage) {
		$('#contenttitle').hover(function(){
			var html = $('#content').html();
			
			if ($.trim(html)!='' && $.trim(html)!='&nbsp;')$('#content').stop(true, true).show('slow');
		},function(){});
		$('#contentdiv').hover(function(){},function(){
			$('#content').stop(true, true).hide('slow');
		});
	}
}

function stopPlaying() {
	if (playing) {
		$('#projectNavigation a.play').addClass('stopped').attr('title','start PixelPoolShow');
		playing = false;
		clearInterval(projectPlayerInterval);
		randomProjects = null;
	}
}

function projectPlayer(first) {

	if (playing) {
		$('#projectNavigation a.play').removeClass('stopped').attr('title','stop PixelPoolShow');
		if (!randomProjects) {
			prepareProjects();
			nextProject(first);
		}
		else {
			nextProjectPicture();
		}
	}
}



function placeElements() {
	var height = (Math.round(($('#srolldiv').height()-725)/2));
	var left = (Math.round(($('#srolldiv').width()-672)/2));
	$('#wrapper').css('top',height+'px').css('marginLeft',left+'px').show();
	$('#logo').css('left',(left-106)+'px').css('top',height+725-50-172+'px').show();
	$('address').css('left',left+'px').css('top',height+680+'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).css('left','0px').fadeIn('slow',function(){
			$('#contentcontainer').css('backgroundImage','url('+$(a).attr('href')+')');
			$('#projectImages li a.active').removeClass('active');
			$(a).addClass('active');
			$(whitener).css('left','0px').fadeOut('slow',function(){
				$(whitener).css('left','-4000px');
			});
		});

		return false;
	}).mouseup(function(){stopPlaying();});
	if (!playing) $('#projectImages li:first a').click();
	else {
		prepareProjectPictures();
		nextProjectPicture();
	}
}

function ajaxContent() {

	$('#topmenu a').mouseup(function(){stopPlaying();}).click(function(){



		var a = this;
		// alert($(this).parents('li.menuitemlevel1').attr('id') + ':' + projectpage);
		$.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('left','0px').fadeIn('slow',function(){
					document.getElementById('ajax_content').innerHTML=data;
					ajaxContent();
					topmenuFunctions();
					processPictureLinks();
					createContentEvent();
					$(whitener).css('left','0px').fadeOut('slow',function(){
						$(whitener).css('left','-4000px');
					});
				});
			}
			else {
				document.getElementById('ajax_content').innerHTML=data;

				ajaxContent();
				topmenuFunctions();
				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 nextProjectClick();
}

function prevProjectPictureClick() {
	var index = $('#projectImages li').index($('a.active').parent());
	if (index>0) $('#projectImages li a.active').parent().prev().children('a').click();
	else prevProjectClick();
}



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 = 5;
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.linklevel2').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.linklevel2: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() {
	playing=true;
	projectPlayer(true);
	projectPlayerInterval = setInterval('projectPlayer()',8000);
}

function createProjectNavigation() {
	var ul = document.createElement('ul');
	ul.id = 'projectNavigation';
	var str = '<li><a class="prev" href="#_prev_project" title="vorige foto">vorige foto</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="volgende foto">volgende foto</a></li>';
	ul.innerHTML = str;
	if (document.getElementById('wrapper')) document.getElementById('wrapper').appendChild(ul);
	else return;
	$('#projectNavigation a.prev').click(function(){
		stopPlaying();
		prevProjectPictureClick();

	});
	$('#projectNavigation a.play').click(function(){
		if (!playing) {
			$(this).removeClass('stopped');
			playing=true;
			this.clicked=true;
			projectPlayer();
			projectPlayerInterval = setInterval('projectPlayer()',8000);
		}
		else {
			stopPlaying();
		}

	});
	$('#projectNavigation a.next').click(function(){
		stopPlaying();
		nextProjectPictureClick();


	});
}

function processFilesButton() {
	if (projectpage) {
		$('#editButtons div#files').each(function(){
			this.onclick = function() {
				filesWindow=window.open("?window=pb_upload&menuid="+menuid+"&imageonly=1&currentDir=projects/"+menuid,"filesWindow","status=yes, buttons=no, resizable, scrollbars=yes");
				filesWindow.focus();
			}
		});
	}
}


$(document).ready(function() {

	$('html').addClass('js-enabled');


});
