"use strict";

/**
 * Created by JetBrains PhpStorm.
 * User: gatsu
 * Date: 18/07/11
 * Time: 00:04
 * To change this template use File | Settings | File Templates.
 */

var site = {};

/**
 * blockTabs
 */
site.blockTabs = function() {
    this.initialize.apply(this, arguments);
};
site.blockTabs.prototype = {
    constructor:site.blockTabs,
    initialize : function(element) {
        var _this = this;
        this.element = $(element);
        this.element.find('.tabs li').each(function(i, li) {
            $(this).data('tabindex', i).click(function(e) {
                e.preventDefault();
                _this.changeTab($(this).data('tabindex'));
            });

        });
    },

    changeTab : function(tabindex) {
        this.element.find('.tabs li')
            .removeClass('current')
            .eq(tabindex)
            .addClass('current');

        this.element.find('.blkbd>.tabContent')
            .removeClass('tabCurrent')
            .eq(tabindex)
            .addClass('tabCurrent');
    }
};


(function($) {
    $.fn.slideShow = function() {
        var _this = $(this),
            interval,
            images = _this.find('img');
        images.css({
            position:'absolute',
            left:_this.width(),
            top:0,
            display:'block'
        });

        _this.css({
            'height' : images.height()
        });

        var toolbar = $('<div id="ft-buttons-slideShowContent" style="position:absolute; right:10px; top:10px"></div>');

        var strtoobar = [];
        images.each(function(i) {
            strtoobar.push('<a class="ft-button-slideShowContent" href="#" style="padding: 5px;" index="' + i + '">' + (i + 1) + '</a>');
        });
        toolbar.html(strtoobar.join(''));

        var current = -1;

        function animate(newindex, firsttime) {
            clearTimeout(interval);
            if (newindex >= images.length) newindex = 0;
            if (!firsttime && newindex == current) return;
            var newimage = images.eq(newindex);
            if (!firsttime) {
                var oldimage = images.eq(current);
            }
            var width = _this.width();
            if (firsttime) {
                newimage.dequeue().css('left', width - newimage.width());
            } else {
                newimage.dequeue().css('left', width);
            }
            if (!firsttime) {
                newimage.dequeue().animate({'left': width - newimage.width()});
                oldimage.dequeue().animate({'left':  -oldimage.width()});
            }
            current = newindex;
            toolbar.find('a').removeClass('current').eq(current).addClass('current');
            startTimer();
        }


        toolbar.click(function(e) {
            e.preventDefault();
            var target = $(e.target);
            if (target.is('a')) {
                var index = target.attr('index');
                animate(index);
            }

        });

        function startTimer() {
            clearTimeout(interval);
            interval = setTimeout(function() {
                animate(current + 1);
            }, 5000);
        }

        _this.append(toolbar);
        animate(0, true);
    };
})(jQuery);


/**
 * main
 */
$(document).ready(function() {
    $('div.blkTabs').each(function() {
        new site.blockTabs(this);
    });

});

$(window).load(function() {
    $('#slideShowContent').slideShow();
    var maxH = 0;
    $('#leftCol,#mainCol,#rightCol').each(
        function() {
            console.log(this);
            if (maxH < this.offsetHeight) {
                maxH = this.offsetHeight;
            }
        }).each(function() {
            $(this).css('min-height', maxH);
        });
});

