function objectIsEmpty(obj) {
    // check that object has no properties
    for(var prop in obj) return false;
    return true;
}

function showPasswordValue(obj) {
    if (obj.hasClass('password')) {
        var new_obj = obj.clone(true);
        new_obj.type = 'text'; // IE can't change field type via attr() method
        new_obj.val(obj.val()); // IE don't clone value, needs manual set
        obj.replaceWith(new_obj);
    }
}

function hidePasswordValue(obj) {
    if (obj.hasClass('password')) {
        var new_obj = obj.clone(true);
        new_obj.type = 'password'; // IE can't change field type via attr() method
        new_obj.val(obj.val()); // IE don't clone value, needs manual set
        obj.replaceWith(new_obj);
        new_obj.focus();
    }
}


$(function() {
    $('input[placeholder]')
       .focus(function() {
            placeholder = this.getAttribute('placeholder');
        	if(this.value == placeholder) {
        		this.value = '';
        		$(this).removeClass('placeholder_text');
        	    hidePasswordValue($(this));
        }})
    	.blur(function() {
        	if(this.value == '') {
        		this.value = this.getAttribute('placeholder');
        		$(this).addClass('placeholder_text');
        		showPasswordValue($(this));
        	}
        });

    $('input[placeholder]').each(function(i) {
        if (this.value == '') {
            this.value = this.getAttribute('placeholder');
            $(this).addClass('placeholder_text');
            showPasswordValue($(this));
        }
    });
 
    $('form').submit(function() {
        var $this = $(this);

        inputs = $this.find('input');
        $.each(inputs, function(i, object) {
            if (object.getAttribute('placeholder') == object.value) {
                this.value = '';
            }
        });
    });
    
    $('.add_to_friends').click(function(){
       $.post(this.href, { user: this.getAttribute('rel', 2) }, function(response){
           if (response == 'OK') {
               window.location.reload(false);
           } else {
               $.jGrowl('You cannot add this user to your friends.');
           }
       });
       return false;
    });
    
    $('#options').children('li').click(function(){
        var $this = $(this);
        var category_name = $this.attr('id');
        $('#options').children('li').removeClass('hidden');
        $('#id_category').val(category_name);
        $("#search_choices").find("input").attr('checked', category_name == 'all' ? true : false);
        $('#id_'+category_name).attr('checked', true);
        $this.addClass('hidden');
        $('#selected_item').text($this.html());
        $('#options').css('display', 'none');
    });


    $('#search_submit').click(function(){
        $('#search_form').submit();
        return false;
    })

    function formatResult(li) {
    	return '<div style="float: left">'+li[0]+' </div>'+'<div style="float:left;">'+li[1]+'</div>'+'<div style="float: right">'+li[2]+'</div>';
    }

	$("#query")
		.autocomplete("/search/autocomplete/", {
				delay: 10,
				minChars: 2,
				matchContains: 1,
				cacheLength: 0,
				formatItem: formatResult,
                selectFirst: false,
				extraParams: { 'category': function(){return $("#id_category").val()}}
		})
		.result(function(event, item) {
			location.href = item[3];
		});

	$("#search_selected").hover(
	    function(){
	        $(this).addClass('hover');
	        $('.options').css('display', 'block');
	    },
	    function(){
    	    $(this).removeClass('hover');
    	    $('.options').css('display', 'none');
        }
    );

    // temp fix
    if (!($('.thebox').children().length)) {
        $('.col-adds').hide();
    }
});
