var parent;
var section;
var oldsection;
var pressed;
var oldhit;
var fixstart;
var sectionstart;
var child;
var fix;

function pageload(hash, fix) {
    if (hash) {
			
        $("#timer").hide(0);

        $("#timer").show(10, function() {

            $("#loader").fadeIn(100);
            $("#timer").show(110, function() {

                $("#children").fadeIn(50);

                $("#timer").hide(60, function() {

                    position = (window.location + '').split('#');;
                    pressed = position[1];
                    if (pressed == undefined) {
                        pressed = hash;
                    }

                    parent = false;
                    child = false;

                    // check if we're on a menu or not
                    $('#parents > a').each(function() {
                        if ($(this).attr('id') == "p-" + pressed) {
                            parent = true;
                            nextp = '-';

                        }
                    });
                    $('#children > div > a').each(function() {
                        if ($(this).attr('id') == "c-" + pressed) {
                            child = true;

                        }
                    });

                    if (parent == true) {
                        counte = 0;
                        $('#children > div > a').each(function() {

                            if (($(this).hasClass(pressed))) {
                                counte++;

                            }
                        })
                            if (counte > 1) {

                            w = $("#parents").width();

                        } else {

                            w = $("#parents").width();

                        }
                    }
                    if (child == true) {

                        w = $("#parents").width();
                    }

                    if (fix == true) {
                        $('#children > div > a').each(function() {
                            if ($(this).attr('id') == "c-" + pressed) {
                                child = true;
                            }
                        });
                    }

                    if (fixstart == true && counte > 1) {
                        w = $("#parents").width();
                    }

                    $("#loader").css('visibility', 'visible');

                    $(oldhit).removeClass('active');

                    oldhit = "#c-" + hash;
                    oldsectionhit = "#p-" + hash;

                    tempclass = $(oldhit).attr('class');

                    // check if we're on a parent or not
                    $('#parents > a').each(function() {
                        if ($(this).attr('id') == "p-" + hash) {
                            $(this).addClass('active');
                            section = hash;
                            pressed = "p-" + section;

                        } else {
                            $(this).removeClass('active');
                        }
                    });

                    // check if we're on a sub menu
                    if (tempclass == undefined) {

                        // do nothing
                        } else {

                        theclass = $(oldhit).attr('class').substr(5);

                        $('#parents > a').each(function() {

                            if ($(this).attr('id') == "p-" + theclass) {

                                $(this).addClass('active');
                                section = theclass;

                            } else {

                                $(this).removeClass('active');

                            }
                        });

                    }

                    // check if we have subs or not
                    checkpressed = pressed.substr(0, 2);

                    if (checkpressed == "p-") {

                        count = 0;
                        mysubs = "";

                        $('#children > div > a').each(function() {

                            if (($(this).hasClass(section))) {
                                count++;

                                mysubs += $(this).attr('href').substr(1) + ",";

                                $(this).css('display', 'inline');

                            } else {
                                $(this).css('display', 'none');

                            }
                        });

                        subs = mysubs.split(",");

                        if (subs.length == -1) {

                            hash = subs[0];

                            $('#c-' + subs[0]).addClass('active');
                            oldhit = '#c-' + subs[0];
                        }

                    } else {

                        $('#children > div > a').each(function() {

                            if (($(this).hasClass(section))) {

                                $(this).css('display', 'inline');

                            } else {

                                $(this).css('display', 'none');

                            }
                        });

                    }

                    oldsectionhit = "#p-" + hash;
                    $(oldhit).addClass('active');

                    $(oldhit).css('cursor', 'arrow').click(function() {
                        return false;
                    });

                    $("#timer").show(70, function() {

                        $.ajax({
                            url: "home/page/" + hash,
                            cache: false,
                            success: function(html) {

                                $.scrollTo($('#top'), 0);

                                $("#loader").fadeOut(200);
                                $("#container").empty();
                                $("#container").append(html);

                                if (hash == "index") {
                                    $("#prevnext").css("display", "none")
                                    }
                                if (hash != "index") {
                                    $("#prevnext").css("display", "block")
                                    }

                                $("#container").append("&nbsp;");
                                $("#container").fadeIn(50);

                            }
                        });
                        
                    });
                });
            });
        });

    } else {
        $("#container").empty();
    }

}

$(function() {

    subs = new Array();
    subs[0] = "";
    subs[1] = "";
    subs[2] = "";

    parent = true;

    $("#timermenu").hide(0);
    $("#loader").fadeOut(0);

    startpos = (window.location + '').split('#');;
    startis = startpos[1];

    w = 0;

	$("a").live("mouseenter", function(){
    	$(this).fadeTo(150, 0.66);
    }).live("mouseleave", function(){
    	$(this).fadeTo(150, 1);
    });
	$(".thumb").live("mouseenter", function(){
    	$(this).fadeTo(150, 0.9);
    }).live("mouseleave", function(){
    	$(this).fadeTo(150, 1);
    });
        

    //check if we're on a sub on start
    $('#parents > a').each(function() {
        if ($(this).attr('href') == "#" + startis) {
            fixstart = true;
        }

    });

    $.historyInit(pageload);

    $("#parents a[rel='history']").click(function() {

        // if we're on a sub on start fix gap
        if (fixstart == true) {
            fixstart = false;
        }

        $('#children div > a').each(function() {
            $(this).removeClass('active');
        });

        parent = true;
        $("#p-" + section).removeClass('active');

        oldsection = section;
        section_tmp = this.href;
        section = section_tmp.replace(/^.*#/, '');

        $("#p-" + section).addClass('active');
        oldsectionhit = this;
        var hash = this.href;
        hash = hash.replace(/^.*#/, '');

        $.historyLoad(hash);
        $("#timermenu").hide(0);
        return false;
    });

    $("#children div > a[rel='history']").click(function() {
        $('#children div > a').each(function() {
            $(this).removeClass('active');
        });

        $(this).addClass('active');
        parent = false;
        $(oldhit).removeClass('active');
        oldhit = this;
        var hash = this.href;
        hash = hash.replace(/^.*#/, '');
        $.historyLoad(hash);
        return false;
    });
});
