﻿if (typeof GMI.Slideshow == 'undefined') {
    GMI.Slideshow = {};
}

if (typeof GMI.Tabs == 'undefined') {
    GMI.Tabs = {};
}

GMI.Slideshow.CancelCategoryTimer = function() {
    $('body').queue([]);
}

GMI.Slideshow.ShowNextGroup = function() {
    GMI.Slideshow.CancelCategoryTimer();
    var activeElement = $('#box8 > div.active');
    var activePosition = activeElement.index() + 1;
    if (activePosition == $('#box8 > div').length) {
        $('#paneindicator span').first().trigger('click', [true]);
    } else {
        $('#paneindicator span:nth-child(' + (activePosition + 1) + ')').trigger('click', [true]);
    }
    GMI.Slideshow.StartCategoryTimer(5000);
}

GMI.Slideshow.StartCategoryTimer = function(time) {
    $('body').delay(time).queue(function() {
        GMI.Slideshow.ShowNextGroup();
    });
}

GMI.Tabs.ToggleTabs = function(evt) {
    var $this = $(this);
    $('ul.qtabs li.open').removeClass('open').addClass('closed');
    $this.addClass('open').removeClass('closed');
    var thisIndex = $this.index();
    $('#current-ex3 .qtcontent').hide();
    $('#current-ex3 .qtcontent:nth-child(' + (thisIndex + 1) + ')').show();
}

GMI.Slideshow.MoveSlideForward = function() {
    GMI.Slideshow.CancelCategoryTimer();
    $('#box8 > .active div.SlideItMoo_forward').die('click');
    var $itemContainer = $('#box8 > .active #SlideItMoo_items');
    var currentPos = Number($itemContainer.css('left').replace('px', ''));
    var currentIndex = currentPos / 135 * -1;
    if (currentIndex + 1 > $('#box8 > .active div.SlideItMoo_element').length - 4) {
        $itemContainer.append($itemContainer.html());
    }
    var newPos = currentPos - 135;
    $itemContainer.animate({ left: String(newPos) + 'px' }, 300, null, function() {
        $('#box8 > .active div.SlideItMoo_forward').live('click', GMI.Slideshow.MoveSlideForward);
    });
}

GMI.Slideshow.MoveSlideBack = function() {
    GMI.Slideshow.CancelCategoryTimer();
    $('#box8 > .active div.SlideItMoo_back').die('click');
    var $itemContainer = $('#box8 > .active #SlideItMoo_items');
    var totalItems = $('#box8 > .active div.SlideItMoo_element').length;
    var currentPos = Number($itemContainer.css('left').replace('px', ''));
    var currentIndex = currentPos / -135;
    if (currentIndex == 0) {
        var pixelOffset = totalItems * -135;
        currentPos = pixelOffset;
        $itemContainer.prepend($itemContainer.html());
        $itemContainer.css('left', pixelOffset + 'px')
    }
    var newPos = currentPos + 135;
    $itemContainer.animate({ left: newPos + 'px' }, 300, null, function() {
        $('#box8 > .active div.SlideItMoo_back').live('click', GMI.Slideshow.MoveSlideBack);
    });
}

GMI.Slideshow.SwapOuterContainer = function(evt, IsTriggeredByJQ) {
    var $this = $(this);
    if (!IsTriggeredByJQ) {
        GMI.Slideshow.CancelCategoryTimer();
    }
    var boxToShow = $this.index() + 1;
    $('#paneindicator span').css('background', 'url(/images/buttons/btn-pane-off.gif) no-repeat scroll 0 0 transparent');
    $this.css('background', 'url(/images/buttons/btn-pane-on.gif) no-repeat scroll 0 0 transparent');
    $('#box8 > div').fadeOut(500, function() {
        var $elToShow = $('#box8 > div:nth-child(' + boxToShow + ')');
        $elToShow.fadeIn(500).toggleClass('active');
        if ($elToShow.find('div.SlideItMoo_element').length < 5) {
            $elToShow.find('div.SlideItMoo_back').hide();
            $elToShow.find('div.SlideItMoo_forward').hide();
        }
    }).toggleClass('active');
}

$(document).ready(function() {
    $('#box8 > div').first().addClass('active');
    $('#paneindicator span').first().css('background', 'url(/images/buttons/btn-pane-on.gif) no-repeat scroll 0 0 transparent');
    $('#paneindicator span').click(GMI.Slideshow.SwapOuterContainer);

    $('ul.qtabs li').first().addClass('open').removeClass('closed');
    $('#current-ex3 .qtcontent').first().show();
    $('ul.qtabs li').live('click', GMI.Tabs.ToggleTabs);

    for (var i = 0; i < 4; i++) {
        $('#box8 > div div.SlideItMoo_element:nth-child(' + (i + 1) + ')').show();
    }

    $('#box8 > .active div.SlideItMoo_back').live('click', GMI.Slideshow.MoveSlideBack);
    $('#box8 > .active div.SlideItMoo_forward').live('click', GMI.Slideshow.MoveSlideForward);
    GMI.Slideshow.StartCategoryTimer(5000);
});

$(window).load(function() {
    document.getElementById('box8').onselectstart = function() { return false; } // ie
    document.getElementById('box8').onmousedown = function() { return false; } // mozilla
});
