Gamependium.gw = {
	toggleView: function() {
		var linkText = this.firstChild.nodeValue;
		var ourParent = this.parentNode.parentNode;
		
		switch (linkText) {
			case 'Show': // found inside fieldset legends
				this.firstChild.nodeValue = 'Hide';
				ourParent.className = 'expanded';
				break;
			case 'Hide': // found inside fieldset legends
				this.firstChild.nodeValue = 'Show';
				ourParent.className = 'compacted';
				break;
			case 'Expand': // found inside table captions
				this.firstChild.nodeValue = 'Compact';
				ourParent.className = 'skill_list large expanded';
				break;
			case 'Compact': // found inside table captions
				this.firstChild.nodeValue = 'Expand';
				ourParent.className = 'skill_bar';
				break;
		}
		return false;
	},
	shareField: function(el) {
		var target = Gamependium.event.getTarget(el);

		if (target.className == "share") {
			target.select();
		}
	}
}

Gamependium.gw.skills = {
	/*
	 * Initialize our skill description by passing it the element that contains the text
	 * Returns a function we can use to adjust the values in the description by passing the attribute's value
	 */
	initDescription: function(description) {
		var values = description.getElementsByTagName('strong');
		var ranges = [ ];
		
		for (var i = 0, len = values.length; i < len; i++ ) {
			if (values[i].title && values[i].title.indexOf('-')) { // fix for hard coded values
				// 'Range: ' 
				values[i].childNodes[0].nodeValue = values[i].title.substring(6);
			}
			ranges.push( values[i].childNodes[0].nodeValue.split('-') );
		}
		
		return function(attrValue) {
			for (var i = 0, len = ranges.length; i < len; i++) {
				values[i].childNodes[0].nodeValue = 
					Gamependium.gw.skills.calculate(ranges[i][0], ranges[i][1], attrValue);
			}
		}
	},
	calculate: function(at0, at15, attrValue) {
		at0 = parseInt(at0);
		at15 = parseInt(at15);
		attrValue = parseInt(attrValue);
		var range = (at15 - at0) / 15;
		return Math.round(range * attrValue) + at0;
	},
	/*
	 * Function used for adjusting the description to reflect attribute values selected via form elements
	 * Designed to be used via Event Delegation
	 */
	singleAdjust: function(changeDescription, attrSelect) {
		return function(el) {
			var target = Gamependium.event.getTarget(el);
			
			if (target.type == 'button' || target.type == 'select') {
				if (target.value == '+') {
					if (attrSelect.selectedIndex + 1 >= attrSelect.length) {
						return false;
					}
					attrSelect.selectedIndex += 1;
				} else if (target.value == '-') {
					if (attrSelect.selectedIndex <= 0) {
						return false;
					}
					attrSelect.selectedIndex -= 1;
				}
				changeDescription(attrSelect.selectedIndex);
			}
		}
	}
}

/*
 * Initialize 'share' form field elements so they autoselect the field
 */
Gamependium.event.loadEvent(function() {
	for (var i = 0, len = document.forms.length; i < len; i++) {
		Gamependium.event.addListener(document.forms[i], 'DOMfocusIn', Gamependium.gw.shareField);
		Gamependium.event.addListener(document.forms[i], 'click', Gamependium.gw.shareField);
	}
});

/*
 * Initialize single page skill view attribute adjustor
 */
Gamependium.event.loadEvent(function() {
	var description = document.getElementById('skill_description');
	if (description) {
		var attrAdjust = document.getElementById('attr_adjust');
		var updateDescription = Gamependium.gw.skills.initDescription(description);
		var selectBox = attrAdjust.getElementsByTagName('select')[0]
		var adjustFunc = Gamependium.gw.skills.singleAdjust(updateDescription, selectBox);
		selectBox.onchange = function() { updateDescription(this.value) };
		Gamependium.event.addListener(attrAdjust, 'click', adjustFunc);
	}
});

Gamependium.event.loadEvent(function() {
	var captions = document.getElementsByTagName('caption');
	for (var i = 0, len = captions.length; i < len; i++) {
		var link = captions[i].getElementsByTagName('a')[0];
		if (link) {
			link.onclick = Gamependium.gw.toggleView;
		}
	}

	var legends = document.getElementsByTagName('legend');
	for (var i = 0, len = legends.length; i < len; i++) {
		var link = legends[i].getElementsByTagName('a')[0];
		if (link) {
			link.onclick = Gamependium.gw.toggleView;
		}
	}
}); 