var TagSuggestions = {
    nTimer: null,
    strInputName: '',

    init: function()
    {
        // alert('TagSuggestions.init'); 
        $('.addtag').mouseover(function(){ $(this).css({'background-position':'0 -23px'}); });
        $('.addtag').mouseout(function(){ $(this).css({'background-position':'0 0'}); });    
        $('.addtag').mousedown(function(){ $(this).css({'background-position':'0 -46px'}); });
        $('.addtag').click(function(){ 
            $(this).css({'background-position':'0 -23px'});
            var strNewTag = $('input#TagForm').val();
            strNewTag = strNewTag.trim();
            if(strNewTag != '')
            {
                $('textarea#Tags').val($('textarea#Tags').val() + strNewTag + '; ');  
                $('input#TagForm').val('');  
            }
        });    
    
        $('input#TagForm').keyup(function(e) { if(e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 27 && e.keyCode != 13) { TagSuggestions.strInputName = 'input#TagForm'; TagSuggestions.GetItemsTimer($(this).val()) } });
    },
    
    initSearch: function()
    {     
        $('input#Tag').click(function(){
            var strTagStart = $('input#Tag').val();
            
            if(strTagStart.match(/Търсене/gi)) 
            { 
                $('input#Tag').val(''); 
            }
        })
        
        $('input#Tag').keyup(function(e) {if(e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 27 && e.keyCode != 13) { TagSuggestions.strInputName = 'input#Tag';TagSuggestions.GetItemsTimer($(this).val())}});
    },
    
    GetItemsTimer: function(strKey)
    {
        if(strKey.length < 3)
        {
            if(TagSuggestions.nTimer)
            {
                window.clearTimeout(TagSuggestions.nTimer);
            }
            TagSuggestions.RemoveList();
        }
        else
        {
            
            if(TagSuggestions.nTimer)
            {
                window.clearTimeout(TagSuggestions.nTimer);
            }
            TagSuggestions.nTimer = window.setTimeout(function() { TagSuggestions.GetItems(strKey); }, 500);  
        }    
    },
    
    GetItems: function(strKey) 
    {      
        // alert(strRootPath+'tags/'+Url.encode(strKey)+'.html');
        
        $.ajax({
            cache: false,
            url: strRootPath+'tags/'+Url.encode(strKey)+'.html',
            success: function(data) {  
                TagSuggestions.RemoveList();
                if(data != '')
                {     
                    //alert(data); 
                    $('<div class="suggestions">'+data+'</div>').insertAfter($(TagSuggestions.strInputName).next());
                    $('.suggestions a').mousedown(function(e) { $(TagSuggestions.strInputName).val($(this).html()); TagSuggestions.RemoveList(); e.preventDefault(); });
                    
                    $(document).bind('keydown', TagSuggestions.KeyDown); 
                }
            }
        });   
    },
    
    KeyDown: function(event)
    {
        // alert(event.keyCode); 
        switch(event.keyCode) 
        {
            // User pressed "up" arrow
            case 38:
                TagSuggestions.SetSecected(-1);    
            break;
            
            // User pressed "down" arrow
            case 40:
                TagSuggestions.SetSecected(1);
            break;
            
            // User pressed esc
            case 27:
            case 13:
                TagSuggestions.RemoveList();
                event.preventDefault();
            break; 
            
        }     
    },
    
    SetComboValue: function(pValue)
    {        
        $(TagSuggestions.strInputName).val($(pValue).html());
        TagSuggestions.RemoveList();    
    },

    RemoveList: function()
    {
        $('.suggestions').remove();
        $(document).unbind('keydown', TagSuggestions.KeyDown);
    },
    
    SetSecected: function(nDirection)
    {
        if($('.suggestions a.current').length)
        {
            var current = $('.suggestions a.current');
            if(nDirection == 1)
            {
                var next = (current.next().length) ? current.next() : $('.suggestions a:first');
            }
            else
            {
                var next = (current.prev().length) ? current.prev() : $('.suggestions a:last');
            }
            current.removeClass('current');
            next.addClass('current'); 
            $(TagSuggestions.strInputName).val(next.html());             
        }
        else
        {
            if(nDirection == 1)
            {
                $('.suggestions a:first').addClass('current');
                $(TagSuggestions.strInputName).val($('.suggestions a:first').html());    
            }
            else
            {
                $('.suggestions a:last').addClass('current');
                $(TagSuggestions.strInputName).val($('.suggestions a:last').html());  
            }
        }
    }   
};

$(function(){
   TagSuggestions.initSearch(); 
});
