// ----------------------------   cleartype fix
// naprawia błędne wyświetlanie czcionek w systemach z włączonym cleartype + IE7
function fixedFadeIn(element, speed) { $(element).fadeIn(speed, function() { if (jQuery.browser.msie) { this.style.removeAttribute('filter'); } }); }
function fixedFadeOut(element, speed) {	$(element).fadeOut(speed, function() { if (jQuery.browser.msie) { this.style.removeAttribute('filter'); } }); }
// ---------------- koniec fixa

function zwijanie_artykulu() {
	$(".czytaj-dalej").click(function () {
		$(".czytaj-dalej").show();
		$(this).hide();
		$(".pelna-tresc").slideUp("slow");
		$(".zwin-artykul").hide();
		$(this).next().slideDown("slow");
		$(this).prev().show();
	});

	$(".zwin-artykul").click(function () {
		$(this).hide();
		$(this).next().next().slideUp("slow");
		$(this).next().show();
	});
}



function zwijacze() {
	$(".tytul").next("ul").each(function () {
		$(this).parent().children("div").addClass("folder");
		$(this).parent().children("div").removeAttr("onclick");
		//$(this).parent().children("div").children().removeAttr("href");
		var str = $(this).parent().children("div").children("a").text();
		$(this).parent().children("div").children().remove("a");
		$(this).parent().children("div").text(str);
		$(this).hide();
	});

	$(".tytul").next("p").next("ul").each(function () {
		$(this).parent().children("div").addClass("folder");
		$(this).parent().children("div").removeAttr("onclick");
		$(this).hide();
	});


	$(".tytul").click(function () {
		if ($(this).next("ul").hasClass("rozwiniety")) {
			$(this).next("ul").slideUp();
			$(this).next("ul").removeClass("rozwiniety");
		} else {
			$(this).next("ul").slideDown();
			$(this).next("ul").addClass("rozwiniety");
		}

		/*if ($(this).next("p").next("ul").hasClass("rozwiniety")) {
			$(this).next("p").next("ul").slideUp();
			$(this).next("p").next("ul").removeClass("rozwiniety");
		} else {
			$(this).next("p").next("ul").slideDown();
			$(this).next("p").next("ul").addClass("rozwiniety");
		} */
	});


	$(".zwijacz").click(function () {
		if ($(this).hasClass("rozwiniety")) {
			$(this).next().slideUp();
			$(this).removeClass("rozwiniety");
		} else {
			$(this).removeClass("rozwiniety");
			$(this).next().slideDown();
			$(this).addClass("rozwiniety");
		}
	});

}


function tabele() {
	$(".prawa-lewa > tbody > tr").each(function () {
		$(this).children(":first").wrapInner("<p class='tab-prawa'></p>");
		$(this).children(":first").addClass("td-tab-prawa");
	});

	$(".prawa-lewa > tbody > tr").each(function () {
		$(this).children(":first").next().wrapInner("<p class='tab-lewa'></p>");
	});
}

function otworzStrone(url) {
	document.location.href = url;
}

/* 
 fukcja rozwija menu aż do elementu o określonym id
*/
function rozwinMenu(id) {
	var ident = "#"+id;
	$(ident).parent().parent().prev().trigger("click");
	var a = $(ident).parent().parent().prev().attr("id");
	if (a != undefined) {
		rozwinMenu(a);
	}
}


