$(document).ready(function () {
    $(".nojs").removeClass("nojs");
    var loadComplete = false;
    var pageData = "";
    var curPage = window.location.pathname;
    curPage = curPage.substring(curPage.lastIndexOf('/') + 1);
    if (curPage == "") {
        curPage = "index.php";
    }
    $("a:not([href*='http://']):not([href*='mailto:']):not(.outbox):not(.spreadbox>a):not(.video):not(.popgame):not(.gallery-thumbs>a)").live("click", function (e) {
        e.preventDefault();
        var page = $(this).attr("href");
        if (page == "/") {
            page == "index.php";
        }
        $.History.go(page);
    });
    $.History.bind(function (state) {
        if (curPage == state) { return false };
        curPage = state;
        $(".replace").fadeTo(0, 1, function () {
            loadNew();
        });
        if ($(".modal").length != 0) {
            $(".modal-close").trigger("click");
        }
        $.ajax({ type: "GET", url: "/content" + state, success: function (data) {
            pageData = data;
            loadComplete = true;
            _gaq.push(["_trackPageview", curPage]);
        }
        });
    });
    function loadNew() {
        if (loadComplete == false) {
            setTimeout(loadNew, 50);
        } else {
            loadComplete = false;
            animatePage();
        }
    }
    function animatePage() {
        $(".replace").html(pageData);
        check();
    }
    function check() {
        $(".replace").formControl();
        $(".video").ivmodal({
            classname: "vidmodal",
            animation: "-1 0",
            coverfade: .3,
            callback: function (one, two) {
                one.append(two.attr("alt"));
            }
        });
        $(".active").removeClass("active");
        $("a[href='" + curPage + "']").addClass("active");
        if ($.browser.msie) {
            $(".spreadbox>a, .popgame").ivmodal({ speed: "0", callback: function (one, two) { popgames(one, two) } });
        } else {
            $(".spreadbox>a").bind("mouseenter", function () {
                $(".outbox").stop().animate({ "height": "150", "width": "225", "margin-left": "6px", "margin-top": "6px" }, 300, "linear", function () {
                    $(this).remove();
                });
                $(this).clone().addClass("outbox").css({ "top": $(this).position().top + "px", "left": $(this).position().left + "px" }).appendTo($(this).offsetParent()).animate({ "height": "210", "width": "315", "margin-left": "-39px", "margin-top": "-24px", "opacity": "1" }, 300, "linear").bind("mouseenter", function () {
                    $(this).stop().animate({ "height": "210", "width": "315", "margin-left": "-39px", "margin-top": "-24px" }, 300, "linear");
                }).bind("mouseleave", function () {
                    $(this).stop().animate({ "height": "150", "width": "225", "margin-left": "6px", "margin-top": "6px" }, 300, "linear", function () {
                        $(this).remove();
                    });
                }).ivmodal({ animation: "-1 0", callback: function (one, two) { popgames(one, two) } });
            }).ivmodal({ animation: "-1 0", callback: function (one, two) { popgames(one, two) } });
        }
        $(".popgame").ivmodal({ animation: "-1 0", callback: function (one, two) { popgames(one, two) } });
        if ($(".gallery").length > 0) { setupGallery(); }
    }
    function popgames(one, two) {
        $.ajax({
            url: "/content" + two.attr("href"),
            method: "GET",
            success: function (data) {
                one.append(data);
                setupGallery();
            }
        });
    }
    //*** Gallery ***//
    function setupGallery() {
        $("#nojsgallery").remove();
        $(".video").ivmodal({
            classname: "vidmodal",
            animation: "-1 0",
            coverfade: .3,
            callback: function (one, two) {
                one.append(two.attr("alt"));
            }
        });
        if ($("#fb-root").length != 0) {
            window.fbAsyncInit = function () {
                FB.init({ appId: '202544439781701', status: true, cookie: true, xfbml: true });
            };
            (function () {
                var e = document.createElement('script'); e.async = true;
                e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
                document.getElementById('fb-root').appendChild(e);
            } ());
        }
        $("body").animate({ scrollTop: "0" }, 300);
        $(".gallery-thumbs-next").bind("click", function () { slideThumbs("+="); });
        $(".gallery-thumbs-prev").bind("click", function () { slideThumbs("-="); });
        $(".gallery-next, .gallery-display").bind("click", function () { nextImage(); });
        $(".gallery-prev").bind("click", function () { prevImage(); });
        $(".gallery-save").bind("click", function () { $("<iframe src='/saveimg.php?file=" + $(".gallery-display>span>img").first().attr("src").substr(1) + "'></iframe>").appendTo("body"); });
        $(".gallery-saveall").bind("click", function () {
            var s = $(".gallery-display>span>img").first().attr("src");
            $("<iframe src='" + s.slice(0, s.lastIndexOf("/")) + ".zip'></iframe>").appendTo("body");
        });
        $(".gallery-print").bind("click", function () { window.open("/printimg.php?file=" + $(".gallery-display>span>img").first().attr("src").substr(1)) });
        makefull();
        $(".gallery-thumbs>a").bind("click", function (e) {
            e.preventDefault();
            var leftpos = $(this).position().left;
            if ($(".gallery-thumbs").width() * 0.9 < leftpos + $(this).width() || $(".gallery-thumbs").width() * 0.1 > leftpos) {
                $(".gallery-thumbs").stop().animate({ scrollLeft: leftpos + $(".gallery-thumbs").scrollLeft() - $(".gallery-thumbs").width() * 0.1 }, 800);
            };
            var newimgsrc = $(this).children("img").attr("src").replace("thumbs", "album");
            $("<span><span></span><img src='' alt='' /></span>").fadeTo(0, 0).prependTo(".gallery-display").find("img").bind("load", function () {
                $(this).parent().fadeTo(400, 1);
            }).attr("src", newimgsrc);
            $(".gallery-display>span:gt(0)").fadeTo(400, 0, function () { $(this).remove() });
            $(".gallery-current").removeClass("gallery-current").fadeTo(200, 0.4);
            $(this).addClass("gallery-current").fadeTo(200, 1);
            $(".gallery-count").html($(this).index() + 1 + "/" + $(".gallery-thumbs>a").length);
        }).fadeTo(0, 0.4).hover(function () {
            if (!$(this).hasClass("gallery-current")) { $(this).stop().fadeTo(200, 1); }
        }, function () {
            if (!$(this).hasClass("gallery-current")) { $(this).stop().fadeTo(200, 0.4); }
        }).first().trigger("click").fadeTo(0, 1); ;
    }
    function makefull() {
        $(".gallery-fullscreen").unbind("click").bind("click", function () {
            $(".gallery-fullscreen img").attr("src", "/images/icons/Min.png");
            $(".gallery").wrap("<div class='fullscreen' />");
            $(".fullscreen").append("<span class='fsclose'>Close</span>").appendTo("body");
            returnfull();
        });
    }
    function returnfull() {
        $(".fsclose, .gallery-fullscreen").unbind("click").bind("click", function () {
            $(".gallery-fullscreen img").attr("src", "/images/icons/Max.png");
            $(".gallery").insertAfter(".nomodal>h2");
            $(".fullscreen").remove();
            makefull();
        });
    }
    function slideThumbs(direction) {
        $(".gallery-thumbs").stop().animate({ scrollLeft: direction + ($(".gallery-thumbs").width() * 0.8) }, 800);
    }
    function nextImage() { ($(".gallery-current").next().length != 0) ? $(".gallery-current").next().trigger("click") : $(".gallery-thumbs>a").first().trigger("click"); };
    function prevImage() { ($(".gallery-current").prev().length != 0) ? $(".gallery-current").prev().trigger("click") : $(".gallery-thumbs>a").last().trigger("click"); };

    check();
});
