/**
 * configuration for highslide
 */
hs.graphicsDir = 'extlib/highslide/graphics/';
hs.loadingText = 'Nahrávám...';
hs.loadingTitle = 'Klikněte pro zrušení';
hs.showCredits = false;
hs.fullExpandTitle = 'Skutečná velikost';
hs.restoreTitle = 'Vrať původní velikost kliknutím.\nPoužij šipky pro předchozí nebo následující obrázek.';

/**
 * check page age - when the page is too old, it should be refreshed automaticaly
 *
 * @return void
 */
function runCheckAge() {
    var oldTime = document.getElementById('checkAgeTimestamp');
    if (!oldTime) return;
    oldTime = oldTime.value;
    var diff = Math.round((new Date()).getTime() / 1000) - oldTime;
    if (diff < 0) return;
    if (diff > 3 * 60 * 60) {
        alert('Informace:\nStránka je stará, dojde k její obnovení.');
        document.location.reload();
    }
}

function changeTitlePhoto() {
	
	var
		imgElement = $('#titlePhoto')[0],
		siteUrl = 'http://az.pejsa.info/photos/',
		currentSrc = imgElement.src,
		prefix = 'title-',
		postfix = '.jpg',
		lastUrlPart,
		changeToIndex,
		images,
		i,
		cnt;
		
	currentSrc = currentSrc.split('/');
	lastUrlPart = currentSrc.pop();
		
	images = [
		'2012-01-15',
		'2011-12-31',
		'2011-07-10',
		'2010-10-18',
		'2010-07-27'
	];
	
	for (i = 0, cnt = images.length; i < cnt; i++) {
		if (prefix + images[i] + postfix === lastUrlPart) {
			if (i === cnt - 1) {
				changeToIndex = 0;
			}
			else {
				changeToIndex = i + 1;
			}
			break;
		}
	}
	
	imgElement.src = siteUrl + prefix + images[changeToIndex] + postfix;
	imgElement.title = 'Změň si titulní fotku! (nyní je ' + images[changeToIndex] + ')';
	
}

function searchOnWeb(form) {
    var value = form.elements[0].value;
    if ('' === value) {
	return false;
    }
    document.location = 'http://www.google.cz/search?q=site%3Aanna.pejsa.info+' + value;
}

$(document).ready(function() {
	
	$('#titlePhoto').click(function(event) {
		event.preventDefault();
		changeTitlePhoto();
	});
	$('#titlePhoto')[0].style.cursor = 'pointer';
	$('#titlePhoto')[0].title = 'Změň si titulní fotku!';
	
	$('span.monthEntry a').click(function(event) {
		
		event.preventDefault();
		
		var
			parentNode = this.parentNode,
			currentNode = parentNode,
			sibling;
		
		if (!parentNode) {
			return;
		}
		
		do {
			currentNode = currentNode.nextSibling;
			if (!currentNode) {
				break; // exit
			}
			if ($(currentNode).hasClass('monthEntry')) {
				return;
			}
			if (3 === currentNode.nodeType && currentNode.nextSibling) { // type Text
				continue;
			}
			$(currentNode).toggleClass('collapsed');
		} while (true);
		
	});
	
});


