TID = 0; // Timeout ID

function hideAllNews(){
    $("div#newsText div").hide();
}

function hideAllOgloszenia(){
    $("div#ogloszenia p.ogloszenia").hide();
}


function rot_right(){
    c = $("div#ogloszenia p.ogloszenia:visible");
    l = $("div#ogloszenia p.ogloszenia:last");
    if(c[0]==l[0]){
        n = $("div#ogloszenia p.ogloszenia:first");
    }
    else{
        n = c.next();
    }
    c.fadeOut("slow",function(){
        n.fadeIn("slow");
    });
    clearTimeout(TID);
    TID = setTimeout("rotate()",7000);
    return false;
}

function rot_left(){
    c = $("div#ogloszenia p.ogloszenia:visible");
    l = $("div#ogloszenia p.ogloszenia:first");
    if(c[0]==l[0]){
        n = $("div#ogloszenia p.ogloszenia:last");
    }
    else{
        n = c.prev();
    }
    c.fadeOut("slow",function(){
        n.fadeIn("slow");
    });
    clearTimeout(TID);
    TID = setTimeout("rotate()",7000);
    return false;
}

function rotate(){
    rot_right();
    TID = setTimeout("rotate()",7000);
}

function loadStart(href){
    $("a.inner").unbind("click",AJAXUpdate);
    $("a.inner").click(noClick);
	$("#footer").fadeOut("slow");
	$("#content").fadeOut("slow",function(){
		$("#paletka").slideUp("normal",function(){
			$("#paletka_hide").slideUp("normal",function(){
				$("#loader").slideDown("slow",function(){
					$.get(href.attr("href"),function(data){
						dt = $(data);
						$("#contentInside").replaceWith(dt.find("#contentInside"));
						$("#trasaP").replaceWith(dt.find("#trasaP"));
						loadStop(href.attr("href")==$("#menu a:first").attr("href"));
					});
				});
			});
		});
	});
}

function loadStop(main){
    $("#loader").slideUp("slow",function(){
        $("#content").fadeIn("slow");
        $("#footer").fadeIn("slow",function(){
            $("#paletka_hide").slideDown("slow");
            if(main){
                $("#paletka").slideDown("normal");
            }
    		setAJAXHandler();
        });
    });
}

function AJAXUpdate(e){
        loadStart($(this));
        return false;
}

function noClick(e){
	return false;
}

function setAJAXHandler(){
    $("a.inner").unbind("click",AJAXUpdate);
    $("a.inner").unbind("click",noClick);
    $("a.inner").click(AJAXUpdate);
    $("#absolwent_form :input").each(function(i){
    	$(this).attr('title',function(){
    		return this.previousSibling.title;
    	});
    });

    $(":input:not(type=submit)").tooltip({
	    position: "center right",
    	offset: [-2, 10],
    	effect: "fade",
    	opacity: 0.7,
    	tip: '.tooltip'
    });
}

$(document).ready(
function(){
    $("#paletka_hide_button").click(function(e){
        $("#paletka").slideToggle("normal");
        return false;
    });
    $(".newsLink").mouseover(function(e){
        $(this).attr("id",function(arr){
            $(".newsLink").attr("id",function(arr){
                str = $(this).attr('id')
                noStr = str.substring(4,5);
                return "news"+noStr;
            });
            hideAllNews();
            str = $(this).attr('id');
            noStr = str.substring(4,5);
            no = parseInt(noStr);
            $("div#newsText"+noStr).show();
            return str+"over";
        });
    });
    $("#rot_left").click(rot_left);
    $("#rot_right").click(rot_right);

    hideAllNews();
    hideAllOgloszenia();
    $("div#ogloszenia p.ogloszenia:first").show();
    $("div#newsText div:first").show();
    TID = setTimeout("rotate()",7000);

    $("#absolwent_form :input").each(function(i){
    	$(this).attr('title',function(){
    		return this.previousSibling.title;
    	});
    });

    $(":input:not(tye=submit)").tooltip({
	    position: "center right",
    	offset: [-2, 10],
    	effect: "fade",
    	opacity: 0.7,
    	tip: '.tooltip'
    });

	$("#close a").click(function(){
		$("#important").fadeOut();
		return false;
	});

    //setAJAXHandler();
	if(window.location.pathname != "/"){
		$("#paletka").hide();
		$("#important").hide();
	}
}
);

