var site = (function () {
    // document.removeChild(document.getElementById('globalPreloader'));
    var hoverColor = "#0f5aaf";
    var normColor = "#504f50"; 
    var pageContent, ajaxLoader, ajaxWrapper;

    function init() {


        // pageContent = $('#ajax-wrapper>.content');
        ajaxWrapper = $("#ajax-wrapper");
        ajaxLoader = $('<div id="pageLoader" />');


        if (!($.browser.msie && $.browser.version == "6.0")) {
            var currentMenuItem = null;
            $.ajaxSetup({
                cache: false
            });
            var _debug = false;

            // zmienne konfiguracyjne, podtrzymanie sesji
            var currentPageID = $("#current-page-ID").val();
            //console.log("Aktualna strona: " + currentPageID);
            // czyszczenie menu
            $("li.page_item").each(function () {
                $(this).attr("rel", $(this).attr("class").replace("page_item page-item-", ""));
            });
			
            // eventy menu
            var lastItem;
            var currentTarget;
            var loadingLink = false;
            $(".nav>ul>li>ul").hide().css({
                opacity: 0
            });

            $(".nav>ul>li a, .top>ul>li a").not('.externalLink').click(function (event) {
                loadPage($(this));
                event.preventDefault()
            });


            function loadPage(currItem) {
                // alert("loadPage");
                // currItem = $(this)
                //	pageTracker._trackPageview($(this).attr('href'));
                clearInterface();
                hidePage();
                currentMenuItem = currItem.parent();
                var ieMargin;
                // var customMargin = 48;
                if ($.browser.msie) {
                    ieMargin = 0
                } else {
                    ieMargin = 1
                }; // 1px v.margin w ie, nie da sie obejsc..
                


                $(".nav>ul>li").each(function () {
                    $(this).animate({
                        "height": $(this).children("a").height() + ieMargin
                    }, 300, function () {});

                    $(this).children("ul").animate({
                        opacity: 0
                    }, 100);
                })
                //loadingLink = true;
                // wczytaj strone





                var serverPath = $("#template-path").val();
                var newID = $(currItem).parent("li").attr("rel");
                //console.log(serverPath + "/page_content.php");
                // if(!window.isLoading) {
                window.isLoading = true;
                window.setTimeout(function () {
                    var xhr = $.post("http://teatrnasz.pl/tn/ajax/", {
                        id: newID
                    }).success(onLoaded).error(function (xhr, err) {
                        alert("Błąd");
                        window.isLoading = false;
                    }).complete(function () {
                        window.isLoading = false;
                    });;
                }, 500)
                // }
/*
                window.setTimeout(function () {
                    jQuery("#ajax-wrapper").load("http://teatrnasz.pl/tn/ajax/", {
                        id: newID
                    }, function () {
                        onLoaded(newID);
                    });
                }, 400);
*/

                return false;
            }

            function onLoading(id) {

            }

            function onLoaded(data) {
                window.isLoading = false;
                $('#ajax-wrapper').html(data);
                // alert('ok');
                updateMenu();
                //removeSubpageOverlay();
                showPage();
            }

            function removeSubpageOverlay() {
                $("#ajax-wrapper div.overlay").css({
                    opacity: 1
                }).animate({
                    opacity: 0
                }, 500, function () {
                    $("#ajax-wrapper div.overlay").remove()
                });
            }

            function clearInterface() {
                if ($('#player').length) {
                    $("#player").jPlayer("clearFile");
                    $('#player').remove();
                }
            }

            function updateMenu() {
                //	$(".current_page_item a").unwrap();
                $(".current_page_item").removeClass("current_page_item");
                $(".current_page_parent").removeClass("current_page_parent");
                $(".current_page_ancestor").removeClass("current_page_ancestor");
                currentMenuItem.addClass("current_page_item");
                currentMenuItem.parents(".nav>ul>li").addClass("current_page_parent");




                //console.log("Aktualizacja menu");
                $(".nav ul>li").each(function () {
                    var menuItem;

                    if ($(this).is(".current_page_item")) {
                        if ($(".current_page_parent").length) {
                            //console.log("current page parent: " + $(this).parents(".page_item").length);
                            menuItem = $(".current_page_parent");
                        } else {
                            menuItem = $(this);
                        }
                        $(menuItem).animate({
                            "height": $(menuItem).children("a").height() + 25
                        }, 350, function () {
                            $(menuItem).children("ul").css({
                                display: "block"
                            }).animate({
                                opacity: 1
                            }, 350);
                        });
                    }
                })
                //currentMenuItem.children('a').wrap('<h1 />');
            }


            function hidePage() {
		
                // $('#ajax-wrapper').fadeOut(300, function () {
                // ajaxWrapper.prepend(ajaxLoader);
                // })
                if ($('.ie7, .ie8, .ie6').length == 0) {
                // if (false) {
                    $('#ajax-wrapper .content') /*.addClass('animated')*/
                    .animate({
                        opacity: 0
                    }, 300, function () {
                        ajaxWrapper.prepend(ajaxLoader);
                    })
                } else {
                	
                	setTimeout(showPageOverlay, 500);
                };

            }

            function showPage(argument) {
                

                ajaxLoader.remove();
                if ($('.ie7, .ie8, .ie6').length == 0) {
                // if (false) {
                	$('#ajax-wrapper .content').animate({
                    	opacity: 1
		            }, 300);
                } else{
                	hidePageOverlay();
                };
            }

            function showMenu() {
                //console.log("Wy�wietlanie menu");
                $(".nav ul>li").each(function () {
                    var menuItem;

                    if ($(this).is(".current_page_item")) {
                        if ($(".current_page_parent").length) {
                            //console.log("current page parent: " + $(this).parents(".page_item").length);
                            menuItem = $(".current_page_parent");
                        } else {
                            menuItem = $(this);
                        }
                        $(menuItem).animate({
                            "height": $(menuItem).children("a").height() + 25
                        }, 350, function () {
                            $(menuItem).children("ul").css({
                                display: "block"
                            }).animate({
                                opacity: 1
                            }, 350);
                        });
                    }
                })


            }



            function hidePageOverlay() {
                $('.cacheMe').remove();
                setTimeout(function(){
                	$('#globalPreloader').fadeOut(1200);
                },800);
               
               
               
               /*
                $('#globalPreloader').delay(800).fadeOut(1000, function () {
                                   $('#globalPreloader').remove();
                               });*/
               
            }
            
            function showPageOverlay() {
            	$('body').prepend($('<div id="globalPreloader" />'))
                // $('#globalPreloader').css({opacity:0}).fadeIn(1000, function () {
                    // //$('#globalPreloader').remove();
                // });
            }
            
            
            showMenu();

            function enable() {
                hidePageOverlay();
            }


            var AjaxManager = {
                loadAjaxLink: function (href) {

                }
            }

        }

		
		hidePageOverlay();
        // enable();
    }

    return {
        // loadPage:loadPage,
        // enable:enable,
        init: init,
        normColor: normColor,
        hoverColor: hoverColor
    }
})();

$(document).ready(site.init);
// $(document).load(site.enable);
