﻿var scrollable = true;
var currentLevel = 0;
var currentAnch = "";
var currentHash = "";
var timeoutId = 0;

var chained = new Class({
    Implements: [Chain]
});

/*  MAIN HANDLER */
// this method is called right at the start of the site loading.
// a timer is then set to call this method every 100th of a second.
// each time this method is ran, it checks what is currently in the browser's url, and compares it to
// what the site was looking at 100th of a second ago.  If they are the same, great, don't do anything.
// If they are different, the code will check each link that is a navigation item check what it's hash
// value is, if its what is now in the browser, it will stop the timeout, call DoClick, and then set the
// current hash value.
function StartScroll() {

    // we want the user to start at "our-home" so if that is not in the browser, instantly redirect to them to it.    
    
    if (document.location.hash == "") {
        document.location.href = "/#our-home";
        currentHash = "#our-home";
        return;
    }

    var set_timeout = true;
    var doRedirect = false;
    
    if (currentHash != document.location.hash) {
        // so we should redirect
        doRedirect = true;
    }

    if (doRedirect) {
        var found = false;
        $$("a.nav_item").each(function(anchor) {

            var new_hash = anchor.get("href").replace("/default.aspx", "").replace("/", "");

            if (new_hash == document.location.hash) {
                // we need to redirect to here with a DoClick();
                set_timeout = false;
                DoClick(anchor);
                currentHash = document.location.hash;
                found = true;
            }
        });

        if (!found) {
            // could be a news item
            $$("a.news_item").each(function(anchor) {

                var new_hash = anchor.get("href").replace("/default.aspx", "").replace("/", "");

                if (new_hash == document.location.hash) {
                    // we need to redirect to here with a DoClick();
                    set_timeout = false;
                    ShowNews(new_hash.replace("#", ""));
                    DoClick(anchor);                    
                    currentHash = document.location.hash;
                    found = true;
                }
            });
        }        
    }

    if (set_timeout)
        timeoutId = setTimeout(StartScroll, 100);
}

function AddClick(anch) {

    anch.addEvent("click", function(e) {        
        DoClick(this);
    });        
}

function GetScroll(speed) {
    var scroll = new Fx.Scroll('content', {
        wait: false,
        duration: speed,
        offset: { 'x': -240, 'y': 0 },
        transition: Fx.Transitions.Quint.easeInOut,

        onComplete: function(event) {
            //
        }

    });

    return scroll;
}

function ScrollTo(x, y, anch) {

    if (!scrollable) return;
    
    scrollable = false;
    var scroll = GetScroll(1500);
    SetSelected(anch);

    scroll.start(x, y).chain(function() { CompleteScroll(this); }.bind(anch));
}

function DoubleScrollTo(x1, y1, x2, y2, anch) {
    if (!scrollable) return;

    scrollable = false;
    var scroll = GetScroll(1500);    
    SetSelected(anch);

    scroll.start(x1, y1).chain(function() { scroll.start(x2, y2); }).chain(function() { CompleteScroll(this); }.bind(anch));
}

function TripleScrollTo(x1, y1, x2, y2, x3, y3, anch) {
    if (!scrollable) return;

    scrollable = false;
    var scroll = GetScroll(1500);
    SetSelected(anch);

    scroll.start(x1, y1).chain(function() { scroll.start(x2, y2); }).chain(function() { scroll.start(x3, y3); }).chain(function() { CompleteScroll(this); } .bind(anch));
}

function SetSelected(anch) {
    $$(".navigation a").each(function(nav_anch) {
        nav_anch.removeClass("selected");
    });

    if ($(anch).get("parent") != null)
        $(anch.get("parent")).addClass("selected");
    else
        $(anch).addClass("selected");
}

function CompleteScroll(anch) {

    // also hide all the sub nav
    $$(".subnav").each(function(item) {
        item.set("style", "display: none;");
    });

    // current not required as not subnav's are currently involved.
    //    var subnav = $(anch).get("subnav");
    //    if (subnav != null) {
    //        $(subnav).set("style", "display: block;");
    //    }

    scrollable = true;
    timeoutId = setTimeout(StartScroll, 300);
    currentHash = anch.get("href").replace("/default.aspx", "").replace("/", "");

    
}

