﻿
$.fn.rotation = function(options) {

    var t = n = count = 0;

    options = $.extend({
        delay: 2000,
        rotationSelector: ".rotation-selector",
        rotationText: ".rotation-text",
        rotationItems: ".rotation-items"
    }, options || {});

    var self = $(this);
    var itemsWidth = self.width();
    var itemsHeight = self.height();
    var rotationSelector = self.find(options.rotationSelector);
    var rotationText = self.find(options.rotationText);
    var rotationItems = self.find(options.rotationItems);
    var delay = options.delay;

    count = rotationItems.find("img").size();
    rotationItems.find("img").css({ "width": itemsWidth, "height": itemsHeight });
    //rotationText.html(rotationItems.find("a:first-child img").attr('alt'));

    rotationSelector.find("td:first-child").css({ "background": "#fff", 'color': '#000' });
    rotationText.click(function() { window.open(rotationItems.find("a:first-child").attr('href'), "_blank") });

    rotationSelector.find("td").click(function() {
        var i = $(this).text() - 1;
        n = i;
        if (i >= count) return;
        //rotationText.html(rotationItems.find("a:eq(" + i + ") img").attr('alt'));
        //rotationText.unbind().click(function() { window.open(rotationItems.find("a:eq(" + i + ")").attr('href'), "_blank") });
        rotationItems.find("img").hide().eq(i).fadeIn(0);
        $(this).css({ "background": "#fff", 'color': '#000' }).siblings().css({ "background": "#000", 'color': '#fff' });
    });

    var showAuto = function() {
        n = n >= (count - 1) ? 0 : n + 1;
        rotationSelector.find("td").eq(n).trigger('click');
    }

    t = setInterval(showAuto, delay);
    self.hover(function() { clearInterval(t) }, function() { t = setInterval(showAuto, delay); });
}


$.fn.rotationImage = function(options) {
    options = $.extend({
        delay: 10000
    }, options || {});

    var self = $(this);
    var delay = options.delay;

    var showImage = function() {
        self.each(function() {
            var rotation = $(this);

            var count = rotation.children("a").size();

            var i = rotation.data("index") || 0;
            rotation.children("a").hide().eq(i).show();
            i = i >= (count - 1) ? 0 : i + 1;
            rotation.data("index", i);
        });
    };

    showImage();

    setInterval(showImage, delay);
}



