/**
 * @fileOverview
 * Index Tab.
 * @author redky
 * @update 2012-01-14
 */




$(function () {

    var help = $('#gallery-help'),
        panels = $('#gallery-panels'),
        list = panels.find('>li'),
        len = list.length,
        helpLink = '';

    if (len) {
        for (var i = 0; i < len; i++) {
            helpLink += '<a href="#"></a>';
        }
        help.html(helpLink);
    }

    function loading(src, id, context) {
        context = context || document;
        var img = new Image;
        img.src = src;
        var elem = $('#' + id, context);

        img.onload = function () {
            setTimeout(function () {
                elem.attr('src', src);
            }, 10);
        };

        img.onerror = function () {
            setTimeout(function () {
                var def = elem.attr('data-def');
                def && elem.attr('src', def);
                var closest = elem.closest('[data-default-img]');
            }, 10);
        };
    }

    var TIMEOUT = 8000,
        timer = null,
        tabsapi = null;

    help.tabs('#gallery-panels > li', {rotate: true, onBeforeClick: function (event, index) {
        var panels = this.getPanes(),
            img = panels.eq(index).find('img'),
            src = img.attr('data-src');
            id = img.attr('id');
        loading(src, id);
    }});
    tabsapi = help.data('tabs');

    (function () {

        var self = arguments.callee;
        timer && clearTimeout(timer);
        timer = setTimeout(function () {
            tabsapi.next();
            self();
        }, TIMEOUT);

    }());

});




