function toggle(id) {
	if(document.getElementById(id).style.display == 'none') {
		document.getElementById(id).style.display = 'block';
	}
	else {
		document.getElementById(id).style.display = 'none';
	}
}

function blackedOutContent(element) {
	if(document.getElementById('blackout').style.display != "none") {
		$('div#blackout').hide();
		$('div#blackout').children().hide();
	}
	else {
		if(element.parentNode == undefined || element.parentNode.id != "blackout") {
			document.getElementById('blackout').appendChild(element);
		}
		$(element).show();
		$('div#blackout').show();
	}
}

function stripAlphaChars(string) 
{ 
	//var output = new String(string); 
	output = string.replace(/[^0-9]/g, ''); 

	return output; 
}


function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

function d2h(dec, pad) {	// Convet Decimal to Hex
	hex = Number(dec).toString(16);
	if(pad !== undefined) {
		while(hex.length < pad) {
			hex = "0"+hex;
		}
	}
	return hex;
}	
function h2d(hex) {	// Convert Hex to Decimal
	dec = parseInt(hex,16);
	return dec;
}

function num2word(num) {
	var word = "";
	switch (num) {
		case 0:
			word = "zero";
			break;
		case 1:
			word = "one";
			break;
		case 2:
			word = "two";
			break;
		case 3:
			word = "three";
			break;
		case 4:
			word = "four";
			break;
		case 5:
			word = "five";
			break;
		case 6:
			word = "six";
			break;
		case 7:
			word = "seven";
			break;
		case 8:
			word = "eight";
			break;
		case 9:
			word = "nine";
		case 10:
			word = "ten";
	}
	return word;
}

function updateTooltipRank(element, rank) {
	jQuery(element).removeClass('tooltip-rank0');
	jQuery(element).removeClass('tooltip-rank1');
	jQuery(element).removeClass('tooltip-rank2');
	jQuery(element).removeClass('tooltip-rank3');
	jQuery(element).removeClass('tooltip-rank4');
	jQuery(element).removeClass('tooltip-rank5');
	jQuery(element).addClass('tooltip-rank'+rank);
}

function displayTooltip(element) {
	jQuery(element).addClass('tooltip-active');
	element.style.zIndex = "1000";	// IE: z-index fix
	if(jQuery(element.parentNode.parentNode).hasClass('soul-container')) {
		element.parentNode.parentNode.style.zIndex = "1000";	// IE: z-index fix
	}
	document.getElementById('content').style.zIndex = "1000";	// IE: z-index fix
	
	if(jQuery(element).children('div.tooltip').length == 0) {	// If no tooltip exists request it
		var site = jQuery(element).data('site');
		var id = jQuery(element).data('id');
		var type = jQuery(element).data('type');
		var rank = '';
		
		if(type == 'ability' || type == 'ability-history') {
			rank = "&rank=0";
			if(jQuery(element).data('rank')) {
				rank = "&rank="+jQuery(element).data('rank');
			}
		}
		
		var siteUrl = [];	// Define available sites
		siteUrl['rift'] = "/tgg/rift/tooltip.php";
		
		jQuery.ajax({
			cache: false,
			url: siteUrl[site],
			data: "type="+type+"&id="+id+rank,
			success: function (data) {
				if(jQuery(element).children('div.tooltip').length == 0) {
					if($(element).get(0).tagName == 'a') {
						link = $(element).text();
						$(element).empty().append(link+data);
					}
					else {
						$(element).append(data);
					}
				}
			}
		});
	}
}

function attachTooltips() {
	jQuery('.ability').each( function() {
		if($(this).children('div.tooltip').length == 0){
			$(this).hover(function () {
				displayTooltip(this);
			}, function (){
				jQuery(this).removeClass('tooltip-active');
				this.style.zIndex = "";
				if(jQuery(this.parentNode.parentNode).hasClass('soul-container')) {
					this.parentNode.parentNode.style.zIndex = "";
				}
				document.getElementById('content').style.zIndex = "";
			});
		}
	});
	
	jQuery('.item').each( function() {
		if($(this).children('div.tooltip').length == 0){
			$(this).hover(function () {
				displayTooltip(this);
			}, function (){
				jQuery(this).removeClass('tooltip-active');
				this.style.zIndex = "";
				document.getElementById('content').style.zIndex = "";
			});
		}
	});
}

function rateContent(button) {
	var contentId = $(button).data('id');
	var contentType = $(button).data('type');
	var rating = $(button).data('rate');
	var site = $(button).data('site');
	
	var siteUrl = [];	// Define available sites
		siteUrl['rift'] = "/tgg/rift/rating.php";
	
	jQuery.ajax({
		cache: false,
		type: 'POST',
		url: siteUrl[site],
		data: "rate_content_id="+contentId+"&rate_type="+contentType+"&rate_rating="+rating,
		success: function (data) {
			$('input.rate').each( function () {
				if($(this).data('id') == contentId && $(this).data('type') == contentType) {
					$(this).remove();
				}
			});
			$('span.rate-value').each( function () {
				if($(this).data('id') == contentId && $(this).data('type') == contentType) {
					this.firstChild.nodeValue = data;
				}
			});
		}
	});
}

function attachRating() {
	$('input.rate').each( function() {
		$(this).click(function () {
			rateContent(this);
		});
	});
}

function submitComment(button) {
	var contentId = $(button).data('content-id');
	var contentType = $(button).data('content-type');
	var threadName = $(button).data('thread-name');
	var site = $(button).data('site');
	var postName = document.getElementById('form-post-name').value;
	var postMessage = document.getElementById('form-post-message').value;

	var siteUrl = [];	// Define available sites
		siteUrl['rift'] = "/tgg/common/comment.php";
	
	jQuery.ajax({
		cache: false,
		type: 'POST',
		url: siteUrl[site],
		data: "form-post-comment-content-id="+contentId+"&form-post-comment-content-type="+contentType+"&form-post-comment-thread-name="+threadName+"&form-post-comment-post-name="+postName+"&form-post-comment-post-message="+postMessage,
		success: function (data) {
			if(data == 'success') {
				postCommentElement = document.getElementById('form-post-comment').parentNode;
				$('#form-post-comment').remove();
				postSuccessElement = document.createElement('p');
				postSuccessTextElement = document.createTextNode('Your comment has been saved.  Thanks for posting!');
				postSuccessElement.appendChild(postSuccessTextElement);
				postCommentElement.appendChild(postSuccessElement);
			}
			else {
				alert(data);
			}
		}
	});
}

function attachComment() {
	$('#form-post-submit').click( function () {
		submitComment(this);
	});
}

addLoadEvent(attachTooltips);
addLoadEvent(attachRating);
addLoadEvent(attachComment);
