function toggleLayer( elemId ) {
	$( '#' + elemId ).slideToggle('slow');
}

// cookie
function createCookie( name, value, days ) {
	if( days ) {
		var date = new Date();
		date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );
		var expires = "; expires=" + date.toGMTString();
	}
	else
		var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie( name ) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for( var i=0; i < ca.length; i++ ) {
		var c = ca[i];
		while( c.charAt(0) == ' ' ) {
			c = c.substring( 1, c.length );
		}
		if( c.indexOf( nameEQ ) == 0 ) {
			return c.substring( nameEQ.length, c.length );
		}
	}
	return null;
}
function eraseCookie( name ) {
	createCookie( name, "", -1 );
}

// jQuery extension
(function($) {
	$.fn.extend({
		bannerImage: function() {
			var banner = $(this);
			banner.fadeIn( 900 );
			banner.delay( 5000 );
			if( banner.nextAll('img').length > 0 ) {
				banner.hide( function() {
						banner.nextAll('img').first().bannerImage();
					});
			}
			else if( banner.prevAll('img').length > 0 ) {
				banner.hide( function() {
						banner.prevAll('img').first().bannerImage();
					});
			}
		},
		bannerRotator: function() {
			var logos = $(this).find('.logo');
			var width = 0;
			for( i = 0; i < logos.length; i++ ) {
				width += $(logos[i]).outerWidth();
			}
			var logoHolder = $(this).find('div:first');
			logoHolder.width( width );

			logoHolder.wrap('<div style="position:absolute;left:0;" />');	
			logoHolder.css({'width':width+'px',float:'left'});
			width = logoHolder.width();
			var parentWidth = logoHolder.parent().parent().width();
			while( width < ( 2 * parentWidth ) ) {
				logoHolder.parent().append( logoHolder.clone() );
				width += logoHolder.width();
			}
			logoHolder = logoHolder.parent();
			logoHolder.width( width );
			logoHolder.bannerRotateLeft();

			$(this).find('.prev').click( function(e) { logoHolder.stop(); logoHolder.bannerRotateLeft(); e.preventDefault(); } );
			$(this).find('.next').click( function(e) { logoHolder.stop(); logoHolder.bannerRotateRight(); e.preventDefault(); } );
		},
		bannerRotateLeft: function() {
			var width = $(this).children().first().width();
			var delta = width - Math.abs( parseInt( $(this).css('left'), 10 ) );
			if( ( delta <= 0 ) || ( delta >= width ) ) {
				delta = width;
				$(this).css('left', 0);
			}
			$(this).animate(
						{ left: '-=' + delta + 'px'},
						{ 
							duration: 10 * delta,
							easing: 'linear', 
							complete: function() {
											$(this).css('left',0);
											$(this).bannerRotateLeft();
										}
						});
		},
		bannerRotateRight: function() {
			var width = $(this).children().first().width();
			var delta = Math.abs( parseInt( $(this).css('left'), 10 ) );
			if( ( delta <= 0 ) || ( delta >= width ) ) {
				delta = width;
				$(this).css('left', '-'+width+'px');
			}
			$(this).animate(
						{ left: '+=' + delta + 'px' },
						{
							duration: 10 * delta,
							easing: 'linear',
							complete: function() {
											$(this).css('left','-'+width+'px');
											$(this).bannerRotateRight();
										}
						});
		},
		changeFontSize: function() {
			var fontSizes = {
							'rozm-1': '12px',
							'rozm-2': '14px',
							'rozm-3': '16px'
			};
			$('section#content').css( 'font-size', fontSizes[ $(this).attr('class') ] );
			createCookie('fontSizeCookie', $(this).attr('class'), 15 );
			$(this).parent().addClass('on').siblings().removeClass('on');
		}
	});
})(jQuery);

// page update
$(document).ready( function () {
	if( $('#baner img').length > 0 ) {
		$('#baner img').hide();
		$('#baner img').first().delay( 400 ).bannerImage();
	}
	if( $('#logo-rotator').length > 0 ) {
		$('#logo-rotator').first().delay( 500 ).bannerRotator();
	}
	if( $('.kontakt-listy').length > 0 ) {
		$('.kontakt-listy').children('li').children('a').bind('click', function(e) { $(this).next().slideToggle('slow'); e.preventDefault(); } );
	}

	$('#fontSize a').bind( 'click', function(e) { $(this).changeFontSize(); e.preventDefault(); } );
	var fontSize = readCookie('fontSizeCookie');
	if( !fontSize ) {
		fontSize = 'rozm-1';
	}
	$('#fontSize .' + fontSize ).click();

	$('input[name=query]').focus( function() { $(this).attr('value',''); } );

	$(".printLink").bind( 'click', function(e) {
		var tUrl = document.location + ( document.location.href.indexOf('?') > 0 ? '&' : '?') + '_view=print';
		var tWin = window.open( tUrl, 'tPrint', 'status=no,scrollbars=1,resizable=1,width=800,height=800,top=20,left=20');
		tWin.focus();
		e.preventDefault();
	});
});

