﻿function css_browser_selector(u) { var ua = u.toLowerCase(), is = function (t) { return ua.indexOf(t) > -1 }, g = 'gecko', w = 'webkit', s = 'safari', o = 'opera', m = 'mobile', h = document.documentElement, b = [(!(/opera|webtv/i.test(ua)) && /msie\s(\d)/.test(ua)) ? ('ie ie' + RegExp.$1) : is('firefox/2') ? g + ' ff2' : is('firefox/3.5') ? g + ' ff3 ff3_5' : is('firefox/3.6') ? g + ' ff3 ff3_6' : is('firefox/3') ? g + ' ff3' : is('gecko/') ? g : is('opera') ? o + (/version\/(\d+)/.test(ua) ? ' ' + o + RegExp.$1 : (/opera(\s|\/)(\d+)/.test(ua) ? ' ' + o + RegExp.$2 : '')) : is('konqueror') ? 'konqueror' : is('blackberry') ? m + ' blackberry' : is('android') ? m + ' android' : is('chrome') ? w + ' chrome' : is('iron') ? w + ' iron' : is('applewebkit/') ? w + ' ' + s + (/version\/(\d+)/.test(ua) ? ' ' + s + RegExp.$1 : '') : is('mozilla/') ? g : '', is('j2me') ? m + ' j2me' : is('iphone') ? m + ' iphone' : is('ipod') ? m + ' ipod' : is('ipad') ? m + ' ipad' : is('mac') ? 'mac' : is('darwin') ? 'mac' : is('webtv') ? 'webtv' : is('win') ? 'win' + (is('windows nt 6.0') ? ' vista' : '') : is('freebsd') ? 'freebsd' : (is('x11') || is('linux')) ? 'linux' : '', 'js']; c = b.join(' '); h.className += ' ' + c; return c; }; css_browser_selector(navigator.userAgent);

$(document).ready(function () {

    initClearerDivs();
    configureBackgrounds();
    initNavigation();
    writeSounds();
    initScrollers();
    initTooltip();
    initCentredImages();
    initLightbox();
    initAlbumImages();
    initVideos();
});

function writeSounds() {
    $("#sounds").empty();
    var params = {
        menu: "false",
        scale: "noScale",
        wmode: "opaque"
    };

    swfobject.embedSWF("http://p1.soundcloud.com/player.swf?url=http%3A%2F%2Fsoundcloud.com%2Fkevinwuzhere%2Fsets%2Fbig-fuzz&show_comments=true&auto_play=false&show_playcount=true&show_artwork=true&color=000000&theme_color=1c1c1c", "sounds", "275", "202", "9.0.28", "/Flash/expressInstall.swf", null, params);
}

function initVideos() {

    $("#video-list").delegate("a", "click", function (e) {
        e.preventDefault();

        // resets the current music.
        writeSounds();

        var id = $(this).attr("href");
        id = id.substring(id.indexOf("watch?v=") + 8);
        if (id.indexOf("&") != -1) {
            id = id.substring(0, id.indexOf("&"));
        }

        var newEmbed = $("<iframe></iframe>")
            .attr("width", "560")
            .attr("height", "349")
            .attr("frameborder", "0")
            .attr("allowfullscreen", "allowfullscreen")
            .attr("src", "http://www.youtube.com/embed/" + id + "?rel=0&amp;showinfo=0&amp;controls=0&amp;autoplay=1&amp;iv_load_policy=3");

        $("#video-player iframe").replaceWith(newEmbed);
        $("#video-list a.active").removeClass("active");
        $(this).addClass("active");
    });
}

function initClearerDivs() {
    $(".clear").append($("<span></span>").addClass("clear").html("&nbsp;")).removeClass("clear");
}

function initNavigation() {
    $("#nav li").delegate("a", "click", function (e) {
        e.preventDefault();

        $("#content").scrollTo($(this).attr("href"), 750, { axis: 'x', easing: 'easeOutCubic' });
        $("#nav li a.active").removeClass("active");
        $(this).addClass("active");

        location.hash = $(this).attr("href");
    });

    $("h1").delegate('a', 'click', function (e) {
        e.preventDefault();
        $("#content").scrollTo("#home", 750, { axis: 'x', easing: 'easeOutCubic' });
        $("#nav li a.active").removeClass("active");
        location.hash = "";
    });

    $("#content").scroll(function () {
        var left = $('#content').scrollLeft();
        var width = $("#content-areas").width();
        var pct = (left / width);

        $("#facebook-feed").css("left", "-" + (pct * 0.025) * width + "px");
        $("#band-pics").css("left", "-" + (pct * 0.25) * width + "px");
    });

    if (location.hash != '') {
        $("#nav a[href='" + location.hash + "']").click();
    }
}

function initScrollers() {

    $('#gigs-container').jScrollPane();
    $('#bio-box .bio-details>div').jScrollPane();
}

function initTooltip() {
    $("body").prepend($("<div></div>").attr("id", "tooltip").hide());
    $(".tooltip").hover(function () {
        var offset = $(this).offset();
        var width = $(this).width();
        $("#tooltip").text($(this).text());
        $("#tooltip").css("top", offset.top - 37 + "px");
        $("#tooltip").css("left", offset.left - (120 - (width / 2)) + "px");
        $("#tooltip").show();
    }, function () {
        $("#tooltip").hide();
    });
}

