var showId = null;
var showIndex = null;

var indexOpen = false;
var indexOpenKey = null;
var playing = false;
var playTimer = null;
var imageData = {};
var loadCounter = {};

var speed = 800;
var _visible = false;
var _redirect = false;
var content = {};

var e_control = null;
var e_next = null;
var e_prev = null;
var e_play = null;
var e_stop = null;
var e_index = null;
var e_running = null;
var e_pictureIndex = null;
var e_pictureCount = null;


$(document).ready(function(){
	
	if (_redirect)
		return;
	$('body').disableTextSelect();
	e_control = $('#e_control');
	$('.center').hide();
	$('noscript').remove();
	$('body script').remove();
	
	$('.menu a')
		.click(function() { return href(this); })
		.each(function() {
			this.href = root + '#' + this.href.split('/').pop();
	});

	e_next = $('#e_next').click(function() { if (!indexOpen) href(this); return false; });
	e_prev = $('#e_prev').click(function() { if (!indexOpen) href(this); return false; });
		
	e_index = $('#e_index').click(function(){ return href(this); });
	e_play = $('#e_play').show().click(function() { play(); });
	e_stop = $('#e_stop').click(function() { stop(); });
	e_running = $('#e_running');
	e_pictureIndex = $('#e_pictureindex');
	e_pictureCount = $('#e_picturecount')
	
	$(jQuery.browser.mozilla ? window : "body").keydown(function(e) {
		switch(e.keyCode)
		{
			case 37: if (content[showId]._type == 2) e_prev.click(); break;
			case 39: if (content[showId]._type == 2)e_next.click(); break;
			case 33: case 36: if (content[showId]._type == 2)$.historyLoad(showId + '/1'); break;
			case 34: case 35: if (content[showId]._type == 2)$.historyLoad(showId + '/' + gallery[showId].length);  break;
			case 13: if (content[showId]._type == 2) { if (playing) stop(); else play(); } break;
		}
	});
		
	$.historyInit(show, "/");	
	
	$(window).resize(function(){
		resizeImages();
	});
	resizeImages();
});


