$(function(){
	$('.png').ifixpng(); 
	$.lastGame = '';

	$('a.reply').click(function(){
		window.open(this.href, "bloggerPopup", "toolbar=0,location=0,statusbar=1,menubar=0,scrollbars=yes,width=400,height=450");
		return false;
	});

	$.opacityInactive = 0.8;
	$('.game-banner,#character-list img').css('opacity', $.opacityInactive).attr('isOpen', 'false');

	/* Highlight on hover */
	$('.game-banner,#character-list img').hover(function(){
		$(this).css('opacity', '1');
	}, function() {
		/* Do not disable if the item is open */
		if ($(this).attr('isOpen') == 'false') {
			$(this).css('opacity', $.opacityInactive);
		}
	});

	/* Assign open/close functions to all game banners */
	$('.game-banner').click(function(){
		/* Create a jquery id for the clicked game */	
		var id = $(this).attr('id');
		$.openGame = '#' + id + '-details';

		/* If lastGame is empty. just slide open the game */
		if ($.lastGame == '') {
			$($.openGame).slideDown();
			$(this).attr('isOpen', 'true');
		}
		/* Else, close the open game */
		else {
			$($.lastGame).slideUp();
			/* Disable the highlighting */
			var gameBanner = $($.lastGame.replace(/-details$/, ''));
			gameBanner.css('opacity', $.opacityInactive).attr('isOpen', 'false');

			/* If the closed game is not the clicked game, open that one */
			if ($.openGame != $.lastGame) {
				$($.openGame).slideDown();
				$(this).attr('isOpen', 'true');
			} else {
				$._id = '';
			}
		}

		/* Set lastGame to the game we just opened and show the japanese title, or remove both */
		if ($.lastGame == $.openGame) {
			$.lastGame = '';
			removeJapTitle();
		} else {
			if ($(this).attr('ref') != 'no-title') {
				showJapTitle($('#' + id));
			}
			$.lastGame = $.openGame;
		}
	});
	
	$.preload = [];
	
	/* Preload all japanese game title images */
	$('.game-banner').each(function() {
		var id = $(this).attr('id');
		var imgName = '/img/sidebar-title-' + id.replace(/^game-/,'') + '.png';
		$.preload[id] = new Image();
		$.preload[id].src = imgName;
	});
	
	var dPopShown = false;
	var dTimer = null;
	var dImg = new Image;
	dImg.src = '/img/download-popup.png';
	
	/* Download popups */
	$('div.details a[ref]').hover(function() {
		if (dTimer) clearTimeout(dTimer);
		if (dPopShown) return;
		
		dPopShown = true;
		$('#downloadPopup').remove();
		$$ = $(this);
		$$.attr('title', '');
		
		var pos   = $$.position();
		var data  = $$.attr('ref').split(';');
		var dPerc = data[0];
		var dSize = data[1];
		var dDate = data[2];
		
		$('<div id="downloadPopup" style="left:' + pos.left + 'px;top:' + (pos.top - 56) + 'px;display:block;"><strong>Size:</strong> ' + dSize + '<br /><strong>Date:</strong> ' + dDate + '<br /><strong>Status:</strong> ' + dPerc + '%</div>')
			.css('opacity', 0)
			.insertAfter($(this))
			.ifixpng()
			.animate({
				top: '-=15px',
				opacity: 1
			}, 333, 'swing');
	}, function() {
		if (dTimer) clearTimeout(dTimer);
		dTimer = setTimeout(function () {
			dTimer = null;
			$('#downloadPopup').animate({
				top: '-=10px',
				opacity: 0
			}, 333, 'swing', function() { $(this).remove(); dPopShown = false; });
		}, 500);
	});
	
	/* DLsite form */
	$('#dlsiteForm').find('input[type="text"]').click(function(){ $(this).val('').unbind('click'); });
	$('#dlsiteForm').find('input[name="showlink"]').click(function(){
		$('#dlsiteLink').remove();
		$('<p id="dlsiteLink" style="margin-top:4px;display:none;"></p>').insertAfter($(this)).load('/xhr/dlsite?href=' + encodeURIComponent($('#dlsiteForm').find('input[type="text"]').val()).replace(/%20/g, '+'), function() { $(this).slideDown('slow'); });
		return false;
	});
});

/* Show the vertical japanese title */
function showJapTitle(jqObj) {
	removeJapTitle();

	/* Determine sidebar coordinates */
	var sidebarPos = $('#sidebar').position()

	/* Create an image string from the given objects details */
	var imgName = $.preload[jqObj.attr('id')].src;

	/* Create a new positioned div and add it to the container. */
	var leftPos = parseInt(sidebarPos.left) - 36;
	var topPos = sidebarPos.top + 52;

	$('<div id="game-title" style="display:none;z-index:10;position:absolute; top: ' + topPos + 'px; left: ' + leftPos + 'px;"><img id="' + imgName + '" src="' + imgName + '" alt="" /></div>').appendTo($('#container')).fadeIn();
	$('#' + imgName).ifixpng();	/* Unique id for IE6 fix */
}

/* Remove the vertical japanese title */
function removeJapTitle() {
	$('#game-title').fadeOut(function(){ $(this).remove() });
}

/* Load the poll results in the poll container */
function pollResults() {
	$('#poll').load('/xhr/pollResults');
	return false;
}

/* Load the poll options in the poll container */
function pollOptions() {
	$('#poll').load('/xhr/pollOptions');
	return false;
}

/* Vote in the poll */
function pollVote(poll) {
	/* Detect the vote. Raise an alert if none is selected */
	$.poll = poll;
	$.vote = 0;
	
	/* Find the option the user selected */
	$('#poll input[type="radio"]').each(function() {
		if ($(this).attr('checked') == true) {
			$.vote = $(this).val();
		}
	});
	
	/* If no option selected, show an alert */
	if ($.vote == 0) {
		alert('You did not select an option.');
	} else {
		/* Slide up the poll, post the checked option and show the results. */
		$('#poll').slideUp('slow', function() {
			$.post('/xhr/pollVote', {poll: $.poll, vote: $.vote}, function(data) {
				$('#poll').html(data).slideDown('slow');
			});
		});
	}
}