function hoverowanie() {
	$(".menu-lewe-li").bind("mouseenter",function(){
		$(this).addClass("menu-lewe-li-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("menu-lewe-li-hover");
    });

	$(".tresc-ul-li").bind("mouseenter",function(){
		$(this).addClass("tresc-ul-li-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("tresc-ul-li-hover");
    });

	$("div .tytul").bind("mouseenter",function(){
		$(this).addClass("tytul-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("tytul-hover");
    });

	$(".folder").bind("mouseenter",function(){
		$(this).addClass("folder-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("folder-hover");
    });

	$("div .link-zewnetrzny").bind("mouseenter",function(){
		$(this).addClass("link-zewnetrzny-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("link-zewnetrzny-hover");
    });

	$("#male-menu .tresc .link").bind("mouseenter",function(){
		$(this).addClass("link-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("link-hover");
    });
	

	$(".naglowek-tabeli-srodek").bind("mouseenter",function(){
		$(this).addClass("naglowek-tabeli-srodek-hover");
		$(this).prev().addClass("naglowek-tabeli-lewa-hover");
		$(this).next().addClass("naglowek-tabeli-prawa-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("naglowek-tabeli-srodek-hover");
		$(this).prev().removeClass("naglowek-tabeli-lewa-hover");
		$(this).next().removeClass("naglowek-tabeli-prawa-hover");
    });

	$(".tr").bind("mouseenter",function(){
		$(this).addClass("tr-hover");
		}).bind("mouseleave",function(){
		$(this).removeClass("tr-hover");
    });

	$(".podstrona-zamknij").bind("mouseenter",function(){
		$(this).addClass("podstrona-zamknij-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("podstrona-zamknij-hover");
    });
	$(".podstrona-wstecz").bind("mouseenter",function(){
		$(this).addClass("podstrona-wstecz-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("podstrona-wstecz-hover");
    });
	$(".podstrona-glowna").bind("mouseenter",function(){
		$(this).addClass("podstrona-glowna-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("podstrona-glowna-hover");
    });
	$(".podstrona-drukuj").bind("mouseenter",function(){
		$(this).addClass("podstrona-drukuj-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("podstrona-drukuj-hover");
    });
	$(".podstrona-pdf").bind("mouseenter",function(){
		$(this).addClass("podstrona-pdf-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("podstrona-pdf-hover");
    });
	$(".podstrona-lista-ofert").bind("mouseenter",function(){
		$(this).addClass("podstrona-lista-ofert-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("podstrona-lista-ofert-hover");
    });

	$("#nav-startowa").bind("mouseenter",function(){
		$(this).addClass("nav-startowa-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("nav-startowa-hover");
    });
	$("#nav-praca").bind("mouseenter",function(){
		$(this).addClass("nav-praca-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("nav-praca-hover");
    });
	$("#nav-praca-ogolnopolska").bind("mouseenter",function(){
		$(this).addClass("nav-praca-ogolnopolska-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("nav-praca-ogolnopolska-hover");
    });
	$("#nav-powrot").bind("mouseenter",function(){
		$(this).addClass("nav-powrot-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("nav-powrot-hover");
    });
	$("#nav-poprzednia").bind("mouseenter",function(){
		$(this).addClass("nav-poprzednia-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("nav-poprzednia-hover");
    });
	$("#nav-nastepna").bind("mouseenter",function(){
		$(this).addClass("nav-nastepna-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("nav-nastepna-hover");
    });
	
	$(".strona").bind("mouseenter",function(){
		$(this).addClass("strona-hover");
    }).bind("mouseleave",function(){
		$(this).removeClass("strona-hover");
    });
	
}


function wczytajTresc(co_wczytywac, strona_nadrzedna) {
	var zawartosc = "zawartosc="+co_wczytywac+"&strona_nadrzedna="+strona_nadrzedna;

	$.ajax({
	  url: "./tresc.php",
	  type: "POST",
	  data: zawartosc,
	  cache: false,
	  beforeSend: function() {
		fixedFadeOut("#content","fast");
		fixedFadeOut("#wyniki-wyszukiwania","fast");
		fixedFadeOut("#nawigacja", "fast");
		fixedFadeIn("#loading", "fast");
	  },
	  success: function(wczytane_dane)
	  {
		$("#tresc").html(wczytane_dane);
		$("#nav-powrot").show();
		fixedFadeOut("#loading","slow");
		fixedFadeIn("#tresc","fast");
		fixedFadeIn("#nawigacja", "fast");
		hoverowanie();
		zwijanie_artykulu();
	  }
	});
}

function zrobPDF(id_strony) {
	var zawartosc = "strona="+id_strony;

	$.ajax({
	  url: "./pdf.php",
	  type: "GET",
	  data: zawartosc,
	  cache: false,
	  beforeSend: function() { },
	  success: function(wczytane_dane) { }
	});
}

function wczytajOfertyPracy(nr_oferty, strona, sortowanie, menu, szukaj, po_ile) {
	var zawartosc = "?akcja=oferty&menu="+menu+"&nr_oferty="+nr_oferty+"&strona="+strona+"&sort="+sortowanie+"&szukaj="+szukaj+"&po_ile="+po_ile;
	otworzStrone("index.php"+zawartosc);
}


function zamknijPodstrone() {
	$("#nav-powrot").hide();
	$("#nav-praca").show();
	fixedFadeOut("#tresc", "fast");
	fixedFadeOut("#wyniki-wyszukiwania", "fast");
	fixedFadeIn("#content", "slow");
	fixedFadeIn("#menu", "slow");
}


$(document).ready(function(){
	zwijanie_artykulu();
	zwijacze();
	hoverowanie();
	tabele();
	
	if ($("#r").text() != "") {
		var a = $("#r").text();
		rozwinMenu(a);
	}

	
	/* wyszukiwarka */
    var opcje = {
        target:        '#wyniki-wyszukiwania',
        beforeSubmit:  function() { fixedFadeOut("#content", "fast"); fixedFadeOut("#nawigacja", "fast"); fixedFadeOut("#tresc", "fast"); fixedFadeIn("#loading", "fast"); },
        success:       function() { fixedFadeOut("#loading", "fast"); $("#nav-powrot").show(); fixedFadeIn("#wyniki-wyszukiwania"); fixedFadeIn("#nawigacja", "fast");  hoverowanie(); },
        type:      'post'
    };
	$("#wyszukiwarka").validate
	(
		{
			//errorClass: "walidacja",
			//submitHandler: function(form) { $(form).ajaxSubmit(opcje); return false; },
			rules:
			{
				szukaj: { required: true, minlength: 3 }
			},
			messages:
			{
				szukaj: { required: "Wprowadź słowo kluczowe!", minlength: "<br />Słowo kluczowe musi mieć minimum 3 znaki!" }
			}
		}
	);

});
