var PortalSubmitController = Class.create({
	initialize: function() {
		this.AJAX_SUBMIT_PAGE = '/games/save/';
		
		this.busy = false;
		
		this.IconController = new FileUploadController('portalicon', new Array('gif', 'jpg', 'jpeg'));
		this.FlashController = new FileUploadController('portalflash', new Array('swf'));
		
		// We have to wait for the page to load before we can start adding stuff to specific elements
		Event.observe(window, 'load', function() {
			// Set up a tooltip for the Mochi Leaderboard Integration field
			new Tooltip("what");
		});
		
		HandleTimeOut(this);
	},
	
	Save: function() {
		//No simultaneous requests
		if(this.busy){
			alert('Please wait until all file activity on the page finishes.');
			return;	
		}
		
		var name = Trim($F('name_field'));
		if(name.length == 0){
			$('name_field').focus();
			alert('Please enter a name for your game.');
			return;
		}
		
		// Replace all multiple spaces in the game name with one space
		// i.e. "Super     Sun    Run" becomes "Super Sun Run"
		var pattern = new RegExp(/  /);
		while (match = pattern.exec(name)) {
			name = name.replace('  ', ' ');
		}
		$('name_field').value = name;
		
		if(!NumberIsBetween(Trim($F('width_field')), 10, 720)){
			$('width_field').focus();
			alert('Please keep the width between 10px and 720px.');
			return;
		}
		
		if(!NumberIsBetween(Trim($F('height_field')), 10, 768)){
			$('height_field').focus();
			alert('Please keep the height between 10px and 768px.');
			return;
		}
		
		if($F('genre_element') == 'null'){
			$('genre_element').focus();
			alert('Please select a genre for your game.');
			return;
		}
		
		if(!NumberIsBetween($F('shortdescription_field').length, 3, 85)){
			$('shortdescription_field').focus();
			alert('Please keep the short description between 3 and 85 characters');
			return;
		}
		
		if(!NumberIsBetween($F('description_field').length, 10, 2000)){
			$('description_field').focus();
			alert('Please keep the description between 10 and 2000 characters');
			return;
		}
		
		if(!$('portalicon_iframe').contentWindow.success){
			$('portalicon').focus();
			alert('Please upload a valid game icon.');
			return;
		}
		
		if(!$('portalflash_iframe').contentWindow.success){
			$('portalflash').focus();
			alert('Please upload a valid game file.');
			return;
		}
		
		$('submit_button').disabled = 'disabled';
		
		var params = new Array();
		params['data[Game][id]'] = this.GetSubmissionID();
		params['status'] = $F('status');
		params['data[Game][name]'] = Trim($F('name_field'));
		params['data[Game][width]'] = Trim($F('width_field'));
		params['data[Game][height]'] = Trim($F('height_field'));
		params['data[Game][genre_id]'] = $F('genre_element');
		params['data[Game][scores_enabled]'] = $('scores_enabled').checked ? 1 : 0;
		params['data[Game][short_description]'] = $F('shortdescription_field');
		params['data[Game][description]'] = $F('description_field');
		params['data[Icon][name]'] = $('portalicon_iframe').contentWindow.filename;
		params['data[Flash][name]'] = $('portalflash_iframe').contentWindow.filename;
		
		this.busy = true;
		new Ajax.Request(this.AJAX_SUBMIT_PAGE,
		{
			method:'post',
			parameters: params,
			onSuccess: this.HandleResponse.bind(this)
		});
	},
	
	HandleResponse: function(transport) {
		//Let them be able to try again if there were errors
		this.busy = false;
		
		var response = transport.responseText.evalJSON();
		
		if(!response.signedIn){
			alert('You need to log in to upload games.');
			$('submit_button').disabled = null;
			return;
		}
		
		if(response.nameTaken){
			alert('A game under that name already exists.  Please choose a different one.');
			$('submit_button').disabled = null;
			return;
		}
		
		if(!response.canAccess){
			alert('This isn\'t your game!');
			$('submit_button').disabled = null;
			return;
		}
		
		// Let them know it was successful then put them on the appropriate page
		if(this.IsEdit()) {
			alert('Your changes have been saved.');
			window.location = $F('status') == 'unapproved' ? '/unapproved_games/play/'+this.GetSubmissionID()+'/' : '/play/'+this.GetSubmissionID();
		} else {
			alert('Your game has been successfully submitted and is awaiting approval.');
			window.location =  '/profiles/edit/games/';
		}
	},
	
	GetSubmissionID: function(){
		return($('submission_id') ? $F('submission_id') : null);
	},
	
	IsEdit: function(){
		return(this.GetSubmissionID() != null);
	},
	
	// Has the game been approved?
	GetStatus: function(){
		return(this.IsEdit() != null ? $('status').value : '');
	},
	
	FillElements: function(data){
		/*$('name_field').value = data['name'];
		$('width_field').value = data['width'];
		$('height_field').value = data['height'];
		
		// Stupid ie
		try {
			$('genre_element').selectedIndex = ObjectsCollectionArray($('genre_element').options).indexOf(data['genre']);
		} catch(e) {
			for(var i = 0; i < $('genre_element').length; i++) {
				if($('genre_element')[i].value == data['genre']) {
					$('genre_element').selectedIndex = i;
				}
			}
		}
		
		$('shortdescription_field').value = data['shortdescription'];
		$('description_field').value = data['description'];*/
		//$('portalicon_iframe').src = '/filedisplay.php?type=icon&id='+data['id']+'&status='+this.GetStatus();
		//$('portalflash_iframe').src = '/filedisplay.php?type=flash&id='+data['id']+'&status='+this.GetStatus();
	}
});