function PageManager(wcf, minified)
{
    var isPageRequestOut = false;
    var isMinified = minified;
    var curPage = null;
    var curPageParameter = null;
    var oldPage = "home";
    var browser = $.browser;
    var vipScripts = new Scripts();
    var curPageObj = null;
    var gMapsURL = "http://maps.google.com/maps/api/js?sensor=false";
    var isAnchorActive = false;
    var wcfService = wcf;
    var mm = null;


    this.initPageManager = function ()
    {
        $(window).hashchange(function () { onHashChanged(); });
        var hash = GetLocationHash();
        if (hash[0] == "")
            _GoToPage("home");
        else
            loadPageScript(hash);
    };

    this.SetModalManager = function (mMan)
    {
        mm = mMan;
    };

    this.GetCurrentPageParameter = function ()
    {
        return curPageParameter;
    };

    this.GetAnchorState = function ()
    {
        return isAnchorActive;
    };

    this.GoToPage = _GoToPage;
    function _GoToPage(page)
    {
       
    };

  
    function onGetPageContent_Complete(responseText, textStatus, XMLHttpRequest)
    {
        if (textStatus == "success")
        {
            document.title = "Vip Tanning - " + $("#PageTitle").html();
            $("#PageContentArea").onImagesLoad(
        {
            callbackIfNoImagesExist: true,
            selectorCallback: function ()
            {
                showContent();
            }
        });
        }
        else
        {
            onPageLoadError();
        }
    }

    function showContent()
    {
        var animateHeader = true;
        setTimeout(curPageObj.onLoad, 1000);
        if ($(".header").attr("animate") != "true")
        {
            animateHeader = false;
            $("#Header_Content").hide();
        }
        $("#PageContentArea").slideDown('slow', function ()
        {
            if (animateHeader)
            {
                setTimeout(AnimateHeaderText, 100);
            }
            else
            {
                AnimateContent();
            }
        });
    }

    function AnimateHeaderText()
    {
        $("#Header_RightContent").slideDown('normal', function ()
        {
            $(".TitleImage").fadeIn("normal", function ()
            {
                $("#Header_Text").show('slide', function ()
                {
                    setTimeout(AnimateContent, 100);
                });
            });
        });
    }

    function AnimateContent()
    {
        $(".AjaxLoading").fadeOut();
        if (curPage == 'locations' && browser.msie)
            $("#Body_Content").show('slide', onAnimateContentComplete);
        else
            $("#Body_Content").slideDown('slow', onAnimateContentComplete);
    }

    function BeginPageAnimation()
    {
        if (isAnchorActive)
            _ToggleAnchors();
        $(".navLink").stop().unbind('mouseenter').unbind('mouseleave').animate({ "color": '#666666' }, 1000);
        isPageRequestOut = true;
    }

    function onAnimateContentComplete()
    {
        isPageRequestOut = false;
        if (!isAnchorActive)
            _ToggleAnchors();
        $(".navLink").animate({ "color": '#FFFFFF' }, 1000).hover(function ()
        {
            $(this).stop().animate({ "color": '#F26D23' }, 500);
        },
    function ()
    {
        $(this).stop().animate({ "color": '#FFFFFF' }, 500);
    });
        $("#NavBar").fadeIn();
        curPageObj.onAnimationsComplete();
    }

    function onHashChanged()
    {
        var hash = GetLocationHash();
        loadPageScript(hash);
    }

    function GetLocationHash ()
    {
        var temp = window.location.hash;
        if (temp == "")
        {
            return [""];
        }
        else
        {
            var hashParts = temp.substring(1).split("=");
            return hashParts;
        }
    }

    this.ToggleAnchors = _ToggleAnchors;
    function _ToggleAnchors()
    {
        $('a').anchorToggle();
        isAnchorActive = !isAnchorActive;
    }

    function loadPageScript(urlParts)
    {
        if (curPage == "")
            oldPage = "home";
        else
            oldPage = curPage;     

       if (curPage != null && curPageObj !=null)
            curPageObj.onUnLoad();

        curPage = urlParts[0];
        curPageObj = null;
        if (vipScripts.hasOwnProperty(urlParts[0]))
        {
            if (isMinified)
                var url = "Scripts/PageScripts/" + urlParts[0] + ".min.js";
            else
                var url = "Scripts/PageScripts/" + urlParts[0] + ".js";
            var scriptCompleteFunction = null;

            switch (urlParts[0])
            {
                case "home":
                    if (!vipScripts.home)
                    {
                        scriptCompleteFunction = function ()
                        {
                            vipScripts.home = true;
                            curPageObj = new home();
                        };
                        getScript(url);
                    }
                    else
                    {
                        curPageObj = new home();
                        LoadContent(urlParts);
                    }
                    break;
                case "locations":
                    if (!vipScripts.locations)
                    {
                        scriptCompleteFunction = function ()
                        {
                            vipScripts.locations = true;
                            curPageObj = new locations(wcfService);
                        };
                        getScript(url);
                    }
                    else
                    {
                        curPageObj = new locations(wcfService);
                        LoadContent(urlParts);
                    }
                    break;
                case "location":
                    if (!vipScripts.location)
                    {
                        scriptCompleteFunction = function ()
                        {
                            vipScripts.location = true;
                            curPageObj = new loc(wcfService, urlParts[1], mm);
                        };
                        getScript(url);
                    }
                    else
                    {
                        curPageObj = new loc(wcfService, urlParts[1], mm);
                        LoadContent(urlParts);
                    }
                    break;
                case "products":
                    if (!vipScripts.products)
                    {
                        scriptCompleteFunction = function ()
                        {
                            vipScripts.products = true;
                            curPageObj = new products(wcfService);
                        };
                        getScript(url);
                    }
                    else
                    {
                        curPageObj = new products(wcfService);
                        LoadContent(urlParts);
                    }
                    break;
                case "equipment":
                    if (!vipScripts.equipment)
                    {
                        scriptCompleteFunction = function ()
                        {
                            vipScripts.equipment = true;
                            curPageObj = new equip();
                        };
                        getScript(url);
                    }
                    else
                    {
                        curPageObj = new equip();
                        LoadContent(urlParts);
                    }
                    break;
                case "equipment_details":
                    if (!vipScripts.equipment_details)
                    {
                        scriptCompleteFunction = function ()
                        {
                            vipScripts.equipment_details = true;
                            curPageObj = new equip_Details(wcfService);
                        };
                        getScript(url);
                    }
                    else
                    {
                        curPageObj = new equip_Details(wcfService);
                        LoadContent(urlParts);
                    }
                    break;
                case "truth":
                    if (!vipScripts.truth)
                    {
                        scriptCompleteFunction = function ()
                        {
                            vipScripts.truth = true;
                            curPageObj = new truth(mm);
                        };
                        getScript(url);
                    }
                    else
                    {
                        curPageObj = new truth(mm);
                        LoadContent(urlParts);
                    }
                    break;
                case "faq":
                    if (!vipScripts.faq)
                    {
                        scriptCompleteFunction = function ()
                        {
                            vipScripts.faq = true;
                            curPageObj = new faq(mm);
                        };
                        getScript(url);
                    }
                    else
                    {
                        curPageObj = new faq(mm);
                        LoadContent(urlParts);
                    }
                    break;
                case "getdark":
                    if (!vipScripts.getDark)
                    {
                        scriptCompleteFunction = function ()
                        {
                            vipScripts.getDark = true;
                            curPageObj = new getDark();
                        };
                        getScript(url);
                    }
                    else
                    {
                        curPageObj = new getDark();
                        LoadContent(urlParts);
                    }
                    break;
                case "sunless":
                    if (!vipScripts.sunless)
                    {
                        scriptCompleteFunction = function ()
                        {
                            vipScripts.sunless = true;
                            curPageObj = new sunless(mm);
                        };
                        getScript(url);
                    }
                    else
                    {
                        curPageObj = new sunless(mm);
                        LoadContent(urlParts);
                    }
                    break;
                default:
            }
        }
        else
        {
            onPageLoadError();
        }
        function getScript(url)
        {
            $.getScript(url, onGetScriptComplete); 
        }
        function onGetScriptComplete()
        {
            scriptCompleteFunction();
            LoadContent(urlParts); 
        }
    }

    function onPageLoadError()
    {
        mm.showError();
        isPageRequestOut = false;     
        _GoToPage(oldPage);
        if (!isAnchorActive)
            _ToggleAnchors();
    }
}

function Scripts()
{
    this.home = false;
    this.locations = false;
    this.location = false;
    this.products = false;
    this.equipment = false;
    this.employment = false;
    this.contact = false;
    this.truth = false;
    this.sunless = false;
    this.faq = false;
    this.getdark = false;
    this.equipment_details = false;
}

