$(function(){
    //fix png
    //$(document).pngFix();
    
    //hover the quick admin menu
    $('#admin').hover(
        function () {$(this).addClass('hover');}, 
        function () {$(this).removeClass('hover');}
    );
    
    //show-hide content links
    $('a.expander').live('click', function() {
        $(this).siblings('.expanding').toggleClass('hide');
        return false;
    });
    
    //infos switcher
    $('#infoswitch a').live('click', function() {
        $(this).parent().children().toggleClass('hide');
        $('#infocontent').children().toggleClass('hide');
        
        return false;
    });
    
    //search form
    $('#search input').focus(function() {
        if($(this).val()=='Поиск') {
            $(this).val('');
        }
    });
    $('#search input').blur(function() {
        if($(this).val()=='') {
            $(this).val('Поиск');
        }
    });
    $('#search img').click(function() {
        $(this).parents('form').submit();
    });
    
    //autocomplete
    $('.autocomplete').each(function() {
        str = 'Начните вводить название';
        $(this).val(str);
        $(this).focus(function() {
            if($(this).val()==str) {
                $(this).val('');
            }
        });
        
        url = $(this).attr('url');
        $(this).autocomplete("/" + url + "/autocomplete", {
            minChars: 1,
            cacheLength: 1,
            maxItemsToShow: 10,
            onItemSelect: selectItem,
            onFindValue: findValue,
            formatItem: formatItem,
            autoFill: false
        });
    });

    function selectItem(li) {
        findValue(li);
    }

    function findValue(li) {
        if( li == null ) return alert("No match!");
        
        // if coming from an AJAX call, let's use the product id as the value
        if( !!li.extra ) var sValue = li.extra[0];

        // otherwise, let's just display the value in the text box
        else var sValue = li.selectValue;

        //alert("The value you selected was: " + sValue);
    }

    function formatItem(row) {
        if(row[1] == undefined) {
            return row[0];
        }
        else {
            //return row[0] + " (id: " + row[1] + ")";
            return row[0];
        }
    }
    
    
    //datepicker
    /*
    $.datepicker.regional['ru'] = {
		closeText: 'Закрыть',
		prevText: '&#x3c;Пред',
		nextText: 'След&#x3e;',
		currentText: 'Сегодня',
		monthNames: ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'],
		monthNamesShort: ['Янв','Фев','Мар','Апр','Май','Июн', 'Июл','Авг','Сен','Окт','Ноя','Дек'],
		dayNames: ['воскресенье','понедельник','вторник','среда','четверг','пятница','суббота'],
		dayNamesShort: ['вск','пнд','втр','срд','чтв','птн','сбт'],
		dayNamesMin: ['Вс','Пн','Вт','Ср','Чт','Пт','Сб'],
		dateFormat: 'yyyy-mm-dd', firstDay: 1,
		isRTL: false};
	$.datepicker.setDefaults($.datepicker.regional['ru']);
    
    $('#datepicker').datepicker();
    //*/
    
    function setEqualHeight(columns) {
        var tallestcolumn = 0;
        columns.each(function() {
            currentHeight = $(this).height();
            if(currentHeight > tallestcolumn) {
                tallestcolumn  = currentHeight;
            }
        });
        columns.height(tallestcolumn);
    }
    
    setEqualHeight($(".cols  > div"));
    
    //color tables
    $('table').each(function() {$(this).find('tr:first').addClass('first');});
    $('table').each(function() {$(this).find('tr:even').addClass('even');});
    $('table').each(function() {$(this).find('tr:odd').addClass('odd');});
    $('tr').each(function() {$(this).find('td:first').addClass('first');});
    
    $('textarea.editor').ckeditor();
});