/* AS WE DON'T "CLICK" PER SAY THIS CODE IS USED TO SCROLL THE SCREEN */
// do click makes a simple logic choice.  It checks to see if the item clicked is
// 1. in the main menu
// 2. a sub nav item
// and this is achieved as all sub nav items have a parent tag set in its properties.
function DoClick(anch) {

    clearTimeout(timeoutId);
    if (scrollable) {

        $$("ul.navigation li.top").each(function(li) {
            li.fireEvent("mouseout");            
        });
        
        document.title = $(anch).get("title");
        
        var parent = anch.get("parent");
        
        if (parent != null) {
            // child menu item move            
            MoveChildLevel(anch, $(parent));
        }
        else {
            // top level menu item move
            MoveTopLevel(anch);
        }
    }
}

// MoveChildLevel will check to see if the current level is the parent of the node clicked.
// Why does it do this?  Should it already be on the parent level if the user is able to click?
// Nope.  1.  The user has typed the url plus the achor tag in the browser, or 2. The user has hit
// back/forward on the browser (this functionality doesn't seem to work in IE6, but who cares?)
// 
function MoveChildLevel(anch, parent) {
    
    var parent_hash = parent.get("href");
    var point1 = parent.get("rel").split("|")[0].split(",");
    
    // current hash's parent
    var sameLevel = false;
    $$("a.nav_item").each(function(current_anch) {
        if (current_anch.get("href") == "/" + currentHash) {
            if (current_anch.get("parent") != null) {
                var current_anch_parent_hash = $(current_anch.get("parent")).get("href");
                sameLevel = true;
            }
        }
    });

    if (parent_hash == ("/" + currentHash) || sameLevel) {
        var point = anch.get("rel").split(",");
        ScrollTo(parseInt(point[0]), parseInt(point[1]), anch);        
    }
    else {
        var point2 = anch.get("rel").split(",");
        var tripleScroll = false;
        var triplePoint;

        // it could be that the current hash has a parent as well as being a sub item, and because
        // that we are going from a sub item to a sub item we need to do three manouvers.
        $$("a").each(function(_anch) {
            if (_anch.get("href") == "/" + currentHash) {
                if (_anch.get("parent") != null) {
                    triplePoint = $(_anch.get("parent")).get("rel").split("|")[0].split(",");
                    tripleScroll = true;
                }
            }
        });      
        
        if (tripleScroll)
            TripleScrollTo(parseInt(triplePoint[0]), parseInt(triplePoint[1]), parseInt(point1[0]), parseInt(point1[1]), parseInt(point2[0]), parseInt(point2[1]), anch);
        else
            DoubleScrollTo(parseInt(point1[0]), parseInt(point1[1]), parseInt(point2[0]), parseInt(point2[1]), anch);        
    }

    SetTopMenu(parseInt(point1[0]), parseInt(point1[1]));
}

function MoveTopLevel(anch) {
    var rels = anch.get("rel").split("|");    
    if (rels.length == 2) {
        var point1 = rels[0].split(",");
        var point2 = rels[1].split(",");
        DoubleScrollTo(parseInt(point2[0]), parseInt(point2[1]), parseInt(point1[0]), parseInt(point1[1]), anch);
    }
    else {
        var point = rels[0].split(",");
        ScrollTo(parseInt(point[0]), parseInt(point[1]), anch);
    }

    SetTopMenu(0, 0);
}

// This setup's the top menu item's coordinates.  It's possible that we are looking at a sub nav item, but hit a top
// level item.  This code will add the sub level item's parent's coordinates into the top menu item's coordinates.
// It will always place them last, because its easier to add/remove them from the end of the string, that trying
// to add them into the start of the string.
// These extra coordinates are then used when the user moves from sub level to top level.
function SetTopMenu(x, y) {

    $$(".navigation a").each(function(anch) {

        if (anch.get("parent") == null) {
            // get the first rel
            var rel = anch.get("rel").split("|");
            var firstPoint = rel[0];
            var newRel = "";

            if (x == 0 && y == 0) {
                newRel = firstPoint;
                anch.set("rel", newRel);
            }
            else {
                if (firstPoint != x + "," + y) // we don't want 2 points in the rel the same, else we get flicker.
                    newRel = firstPoint + "|" + x + "," + y;
                else
                    newRel = firstPoint;

                anch.set("rel", newRel);
            }
        }
    });
}


