jQuery(document).ready(function()
{
    // switch
    jQuery('#content .switch').click(function() {
        jQuery(this).toggleClass('active').next().toggle();
        return false;
    });

    // indexes
    jQuery('.rightcol .tabs a').click(function()
    {
        var id = jQuery(this).attr('id');
        jQuery('.rightcol .tabs a').removeClass('active');
        jQuery('#indexes .indexes').hide();

        jQuery(this).addClass('active');
        jQuery('#block_'+ id ).show();
        return false;
    });
});


/*!
 * Menu class
 *
 * Класс для создания выпадающего меню
 * items - пункты меню
 * subitems - блок подменю
 */
function Menu(options)
{
    this.options = {
        items: '#menu a',
        subitems: '.menu_points',
        debug: false
    };

    if (typeof options == 'object')
    {
        for (var key in options)
        {
            this.options[key] = options[key];
        }
    }

    this.init();
}

Menu.prototype = 
{
    init: function() {
        var $menu_subitems = jQuery(this.options.subitems);

        if (this.options.debug)
            return;

        // menu
        jQuery(this.options.items).mouseover(function() 
        {
            var point_id = jQuery(this).attr('id');
            var $active_menu_point = jQuery('#'+ point_id +'_points');
            if ($active_menu_point.length == 1)
            {
                $menu_subitems.hide();
                $active_menu_point.show();
            }
        }).mouseout(function() {
            $menu_subitems.hide();
        });


        // menu points
        $menu_subitems.mouseover(function() {
            var point_id = jQuery(this).attr('id').replace('_points', '');
            jQuery('#' + point_id).addClass('active');
            jQuery(this).show();

        }).mouseout(function() {
            var point_id = jQuery(this).attr('id').replace('_points', '');
            jQuery('#' + point_id).removeClass('active');
            jQuery(this).hide();
        });
        
        
    }
}


/*!
 * Selector class
 *
 * Класс для создания кастомного селекта (аналог <select>)
 */
function Selector(options)
{
    this.options = {
        name: 'selector',
        link_jump: false,
        items_click: false,
        width: 'auto'
    };
    
    this.current_value = '';
    this.current_text = '';

    // redefine options
    if (typeof options == 'object')
    {
        for (var key in options)
        {
            this.options[key] = options[key];
        }
    }

    // признак того, что по селектору кликнули
    this.click_on_selector = false;
    this.init();
}

Selector.prototype = 
{
    init: function() {
        var that = this;
        var $switch = jQuery('#'+ this.options.name +'_switch');
        var $list = jQuery('#'+ this.options.name +'_list');
        var switch_width = isNaN(Number(this.options.width)) ? 'auto' : Number(this.options.width) + 'px';
        var list_width = isNaN(Number(this.options.width)) ? 'auto' : (Number(this.options.width)+21) + 'px';

        if (switch_width != 'auto')
            $switch.css('width', switch_width);

        if (list_width != 'auto')
            $list.css('width', list_width);

        // switch handler
        $switch.bind('click', function() {
            if ($list.css('display') == 'block')
                that.hide();
            else
                that.show();

            that.click_on_selector = true;
            return false;
        });
        
        // list handler
        $list.bind('click', function() { that.click_on_selector = true; });

        // list elements handler
        jQuery('#'+ this.options.name +'_list a').bind('click', function() {
            that.current_text = jQuery(this).text();
            jQuery('#' + that.options.name +'_switch dfn').text(that.current_text);
            that.hide();
            
            if (typeof that.options.items_click == 'function')
                that.options.items_click(this);

            if (!that.options.link_jump)
            {
                return false;
            }
        });

        // document handler
        jQuery(document).bind('click', function() {
            that.click_on_selector = false;
            if (that.click_on_selector == false)
            {
                that.hide();
            }
        });
    },

    show: function() {
        jQuery('#'+ this.options.name + '_list').addClass('open');
    },

    hide: function() {
        jQuery('#'+ this.options.name + '_list').removeClass('open');
    }
}


/*!
 * Bubble class
 *
 * Класс для создания бабла (всплывающего слоя)
 */
function Bubble(options)
{
    this.options = {
        name: 'bubble',
        additional_class: '',
        place: document.body,
        wrap: false,
        close_button_caption: 'Закрыть',
        handle: false
    };

    // redefine options
    if (typeof options == 'object')
    {
        for (var key in options)
        {
            this.options[key] = options[key];
        }
    }

    this.bubble_str  = '<div class="bubble" id="bubble_'+ this.options.name +'">';
    this.bubble_str += '<div class="up"><span class="close_button" title="'+ this.options.close_button_caption +'"></span></div>';
    this.bubble_str += '<div class="body"><div class="in'+ this.options.additional_class +'"></div></div>';
    this.bubble_str += '<div class="down"></div>';
    this.bubble_str += '</div>';
    
    if (this.options.wrap)
    {
        this.bubble_str = '<div class="bubble_wrap">'+ this.bubble_str +'</div>';
    }

    this.init();
}

Bubble.prototype = 
{
    init: function() {
        var that = this;
        jQuery(this.options.place).prepend(this.bubble_str);
        jQuery('#bubble_' + this.options.name + ' .close_button').click(function()
        {
            that.hide();
        });

        if (this.options.handle_elements)
            this.handle(this);

        return this;
    },

    getBubble: function() {
        return jQuery('#bubble_' + this.options.name);
    },

    show: function() {
        this.getBubble().show();
        return this;
    },

    hide: function() {
        this.getBubble().hide();
        return this;
    },
    
    text: function(text) {
        jQuery('#bubble_' + this.options.name + ' .in').html(text);
        return this;
    },

    handle: function(bubble) {
        jQuery(this.options.handle_elements).click(function()
        {
            var details = jQuery(this).find('.details').html();
            if (details)
            {
                var left = jQuery('#content').offset().left;
                var top = jQuery(this).offset().top;
                top -= (jQuery.browser.mozilla || jQuery.browser.webkit) ? 50 : 20;
                left -= 10;

                bubble.getBubble().css({
                    top: Math.round(top), 
                    left: Math.round(left)
                });

                bubble.text(details).show();
                return false;
            }
        });
    }

}

function get_swf(movieName) {
    return document.getElementById(movieName);
}

function array_max(arg) {
    if (arg instanceof Array)
    {
        var max = arg[0];
        for (var i = 0, len = arg.length; i < len; i++)
        {
            if (arg[i] > max)
                max = arg[i];
        }
        return max;
    }
    return false;
}

function array_rand(input, num_req) {
    // Return key/keys for random entry/entries in the array  
    // 
    // version: 1004.2314
    // discuss at: http://phpjs.org/functions/array_rand    // +   original by: Waldo Malqui Silva
    // *     example 1: array_rand( ['Kevin'], 1 );
    // *     returns 1: 0
    var indexes = [];
    var ticks = num_req || 1;    var checkDuplicate = function ( input, value ) {
        var exist = false, index = 0;
        while ( index < input.length ) {
            if ( input [ index ] === value ) {
                exist = true;                break;
            }
            index++;
        }
        return exist;
    };
 
    if ( input instanceof Array && ticks <= input.length ) {
        while ( true ) {
            var rand = Math.floor( ( Math.random( ) * input.length ) );            if ( indexes.length === ticks ) { break; }
            if ( !checkDuplicate( indexes, rand ) ) { indexes.push( rand ); }
        }
    } else {
        indexes = null;    }
 
    return ( ( ticks == 1 ) ? indexes.join( ) : indexes );
}

jQuery.noConflict();