var currPhoto = 0,
	allPhotos = [
	 				{src: 'mode-1', type: 1}, 
					{src: 'mode-2', type: 1},
					{src: 'mode-3', type: 1}, 
					{src: 'mode-4', type: 1},
					{src: 'fam-12', type: 1}, 
					{src: 'fam-2', type: 1},
					{src: 'fam-9', type: 1}, 
					{src: 'fam-3', type: 1}, 
					{src: 'fam-4', type: 1}, 
					{src: 'fam-5', type: 1}, 
					{src: 'fam-6', type: 1}, 
					{src: 'fam-8', type: 1}, 
					{src: 'fam-10', type: 1}, 
					{src: 'fam-11', type: 1},
					{src: 'fam-1', type: 1},
					{src: 'loc-10', type: 1}, 
					{src: 'loc-11', type: 1}, 
					{src: 'loc-12', type: 1}, 
					{src: 'loc-13', type: 1}, 
					{src: 'loc-14', type: 1}, 
					{src: 'loc-4', type: 1}, 
					{src: 'loc-5', type: 1}, 
					{src: 'loc-6', type: 1}, 
					{src: 'loc-7', type: 1}, 
					{src: 'loc-8', type: 1},
					{src: 'loc-9', type: 1},
					{src: 'loc-1', type: 1}, 
					{src: 'loc-2', type: 1}, 
					{src: 'loc-3', type: 1},
					{src: 'jugend-1', type: 1}, 
					{src: 'jugend-2', type: 1}, 
					{src: 'jugend-3', type: 1}, 
					{src: 'jugend-4', type: 1}, 
					{src: 'jugend-5', type: 1}, 
					{src: 'jugend-6', type: 1}, 
					{src: 'jugend-7', type: 1}, 
					{src: 'jugend-8', type: 1}, 
					{src: 'jugend-11', type: 1},
					{src: 'jugend-13', type: 1},
					{src: 'jugend-12', type: 1},
					{src: 'hochzeit-2', type: 1}, 
					{src: 'hochzeit-1', type: 1}, 
					{src: 'hochzeit-19', type: 1}, 
					{src: 'hochzeit-17', type: 1}, 
					{src: 'hochzeit-16', type: 1}, 
					{src: 'hochzeit-20', type: 1}, 
					{src: 'hochzeit-4', type: 1}, 
					{src: 'hochzeit-5', type: 1}, 
					{src: 'hochzeit-6', type: 1}, 
					{src: 'hochzeit-7', type: 1}, 
					{src: 'hochzeit-8', type: 1},
					{src: 'hochzeit-9', type: 1},
					{src: 'hochzeit-10', type: 1}, 
					{src: 'hochzeit-11', type: 1}, 
					{src: 'hochzeit-12', type: 1}, 
					{src: 'hochzeit-13', type: 1}, 
					{src: 'hochzeit-14', type: 1}, 
					{src: 'hochzeit-18', type: 1}, 
					{src: 'hochzeit-2', type: 1}, 
					{src: 'hochzeit-3', type: 1},
					{src: 'baby-1', type: 1}, 
					{src: 'baby-2', type: 1}, 
					{src: 'baby-3', type: 1}, 
					{src: 'baby-4', type: 1}, 
					{src: 'baby-5', type: 1}, 
					{src: 'baby-6', type: 1}, 
					{src: 'baby-7', type: 1}, 
					{src: 'baby-8', type: 1}, 
					{src: 'baby-9', type: 1}, 
					{src: 'baby-10', type: 1},
					{src: 'werbung-3', type: 1}, 
					{src: 'werbung-10', type: 1}, 
					{src: 'werbung-11', type: 1}, 
					{src: 'werbung-1', type: 1}, 
					{src: 'werbung-2', type: 1}, 
					{src: 'werbung-4', type: 1}, 
					{src: 'werbung-5', type: 1}, 
					{src: 'werbung-6', type: 1}, 
					{src: 'werbung-8', type: 1}, 
					{src: 'werbung-9', type: 1}
				],
	photoArray = allPhotos,
	timespan = 5000;
	
/*
function preLoadImages() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
		$('<img />')
		    .attr('src', "media/"+arguments[i]+".jpg")
		    .load(function(){
			    photoArray.push(this);
		        // Your other custom code
		    });
    }
}

$(function(){
	preLoadImages('mode-1', 'hochzeit-1', 'baby-2', 'baby-12');
});*/



