﻿$(function() {
    $('.tabset a.tab').click(function() {
        stopImageRotate();
        setupTabs($(this).attr('href').substring(3));
        return false;
    });

    $('a.byclient').click(function() { $('.client-t a').click(); return false; });
    $('a.byservice').click(function() { $('.service-t a').click(); return false; });

    $.history.init(callback);
    $("a[rel='history']").click(function() {
        stopImageRotate();
        $.history.load(this.href.replace(/^.*#/, ''));
        return false;
    });

    $('#gal-imagelist-next').click(function() {
        stopImageRotate();
        nextImage();
        return false;
    });

    $('#gal-imagelist-prev').click(function() {
        stopImageRotate();
        prevImage();
        return false;
    });
});

function nextImage() {
    var nextImg = $('li.gal-image.active').next('li.gal-image');
    if (nextImg.length == 0) { //end of list
        nextImg = $('li.gal-image:first');
    }

    nextImg.children('a').click();
}
function prevImage() {
    var prevImg = $('li.gal-image.active').prev('li.gal-image');
    if (prevImg.length == 0) { //first of list
        prevImg = $('li.gal-image:last');
    }

    prevImg.children('a').click();
}

function callback(hash) {
    $("a[rel='history']").removeClass('active');

    if (hash) {
        $("div.tab li a[href='#" + hash + "']").addClass('active');
        $("#gal-content").load("/content/gallery/" + hash + ".htm?rand=" + Math.floor(Math.random() * 1000000), setupImages);
        $.pageTracker._trackPageview("/gallery/" + hash);
    } else {
        setupImages();
    }
    setupTabs(hash);
}

function setupImages() {
    var images = $('#gal-content img');
    if (images && images.length > 0) {
        $('#gal-image').attr('src', $(images[0]).attr('src'));

        $('#gal-imagelist li.gal-image').remove();
        var lastCurly = $('#gal-imagelist li:last');
        $.each(images, function(i, val) {
            var item = $("<li class='gal-image' />")
                        .append($("<a href='#gal-images" + (i + 1) + "'>" + (i + 1) + "</a>")
                            .click(function() {
                                $('#gal-image').attr('src', $(val).attr('src'));
                                $('#gal-imagelist li.gal-image').removeClass('active');
                                $(this).parent().addClass('active');
                                return false;
                            })
                        );
            if (i == 0) item.addClass('active');
            lastCurly.before(item);
        });

        startImageRotate();
    }
}

function setupTabs(hash) {
    var tab = (hash && hash.indexOf('service') == 0) ? "byservice" : "byclient";

    $('.tabset a.tab').removeClass('active');
    $('.tab-box div.tab').hide();

    $(".tabset a.tab[href='#" + tab + "']").addClass('active');
    $('#' + tab).show();
}

var timer;
function startImageRotate() {
    timer = setTimeout(function() { $('#gal-imagelist-next').click(); startImageRotate(); }, 4000);
}

function stopImageRotate() {
    clearTimeout(timer);
}