/**
 * ajax.js
 * Hammer of Goel Armory Toolkit
 * Duane Sibilly II <admin@hammerofgoel.com>
 * 
 * This script provides basic AJAX functionality to the Hammer of Goel Armory
 * toolkit.
 *
 * The Signature namespace handles updating and animating the forum signature
 * generator.
 *
 * The Application namespace handles querying the Armory for the purposes of
 * validating guild applicants and displays either an error message if invalid
 * or the full application if valid.
 *
 * Prototype 1.6.x or higher is required.
 */

HAMMER = {
	Signature: {
		target_pane: 'details',
		load_pane: 'loading',
		url: 'includes/formlogic.php',
		ani_duration: 0.3,
		kill: false,
        
		init: function () {
			if(this.kill) {
                alert('The Hammer of Goel Armory Tools are currently OFFLINE for maintenance. We apologize for the inconvenience.');
                $('submit').disabled = true;
            } else {
                $('submit').style.display = 'none';
                Event.observe(
                    'character', 
                    'keyup', 
                    function(e) {
                        if(!e)
                            var e = window.event;
                            
                        if(e.keyCode == 13) {
                            HAMMER.Signature.generate();
                        }
                        return true;
                    }
                );
            }
		},
		
		generate: function() {
			this.hidePane(this.getTargetPane());
		},
		
		hidePane: function(pane_name) {
			var options = {
				duration: this.getDuration(),
				afterFinish: function() {
					if(pane_name == HAMMER.Signature.getTargetPane())
						HAMMER.Signature.showPane(HAMMER.Signature.getLoadPane());
					else
						HAMMER.Signature.showPane(HAMMER.Signature.getTargetPane());
				}
			};
			new Effect.BlindUp(pane_name, options);
		},
		showPane: function(pane_name) {
			var options = {
				duration: this.getDuration(),
				afterFinish: function() {
					if(pane_name == HAMMER.Signature.getLoadPane())
						HAMMER.Signature.doReplace();
				}
			};
			new Effect.BlindDown(pane_name, options);
		},
		doReplace: function() {
			var url = this.getURL();
			var target = this.getTargetPane();
			var options = {
				method: 'post',
				parameters: { 
					character: $F('character'), 
					realm: $F('realm') 
				},
				onComplete: function() {
					HAMMER.Signature.hidePane(HAMMER.Signature.getLoadPane());
				}
			};
			var updater = new Ajax.Updater(target, url, options);
		},
		// ACCESSORS
		getTargetPane: function() {
			return this.target_pane;
		},
		getLoadPane: function() {
			return this.load_pane;
		},
		getURL: function() {
			return this.url;
		},
		getDuration: function() {
			return this.ani_duration;
		},
		setTargetPane: function(str) {
			this.target_pane = str;
		},
		setLoadPane: function(str) {
			this.load_pane = str;
		},
		setURL: function(url) {
			this.url = url;
		},
		setDuration: function(dur) {
			this.ani_duration = dur;
		}
	},
	
	Application: {
		target_pane: 'armorydata',
		load_pane: 'loading',
		url: 'http://hammerofgoel.com/armory/appquery.php',
		ani_duration: 0.3,
		
		init: function () {
			Event.observe(
				'character', 
				'keyup', 
				function(e) {
					if(!e)
						var e = window.event;
						
					if(e.keyCode == 13) {
						HAMMER.Application.start();
					}
					return true;
				}
			);
		},
		
		start: function() {
			this.hidePane(this.getTargetPane());
		},
		
		hidePane: function(pane_name) {
			var options = {
				duration: this.getDuration(),
				afterFinish: function() {
					if(pane_name == HAMMER.Application.getTargetPane())
						HAMMER.Application.showPane(HAMMER.Application.getLoadPane());
					else
						HAMMER.Application.showPane(HAMMER.Application.getTargetPane());
				}
			};
			new Effect.BlindUp(pane_name, options);
		},
		showPane: function(pane_name) {
			var options = {
				duration: this.getDuration(),
				afterFinish: function() {
					if(pane_name == HAMMER.Application.getLoadPane())
						HAMMER.Application.doReplace();
				}
			};
			new Effect.BlindDown(pane_name, options);
		},
		doReplace: function() {
			var url = this.getURL();
			var target = this.getTargetPane();
			var options = {
				method: 'post',
				parameters: { 
					character: $F('character') 
				},
				onComplete: function() {
					HAMMER.Application.hidePane(HAMMER.Application.getLoadPane());
				}
			};
			var updater = new Ajax.Updater(target, url, options);
		},
		// ACCESSORS
		getTargetPane: function() {
			return this.target_pane;
		},
		getLoadPane: function() {
			return this.load_pane;
		},
		getURL: function() {
			return this.url;
		},
		getDuration: function() {
			return this.ani_duration;
		},
		setTargetPane: function(str) {
			this.target_pane = str;
		},
		setLoadPane: function(str) {
			this.load_pane = str;
		},
		setURL: function(url) {
			this.url = url;
		},
		setDuration: function(dur) {
			this.ani_duration = dur;
		}
		
	}
};