function togglePhoto() {
	
	if($('#overlay').css('display') == 'none') 
	{
		$('#menu li ul').addClass('hide');
		$('#overlay').fadeIn(); //{ to: 0.5 }.removeClassName('hide');
		$('#foto').fadeIn(); //.removeClassName('hide');
	}
	else
	{
		$('#menu li ul').removeClass('hide');
		$('#overlay').hide('fade'); //.addClassName('hide');
		$('#foto').hide('fade'); //.addClassName('hide');
	}
	
	return false;
}

function previousPhoto(autoChangeOff) {
	if(currPhoto-1>=0)
		currPhoto--;
	else
		currPhoto = photoArray.length-1;
	
	changePhoto(photoArray[currPhoto], autoChangeOff);
	return false;
}

function nextPhoto(autoChangeOff, nextId) {
	if(currPhoto+1<photoArray.length)
		currPhoto++;
	else
		currPhoto = 0;
	if(nextId != null)
		currPhoto = nextId;
	
	changePhoto(photoArray[currPhoto], false);
	return false;
}

var changeTimer;
function changePhoto(obj, autoChangeOff, animated) {
	clearTimeout(changeTimer);
	if(obj.type == 1)
	{
		var myPic = new Image();
		myPic.src = "media/"+obj.src+".jpg";
		myPic.onload = function(e) {
		  $('#mainPhoto').attr('src', "media/"+obj.src+".jpg");
		window.location.hash = '#'+obj.src;
		$('fb:like').attr('href', window.location.href);

		  if(!autoChangeOff)
			changeTimer = setTimeout("nextPhoto()", timespan);
		}
	}
	
}


document.onkeydown = function(e) {
	switch(e.which) {
		case 37: previousPhoto();	break;
		case 39: nextPhoto();		break;
	}
}

changeTimer = setTimeout("nextPhoto()", timespan);




