/* global.js
 * General Javascript Functions for Feathered Melody and Arenthil
 *  by Laogeodritt <laogeodritt
 * Email                     at featheredmelody dot com>
 */

// for IE <= 6 compatibility, only use within the head
function include(file) {
	// workaround for IE6 issue
	if ( navigator.appName == 'Microsoft Internet Explorer' ) {
		var ieVerArray = navigator.appVersion.split('MSIE');
		// Parse the string for the version as an int
		var ieVer = parseInt(ieVerArray[1]);
		
		// if IE6 or lower, write it instead of using the DOM, which causes error
		if ( ieVer <= 6 ) {
			document.write('<script type="text/javascript" src="'+ file + '"></scr' + 'ipt>');
			return true; // stop here
		}
		// if IE7 or later, continue normally
	}

	var newScript = document.createElement('script');
	newScript.type = "text/javascript";
	newScript.src = file;
	
	document.getElementsByTagName('head')[0].appendChild(newScript);
}

// http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
function addEvent(obj, type, fn) {
	if (obj.addEventListener)
		obj.addEventListener(type, fn, false);
	else if (obj.attachEvent)
	{
		obj['e' + type + fn] = fn;
		obj[type + fn] = function() { obj['e' + type + fn](window.event); }
		obj.attachEvent('on' + type, obj[type + fn]);
	}
}

function function_exists( function_name ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Steve Clay
    // +   improved by: Legaev Andrey
    // *     example 1: function_exists('isFinite');
    // *     returns 1: true
 
 
    if (typeof function_name == 'string'){
        return (typeof window[function_name] == 'function');
    } else{
        return (function_name instanceof Function);
    }
}

// http://www.dustindiaz.com/getelementsbyclass/
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\\\s)"+searchClass+"(\\\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function hideElements(elements) {
	var max = elements.length;
	for ( i = 0; i < max; i++ ) {
		elements[i].style.display = 'none';
	}
}

function hideElementById(theId) {
	document.getElementById(theId).style.display = 'none';
}

function hideElementsByClass(theClass,node,tag) {
	if ( node == null )
		node = document;
	if (tag == null )
		tag = '*';
	hideElements(getElementsByClass(theClass,node,tag));
}
function init() {
	// remove noscript stuff
	hideElementsByClass('noscript',document);
	// toggler
	hideElementsByClass('toggle',document);
	toggleSw = getElementsByClass('toggleSw');
	for ( i = 0; i < toggleSw.length; i++ )
		toggleSw[i].innerHTML = '[Show]';
	toggleBtn = getElementsByClass('toggleBtn');
	for ( i = 0; i < toggleBtn.length; i++ )
		toggleBtn[i].value = 'Show';
}

// includes
include('/img/toggle.js');
include('/img/ctEm.js'); // deprecated
include('/img/spamspan.js');
addEvent(window, 'load', init);