/*
    Jquery content rotator by Ugol Zreniya

    Notes:
    - Only one rotator at page supported in current version

//*/

//
// Code
//
var uzContentRotator = {
    contentClassName: "uz_rotation_item",
    contentBoxId: "uz_content_rotator_box",
    contentBox: false,
    autoRotateSpeed: 5000,
    rotateSpeed: 500,

    aContents: new Array(),
    itemBefore: 0,
    currentItem: 0,
    cnt: 0,
    tabsPosX: 0,
    tabsPosY: 0,
    tabsHeight: 0,
    tabsWidth: 0,
    uiObj: false,
    skipNextAutorotate: 0,

    init: function(opt){

        // Set options
        if(typeof(uzContentRotatorOptions) != 'undefined'){
            var opt = uzContentRotatorOptions;
        }
        
        if(typeof(opt) != 'undefined'){
            if(typeof(opt.autoRotateSpeed) != 'undefined'){
                this.autoRotateSpeed = opt.autoRotateSpeed;
            }
            if(typeof(opt.autoRotateSpeed) != 'undefined'){
                this.rotateSpeed = opt.rotateSpeed;
            }
            if(typeof(opt.contentClassName) != 'undefined'){
                this.contentClassName = opt.contentClassName;
            }
        }

        // Init content box
        this.contentBox = $("#"+this.contentBoxId);
        if(this.contentBox.length != 1){
            return false;
        }

        // Do not resize content box
        this.contentBox.css("min-width", this.contentBox.width());
        this.contentBox.css("min-height", this.contentBox.height());
        this.contentBox.css("vertical-align", "top");


        // Init contents
        this.aContents = $("div."+this.contentClassName);
        this.cnt = this.aContents.length;

        if(this.cnt){

            // Init tabs

            // Tabs html
            uiBox = '';
            uiBox += '<div class="uz_tabs_ui" style="list-style:none;">';

            uiBox += '<div class="uz_tabs_li" style="position:absolute;top:50%; left:-13px;border:#ff0000 0px solid;width:190px;"><table cellpadding=0 cellspacing=0 class="uz_tabs_li_content"><tr><td class="uz_tabs_li_content"><div class="uz_tabs_li_content" onMouseUp="javascript:uzContentRotator.manualShowItem(\'prev\');"><img class="uz_rotator_tab_pic" src="_img/rotate_arr_left.gif"></div></td></tr></table></div>\n';
            for(var i=0; i < this.cnt; i++){
                uiBox += '<li class="uz_tabs_li"><div style="display:none;"><table cellpadding=0 cellspacing=0 class="uz_tabs_li_content"><tr><td class="uz_tabs_li_content"><div class="uz_tabs_li_content" onMouseUp="javascript:uzContentRotator.manualShowItem('+i+');">'+(i+1)+'</div></div></td></tr></table></div>\n';
            }
            uiBox += '<div class="uz_tabs_li" align="right" style="position:absolute;top:50%; right:-12px; border:#00ff00 0px solid;width:190px;"><table cellpadding=0 cellspacing=0 class="uz_tabs_li_content"><tr><td class="uz_tabs_li_content"><div class="uz_tabs_li_content" onMouseUp="javascript:uzContentRotator.manualShowItem(\'next\');"><img class="uz_rotator_tab_pic" src="_img/rotate_arr_right.gif"></div></td></tr></table></div>\n';
            uiBox += '</div>';
            uiBox += '';

            // Place tabs html to document
            this.contentBox.prepend(uiBox);
            this.tabsUl = $("ui.uz_tabs_ui");


            this.tabsWidth = $("div.uz_tabs_container").width();
            this.tabsHeight = $("div.uz_tabs_container").height();

            // Do not resize tabs box
            $("div.uz_tabs_container").css("min-width", this.tabsWidth);
            $("div.uz_tabs_container").css("min-height", this.tabsHeight);

            // Calc tabs position
            this.setTabsPos();

            // Show first item and activate tab
            this.showCurrentItem();

            // Start auto rotate
            this.autoRotateStart();
        }
    },

    setTabsPos: function(){

        // Detect content box position and sizes
        var offset = this.contentBox.offset();
        var contentXSize = this.contentBox.width();
        var contentYSize = this.contentBox.height();

        // Calc tabs position
        this.tabsPosX = offset.left + contentXSize - this.tabsWidth - 0;
        if(this.tabsPosX < 0){
            this.tabsPosX = 0;
        }
        this.tabsPosY = offset.top + contentYSize - this.tabsHeight - 0;
        if(this.tabsPosY < 0){
            this.tabsPosY = 0;
        }

        $("div.uz_tabs_container").offset({ top: this.tabsPosY, left: this.tabsPosX });
        $("div.uz_tabs_container").offset({ top: this.tabsPosY, left: this.tabsPosX }); // Set twice to fix for IE and Chrome
        
        setTimeout('uzContentRotator.setTabsPos()', 400);
    },

    showNextItem: function(){
        this.itemBefore = this.currentItem;
        this.currentItem++;
        if(this.currentItem >= this.cnt){
            this.currentItem = 0;
        }
        this.showCurrentItem();
    },

    showCurrentItem: function(){
        var aTabsLi = $("li.uz_tabs_li");
        if(this.itemBefore != this.currentItem){
            this.fadeItem(this.itemBefore, "out");
            this.setTabActiveStatus(this.itemBefore, 0);
            setTimeout('uzContentRotator.setTabActiveStatus(uzContentRotator.currentItem, 1)', uzContentRotator.rotateSpeed + 10);
            setTimeout('uzContentRotator.fadeItem(uzContentRotator.currentItem, "in")', uzContentRotator.rotateSpeed + 10);
        } else {
            $(this.aContents[this.currentItem]).show();
            this.setTabActiveStatus(this.currentItem, 1);
        }
    },

    setTabActiveStatus: function(num, status){
        num++;  // skip first tab (arrow)
        var aTabsLi = $("li.uz_tabs_li");
        if(typeof(aTabsLi[num]) != "undefined"){
            var tmpDiv = $(aTabsLi[num]).find("div.uz_tabs_li_content");
            if(tmpDiv.length){
                if(status == 1){
                    // Select tab
                    $(tmpDiv[0]).addClass("uz_tabs_li_content_sel");
                } else {
                    // Deselect tab
                    $(tmpDiv[0]).removeClass("uz_tabs_li_content_sel");
                }
            }
        }
    },

    showItem: function(num){
        // Hide old item
        if(num == "next"){
            num = this.currentItem + 1;
        }
        if(num == "prev"){
            num = this.currentItem - 1;
        }
        if(!isNaN(num)){
            num = parseInt(num);
            if(num >= 0 && num < this.cnt){
                this.itemBefore = this.currentItem;
                this.currentItem = num;
                this.showCurrentItem();
            }
        }
    },

    manualShowItem: function(num){
        this.skipNextAutorotate = 2;
        this.showItem(num);
    },

    fadeItem: function(num, to){
        if(to == "in"){
            $(this.aContents[num]).fadeIn(this.rotateSpeed);
        } else {
            $(this.aContents[num]).fadeOut(this.rotateSpeed);
        }
    },


    autoRotateStart: function(){
        if(uzContentRotator.autoRotateSpeed > 0){
            setTimeout(uzContentRotator.autoRotate, uzContentRotator.autoRotateSpeed);
        }
    },

    autoRotate: function(){
        // Rotate to next
        if(!uzContentRotator.skipNextAutorotate){
            uzContentRotator.showNextItem();
        }
        uzContentRotator.skipNextAutorotate--;
        if(uzContentRotator.skipNextAutorotate < 0){
            uzContentRotator.skipNextAutorotate = 0;
        }
        // Restart auto rotate
        uzContentRotator.autoRotateStart();
    },

    endvar: 1
};

//
// Init
//

$(document).ready(function(){
    uzContentRotator.init();
});

