/**
 * Executed when document is finished loading elements into DOM
 */
$(document).ready(function() {
	$('body').click(function(){
		flash = document.getElementById('topmenu');
		if (typeof flash.klikKlik == 'function' || typeof flash.klikKlik == 'object') {
			flash.klikKlik();
		}
	});

	$('footer').hover(
		function(){ $(this).find('img').addClass('hover'); },
		function(){ $(this).find('img').removeClass('hover'); }
	);

	$('#hrurku-hrurku').focus(function(){
		if ($('#hrurku-hrurku').val() == 'YOUR E-MAIL') {
			$('#hrurku-hrurku').val('');
		}
	});

	$('#hrurku-hrurku').blur(function(){
		if ($('#hrurku-hrurku').val() == '') {
			$('#hrurku-hrurku').val('YOUR E-MAIL');
		}
	});

	$('#palette a').click(function(){
		changeColor($(this).attr('rel'));
		return false;
	});

	if ($('#postimg img').length > 1) {
		setInterval("slideSwitch()", 5000);
	}
});

function slideSwitch() {
	var $active = $('#postimg .active');
	if ($active.length == 0) {
		$active = $('#postimg img:last');
	}
	var $next = $active.next().length ? $active.next()	: $('#postimg img:first');
	$active.addClass('last-active');
	$next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() {
		$active.removeClass('active last-active');
	});
}

/**
 * Print email link (protection from spambots)
 * If javascript is disabled, nothing is visible
 *
 * @param string p1 Part 1 - this is username
 * @param string p2 Part 2 - this is domain and TLD
 */
function writemail(p1, p2) {
	document.write('<a href="mailto:'+p1+'@'+p2+'">'+p1+'@'+p2+'</a>');
}

/**
 * Change external CSS file dynamically. Existing
 * css definition with id=css_Style must be present
 *
 * @param string stylesheet CSS filename
 */
function loadCSS(stylesheet) {
	$('#css_style').attr('href', webroot+'css/'+stylesheet);
}

function openGallery(strid) {
	var w = 680;
	var h = 680;
	var x = (screen.width - w) / 2;
	var y = (screen.height - h) / 2;

	window.open(webroot+'galleries/view/'+strid, 'gallery', 'scrollbars=1,width='+w+',height='+h+',left='+x+',top='+y);

	return false;
}


/**
 * Redirect user to specified page
 *
 * @param string url Address to redirect to
 */
function redirect(url) {
	document.location = url;
}

/**
 * Change page color
 */
function changeColor(id) {
	$.cookie('color', id, { expires: 365, path: '/' });
	$('#css_style').attr('href', webroot+'css/color'+id+'.css');
}