function href(e)
{
	var hash = e.href;
	hash = hash.replace(/^.*#/, '');
	$.historyLoad(hash);
	return false;
}

//----------------------------------------------------------------------------------------------------


function addShadow(elm)
{
	$('<div class="top"></div><div class="topleft"></div><div class="topright"></div><div class="left"></div><div class="right"></div><div class="bottom"></div><div class="bottomleft"></div><div class="bottomright"></div>')
		.appendTo(elm);
}


function createPage(id)
{
	if (content[id] || page[id] === undefined)
		return;
		
	content[id] = $('<div id="' + id + '" class="rightcolumn" style="display:none;"><div class="content"><div class="page">' + page[id] + '</div></div></div>');
	addShadow(content[id]);
	content[id].appendTo('.center');

	content[id]._visible = false;
	content[id]._type = 1;
}


function createGallery(id)
{
	if (content[id] || gallery[id] === undefined)
		return;
		
	loadCounter[id] = gallery[id].length;
	
	if (!$.browser.opera)
		showLoader(id);
	
	var h = $(window).height();
	var f = h < 700 ? (h - 50) / 650 : 1;
	if (f < 0.75) f = 0.75;
	
	content[id] = $('<div id="' + id + '" class="rightpicture" style="display:none;"></div>');
		
	content[id]._index = $('<div id="index_' + id + '" class="index"><div class="top"></div><div class="topleft"></div><div class="topright"></div><div class="left"></div><div class="right"></div><div class="bottom"></div><div class="bottomleft"></div><div class="bottomright"></div></div>');
	content[id]._pic = [];
	for(var index = 0; index < gallery[id].length; index++)
	{
		content[id]._pic[index] = {};
		
		content[id]._pic[index].div = $('<div id="' + id + '_' + (index+1) + '" class="picdiv">')
			.hide()
			.width(Math.round(gallery[id][index][1] * f))
			.height(Math.round(gallery[id][index][2] * f))
			.appendTo(content[id])
			.click(function(){ 
				var pos = this.id.lastIndexOf('_');
				var k = this.id.substring(0, pos);
				var i = this.id.substring(pos + 1);
				$.historyLoad(k + "/" + (i < gallery[k].length ? parseInt(i) + 1 : i));
				return false;
			 });
			 
		content[id]._pic[index].img = $(new Image());
		
		var container;
			 
		if ($.browser.opera)
		{
			content[id]._pic[index].div
				.width(Math.round(gallery[id][index][1] * f + 20))
				.height(Math.round(gallery[id][index][2] * f + 20));
			
			content[id]._pic[index].wrap = $('<div class="picwrap">')
				.width(Math.round(gallery[id][index][1] * f))
				.height(Math.round(gallery[id][index][2] * f))
				.prependTo(content[id]._pic[index].div);
				
			addShadow(content[id]._pic[index].wrap);
			
			container = content[id]._pic[index].wrap;
		}
		else
		{
			addShadow(content[id]._pic[index].div);
			content[id]._pic[index].div.addClass('loading');
			content[id]._pic[index].img
				.hide()
				.load(function() { loadPicture(this) });
				
			container = content[id]._pic[index].div;
		}
		
		content[id]._pic[index].img
			.prependTo(container)
			.width(Math.round(gallery[id][index][1] * f))
			.height(Math.round(gallery[id][index][2] * f))
			.attr('src', root + 'files/gallery/pic_' + gallery[id][index][0] + '.jpg');
			

		$('<img id="thumb_' + id + '_' + (index+1) + '" src="' + root + 'files/gallery/tn_' + gallery[id][index][0] + '.jpg">')
			.appendTo(content[id]._index)
			.click(function() {
				var pos = this.id.lastIndexOf('_');
				$.historyLoad(this.id.substring(6, pos) + "/" + this.id.substring(pos + 1));
				return false;
			});
	}
	
	content[id]._index
		.hide()
		.appendTo(content[id]);
		
	content[id].appendTo('.center');
	content[id]._visible = false;
	content[id]._type = 2;
}


function loadPicture(img)
{
	var id;
	
	id = $(img).parent().attr('id');
	var pos = id.lastIndexOf('_');
	var key = id.substring(0, pos);
	var index = id.substring(pos + 1);
	loadCounter[key]--;

	content[key]._pic[index - 1].div.removeClass('loading');
	content[key]._pic[index - 1].img.fadeIn(speed);
	
	if (!loadCounter[key])
		hideLoader(key);
}

function show(key, y)
{
	if (!key)
		key = showPage ? showPage : showAlbum;
		
	var k = key.split('/');
	if (k.length < 2)
		k[1] = null;
		
	if (k[0] != showId && content[k[0]] === undefined)
	{
		createPage(k[0]);
		createGallery(k[0]);
	}
	
	if (content[k[0]] == undefined)
	{
		for (var i in gallery)
		{
			k[0] = i;
			break;
		}
		createGallery(k[0]);
	}
	
	if (k[0] == showId && k[1] != showIndex && k[1] != "index")
		showPicture(k[1]);
	else 
		changePage(k[0], k[1]);
}



function changePage(key, index)
{
	if (!_visible)
		$('.center').fadeIn(speed);
		
	if (playing)
		stop();
	
	if (key != showId)
	{
		if (showId)
			content[showId].fadeOut(speed);
			
		content[key].fadeIn(speed);
		
		if (content[key]._type == 2 && (!showId || content[showId]._type == 1))
			e_control.fadeIn(speed);
			
		if (content[key]._type == 1 && (!showId || content[showId]._type == 2))
			e_control.fadeOut(speed);
		
		e_index.attr('href', root + '#' + key + '/index');
	}
	
	if (key != showId || index != showIndex)
	{
		if (index != "index")
		{
			if (content[key]._type == 2 && !parseInt(index))
				index = 1;
			if (content[key]._type == 2 && index > gallery[key].length)
				index = gallery[key].length;
		}

		showPicture(index, key);	
	}
	
	$('.menu a').removeClass('selected');
	$('#l_' + key).addClass('selected');

	document.title = 'patrik budenz | photography | ' + $('#l_' + key).html();
	
	_visible = true;
}


//----------------------------------------------------------------------------------------------------


function showLoader(key)
{
	$('<img id="loader_' + key + '" src="' + root + 'img/loader1.gif" class="loader1" align="texttop">').insertAfter('#l_' + key);
}

function hideLoader(key)
{
	$('#loader_' + key).remove();
}


//----------------------------------------------------------------------------------------------------



function nextPicture()
{
	if (indexOpen)
		return;
		
	if (showIndex < gallery[showId].length)
	{
		showPicture(showIndex + 1);
		return true;
	}
		
	return false;
}


function prevPictures()
{
	if (indexOpen)
		return;
		
	if (showIndex > 1)
		showPicture(showIndex - 1);
}


function showPicture(index, id)
{
	if (index == showIndex && (id == undefined || id == showId))
		return;
		
	if (!index)
		index = 1;
		
	if (indexOpen) 
		closeIndex();
	else if (showId && showIndex && content[showId]._type == 2)
		picHide(showId, showIndex);
	
	var lastIndex = showIndex;
	showIndex = index;	
	if (id != undefined)
		showId = id;
		
	if (content[showId]._type == 2)
	{
		if (showId && showIndex)
		{
			if (showIndex == "index")
				openIndex(lastIndex);
			else
				picShow(showId, parseInt(showIndex));
		}
		
		setCounter();
	}
}

function picShow(id, index)
{
	if ($.browser.msie)
		content[id]._pic[index - 1].div.children('div').hide();
	content[id]._pic[index - 1].div.fadeIn(speed, function() {
		if ($.browser.msie)
			content[id]._pic[index - 1].div.children('div').show();
	 });
	
}

function picHide(id, index)
{
	if ($.browser.msie)
		content[id]._pic[index - 1].div.children('div').hide();
	content[id]._pic[index - 1].div.fadeOut(speed);
}

//----------------------------------------------------------------------------------------------------


function setCounter()
{
	if (showIndex != "index")
		e_pictureIndex.html((showIndex < 10 ? '0' : '') + showIndex);
	e_pictureCount.html((gallery[showId].length < 10 ? '0' : '') + gallery[showId].length);
	
	if (showIndex != "index")
	{
		showIndex = parseInt(showIndex);
		if (showIndex == 1)
			e_prev.addClass('disable').attr('href', root + '#' + showId + '/1');
		else
			e_prev.removeClass('disable').attr('href', root + '#' + showId + '/' + (showIndex - 1));
			
		if (showIndex == gallery[showId].length)
			e_next.addClass('disable').attr('href', root + '#' + showId + '/' + showIndex);
		else
			e_next.removeClass('disable').attr('href', root + '#' + showId + '/' + (showIndex + 1));
			
		e_index.attr('href', root + '#' + showId + '/index');
	}
}


//----------------------------------------------------------------------------------------------------


function openIndex(lastIndex)
{
	if (indexOpen || content[showId]._type != 2)
		return;
	
	content[showId]._index.fadeIn(speed);
	
	e_prev.addClass('disable');
	e_next.addClass('disable');
	e_play.addClass('disable');
	e_index.attr('href', root + '#' + showId + '/' + lastIndex);
	
	indexOpen = showId;
	showIndex = "index";
}

function closeIndex()
{
	if (!indexOpen)
		return;

	content[indexOpen]._index.fadeOut(speed);
	e_play.removeClass('disable');
	indexOpen = false;
}


function play()
{
	if (indexOpen)
		return;
		
	if (playing)
		return;
		
	e_play.hide();
	e_stop.show();
	e_running.show();
		
	playTimer = setTimeout('playNext();', 2000);
	playing = true;
}

function playNext()
{
	e_next.click();
	if (showIndex < gallery[showId].length)
		playTimer = setTimeout('playNext();', 5000);
	else
		stop();
}

function stop()
{
	if (!playing)
		return;
		
	e_play.show();
	e_stop.hide();
	e_running.hide();
	
	if (playTimer)
		clearTimeout(playTimer);
	
	playTimer = null;
	playing = false;
}


//----------------------------------------------------------------------------------------------------


function resizeImages()
{
	var h = $(window).height();
	var f = h < 700 ? (h - 50) / 650 : 1;

	if (f < 0.75) f = 0.75;
	
	for(var id in content)
		if (content[id]._type == 2)
		{
			for (var index = 0; index < gallery[id].length; index++)
			{
				if ($.browser.opera)
				{
					content[id]._pic[index].div.width(Math.round(gallery[id][index][1] * f + 20)).height(Math.round(gallery[id][index][2] * f + 20));
					content[id]._pic[index].wrap.width(Math.round(gallery[id][index][1] * f)).height(Math.round(gallery[id][index][2] * f));
				}
				else
					content[id]._pic[index].div.width(Math.round(gallery[id][index][1] * f)).height(Math.round(gallery[id][index][2] * f));
				
				content[id]._pic[index].img.width(Math.round(gallery[id][index][1] * f)).height(Math.round(gallery[id][index][2] * f));
			}
		}
}


//----------------------------------------------------------------------------------------------------


function UnCryptMailto( s )
{
	var n = 0;
	var r = "";
	for( var i = 0; i < s.length; i++)
	{
	    n = s.charCodeAt( i );
	    if( n >= 8364 )
	    {
	        n = 128;
	    }
	    r += String.fromCharCode( n - 1 );
	}
	return r;
}

function linkTo_UnCryptMailto( s )
{
	location.href=UnCryptMailto( s );
}
