var WeFollow = {
    init: function() {
        WeFollow.bindButtons();
    },
    unBindButtons: function() {
        $('.a-info').unbind().click(function() {
            return false;
        });
    },
    bindButtons: function() {
        $('.a-info').click(function() {
            WeFollow.unBindButtons();
            $('.more-info:not(#info-' + this.id + ')').hide('fast');
            $('#info-' + this.id).toggle('fast', WeFollow.bindButtons);
            return false;
        });

        $('.autocomplete').click(function() {
            var id = $(this).siblings('div').attr('id');
            $('.autocomplete').each(function() {
                if ($(this).siblings('div').attr('id') != id) {
                    $(this).siblings('div').hide();
                    $(this).siblings('div').html('');
                }
            });
        });

        $('.autocomplete').keyup(function(e) {
            if (e.keyCode != 32 && e.keyCode != 8
                && (e.keyCode < 65 || e.keyCode > 90)) {
                return;
            }
            var value = this.value;
            if (value.length < 2) {
                return;
            }

            var date = new Date();
            var time = date.getSeconds().toString() + date.getMilliseconds().toString();
            WeFollow.lastAutoComplete = time;
            setTimeout('WeFollow.autoComplete(\'' + this.id + '\', \'' + value + '\', \'' + time + '\')', 150);
        });

        $('.autocomplete').keydown(function(e) {
            if (this.id != 'tag-1' && this.id != 'big-ass-search' && e.keyCode == 32) {
                return false;
            }

            switch (e.keyCode) {
                case 9:
                    var li = $(this).siblings('div').children('ul').children('li');
                    if (li.length == 0) {
                        return;
                    }

                    if (li.length == 1) {
                        this.value = li.children('a').html();
                        $(this).siblings('div').hide();
                        return;
                    }

                    WeFollow.autoCompleteScroll(this, true);
                    return false;
                    break;
                case 8:
                case 27: //listen for escape key - added by Jamie 8/16/09
                    $(this).siblings('div').hide();
                    break;
                case 40:
                    WeFollow.autoCompleteScroll(this, true);
                    return false;
                    break;
                case 38:
                    WeFollow.autoCompleteScroll(this, false);
                    return false;
                    break;
                case 13:
                    $(this).siblings('div').children('ul').children('li').each(function() {
                        if ($(this).hasClass('selected')) {
                            WeFollow.autoCompleteSelect($(this).children('a'));
                            return;
                        }
                    });

                    if (this.id == 'big-ass-search') {
                        WeFollow.autoCompleteSelect(this);
                    } else {
                        var el = $(this).parent('div').siblings('div').get(this.id.charAt(4) - 1);
                        $(el).children('input').focus();
                    }

                    $(this).siblings('div').hide();
                    return false;
                    break;
            }

        });

    },
    autoComplete: function(id, value, time) {
        if (time != WeFollow.lastAutoComplete) {
            return false;
        }

        var el = $('#' + id).siblings('div');
        if ($('#' + id).val().length < 1) {
            el.hide();
            el.html('');
            return;
        }

        if (value != $('#' + id).val()) {
            return;
        }

        var type = 0;
        if (id == 'tag-1') {
            type = 1;
        } else if (id == 'big-ass-search') {
            type = '';
        }

        var url = '/autocomplete.json?term=' + value + '&type=' + type;
        $.ajax({
            type: 'GET',
            url:  url,
            dataType: 'JSON',
            success: function(json) {
                json = eval(json);
                if (json.length < 1) {
                    el.hide();
                    return;
                }

                var ul = $('<ul>');
                $.each(json, function(i, value) {
                    var li = $('<li><a href="#" class=' + value.type + ' onclick="WeFollow.autoCompleteSelect(this); return false;">' + value.name + '</a> (' + value.users + ')</li>');
                    ul.append(li);
                });

                $(el).html('<ul>' + ul.html() + '</ul>');
                $(el).show();
            }
        });
    },
    autoCompleteSelect: function(el) {
        if (el != null && undefined != el.keyCode) {
            el = this;
        }

        if (el.id == 'big-ass-search') {
            window.location = '/twitter/' + el.value;
            return false;
        }

        var input = $(el).closest('div').siblings('input');
        input.val($(el).text());

        if (input.attr('id') == 'big-ass-search') {
            var type = $(el).attr('class');
            if (type == 1) {
                type = 'city';
            } else {
                type = 'twitter';
            }

            var location = '/' + type + '/' + input.val().replace(/[^A-Za-z0-9_]+/g, '_').toLowerCase();
            window.location = location;
        } else {
            $('.auto_complete').hide();
            $('.auto_complete').html('');
        }

        return false;
    },
    autoCompleteScroll: function(el, down) {
        var li = $(el).siblings('div').children('ul').children('li');
        if (li.hasClass('selected') == false) {
            li.eq(0).addClass('selected');
            return;
        }

        li.each(function() {
            if ($(this).hasClass('selected')) {
                $(this).removeClass('selected');
                if (down) {
                    $(this).next().addClass('selected');
                } else {
                    $(this).prev().addClass('selected');
                }

                return false;
            }
        });


    }
};

$(document).ready(function() {
    WeFollow.init();

    var welcomed = $.cookie('welcomed');
    if (welcomed == null || welcomed == 0) {
        $('#welcome').show();
        $('#welcome-window').show();
    }

    $('.close-window').click(function() {
        $('#welcome').hide();
        $('#welcome-window').hide();
        return false;
    });

    $('#welcome-window > a').click(function() {
        $.cookie('welcomed', 1, { expires: 999 });
    });

});

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
