$( function() {

	var fullscreen = false;
	var originalWidth = $("#game-object").attr("width");
	var originalHeight = $("#game-object").attr("height");
	
	var originalRateFormClass = $("#game-controlbar-rate").attr("class");
	
	$("#game-controlbar-fsinfo>a").colorbox( {
		iframe: true,
		innerWidth: 600,
		innerHeight: 360
	} );
	
	$("#game-controlbar-rate")
		.delegate( "button", "mouseover", function( e ) {
			$("#game-controlbar-rate").attr( "class", "rated" + $(e.target).val() );
		} )
		.delegate( "button", "mouseout", function( e ) {
			$("#game-controlbar-rate").attr( "class", originalRateFormClass );
		} );
	
	$("#game-controlbar-rate").submit( function( e ) {
		e.preventDefault();
	} );
	
	$("#game-controlbar-rate").delegate( "button", "click", function( e ) {
		var $form = $("#game-controlbar-rate");
		if ( Plastic.User === null ) {
			alert( "Please log in to rate games." );
		} else {
			var $button = $(e.target);
			var rating = $button.val();
			var gameId = $("input[name=game_id]",$form).val();
			$form.busy();
			$.ajax( {
				url: "/api/",
				type: "POST",
				dataType: "json",
				data: {
					method: "rateGame", 
					game_id: gameId,
					rating: rating
				},
				success: function( response ) {
					if ( response.successful ) {
						var newClass = "rated" + rating;
						originalRateFormClass = newClass;
						$form.attr( "class", newClass );
						$("#game-controlbar-rate-average").text( response.data.toFixed(1) );
					} else {
						// TODO
					}
				},
				error: function( xhr, textStatus, errorThrown ) {
					// TODO
				},
				complete: function() {
					$form.unbusy();				
				}
			} );
		}
	} );
	
	$("#game-controlbar-share>a").colorbox( {
		iframe: true,
		innerWidth: 600,
		innerHeight: 360,
		title: " "
	} );
	
	$("#game-controlbar-fullscreen>div").click( function( e ) {
		if ( !fullscreen ) {
			e.preventDefault();
			fullscreen = true;
			$("body").addClass("fullscreen");
			$("#game-controlbar").css("width","100%");
			fitToWindow();
		} else {
			fullscreen = false;
			$("#game-wrapper")
				.css( "padding-top", "0" )
				.css( "width", originalWidth + "px" )
				.css( "height", originalHeight + "px" );
			$("#game-object").attr("width",originalWidth).attr("height",originalHeight);
			$("#game-object>object").attr("width",originalWidth).attr("height",originalHeight);
			$("#game-controlbar").css( "width", ( originalWidth > 320 ? originalWidth : 320 ) + "px" );
			$("body").removeClass("fullscreen");
		}
	} );

	$(window).resize( function() {
		if ( fullscreen ) {
			fitToWindow();
		}
	} );
	
	$("#game-controlbar-favorite").submit( function( e ) {
		var $form = $(this);
		e.preventDefault();
		if ( Plastic.User === null ) {
			alert( "Please log in to add favorites." );
		} else {
			$form.busy();
			var $fn = $("input[name=fn]",$form);
			var gameId = $("input[name=game_id]",$form).val();
			switch ( $fn.val() ) {
				case "add":
					$.ajax( {
						url: "/api/",
						type: "POST",
						dataType: "json",
						data: {
							method: "addFavorite", 
							game_id: gameId					
						},
						success: function( response ) {
							if ( response.successful ) {
								$form.addClass("favorited");
								$("button",$form).attr("title","Remove from Favorites").text("Remove from Favorites");
								$fn.val("remove");
							} else {
								// TODO
							}
						},
						error: function( xhr, textStatus, errorThrown ) {
							// TODO
						},
						complete: function() {
							$form.unbusy();				
						}
					} );
					break;
				case "remove":
					$.ajax( {
						url: "/api/",
						type: "POST",
						dataType: "json",
						data: {
							method: "deleteFavorite", 
							game_id: gameId					
						},
						success: function( response ) {
							if ( response.successful ) {
								$form.removeClass("favorited");
								$("button",$form).attr("title","Add to Favorites").text("Add to Favorites");
								$fn.val("add");
							} else {
								// TODO
							}
						},
						error: function( xhr, textStatus, errorThrown ) {
							// TODO
						},
						complete: function() {
							$form.unbusy();				
						}
					} );
					break;
			}
			
		}
	} );

	function fitToWindow() {
		var newWidth = null;
		var newHeight = null;
		var availableWidth = $(window).width();
		var availableHeight = $(window).height() - 24;
		var hRatio = availableWidth / originalWidth;
		var hHeight = Math.floor( hRatio * originalHeight );
		if ( hHeight <= availableHeight ) {
			newWidth = availableWidth;
			newHeight = hHeight; 
		} else {
			var vRatio = availableHeight / originalHeight;
			newWidth = Math.floor( vRatio * originalWidth );
			newHeight = availableHeight;
		}
		var topPadding = Math.floor( ( availableHeight - newHeight ) / 2 ); 
		$("#game-wrapper")
			.css( "padding-top", topPadding + "px" )
			.css( "width", newWidth + "px" )
			.css( "height", ( availableHeight - topPadding ) + "px" );
		$("#game-object").attr("width",newWidth).attr("height",newHeight);
		$("#game-object>object").attr("width",newWidth).attr("height",newHeight);
	}
	
} );
