blockTab = function() {
    $('div#bd-main-bd').block({ 
        centerY: 0,
        css: {
            top: '100px'
    }});    
}
unblockTab = function() {
    initPage();
    $('div#bd-main-bd').unblock();
}

initPage = function() {
    $(".tip").tipTip({delay: 0});
    
    $(".hide").hide();
    
    $(".rating").rating({
        focus: function(value, link) {
            var tip = $('#rating-text');
            tip[0].data = tip[0].data || tip.html();
            tip.html(link.title || 'value: '+value);
        },
        blur: function(value, link) {
            var tip = $('#rating-text');
            $('#rating-text').html(tip[0].data || '');
        }
    });
    $("#rating-submit").click(function() {
        var value = $("#rating-form input:checked.rating").val();
        if(typeof value == 'undefined') {
            alert('You need to select at least 1 Star!');
            return false;
        } else {
            return true;
        }
    });
    
    if(ps3heroes_context.hideComments == true) {
        $("li.comment[language!=" + ps3heroes_context.language + "]").each(function() {
            ps3heroes_context.commentsHidden++;
            $(this).hide();
        });
        if(ps3heroes_context.commentsHidden > 0) {
            $("p.hidden-comments-switch").show();
            $("p.hidden-comments-switch a").click(function() {
                $("li.comment[language!=" + ps3heroes_context.language + "]").toggle();
                return false;
            });
        }
    }
    
    // Whenever a vote is cast
	$('div.thumbsup_template_mini-thumbs input[name=vote]').click(function() {
		var box = $('#' + $(this).closest('div.thumbsup').attr('id'));
		var form = $('form', box);
		var balance = $('strong.votes_balance', form);
		$(':submit', form).attr('disabled', 'disabled').blur();
		form.addClass('closed');
		balance.text('···');
		var postdata = {
			news_id : $('input[name=news_id]', form).val(),
			vote: $(this).val()
		};
		$.post('/ajax/vote', postdata, function(vote) {
			if(vote.error) {
				alert(vote.error);
			} else {
			    balance.hide().text(((vote.balance > 0) ? '+' : '')+vote.balance).fadeIn('slow');
            }
		}, 'json');

		return false;
	});
	
	/* uploads
	$('.upload').each(function() {
	    var id = $(this).attr('id');
	    new AjaxUpload(id, {action: '/ajax/upload_trophy/id:' + id, onComplete: trophyUploadDone, responseType: 'json'});
	});
	*/
	
	// trophy-upload
	$('img.upload').click(function() {
	    var id = $(this).attr('id');
	    var parts = id.split('-');
	    id = parts[1];
	    $('div#upload-' + id).toggle('fast');
	});
	
	$('form.trophy-upload').submit(function() {
	    var trophyId = $('input[name=trophy_id]', this).val();
	    var trophyUrl = $('input[name=trophy_url]', this).val();
	    if (trophyUrl) {
	        $('img#trophy-' + trophyId).attr('src', '/img/spinner.gif');
	        $(this).parents('div.trophy-upload').hide('fast');
	        $.post('/ajax/upload_trophy/id:' + trophyId, {url: trophyUrl}, function(data) {
	            $('img#trophy-' + trophyId).attr('src', 'http://static.ps3heroes.com/trophy/' + data + '.png?r=' + Math.random());
	        });
        } else {
            alert('You need to specify an URL to update the trophy image');
        }
	    return false;
	});
	
	$('a.youtube').click(function() {
	    var rel = $(this).attr('rel');
	    $(this).replaceWith('<object width="400" height="246"><param name="movie" value="http://www.youtube.com/v/' + rel + '&hl=de&fs=1&rel=0&color1=0x3a3a3a&color2=0x999999"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + rel + '&hl=de&fs=1&rel=0&color1=0x3a3a3a&color2=0x999999" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="246"></embed></object>');
	    
	    return false;
	});
}

$(document).ready(function() 
{
    setTimeout('flashMessageUp()', 4000);
    
    $('a.toggle').click(function(e) 
    {
        if($(this).text() == '(-)') {
            $(this).text('(+)');
        } else {
            $(this).text('(-)');
        }
        $(this).siblings('ul').toggle();
        
        return false;
    });
    
	// Tabs
	$('li.ajaxtab').click(function() 
	{
		$('li.ajaxtab').removeClass('active');
		$(this).addClass('active');
		blockTab();
		var atag = $(this).find('a');
		var url = $(atag).attr('href') + '&ajax';
		$.get(url, {}, function(data) {
		    pageTracker._trackPageview(url);
		    $('div#bd-main-bd').html(data);
		    unblockTab();
		});
		
		return false;
	});
    $('a.ajax').live('click', function() 
    {
        var url = $(this).attr('href') + '&ajax';
        blockTab();
		$.get(url, {}, function(data) {
		    pageTracker._trackPageview(url);
		    $('div#bd-main-bd').html(data);
		    unblockTab();
		});
		
        return false;
    });
    
    // forums
    $('a.reply').click(function() 
    {
        var rel = $(this).attr('rel');
        $.get('/ajax/reply/' + rel, {}, function(data) {
            var textarea = $('#comment');
            textarea.val(data);
            var offset = textarea.offset();
            textarea.focus();
            window.scrollTo(0, offset.top);
        });
        
        return false;
    });
    
    $('input.sluggify').change(function() 
    {
        var rel = $(this).attr('rel');
        var toSlug = this.value.replace(/:/g, '-');
       
        $.get('/ajax/sluggify/' + toSlug, {}, function(data) {
            $('#'+rel).val(data);
        });
    });
    
    initPage();
});

function flashMessageUp() 
{
    $('div.flash_message').slideUp('fast');
}