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();
/*******************************************************************************************/
// jquery.event.wheel.js - rev 1
// Copyright (c) 2008, Three Dub Media (http://threedubmedia.com)
// Liscensed under the MIT License (MIT-LICENSE.txt)
// http://www.opensource.org/licenses/mit-license.php
// Created: 2008-07-01 | Updated: 2008-07-14
// $(body).bind('wheel',function(event,delta){    alert( delta>0 ? "up" : "down" );    });
/*******************************************************************************************/
; (function ($) { $.fn.wheel = function (a) { return this[a ? "bind" : "trigger"]("wheel", a) }; $.event.special.wheel = { setup: function () { $.event.add(this, b, wheelHandler, {}) }, teardown: function () { $.event.remove(this, b, wheelHandler) } }; var b = !$.browser.mozilla ? "mousewheel" : "DOMMouseScroll" + ($.browser.version < "1.9" ? " mousemove" : ""); function wheelHandler(a) { switch (a.type) { case "mousemove": return $.extend(a.data, { clientX: a.clientX, clientY: a.clientY, pageX: a.pageX, pageY: a.pageY }); case "DOMMouseScroll": $.extend(a, a.data); a.delta = -a.detail / 3; break; case "mousewheel": a.delta = a.wheelDelta / 120; if ($.browser.opera) a.delta *= -1; break } a.type = "wheel"; return $.event.handle.call(this, a, a.delta) } })(jQuery);

/*
* jQuery crawlLine v1.2.0
* Copyright (c) 2008 Taranets Aleksey
* email: aleks_tar@ukr.net
* www: markup-javascript.com
* Licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*/

jQuery.fn.crawlLine = function (_options) {
    // defaults options
    var _options = jQuery.extend({
        speed: 0.5,
        crawElement: 'div',
        textElement: 'dl',
        hoverClass: 'viewText'
    }, _options);

    return this.each(function () {
        var _THIS = jQuery(this);
        var _el = $(_options.crawElement, _THIS).css('position', 'relative');
        var _text = $(_options.textElement, _THIS);
        var _clone = _text.clone();
        var _elHeight = 0;
        var _k = 1;
        // set parametrs *******************************************************
        var _textHeight = 0;
        _text.each(function () {
            _textHeight += $(this).outerHeight(true);
        });
        var _duration = _textHeight * 50 / _options.speed;
        _el.append(_clone);
        _el.css('Height', _textHeight * 2);

        var animate = function () {
            _el.animate({ top: -_textHeight }, { queue: false, duration: _duration * _k, easing: 'linear', complete: function () {
                _el.css('top', '0');
                _k = 1;
                animate();
            } 
            })
        }
        animate();

        _THIS.hover(function () {
            _el.stop();
            _THIS.addClass(_options.hoverClass);
        }, function () {
            _THIS.removeClass(_options.hoverClass);
            _k = (_textHeight + parseInt(_el.css('top'))) / _textHeight;
            animate();
        })
        _THIS.bind('wheel', function (event, delta) {
            var _marginScroll;
            if (delta < 0) {
                _marginScroll = parseInt(_el.css('top')) - 20;
                _el.animate({ top: _marginScroll }, { queue: false, duration: 100, easing: 'linear', complete: function () {
                    _k = (_textHeight + parseInt(_el.css('top'))) / _textHeight;
                } 
                });
            } else {
                _marginScroll = parseInt(_el.css('top')) + 20;
                if (_marginScroll > 0) _marginScroll = 0;
                _el.animate({ top: _marginScroll }, { queue: false, duration: 100, easing: 'linear', complete: function () {
                    _k = (_textHeight + parseInt(_el.css('top'))) / _textHeight;
                } 
                });
            }
            return false;
        });
    });
}

