﻿$(document).ready(function() {
    var currentImage = null;
    var loop = null;
    var currentIndex = 0;
    var isPaused = false;
    var isStopped = false;
    var title = $('#focus-cover .photo-title');
    var mask = $('#focus-cover .photo-mask');

    jQuery.easing.def = "easeInOutCubic";
    var images = $('#first-list img, #second-list img').fadeTo(0, 0.3).hover(
        function() { isPaused = true; if ($(this).hasClass('active')) return; $(this).fadeTo(350, 1); },
        function() { isPaused = false; if ($(this).hasClass('active')) return; $(this).fadeTo(350, 0.3); }
    );
    var cover = $('#focus-cover .photo').css('width', images.length * 200);
    images.each(function(i) {
        this.id = i;
        cover.append('<img src="' + this.src + '" />');
    });
    images.click(function() {
        setActive(this, true);

        var number = this.src.substring(this.src.lastIndexOf('/')).replace(/[^0-9]/gi, '');
        var url = 'http://issuu.com/zydo/docs/press' + number + '?mode=embed&layout=http://zydo.affinica.com/issuu/themes/default/layout.xml';
        
        GB_showFullScreen(this.title, url, function() {
            isStopped = false;
            setActive();
            loop = window.setTimeout(setActive, 2000);
        });
        return false;
    });

    var left = 0;

    var setActive = function(image, stop) {
        if (isStopped) {
            return;
        }
        window.clearTimeout(loop);
        if (isPaused && !stop) {
            loop = window.setTimeout(setActive, 2000);
            return;
        }
        if (!stop) {
            loop = window.setTimeout(setActive, 2000);
        } else {
            isStopped = true;
        }
        if (!image || !image.id) {
            if (currentIndex + 1 > images.length) {
                currentIndex = 0;
            }
            image = images[currentIndex++];
        } else {
            for (var i = 0; i < images.length; i++) {
                if (image == images[i]) {
                    currentIndex = i + 1;
                    break;
                }
            }
        }
        $(currentImage).removeClass('active').fadeTo(150, 0.3);
        currentImage = image;
        $(image).addClass('active').fadeTo(350, 1).parent().blur();
        left = -image.id * 200;
        cover.animate({
            left: left
        }, 410);
        mask.show().fadeTo(250, 0);
        title.show().fadeTo(250, 0);
        window.setTimeout(function() {
            name = image.title.split(' | ');
            title.children('span').html(name[2]);
            try {
                title.children('strong').html(name[0]).children('em').html(name[1]);
            } catch (e) { }
            mask.fadeTo(250, 0.7);
            title.fadeTo(250, 1);
        }, 410);
    };
    setActive();
    loop = window.setTimeout(setActive, 2000);
    //]]>
});