/**
 * Controls the page interactivity for Game Profiles
 * 
 * @author Karl Stanton (Fi)
 * @author Filip Michalowski (Fi)
 * @version 1.0
 */

var platformSelection = 'div.gameDetails ul.selector';

// remove noJS CSS class
$(platformSelection).removeClass("noJS");

// Do the radio inputs
$('input[type=radio]', platformSelection).each(function (i, el) {

	// Only create the span if it hasn't be made previously
	var input = $(this);
	input.hide();
	var span = $('<span class="radio"></span>');
	input.parent().prepend(span);
	
	if (input.attr("checked")) {
		addClass(span);
	}
	
	// Click
	input.parent().click(function () {
		fireAjax();
	});
	input.siblings('label').click(function () {
		fireAjax();
	});
	
	function fireAjax () {
		
		// if already selected do nothing
		if ($(el).get(0).checked === true){
			return false;
		}
		
		toggle();
		
		// Make AJAX call to refresh page information
		refreshGameInformation(input.attr('id'));
		
		return false;
		
	}
	
	// Listener for when people click the label
	$(el).change(function () {
		toggle();
	});

	// Runs the class togglers
	function toggle () {
		resetClasses();
		addClass(span);
	}
	
	// Adds the class to the span / li, also sets the input to checked
	function addClass (span){
		
		$(el).get(0).checked = true;
		span.addClass('radioChecked').parents('li').addClass('selected');
	}
	
	function resetClasses(){
		$('.radioChecked', platformSelection).removeClass('radioChecked');
		$('li.selected', platformSelection).removeClass('selected');
	}
	
	// Fires off the AJAX request based on the ID of the radio button
	function refreshGameInformation (platform) {

		var bundle = {
			data: {
				'platform': platform,
				'ajaxCall': 1,
				'directory': $('#directory').val(),
				'module': 'pages',
				'action': 'game'
			},
			success: onRefreshSuccess
		};
		
		// Fire the bundle off
		AtariGlobal.WebService('gameProfile', bundle);

	}
	
	// Called on success event from AJAX request
	function onRefreshSuccess (data) {
		
		// update the DOM with new data
		AtariGlobal.WebService.PageUpdate(data);
		// initialize the EP
		$("#ep").editorial(editorialPodOptions);
		// Initialize sliding tabs
		$('div.systemRequirements').slidingTabs();
		// Style radio buttons
		$('input[type="radio"]').checkbox({
			styleClass: 'styledRadiobutton',
			checkedStyleClass: 'styledRadiobuttonChecked'
		});
		// Fixes an IE8 issue with form buttons with background image
		AtariGlobal.setupFormButtonsIE8();
		// Initialize external links
		AtariGlobal.setupExternalLinks();
	}
	
});


$(document).ready(function () {

	/** Initialize the Editorial Pod module
	 *  @see jqeury.fi.editorial.js
	 */
	$("#ep").editorial(editorialPodOptions);
	
	/**
	 * Initialize sliding tabs
	 */
	$('div.systemRequirements').slidingTabs();
	
	/** Style radio buttons */
	$('input[type="radio"]').checkbox({
		styleClass: 'styledRadiobutton',
		checkedStyleClass: 'styledRadiobuttonChecked'
	});

});













