var SkillDBTooltip = Class.create();

SkillDBTooltip.prototype = {

	version : {
		DEFAULT : 0,
		PVE : 1,
		PVP : 2
	},

	initialize : function() {
		this.skills = new Array();
		this.skillIDs = new Array();
		this.ajaxRequest = new AjaxRequest();
		this.tooltipDiv = $$('.tooltip .bc-content')[0];

		this.currentSkill = false;

		this.elements = $$('.skillDBTooltip');

		this.campaignColors = ['#AAAAAA', '#0099FF', '#FF2033', '#EEAA00', '#66FF33'];

		this.useVersion = this.version.DEFAULT;
		this.loadOtherSkills = false;
		if(typeof SKILLDB_SKILL_VERSION != 'undefined') {
			this.useVersion = SKILLDB_SKILL_VERSION;
			this.loadOtherSkills = true;
		}

		this.elements.each(this.attachToElement, this);
	},

	attach : function(e) {
		this.attachToElement(e);
		this.loadSkills();
	},

	attachToElement : function(e) {
		var argumentArray = e.className.split('skillDBTooltip ')[1].split(' ');
		var arguments = {
			skillID : argumentArray[0],
			attributeValue : argumentArray[1]
		};
		if(arguments.skillID != 0) {
			if(typeof this.skills[arguments.skillID] == 'undefined')
				this.skillIDs.push(arguments.skillID);
			ToolTips.attach(e, this.getSkillTooltipTitle(e), this
				.getSkillTooltipContent(e), 400);
		}
	},

	detach: function(e) {
		ToolTips.detach(e);
	},

	showLoadingInfo : function(element, args) {
		if(SkillDBTooltipManager.tooltipDiv.visible()) {
			SkillDBTooltipManager.currentSkill = element;
			SkillDBTooltipManager.skillIDs.push(args.skillID);
			SkillDBTooltipManager.loadSkills();
		}
	},

	getSkillTooltipTitle : function(e) {
		var element = e;
		return function() {
			var argumentArray = element.className.split('skillDBTooltip ')[1].split(' ');
			var args = {
				skillID : argumentArray[0],
				attributeValue : argumentArray[1]
			};
			this.tooltipDiv = $$('.tooltip .bc-content')[0];
			var skill = SkillDBTooltipManager.getSkill(args.skillID);
			if(skill === false) {
				if(SkillDBTooltipManager.tooltipDiv.visible()) {
					SkillDBTooltipManager.showLoadingInfo(element, args);
				}

				return '';
			}
			else {

				var version = '';
				if(skill.version == 1) version = SKILLDB_PVE_VERSION;
				else if(skill.version == 2) version = SKILLDB_PVP_VERSION;
				else if(skill.version == 0 && skill.otherSkillID != 0) version = SKILLDB_PVE_VERSION;

				var content = Builder.node('div', [Builder.node('div', [Builder.node('div', {
					style : 'float:left;',
					className : 'skillTitle' + (skill.elite ? 'Elite' : 'Normal')
				}, skill.name),Builder.node('div', {
					style : 'float:right;',
					className : 'normalTitle smallFont light skillTitle' + (skill.elite ? 'Elite' : 'Normal')
				}, version), Builder.node('div', {
					className: 'gray smallFont light normalTitle',
					style : 'clear:both;'
				}, [skill.nameEnglish, Builder.node('span', {
					className: 'normalTitle',
					style: 'float:right;color:' + SkillDBTooltipManager.campaignColors[skill.campaignID]
				}, skill.campaignName)]
				), Builder.node('div', {
					style : 'clear:both;'
				}), ]), ]);
	
				return content.innerHTML;
			}

		};
	},

	getSkillTooltipContent : function(e) {
		var element = e;
		return function() {
			var argumentArray = element.className.split('skillDBTooltip ')[1].split(' ');
			var args = {
				skillID : argumentArray[0],
				attributeValue : argumentArray[1]
			};
            
			this.tooltipDiv = $$('.tooltip .bc-content')[0];
			var skill = SkillDBTooltipManager.getSkill(args.skillID);
	    
			if(skill === false) {
				SkillDBTooltipManager.showLoadingInfo(element, args);
		
				return '';
			}
			else {
				var description = Builder.node('div');
				var aValue = -1;
				var aMax = 15;
				if (typeof (args.attributeValue) != 'undefined') {
					if (args.attributeValue == parseInt(args.attributeValue)) {
						aValue = parseInt(args.attributeValue);
					} else if($(args.attributeValue)) {
						aValue = $(args.attributeValue).value;
					}
				}
				if(skill.attributeID == -4 || skill.attributeID == -5) aMax = 12;
				else if(skill.attributeID < -1) aMax = 10;
				description.innerHTML = SkillDBTooltipManager.convertDescription(
					skill.description, aValue, aMax);

				var emptyValue = Builder.node('div');
				var activationSpan = Builder.node('span');
				activationSpan.innerHTML = ' ' + skill.activation;
				var numbers = Builder.node('div', [
					skill.recharge != 0 ? Builder.node('div', {
						className : 'skillValue'
					}, [Builder.node('img', {
						src : RELATIVE_WCF_DIR + 'icon/rechargeS.png'
					}), ' ' + skill.recharge]) : emptyValue,
					skill.activation != 0 ? Builder.node('div', {
						className : 'skillValue'
					}, [Builder.node('img', {
						src : RELATIVE_WCF_DIR + 'icon/activationS.png'
					}), activationSpan]) : emptyValue,
					skill.energy != 0 ? Builder.node('div', {
						className : 'skillValue'
					}, [Builder.node('img', {
						src : RELATIVE_WCF_DIR + 'icon/energyS.png'
					}), ' ' + skill.energy]) : emptyValue,
					skill.exhaustion != 0 ? Builder.node('div', {
						className : 'skillValue'
					}, [Builder.node('img', {
						src : RELATIVE_WCF_DIR + 'icon/exhaustionS.png'
					}), ' ' + skill.exhaustion]) : emptyValue,
					skill.upkeep != 0 ? Builder.node('div', {
						className : 'skillValue'
					}, [Builder.node('img', {
						src : RELATIVE_WCF_DIR + 'icon/upkeepS.png'
					}), ' ' + skill.upkeep]) : emptyValue,
					skill.sacrifice != 0 ? Builder.node('div', {
						className : 'skillValue'
					}, [Builder.node('img', {
						src : RELATIVE_WCF_DIR + 'icon/sacrificeS.png'
					}), ' ' + skill.sacrifice + '%']) : emptyValue,
					skill.adrenaline != 0 ? Builder.node('div', {
						className : 'skillValue'
					}, [Builder.node('img', {
						src : RELATIVE_WCF_DIR + 'icon/adrenalineS.png'
					}), ' ' + skill.adrenaline]) : emptyValue, ]);

				var content = Builder.node('div', [Builder.node('div', [
					Builder.node('div', {
						className : 'skillToolTip'
					}, [
					Builder.node('div', {
						className : 'skillContentLeft'
					}, [
					Builder.node('img', {
						src : RELATIVE_WCF_DIR
						+ 'images/skills/'
						+ skill.skillID + '.jpg',
						className : 'skill' + (skill.elite
							? 'Elite'
							: 'Normal')
					}),
					Builder.node('img', {
						src : RELATIVE_WCF_DIR
						+ 'icon/profession'
						+ skill.professionID + 'M.png'
					}), ]), Builder.node('div', {
						className : 'skillContentRight'
					}, [Builder.node('div', {
						className : 'skillContentRightDescription'
					}, [description]), Builder.node('div', {
						className : 'skillContentRightNumbers'
					}, [Builder.node('div', {
						style : 'clear:both;'
					}), numbers, ]), Builder.node('div', {
						style : 'clear:both;'
					}), ]), Builder.node('div', {
						style : 'clear:both;'
					}), ]), ]), ]);

				$A(SkillDBTooltipManager.tooltipDiv.parentNode.children).each(function(e, index) {
					if(index == 0) return;
					e.parentNode.removeChild(e);
				});
            
				SkillDBTooltipManager.tooltipDiv.show();
				return content.innerHTML;
			}
		};
	},

	convertDescription : function(description, attribute, range) {
		if (typeof (attribute) == 'undefined'
			|| (attribute == '' && attribute != 0))
			attribute = -1;
		if (attribute != parseInt(attribute))
			attribute = -1;
		if (typeof (range) == 'undefined')
			range = 15;
		if ((attribute < 0 || attribute > 30 || (range == 12 && attribute > 12)
			|| (range == 10 && attribute > 10)) && attribute != 42 && attribute != 1337)
			return description;
		var i = 0;

		while (description.indexOf("..") != -1 && i < 10) {
			var matches = description.match(/([0-9]+)(?:\.\.|-)([0-9]+)/);
			if (matches) {
				for ( var i = 0; i < matches.length; i += 3) {
					if (attribute > -1) {
						description = description.replace(matches[i], parseInt(matches[i + 1]) + Math.round((matches[i + 2] - matches[i + 1]) * attribute / range));
					}
				}
			}
			i++;
		}

		if(i != 0) {
			var aDesc = description.split(')');
			if(aDesc.length > 1) {
				aDesc.pop();
				description = aDesc.join(')') + '&nbsp;' + attribute + ')';
			}
		}
		return description;
	},

	loadSkills : function(onLoad) {
		if (typeof onLoad == 'undefined')
			onLoad = false;
		if (this.skillIDs.length != 0) {
			var postData = this.skillIDs.join('&id[]=');
			if(postData === '') return;
			postData = 'id[]=' + postData;
			if(this.loadOtherSkills) postData += '&otherSkills=1';
			if (onLoad) {
				onloadEvents.push(function() {
					SkillDBTooltipManager.ajaxRequest.openPost(
						'index.php?page=SkillDBSkillXML' + SID_ARG_2ND,
						postData, function() {
							SkillDBTooltipManager.receiveResponse();
						});
				});
			} else {
				SkillDBTooltipManager.ajaxRequest.openPost(
					'index.php?page=SkillDBSkillXML' + SID_ARG_2ND,
					postData, function() {
						SkillDBTooltipManager.receiveResponse();
					});
			}
		}
	},

	getSkill : function(id) {
		if (typeof (SkillDBTooltipManager.skills[id]) != 'undefined') {
			if(SkillDBTooltipManager.useVersion == SkillDBTooltipManager.version.DEFAULT ||
				(SkillDBTooltipManager.useVersion == SkillDBTooltipManager.version.PVE && SkillDBTooltipManager.skills[id].version == 0) ||
				(SkillDBTooltipManager.useVersion == SkillDBTooltipManager.version.PVP && SkillDBTooltipManager.skills[id].version == 2) ||
				SkillDBTooltipManager.skills[id].otherSkillID == 0 ||
				typeof SkillDBTooltipManager.skills[SkillDBTooltipManager.skills[id].otherSkillID] == 'undefined')
				return SkillDBTooltipManager.skills[id];
			else
				return SkillDBTooltipManager.skills[SkillDBTooltipManager.skills[id].otherSkillID];
		} else {
			return false;
		}
	},

	receiveResponse : function() {
		if (this.ajaxRequest && this.ajaxRequest.xmlHttpRequest.readyState == 4
			&& this.ajaxRequest.xmlHttpRequest.status == 200
			&& this.ajaxRequest.xmlHttpRequest.responseXML) {
			var skills = this.ajaxRequest.xmlHttpRequest.responseXML.getElementsByTagName('skill');
			for (var i = 0; i < skills.length; i++) {
				var skillID = skills[i].attributes.getNamedItem('id').value;
				this.skills[skillID] = {
					skillID : skillID,
					name : skills[i].childNodes[0].firstChild.nodeValue,
					elite : skills[i].childNodes[1].firstChild.nodeValue == "1",
					nameEnglish : skills[i].childNodes[2].firstChild.nodeValue,
					campaignID : skills[i].childNodes[3].firstChild.nodeValue,
					attributeID : skills[i].childNodes[4].firstChild.nodeValue,
					typeID : skills[i].childNodes[5].firstChild.nodeValue,
					professionID : skills[i].childNodes[6].firstChild.nodeValue,
					upkeep : skills[i].childNodes[7].firstChild.nodeValue,
					exhaustion : skills[i].childNodes[8].firstChild.nodeValue,
					energy : skills[i].childNodes[9].firstChild.nodeValue,
					activation : skills[i].childNodes[10].firstChild.nodeValue,
					recharge : skills[i].childNodes[11].firstChild.nodeValue,
					adrenaline : skills[i].childNodes[12].firstChild.nodeValue,
					sacrifice : skills[i].childNodes[13].firstChild.nodeValue,
					version : skills[i].childNodes[14].firstChild.nodeValue,
					otherSkillID : skills[i].childNodes[15].firstChild.nodeValue,
					range : skills[i].childNodes[16].firstChild.nodeValue,
					description : skills[i].childNodes[17].firstChild.nodeValue,
					professionName : skills[i].childNodes[18].firstChild.nodeValue,
					attributeName : skills[i].childNodes[19].firstChild.nodeValue,
					campaignName : skills[i].childNodes[20].firstChild.nodeValue,
					typeName : skills[i].childNodes[21].firstChild.nodeValue
				};
			}
			this.skillIDs.clear();
			if(this.currentSkill !== false) {
				ToolTips.refresh();
				this.currentSkill = false;
			}
		}
	},

	usePvPVersion : function() {
		this.useVersion = this.version.PVP;
	},
	usePvEVersion : function() {
		this.useVersion = this.version.PVE;
	},
	useDefaultVersion : function() {
		this.useVersion = this.version.DEFAULT;
	}
};

var SkillDBTooltipManager = null;
var waitFunction = function() {
	if (typeof (ToolTips) == 'undefined' || ToolTips == null) {
		onloadEvents.push(waitFunction);
	} else {
		SkillDBTooltipManager = new SkillDBTooltip();
	}
};

waitFunction();
