﻿function SetFont(txtContId, fontSize) {
    if (fontSize.length > 1) {
        $('#' + txtContId).css('font-size', '13px');
    }
    else {
        var newFSize = parseInt($('#' + txtContId).css('font-size'));
        if (fontSize == "-")
            newFSize = newFSize - 1;
        else
            newFSize = newFSize + 1;
        $('#' + txtContId).css('font-size', newFSize + 'px');
    }
}
function GetSm(handlerUrl, pCatId, catId) {
    $.get( handlerUrl, { pCatId: pCatId, catId: catId }, onSmGetSucc );
    function onSmGetSucc(smHtml) {
        $('.tSm').html($(smHtml).html());
        if ($(smHtml).html().indexOf('href') > 0)
            $('.tSm').css('background-color', '#D3D3D3');
        else
            $('.tSm').css('background-color', '#FFF');
    }
    return false;
}
function ReturnSmOff() {
    var sm = $('.tSm').get(0);
    GetSm(sm.getAttribute('u'), sm.getAttribute('pc'), sm.getAttribute('cc'));

    $('#header').unbind('mouseover');
    $('#content').unbind('mouseover');
    $('#top_menu').bind('mouseover', function() { ReturnSmOn(); });
}
function ReturnSmOn() {
    $('#header').bind('mouseover', function() { ReturnSmOff(); });
    $('#content').bind('mouseover', function() { ReturnSmOff(); });
    $('#top_menu').unbind('mouseover');
}


// Admin
function SetUplPathValue(uploaderValue, pathShowerElementId) {
    document.getElementById(pathShowerElementId).value = uploaderValue;
}


// custom AutoComplete
var currItemIndex = 0;
var contBoxId = 0;
function GetAutoCompleteList(evt, sText, dsTextField, dsValueField, itemsCount, sPrText, dsAssemblyName, dsTypeName, dsMethodName, contBoxIdP, handlerUrl, textLength) {
    contBoxIdP = $('#' + contBoxIdP).parent().attr('id');
    if (sText.length < textLength) {
        $('#' + contBoxIdP + ' ul').remove();
        return;
    }
    contBoxId = contBoxIdP;
    evt = (evt) ? evt : event;
    var keyCode = parseInt(evt.keyCode);
    if (keyCode == 40 || keyCode == 38) {
        var lis = $('#' + contBoxId + ' ul li');
        if (lis.length > 0) {
            var currItInd = parseInt($('#' + contBoxId).attr('currItInd'));
            if (keyCode == 38) //up 
                currItemIndex = currItInd - 1;
            else if (keyCode == 40) // down
                currItemIndex = currItInd + 1;

            if (currItemIndex + 1 > lis.length)
                currItemIndex = lis.length - 1;
            if (currItemIndex < 0)
                currItemIndex = 0;

            lis.each(Colorize);
            $('#' + contBoxId + ' ul li').css('height', '17px');
            $('#' + contBoxId + ' ul li').css('overflow', 'hidden');
            $('#' + contBoxId).attr('currItInd', currItemIndex);
        }
    }
    else {
        $.ajax({
            url: handlerUrl,
            data: { sText1: sText, dsTextField1: dsTextField, dsValueField1: dsValueField, itemsCount1: itemsCount, sPrText1: sPrText, dsAssemblyName1: dsAssemblyName, dsTypeName1: dsTypeName, dsMethodName1: dsMethodName },
            type: 'GET',
            timeout: 3000,
            error: function() { alert('Ошибка'); },
            success: function(auData) {
                $('#' + contBoxId + ' ul').remove();
                $('#' + contBoxId).append($(auData).html());
                $('#' + contBoxId).attr('currItInd', '-1');

                var width = parseInt($('#' + contBoxId + ' input[@type="text"]').width()) + 3;
                var top = parseInt($('#' + contBoxId).offset().top) + 22;
                var left = parseInt($('#' + contBoxId + ' input[@type="text"]').offset().left);
                if ($.browser.mozilla)
                    left -= 60;
                if ($.browser.safari)
                    left += 60;

                $('#' + contBoxId + ' ul').css('width', width + 'px');
                $('#' + contBoxId + ' ul').css('top', top + 'px');
                $('#' + contBoxId + ' ul').css('left', left + 'px');
                $('#' + contBoxId + ' ul li').css('height', '17px');
                $('#' + contBoxId + ' ul li').css('overflow', 'hidden');
                $('#' + contBoxId + ' ul li').bind('mousedown', function() { SetValueOnClick($(this).attr('itId'), $(this).html(), contBoxId); });
            }
        });
        return false;
    }
}
function Colorize(index) {
    if (index == currItemIndex) {
        $(this).attr('style', 'color:#FFF;background-color:#39F;');
        $('#' + contBoxId + ' input[@type="text"]').val($(this).text());
        $('input[@type="hidden"]', $('#' + contBoxId)).val($(this).attr('itId'));
    }
    else
        $(this).removeAttr('style');
}
function SetValue(id, value) { $('input[@type="hidden"]', $('#' + id).parent()).val(value); }
function SetValueOnClick(id, text, contBoxId) { $('#' + contBoxId + ' input[@type="text"]').val(text); $('input[@type="hidden"]', $('#' + contBoxId)).val(id); }
function HideUl() { $('#' + contBoxId + ' ul').remove(); }

