var currentPath = document.location.pathname;

function splitHash(hash)
{
    return hash.substr(1).split("/");
}

function getHashPath() {
    var hashPath = document.location.hash;
    if (hashPath)
        return hashPath.substr(1);
    return document.location.pathname;
}

function checkHash() {
    var hashPath = getHashPath();
    if (currentPath != hashPath) {
        if (hashPath.substr(0,9) == "ieHistory") {
            document.location.hash = hashPath.substr(10);
            hashPath = hashPath.substr(10);
        }
        loadPath(hashPath, currentPath);
    }
}

function defaultQuery(path)
{

    var element = $('#content-area');
    if (element.length) {
        var effect = slideFadeEffect;
        ajaxQuery(path, element, effect);
    }
}

function loadPath(path, oldPath) {
    // FIXME: decide what element to replace and what effect to
    // use based on how much of the path changed
    currentPath = path;
    currentModule = splitHash(currentPath)[0];
    oldModule = splitHash(oldPath)[0];
    currentVars = splitHash(currentPath);
    oldVars = splitHash(oldPath);
    if (currentModule == oldModule && currentModule == "events") {
        if (currentVars.length == 3) {
            // changing months
            var element = $('#ic_grid_container');
            if (element.length) {
                var effect = fadeEffect;
                ajaxQuery(path, element, effect);
            }
       }
       else if (currentVars.length == 4) {
            // changing days
            var element = $('#img_calendar_container');
            if (element.length) {
                var effect = fadeEffect;
                ajaxQuery(path, element, effect);
            }
       }
       else
           defaultQuery(path);
    }
    else
        defaultQuery(path);
}

function ajaxQuery(path, element, effect) {
    var params = { ajaxElement: element.attr('id') };
    effect(element, true);
    $.get(path, params, function(data) {
        element.queue(function() {
            var html = data.replace(RegExp('href="/', 'g'), 'href="#/');
            element.html(html);
            effect(element, false);
            element.dequeue();
        });
    });
}

function slideFadeEffect(element, toggle, callback) {
    var action = toggle ? 'hide' : 'show';
    var props = { opacity: action, height: action };
    var speed = toggle ? 400 : 600;
    return element.animate(props, speed, callback);
}

function fadeEffect(element, toggle, callback) {
    var action = toggle ? '0.0' : '1.0';
    var props = { opacity: action };
    var speed = toggle ? 400 : 600;
    return element.animate(props, speed, callback);
}

function hashAnchors(element) {
    $("a[href^='/']", element).attr('href', function() {
        return '#' + $(this).attr('href');
    });
}

function ieHashChangeHandler(state) {
}

if (!($.browser.msie && $.browser.version == '6.0')) {

$().ready(function() {
    hashAnchors(document);
    if (document.location.hash.substr(0,10) == "#ieHistory")
        document.location.hash = document.location.hash.substr(11);
    if (document.location.pathname == "/" && !document.location.hash) {
        $('#content-and-footer').hide();
        $('#ajax-body-reveal').fadeIn(500, function() {
            $('#content-and-footer').show();
            $('#ajax-content-reveal').fadeIn(1200);
            document.location.hash = "#/";
            var hashPath = "#/";
        });
    } else {
        $('#ajax-body-reveal').show();
        var hashPath = getHashPath();
        if (hashPath == document.location.pathname)
            $('#ajax-content-reveal').show();
        else
            loadPath(hashPath, '');
    }
    if (document.all) {
        if (!hashPath)
            hashPath = "#/";
        else if (document.location.pathname != "/")
            hashPath = document.location.pathname;
        YAHOO.util.History.register("ieHistory", hashPath, ieHashChangeHandler);
        YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe");
        YAHOO.util.History.onReady(function () {
            var ieCurrentState = YAHOO.util.History.getCurrentState("ieHistory");
        });
    }
    setInterval("checkHash()", 100);
});

} /* not msie 6.0 */
