function permalink(text)
{
	if (text != null)
	{
		text = text.replace(/[żŻ]/g, "z");
		text = text.replace(/[źŹ]/g, "z");
		text = text.replace(/[óÓ]/g, "o");
		text = text.replace(/[ńŃ]/g, "n");
		text = text.replace(/[łŁ]/g, "l");
		text = text.replace(/[ćĆ]/g, "c");
		text = text.replace(/[śŚ]/g, "s");
		text = text.replace(/[ąĄ]/g, "a");
		text = text.replace(/[ęĘ]/g, "e");
		text = text.replace(/[?!@#$%\^&*()]/g, "");
		return text.replace(/ /g, "-");
	}
	else
		return text;
}

function mouseOverFunction(obj) 
{
	if (!$(obj).hasClass('active'))
	{
		$(obj).siblings('.active').removeClass('active').animate({'marginLeft' : 0}, 100);
		$(obj).addClass('active');
		$(obj).animate({ 'marginLeft' : +40 }, 100);
	}
	
	text = $('a', obj).attr('href');
	if (text)
		text = text.replace(/\/index\/glowna\/dlaczego-warto\/#/g, "");
		
	changeText(text);
}

//usage: changeText('permalink')
function changeText(toWhat)
{
	$('#dlaczego-warto-content .visible').removeClass('visible');
	$('#content-'+toWhat).addClass('visible');
}

$(document).ready(function() {
	//animowanie przycisków menu
	if ($.support.opacity) //w zależności od przeglądarki wyświetlamy ładne, animowane menu, albo zwykły rollover.
	{
		$("#menu ul li:not(.active) img.over").css("opacity","0");
		$("#menu ul li").hover(
			function() {
				$('img.over', this).stop().animate({'opacity': "1"}, 300); 
			},
			function() {
				if ($(this).hasClass('active'))
					$('img.over', this).stop().animate({'opacity': "1"}, 300);
				else
					$('img.over', this).stop().animate({'opacity': "0"}, 300);
			}
		);
	}
	else
	{
		$("#menu ul li:not(.active) img.over").hide();
		$("#menu ul li").hover(
			function() {
				$('img.over', this).show(); 
			},
			function() {
				if ($(this).hasClass('active'))
					$('img.over', this).show();
				else
					$('img.over', this).hide();
			}
		);
	}	
	
	//auto-zmiana zakładek i tekstów w dlaczego-warto na podstawie odnośnika
	if (window.location.hash)
	{
		hash = window.location.hash;
		
		var re = new RegExp(/#(.*)/); //(#dlaczego-warto)-(coś-tam)
		var m = re.exec(hash);
		if (m != null) {
		  if (m[1])
		  {
			$('ul.moving-items li.active').removeClass('active');
			$('ul.moving-items li a').each(function() {
				var link = $(this).attr('href');
				link = link.replace(/\/index\/glowna\/dlaczego-warto\//g, '');
				if (link == hash)
					$(this).parent().addClass('active');
			});
			changeText(m[1]);
		  }
		}
	}
	
	$('ul.moving-items li.active').animate({ 'marginLeft' : +40 }, 100);
	
	//animowanie listy
	$('ul.moving-items li').live('mouseover', function() {
		mouseOverFunction(this);
	});
	
	//błędy logowania
	if ($("#loginFormErrors").html() != "")
	{
		$("#loginFormErrors").dialog({
			autoOpen: false,
			width: 300,
			buttons: {
				"Ok": function() { 
					$(this).dialog("close"); 
				}
			}
		});
		
		$("#loginFormErrors").dialog("open");
	}
	$(".content-header").each(function() {
		var text = $(this).text();
		var html = "<img src=\"/images/content/header-" + permalink(text) + ".png\" alt=\"" + text + "\" />";
		//console.log(html);
		$(this).html(html);
	});
	
	if ($.browser.msie) {
		if(parseInt($.browser.version) == 6)
		{
			$("<div class=\"acbar\"></div>").html(IE6UPDATE_OPTIONS.message)
			.activebar(IE6UPDATE_OPTIONS);
		}
	}
});
