function enablePaneTouchScroll() {
    if (PANE_TOUCH_SCROLL != null) {
        PANE_TOUCH_SCROLL.destroy()
    }
    PANE_TOUCH_SCROLL = new iScroll("expand-wrap", {
        hScrollbar: true,
        vScrollbar: true
    })
}
function destroyPaneTouchScroll() {
    if (isTouchDevice() && PANE_TOUCH_SCROLL != null) {
        PANE_TOUCH_SCROLL.destroy();
        PANE_TOUCH_SCROLL = null
    }
}
function isTouchDevice() {
    return window.Touch || "ontouchstart" in document.documentElement
}
function onAfter(a, b, c) {
    var d = c.currSlide + 1 + " / " + c.slideCount;
    $("#gallery-paginator").html(d)
}
function animatePane() {
    $("#expand-wrap").unbind("mousemove");
    var a = parseInt($(".range-single").width() * $(".range-single").length);
    $("#pane").css("width", a);
    if (isTouchDevice()) {
        enablePaneTouchScroll();
        return
    }
    var b = 100;
    var c = $("#expand-wrap").offset().left + b;
    var d = f + c;
    var e = $("#expand-wrap").width();
    var f = e - 2 * b;
    var g = e - a;
    if (a > e) {
        $("#expand-wrap").mousemove(function (b) {
            var e = (c - b.pageX) / f;
            var h = Math.round(e * (a - f));
            if (h > 0) h = 0;
            if (h < g) h = g;
            $("#debugging_mouse_axis").html("X Axis : " + b.pageX + " | Y Axis " + b.pageY + " | Content Width: " + a + " | Left value : " + c + " | Right value: " + d);
            $("#pane").animate({
                left: h
            }, {
                queue: false,
                duration: 500
            })
        })
    }
}
function unloadMenu() {
    $("#expand").slideUp("fast", function () {
        destroyPaneTouchScroll();
        $("#expand").remove();
        menuClosed = true
    })
}
function loadContent(a) {
    if (a == "exhibitions-and-events") {
        $("#content").prepend('<div id="expand"><div id="expand-wrap" class="expand-wrap"></div><div class="expand-liner"></div></div>');
        $("#expand-wrap").load("/events/filter/", function () {
            $("html").animate({
                scrollTop: 0
            }, "fast", function () {
                $("#expand").delay("200").slideDown("fast", function () {
                    animatePane();
                    menuClosed = false;
                    clickedlink = a;
                    $("#gsearch").delay("500").slideUp()
                })
            })
        })
    } else {
        $("#content").prepend('<div id="expand"><div id="expand-wrap" class="expand-wrap"></div><div class="expand-liner"></div></div>');
        $("#expand-wrap").load("/menus/" + a, function () {
            $("html").animate({
                scrollTop: 0
            }, "fast", function () {
                $("#expand").delay("200").slideDown("fast", function () {
                    animatePane();
                    menuClosed = false;
                    clickedlink = a;
                    $("#gsearch").delay("500").slideUp()
                })
            })
        })
    }
}
function toggleMenu(a) {
    if (menuClosed) {
        loadContent(a)
    } else {
        nextclick = a;
        if (clickedlink == nextclick) {
            unloadMenu()
        } else {
            destroyPaneTouchScroll();
            $("#pane").remove();
            if (a == "exhibitions-and-events") {
                $("html").animate({
                    scrollTop: 0
                }, "fast", function () {
                    $("#expand-wrap").delay("200").load("/events/filter/", animatePane)
                })
            } else {
                $("html").animate({
                    scrollTop: 0
                }, "fast", function () {
                    $("#expand-wrap").delay("200").load("/menus/" + a, animatePane)
                })
            }
            menuClosed = false;
            clickedlink = a
        }
    }
}
var PANE_TOUCH_SCROLL = null;
$(document).ready(function () {
    menuClosed = true;
    $(".slideshow img").css("display", "block");
    $(".slideshow span").fadeIn("fast");
    $(".gsearch").click(function () {
        if (!menuClosed) {
            unloadMenu()
        }
        $("#gsearch").slideToggle("fast");
        $("#q").focus();
        $("#q").value = ""
    });
    $(".bgswap").hover(function () {
        $(this).css("background", "url(/media/img/" + this.id + "_over.png)")
    }, function () {
        $(this).css("background", "url(/media/img/" + this.id + ".png)")
    });
    $("#menu ul li a").hover(function () {
        $(this).css("background", "url(/media/img/alice.gif) no-repeat 50% 22px")
    }, function () {
        $(this).css("background", "")
    });
    $("#section1 li a.toggle").click(function () {
        pos = $(this).parent().attr("id");
        toggleMenu(pos);
        return false
    });
    $(".slideshow").cycle({
        fx: "scrollLeft",
        speed: 180,
        timeout: 9e3,
        next: ".slideshow img",
        after: onAfter
    });
    $("#main-page-gallery").mouseenter(function () {
        $(".slideshow-arrow").fadeIn("fast");
        $(".slideshow").cycle("pause")
    });
    $("#main-page-gallery").mouseleave(function () {
        $(".slideshow-arrow").fadeOut("fast");
        $(".slideshow").cycle("resume");
        offSlide = true
    });
    $(".single img").click(function () {
        $(".slideshow-arrow").fadeOut("fast")
    });
    $.preLoadImages("/media/img/follow-us-twitter_over.png", "/media/img/join-us-facebook_over.png", "/media/img/alice.gif", "/media/img/turn-left_over.png", "/media/img/turn-right_over.png");
    var a = $(window),
        b = $("#menu-container");
    var c = b.offset().top;
    a.scroll(function () {
        var d = a.scrollTop();
        b.toggleClass("stick", d > c)
    });
    creditOpen = false;
    $("#addressbar a.n").click(function () {
        $(".addr").slideUp("fast");
        $("#addressbar").append('<div class="credit"><p class="left">Spike Island identity by <a href="http://www.jameslangdonwork.net/">James Langdon</a>. Website design and programming by <a href="http://www.polimekanos.com">Polimekanos</a></p></div><p class="right"><a href="#/" class="c">close x</a></p>');
        $(".credit").slideDown("fast");
        return false
    });
    
        $("#addressbar a.c").click(function () {
        $(".credit").slideUp("fast");
        $(".addr").slideDown("fast");
        return false
    });
    
    $("#addressbar").cycle("pause");
    $("#masthead-nav div").hover(function () {
        $(this).addClass("active")
    }, function () {
        $(this).removeClass("active")
    });
    var d = $("#masthead-slides").cycle({
        fx: "scrollLeft",
        speed: 180,
        timeout: 0
    });
    $("#masthead-nav a").hover(function () {
        i = parseInt($(this).attr("id").replace(/\D/g, "") - 1);
        d.cycle(i)
    }, function () {
        $("#masthead-slides").cycle("resume")
    });
    $(".hp-news").hover(function () {
        $(this).css("background-color", "yellow")
    }, function () {
        $(this).css("background-color", "")
    });
    $(".hp-news").click(function () {
        link = $(this).contents().find("a").attr("href");
        window.location = link;
        return false
    });
    $(".submit-form").click(function () {
        $("#subForm").submit();
        return false
    });
    $("#home-columns div.single-col:nth-child(4)").addClass("last");
    $("head").append($('<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/_spikeisland.json?callback=twitterCallback&count=5"></script>'))
})

function loadOtherElements(p_current_elements){
	$("#other_elements").attr('id','other_elements_'+p_current_elements);
	$('#other_elements_'+p_current_elements).load(document.URL, {'current_elements':p_current_elements}, function(){
	});
	return false;
}