function initCentredImages() {

    $('.centered-image img').load(function () {
        var parent = $(this).parent();
        var parentH = parent.height();
        var thisH = $(this).height();

        if (thisH < parentH) {
            $(this).css("top", (parentH - thisH) / 2 + "px");
        }
    });
}

function initAlbumImages() {
    $("#gallery-box a:first").addClass("first");
    $("#gallery-box a:last").addClass("last");
    $("#gallery-box a").click(function (e) {
        e.preventDefault();
        $("gallery-box a.active").removeClass("active");
        $(this).addClass("active");
        loadImage($(this));
    });
}

function loadImage(link) {
    $("#lightbox-bg").show();
    $("#lightbox-bg img").show();
    var lightbox = $("#lightbox");
    lightbox.hide();
    lightbox.find(".content").remove();
    var oldImg = lightbox.find('img.gallery-image');

    var img = $("<img></img>").attr('src', link.attr("href")).addClass("gallery-image");
    if (oldImg.length > 0) {
        oldImg.replaceWith(img);
    } else {
        lightbox.append(img);
    }

    img.load(function () {
        resizeAndCentreLightbox();
        lightbox.fadeIn(500);
        $("#lightbox-bg img").hide();
        lightbox.find("a.next, a.prev").show();
    });
}

function initLightbox() {

    var lightbox = $("<div></div>").attr("id", "lightbox");
    lightbox.append($("<a></a>").attr("href", "#").addClass("close").text("Close"));
    lightbox.append($("<a></a>").attr("href", "#").addClass("next").text("Next"));
    lightbox.append($("<a></a>").attr("href", "#").addClass("prev").text("Prev"));
    $("body").prepend(lightbox.hide());
    $("body").prepend($("<div></div>").attr("id", "lightbox-bg").append($("<img></img>").attr("src", "/images/ui/ajax-loader.gif").attr("alt", "Loading...")).hide());

    $(".lightbox-popup").hide();
    $("a.lightbox-open").click(function (e) {
        e.preventDefault();
        $("#lightbox-bg").show();
        var content = $("#" + $(this).attr("rel")).clone();
        content.addClass("content").show();
        var lightbox = $("#lightbox");
        lightbox.find(".content").remove();
        if (lightbox.find("img.gallery-image").length > 0) {
            lightbox.find("img.gallery-image").replaceWith(content);
        } else {
            lightbox.append(content);
        }

        lightbox.find("a.next, #lightbox a.prev").hide();

        resizeAndCentreLightbox();
        $("#lightbox-bg img").hide();
        lightbox.fadeIn(500);
    });

    lightbox.delegate("a.close", "click", function (e) {
        e.preventDefault();
        $("#gallery-box a.active").removeClass('active');
        hideLightbox();
    });
    
    lightbox.delegate("a.next", "click", function (e) {
        e.preventDefault();
        var active = $("#gallery-box a.active");
        var next = active.next();
        if (active.hasClass("last"))
            next = $("#gallery-box a.first");

        active.removeClass("active");
        next.addClass("active");
        loadImage(next);
    });

    lightbox.delegate("a.prev", "click", function (e) {
        e.preventDefault();
        var active = $("#gallery-box a.active");
        var prev = active.prev();
        if (active.hasClass("first"))
            prev = $("#gallery-box a.last");

        active.removeClass("active");
        prev.addClass("active");
        loadImage(prev);
    });
}

function showLightbox() {
    $("#lightbox-bg, #lightbox").show();
}
function hideLightbox() {
    $("#lightbox-bg, #lightbox").hide();
}

function resizeAndCentreLightbox() {

    var lightbox = $("#lightbox");
    if (lightbox.find("img.gallery-image").length > 0) {
        lightbox.find("img.gallery-image").height($(window).height() * 0.8);
    } 
    if (lightbox.find("div.content").length > 0) {
        var size = $(window).height() * 0.8;
        lightbox.find("div.content").css("width", size * 1.33 + "px").css("height", size + "px");
    }

    var width = lightbox.width();
    var wWidth = $(window).width();
    var height = lightbox.height();
    var wHeight = $(window).height();

    $("#lightbox").css("left", (wWidth - width) / 2 + "px").css("top", (wHeight - height) / 2 + "px");
    $("#lightbox a.next, #lightbox a.prev").css("top", (height - 40) / 2 + "px");
}

function positionCentreBoxes() {

    var wWidth = $(window).width();
    $(".content-container").css("left", (wWidth - 970) / 2 + "px");
}

function configureBackgrounds() {
    var wWidth = $(window).width();
    
    $("#content-areas").css("width", wWidth * 24);
    $("#content-areas > div").css("width", wWidth * 4);
    $("#content-areas > div").each(function (idx) {
        $(this).css("left", (wWidth * 4) * idx);
    });

    $("#band-pics").css("width", wWidth * 6);
    $("#band-pics > span").each(function (idx) {
        $(this).css("width", wWidth);
        $(this).css("left", wWidth * idx + "px");
    });

    $("#facebook-feed").css("width", wWidth * 1.5 + "px");

    positionCentreBoxes();
}

$(window).resize(function () {
    configureBackgrounds();
    positionCentreBoxes();
    resizeAndCentreLightbox();

    var active = $("#nav li a.active").attr("href");
    $("#content").scrollTo(active, 0);
});
