lastComment = null;

function fixHover(className) {
	$('.' + className).hover(
        function() {
            $(this).addClass(className + 'Hover');
        },
        function() {
            $(this).removeClass(className + 'Hover');
        }
    );
       
}

jQuery(function($) {
    if($.browser.msie && $.browser.version < 7) {
    	$('.registrationpage input').hover(
            function() {
                $(this).addClass('ie6hover');
            },
            function() {
                $(this).removeClass('ie6hover');
            }
        );
        fixHover('thumbnail');
        fixHover('badge');
    	
    }
    /*
    $('#badgeButton').mouseover(
        function() {
            $('#badgeSelector').show();
        }
    );
    */
    $('#badgeButtonImg').click(
        function() {
            $('#badgeSelector').toggle();
        }
    );
    
    $('.thumblist li').hover(
        function(event){$(this).css('zIndex','2')},
        function(event){$(this).css('zIndex','1')}
    );
    
    $('li.userBadge').click(function(event){
    
        var img = $(event.target);
        var details = img.parents('.badgeSelector').children('.details');
        
        while (img.children().length > 0) img = img.children().eq(0)
        
        var badge = img.attr('src');
        
        details.children('img.largeBadge').attr('src',badge)
                                          .attr('width',img.attr('width') * 2)
                                          .attr('title',img.attr('title'));
        badge = badge.substring(badge.lastIndexOf('/')+1, badge.length-4);
        
        details.find('#badgeId').val(badge);
        details.find('#badgeText').val('');
        
        details.children('.badgeLabelList').load('/badge_list/'+badge+'/',null,function (responseText, textStatus, XMLHttpRequest) {
            $(this).find('li .badgeText').click(function(event){
                var details = $(event.target).parents('.details');
                details.find('#badgeText').val(event.target.innerHTML);
            });
        });
        details.show();
    });
    $('.editable').click(function(event){
    
        $(this).children('.editor').show();
        $(this).children('.content').hide();
        
    });
    $('.editable .cancel').click(function(event){
    
        $(this).parents('.editable').children('.content').show();
        $(this).parents('.editable').children('.editor').hide();

        event.stopPropagation();
        event.preventDefault();
        return false;       
    });
    $('button').each(function(i){
        var el = $(this);
        if (el.height() > 12) {
            $('body').addClass('ffix');
        }
    });
    
    $('.thumbnail .text').each(function(i){
        var el = $(this);
        el.css('top',((90-el.height())/2) + 'px');
        //alert(((100-el.height())/2));
    });
    
    function clickhandler(event){
        this.value="";
        //$(this).removeClass('label');   // safari bug
        this.style.color="#000000"; 
        $(this).unbind('click', clickhandler);
    }
    
    $('input.label, textarea.label').click(clickhandler);
    
    /*
    $('#badgeButton').click(function(event){
        //$('#commentForm').hide();
        $('#badgeSelector').toggle();
    });
    $('#commentButton').click(function(event){
        //$('#badgeSelector').hide();
        $('#commentForm').toggle();
    });
    */
    setInterval(function(){
    	if (lastComment) {
    		lastComment.fadeOut(2000);
    		lastComment = lastComment.next();
    		if (lastComment.length == 0) lastComment = $('#comments').children().eq(0);
    	}
    	else {
    		lastComment = $('#comments').children().eq(0);
    	}
    	setTimeout(function(){lastComment.fadeIn(2000)}, 2000);
    }, 8000);
    
});