﻿
(function($) {
    var cache = [];
    $.preLoadImages = function() {
        var args_len = arguments.length;
        for (var i = args_len; i--; ) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery)
jQuery.preLoadImages('../Img/BTN_1.png',
                      '../Img/BTN_2.png',
                      '../Img/BTN_3.png',
                      '../Img/BTN_4.png',
                      '../Img/BTN_5.png',
                      '../Img/BTN_6.png',
                      '../Img/BTN_1_active.png',
                      '../Img/BTN_2_active.png',
                      '../Img/BTN_3_active.png',
                      '../Img/BTN_4_active.png',
                      '../Img/BTN_5_active.png',
                      '../Img/BTN_6_active.png',
                      '../Img/1.png',
                      '../Img/2.png',
                      '../Img/3.png',
                      '../Img/4.png',
                      '../Img/5.png',
                      '../Img/6.png',
                      '../Img/1_BG.jpg',
                      '../Img/2_BG.jpg',
                      '../Img/3_BG.jpg',
                      '../Img/4_BG.jpg',
                      '../Img/5_BG.jpg',
                      '../Img/6_BG.jpg',
                      '../Img/TEPE_LOGO.png');

var timerval;
var interval = 12500;
var animating = false;
var selectedId = null;
function slideSwitch(id) {
    if (animating) {
        selectedId = id;
        return;
    }
    selectedId = null;
    animating = true;
    var speed = 800;
    var $selected = $('#slideshow a IMG#' + id);
    var $active = $('#slideshow a IMG.active');
    var $parent = $active.parent().next().children();
    if ($active.length == 0) $active = $('#slideshow a IMG:last');

    var $next;

    if ($selected.length == 0) {
        $next = $active.parent().next().children().length ? $active.parent().next().children()
                : $('#slideshow a IMG:first');
    }
    else {
        $next = $selected;
        clearInterval(timerval);
        timerval = setInterval("slideSwitch()", interval);
    }


    var backgroundimg = $next.attr("backgroundimg");
    var background = $next.attr("background");
    var activebackgroundimg = $active.attr("backgroundimg");
    



    $active
                .css({ opacity: 1.0 })
                .addClass('last-active')
                .animate({ opacity: 0.0 }, speed, function() {
                        $('#homescene')
                            .css('background-color', background);
                        $('#homescene')
                            .css({ opacity: 1.0 })
                            .css('background-image', 'url(' + activebackgroundimg + ')')
                            .animate({ opacity: 0.0 }, speed, function() {
                                $('#homescene')
                                    .css({ opacity: 0.0 })
                                    .css('background-image', 'url(' + backgroundimg + ')')
                                    .animate({ opacity: 1.0 }, speed, function() {
                                        $next
                                            .css({ opacity: 0.0 })
                                            .addClass('active')
                                            .animate({ opacity: 1.0 }, speed, function() {
                                                $active.removeClass('active last-active');
                                                animating = false;
                                                if (selectedId != null) {
                                                    slideSwitch(selectedId);
                                                }
                                        });
                                });
                        });
                });


    var nextId = $next[0].id;
    btnSwitch(nextId.substr(3));
}

$(function() {
    timerval = setInterval("slideSwitch()", interval);
});

function btnSwitch(id) {
    var $img = $('#slideshowbtn DIV IMG');
    $img.each(function() {
        var root = this.src;
        root = root.substr(0, root.indexOf("BTN_"));
        if (this.id == 'btn' + id) {
            this.src = root + 'BTN_' + id + '_active.png';
        }
        else {
            this.src = root + 'BTN_' + this.id.substr(3) + '.png';
        }
    });

}