$(function(){
	
		$('li.click').click(function(){
			location.href = $('a:first', $(this)).attr('href');
			return false;
		});
	
	$('#cMailBox').click(function(e) {
		if(e.originalTarget == this)
			$(this).hide();
		return false;
	});
	
	
	$('#fMenuHz a').click(function(){
		// show Hochzeiten
		photoArray = [
			{src: 'hochzeit-2', type: 1}, 
			{src: 'hochzeit-1', type: 1}, 
			{src: 'hochzeit-19', type: 1}, 
			{src: 'hochzeit-17', type: 1}, 
			{src: 'hochzeit-16', type: 1}, 
			{src: 'hochzeit-20', type: 1}, 
			{src: 'hochzeit-4', type: 1}, 
			{src: 'hochzeit-5', type: 1}, 
			{src: 'hochzeit-6', type: 1}, 
			{src: 'hochzeit-7', type: 1}, 
			{src: 'hochzeit-8', type: 1},
			{src: 'hochzeit-9', type: 1},
			{src: 'hochzeit-10', type: 1}, 
			{src: 'hochzeit-11', type: 1}, 
			{src: 'hochzeit-12', type: 1}, 
			{src: 'hochzeit-13', type: 1}, 
			{src: 'hochzeit-14', type: 1}, 
			{src: 'hochzeit-18', type: 1}, 
			{src: 'hochzeit-2', type: 1}, 
			{src: 'hochzeit-3', type: 1}
			];
		currPhoto = 0;
		nextPhoto(null, 0);
		$('#fotosMenu li').removeClass('curr');
		$('#fMenuHz').addClass('curr');
		return false;
	});
	
	$('#fMenuWerbung a').click(function(){
		// show Werbung
		photoArray = [
			{src: 'werbung-3', type: 1}, 
			{src: 'werbung-10', type: 1}, 
			{src: 'werbung-11', type: 1}, 
			{src: 'werbung-1', type: 1}, 
			{src: 'werbung-2', type: 1}, 
			{src: 'werbung-4', type: 1}, 
			{src: 'werbung-5', type: 1}, 
			{src: 'werbung-6', type: 1}, 
			{src: 'werbung-8', type: 1}, 
			{src: 'werbung-9', type: 1}
			];
		
		currPhoto = 0;
		nextPhoto(null, 0);
		$('#fotosMenu li').removeClass('curr');
		$('#fMenuWerbung').addClass('curr');
		
		return false;
	});
	
	$('#tPall a').click(function(){
		// show All
		photoArray = allPhotos;
		nextPhoto(null, 0);
		$('#fotosMenu li').removeClass('curr');
		$('#fMenuAll').addClass('curr');
		togglePhoto();
		return false;
	});
	
	$('#tPfam a').click(function(){
		// show Fam
		photoArray = [
			{src: 'fam-12', type: 1}, 
			{src: 'fam-2', type: 1},
			{src: 'fam-9', type: 1}, 
			{src: 'fam-3', type: 1}, 
			{src: 'fam-4', type: 1}, 
			{src: 'fam-5', type: 1}, 
			{src: 'fam-6', type: 1}, 
			{src: 'fam-8', type: 1}, 
			{src: 'fam-10', type: 1}, 
			{src: 'fam-11', type: 1},
			{src: 'fam-1', type: 1}
			];
		nextPhoto(null, 0);
		$('#fotosMenu li').removeClass('curr');
		$('#fMenuAll').addClass('curr');
		togglePhoto();
		return false;
	});

	$('#tPbaby a').click(function(){
		// show Baby
		photoArray = [
			{src: 'baby-1', type: 1}, 
			{src: 'baby-2', type: 1}, 
			{src: 'baby-3', type: 1}, 
			{src: 'baby-4', type: 1}, 
			{src: 'baby-5', type: 1}, 
			{src: 'baby-6', type: 1}, 
			{src: 'baby-7', type: 1}, 
			{src: 'baby-8', type: 1}, 
			{src: 'baby-9', type: 1}, 
			{src: 'baby-10', type: 1}
			];
		nextPhoto(null, 0);
		$('#fotosMenu li').removeClass('curr');
		$('#fMenuAll').addClass('curr');
		togglePhoto();
		return false;
	});
	
	$('#tPmode a').click(function(){
		// show Fam
		photoArray = [
			{src: 'mode-1', type: 1}, 
			{src: 'mode-2', type: 1},
			{src: 'mode-3', type: 1}, 
			{src: 'mode-4', type: 1}, 
			{src: 'jugend-7', type: 1}, 
			{src: 'jugend-2', type: 1}, 
			{src: 'jugend-6', type: 1}, 
			{src: 'werbung-10', type: 1}, 
			{src: 'werbung-11', type: 1},
			{src: 'jugend-12', type: 1}
			];
		nextPhoto(null, 0);
		$('#fotosMenu li').removeClass('curr');
		$('#fMenuAll').addClass('curr');
		togglePhoto();
		return false;
	});

	$('#tPjugend a').click(function(){
		// show Baby
		photoArray = [
			{src: 'jugend-1', type: 1}, 
			{src: 'jugend-2', type: 1}, 
			{src: 'jugend-3', type: 1}, 
			{src: 'jugend-4', type: 1}, 
			{src: 'jugend-5', type: 1}, 
			{src: 'jugend-6', type: 1}, 
			{src: 'jugend-7', type: 1}, 
			{src: 'jugend-8', type: 1}, 
			{src: 'werbung-10', type: 1}, 
			{src: 'werbung-11', type: 1}, 
			{src: 'jugend-11', type: 1},
			{src: 'jugend-13', type: 1},
			{src: 'jugend-12', type: 1}
			];
		nextPhoto(null, 0);
		$('#fotosMenu li').removeClass('curr');
		$('#fMenuAll').addClass('curr');
		togglePhoto();
		return false;
	});

	$('#tPloc a').click(function(){
		// show Baby
		photoArray = [
			{src: 'loc-10', type: 1}, 
			{src: 'loc-11', type: 1}, 
			{src: 'loc-12', type: 1}, 
			{src: 'loc-13', type: 1}, 
			{src: 'loc-14', type: 1}, 
			{src: 'loc-4', type: 1}, 
			{src: 'loc-5', type: 1}, 
			{src: 'loc-6', type: 1}, 
			{src: 'loc-7', type: 1}, 
			{src: 'loc-8', type: 1},
			{src: 'loc-9', type: 1},
			{src: 'loc-1', type: 1}, 
			{src: 'loc-2', type: 1}, 
			{src: 'loc-3', type: 1},
			{src: 'hochzeit-5', type: 1},
			{src: 'hochzeit-7', type: 1}
			];
		nextPhoto(null, 0);
		$('#fotosMenu li').removeClass('curr');
		$('#fMenuAll').addClass('curr');
		togglePhoto();
		return false;
	});
	
	$('#nextButton')[0].onclick = nextPhoto;
	$('#backButton')[0].onclick = previousPhoto;
	
	$('#fbControl').html('<fb:like href="http://foto-berger.at" layout="button_count"></fb:like><span style="position: relative; top: 4px;"><g:plusone size="small"></g:plusone></span>');
	
	//TODO: fotospezifische infoboxen
	//TODO: bei hz / werbung -Klick auf 1. Bild wechseln
	//TODO: location-hashes auswerten
	
	if(window.location.hash == '#Werbefotografie' || window.location.hash.indexOf('#werbung') == 0)
		$('#fMenuWerbung a').click();
	else if(window.location.hash == '#Hochzeit' || window.location.hash.indexOf('#hochzeit') == 0)
		$('#fMenuHz a').click();
});
