/**
 * Определение версии IE.
 */
function is_ie_5_or_6()
{
    if (!jQuery.browser.msie)
    {
        return false;
    }

    var ie8 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 8.0") != -1);
    if (jQuery.browser.msie && ie8)
    {
        return false;
    }

    var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
    if (jQuery.browser.msie && ie55)
    {
        return true;
    }


    var is_6 = jQuery.browser.msie6 =
    $.browser.msie
    && /MSIE 6\.0/i.test(window.navigator.userAgent)
    && !/MSIE 7\.0/i.test(window.navigator.userAgent);

    return is_6;
}

var need_fix_png = is_ie_5_or_6();

function show_rom_layer()
{
    // Скрываем слой с остальным софтом.
    $('#other_soft_layer').hide('slide', { 
        direction: "left"
    }, 500, other_soft_hide_callback);
}

function show_other_soft_layer()
{
    // Скрываем слой.
    $('#rom_layer').hide('slide', { 
        direction: "right"
    }, 500, rom_hide_callback);
}

function rom_hide_callback()
{
    // Отображаем слой.
    $('#other_soft_layer').show('slide', { 
        direction: "left"
    }, 500);

    // Переназначаем событие.
    $('#slide_page').unbind('click', show_other_soft_layer);
    $('#slide_page').bind('click', show_rom_layer);

    // Меняем картинку.
    $('#slide_page_container').css('backgroundImage', "url('/images/l_arrow.png')");

    // Правим png (для IE 6).
    if (need_fix_png)
    {
        $('#slide_page').pngFix();
    }
}

function other_soft_hide_callback()
{
    // Отображаем слой c ROM.
    $('#rom_layer').show('slide', { 
        direction: "right"
    }, 500);

    // Переназначаем событие.
    $('#slide_page').unbind('click', show_rom_layer);
    $('#slide_page').bind('click', show_other_soft_layer);

    // Меняем картинку.
    $('#slide_page_container').css('backgroundImage', "url('/images/r_arrow.png')");

    // Правим png (для IE 6).
    if (need_fix_png)
    {
        $('#slide_page').pngFix();
    }
}

// Показ закладок.
function ShowLayer(AItemId, ASpan, AParentId)
{

    var FormElement = null;

    // Получаем элемент родительского контейнера, если указан.

    if (AParentId != '')
    {
        FormElement = document.getElementById(AParentId);
    }
    else
    {
        FormElement = document;
    }

    if (FormElement)
    {
        // Скрываем все tab-page'ы.
        element_array = FormElement.getElementsByTagName("div");

        if (element_array.length > 0)
        {
            for (var i = 0; i < element_array.length; i++)
            {
                if (element_array[i].getAttribute('id') == undefined)
                {
                    continue;
                }

                if (element_array[i].getAttribute('id').search("tab_page_") != -1)
                {
                    // Нельзя display: none; из-за визуального редактора
                    element_array[i].style.display = "none";
                }
            }
        }
    }

    var el = document.getElementById(AItemId);

    if (!el)
    {
        return false;
    }

    // Отображаем выбранный tab-page.
    el.style.display = "block";


    element_array = document.getElementsByTagName("li");

    // Если есть хотя бы один li
    if (element_array.length > 0)
    {
        for (var i = 0; i < element_array.length; i++)
        {
            if (element_array[i].getAttribute('id') == undefined)
            {
                continue;
            }

            // Неактивный
            if (element_array[i].getAttribute('id').search("id_tab_span_") != -1)
            {
                element_array[i].className = "";
            }

            // Активный
            if (element_array[i].getAttribute('id') == ASpan)
            {
                element_array[i].className = "current_li";
            }
        }

    }

    return true;
}

function process_buy()
{
    var elem = document.getElementById('license_count');
    var count = parseInt(elem.value);
    if (isNaN(count) || count <= 0)
    {
        elem.value = 10;
        return false;
    }

    location = 'http://www.softkey.ru/catalog/basket.php?prodid=403367&from=2120339&quantity=' + count;
    return true;
}

function show_hide_faq(AItemId)
{
    var item = $('#' + AItemId);
    var span = $('#' + AItemId + '_span');

    if (item.is(':visible'))
    {
        item.hide('slide', {
            direction: "up"
        }, 500);

        span.css('font-weight', 'normal');
    }
    else
    {
        item.show('slide', {
            direction: "up"
        }, 500);

        span.css('font-weight', 'bold');
    }
}

function show_dotted_span(AItemId)
{
    var span = $('#' + AItemId + '_span');
    var div = $('#' + AItemId + '_div');

    if (!span || !div)
    {
        return;
    }

    if (div.is(':visible'))
    {
        return;
    }

    span.css('border', 'none');
    span.css('cursor', 'auto');
    div.fadeIn("slow");
}

function hide_dotted_span(AItemId)
{
    var span = $('#' + AItemId + '_span');
    var div = $('#' + AItemId + '_div');

    if (!span || !div)
    {
        return;
    }

    if (!div.is(':visible'))
    {
        return;
    }

    div.fadeOut("slow");

    span.css('border-bottom', '1px dashed black');
    span.css('cursor', 'pointer');
}

function slide_down_feature(AItemId)
{
    var item = $('#' + AItemId);

    if (!item)
    {
        return;
    }

    if (!item.is(':visible'))
    {
        // Отображаем.
        item.slideDown(400);
    }
}

function slide_up_feature(AItemId)
{
    var item = $('#' + AItemId);

    if (!item)
    {
        return;
    }

    if (item.is(':visible'))
    {
        // Скрываем.
        item.slideUp(400);
    }
}

var opened_one = null;
var is_hover = false;

function hide_all_panels()
{
    if (!is_hover)
    {
        $('div[id$="panel"]').hide();

        $('div[id$="panel"]').each(
            function (n, element) {
                clearTimeout($(element).data('timeout'));
            }
        );
    }
}

function build_main_menu()
{
    // hide all panels first : bad need to do something about this
    $('div[id$="panel"]').hide();

    // Выход за пределы всего меню.
    $('div#main_menu').hover(
        function(){},
        function(){
            window.setTimeout(
                "hide_all_panels()",
                2000
                );
        });


    // show the panel based on rel attribute of the current hovered link
    $('.main_menu_item').hover(
        function(){
            // hide previously opened panel
            $('div[id$="panel"]').hide();

            is_hover = true;


            var link_rel = $(this).attr('id') + '_panel';

            //get the position of the hovered element
            var pos = $(this).offset();

            // get the panel near by hovered element now
            $('div#' + link_rel).css({
                'left': pos.left + 'px',
                'top': (pos.top + $(this).height() + 6) + 'px',
                'zIndex': '5000'
            });

            $('div#' + link_rel).data('timeout',
                setTimeout(function () {
                    // Отображаем с задержкой.
                    $('div#' + link_rel).fadeIn('slow');
                }, 300));


            // this is currently opened
            opened_one = $('div#' + link_rel);

            // hide it back when mouse arrow moves away
            $('div#' + link_rel).hover(function(){
                is_hover = true;
            }, function(){
                is_hover = false;
                clearTimeout($(this).data('timeout'));
                $(this).fadeOut('slow');
            });
        },
        function(){
            var link_rel = $(this).attr('id') + '_panel';
            clearTimeout($('div#' + link_rel).data('timeout'));
            is_hover = false;
        });
}
