var Browser = {
	a : navigator.userAgent.toLowerCase()
}
Browser = {
	ie : /*@cc_on true || @*/ false,
	ie6 : Browser.a.indexOf('msie 6') != -1,
	ie7 : Browser.a.indexOf('msie 7') != -1,
	opera : !!window.opera,
	safari : Browser.a.indexOf('safari') != -1,
	safari3 : Browser.a.indexOf('applewebkit/5') != -1,
	mac : Browser.a.indexOf('mac') != -1
}
function $(e) {
	if(typeof e == 'string')
    	return document.getElementById(e);
	return e;
}
function createElement(name, attrs, doc, xmlns) {
	var doc = doc ? doc : document;
	var elm;
	if(doc.createElementNS)
		elm = doc.createElementNS(xmlns ? xmlns : "http://www.w3.org/1999/xhtml", name);
	else
		elm = doc.createElement(name);
	if(attrs)
		for(attr in attrs)
			elm.setAttribute(attr, attrs[attr]);
	return elm;
}
function setDisplay(e, display) {
	$(e).style.display = display;
}
function hide(e) {
	setDisplay(e, 'none');
}
function show(e) {
	setDisplay(e, '');
}
function visible(e) {
	return $(e).style.display != 'none';
}
function toggle(e) {
	(visible(e) ? hide : show)(e);
}
function visibleInverse(e) {
	return $(e).style.display != '';
}
function toggleInverse(e, display) {
	setDisplay(e, visibleInverse(e) ? '' : display);
}
function getChildElementsByTagName(e, tagName) {
	var nodes = [];
	for(var i = 0; i < e.childNodes.length; i++)
		if(e.childNodes[i].nodeName.toLowerCase() == tagName)
			nodes.push(e.childNodes[i]);
	return nodes;
}
function removeChildren(e) {
	while(e.firstChild)
		e.removeChild(e.firstChild);
}
function addEvent(obj, evType, fn) {
	if(obj.addEventListener) {
		obj.addEventListener(evType, fn, false);
		return true;
	} else if(obj.attachEvent)
		return obj.attachEvent("on" + evType, fn);
	return false;
}

function getFormQueryString(form) {
	var pairs = [];
	var inputs = form.getElementsByTagName('input');
	var textareas = form.getElementsByTagName('textarea');
	var selects = form.getElementsByTagName('select');

	for(var i = 0, input; input = inputs[i]; i++)
		pairs.push(input.name + '=' + encodeURI(input.value));

	for(var i = 0, input; input = textareas[i]; i++)
		pairs.push(input.name + '=' + encodeURI(input.value));

	for(var i = 0, input; input = selects[i]; i++)
		for(var j = 0, option; option = input.options[j]; j++)
			if(option.selected)
				pairs.push(input.name + '=' + encodeURI(option.value));

	return pairs.join('&');
}

function getURLParams() {
	var map = {};
	var entries = document.location.search.substr(1).split('&');
	for(var i = 0; i < entries.length; i++) {
		var entry = entries[i].split('=', 2);
		if(!map[entry[0]])
			map[entry[0]] = [];
		map[entry[0]].push(entry.length == 2 ? decodeURIComponent(entry[1]) : null);
	}
	return map;
}

function createURLSearchString(map) {
	var search = '';
	for(field in map)
		if(!Object.prototype[field]) {
			var array = map[field];
			for(var i = 0; i < array.length; i++) {
				if(search != '')
					search += '&';
				search += field;
				if(array[i] != null)
					search += '=' + array[i];
			}
		}
	if(search != '')
		search = '?' + search;
	return search;
}

function setURLParam(parameter, value) {
	var url = document.location.protocol + '//' + document.location.host + document.location.pathname;
	var params = getURLParams();
	params[parameter] = [value];
	url += createURLSearchString(params);
	url += document.location.hash;
	return url;
}

function addStylesheet(href, media) {
	document.getElementsByTagName("head")[0].appendChild(createElement('link', {
		'rel': 'stylesheet',
		'type': 'text/css',
		'media': media ? media : 'screen, projection',
		'href': href
	}));
}

function createObject(type, data, width, height, params, doc, fallback) {
	var obj = createElement('object', {
		'type': type,
		'data': data,
		'width': width,
		'height': height
	}, doc);
	if(params)
		for(var i = 0, pair; pair = params[i]; i++)
			obj.appendChild(createElement("param", {
				'name': pair[0],
				'value': pair[1]
			}, doc));
	if(fallback)
		obj.appendChild(fallback);
	return obj;
}

function setFlash(target, data, width, height, params, fallbackMsg) { // avoids IE Eolas patent UI workarounds
	// IE ignores objects created with DOM. Serialize & use innerHTML
	var doc = Browser.ie ? new ActiveXObject('Microsoft.XMLDOM') : document;
	var obj = createObject('application/x-shockwave-flash', data, width, height, params, doc,
			doc.createTextNode(fallbackMsg));
	var targetNode = $(target);
	if(Browser.ie)
		targetNode.innerHTML = obj.xml;
	else {
		removeChildren(targetNode);
		targetNode.appendChild(obj);
	}
}


//flash detection
var MM_contentVersion = 8;
var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
if ( plugin ) {
		var words = navigator.plugins["Shockwave Flash"].description.split(" ");
	    for (var i = 0; i < words.length; ++i)
	    {
		if (isNaN(parseInt(words[i])))
		continue;
		var MM_PluginVersion = words[i]; 
	    }
	var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
}
else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 
   && (navigator.appVersion.indexOf("Win") != -1)) {
	document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
	document.write('on error resume next \n');
	document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
	document.write('</SCR' + 'IPT\> \n');
}

var flashString;
var tempObj;
var flashCount=1;
//printFlash uses innerHTML to render flash objs to get around the IE flash rendering issue
function printFlash(id, src, wmode, menu, bgcolor, width, height, quality, base, flashvars, noflash){
	
	if(MM_FlashCanPlay && getcookie("showflash")!="false" ){
		
		flashString = '<object id= "' + id + 'Flash" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + width + '" height="' + height + '"><param name="movie" value="' + src + '"></param><param name="quality" value="' + quality + '"></param>';
		if(base){
		flashString+='<param name="base" value="' + base + '"/>';
		}
		
		flashString+='<param name="flashvars" value="' + flashvars + '" ></param><param name="bgcolor" value="' + bgcolor + '" ></param><param name="menu" value="' + menu + '" ></param><param name="wmode" value="' + wmode + '" ></param></param><embed name= "' + id + 'Flash" src="' + src + '" wmode="' + wmode + '" menu="' + menu + '" bgcolor="' + bgcolor + '" width="' + width + '" height="' + height + '" quality="' + quality + '" pluginspage="http://www.macromedia.com/go/getflas/new-hplayer" type="application/x-shockwave-flash" base="' + base + '" flashvars="' + flashvars + '" /></object>';
		/* salign = tl was removed from this funtion, since we needed some of our flash movies to be centered */
	}else{

		flashString=noflash;

	}
	
	if(false && Browser.safari && !body_loaded)
		document.write(flashString);
	else
		document.getElementById(id).innerHTML = flashString;
}

function toggleTabsGroup(category, page) {
	
	hideTabs(page);
	hideContents(page);
	
	$('tabs-container').style.display = 'block';

	switch (category) {
		case "pro":
			$('tabs-pro').style.display = 'block';
			$('wow-pro').style.display = 'block';
			$('wow-pro').className = 'active';
			break;
		case "comm":
			$('tabs-comm').style.display = 'block';
			$('wow-koth').style.display = 'block';
			$('wow-koth').className = 'active';
			break;
	}
}

function hideTabs (page) {
	
	switch (page) {
		case "tournaments":
			$('tabs-pro').style.display = 'none';
			$('tabs-comm').style.display = 'none';
			break;
		case "videos":
			$('tabs-videos').style.display = 'none';
			break;
	}
}
function hideContents (page) {
	switch (page) {
		case "tournaments":
			$('wow-pro').style.display = 'none';
			$('sc-pro').style.display = 'none';
			$('wc3-pro').style.display = 'none';
			break;
		case "videos":
			$('general').style.display = 'none';
			$('contest').style.display = 'none';
			$('panel').style.display = 'none';
			break;
	}
}

function toogleTab(elem, parentTable, page) {
	hideContents(page);
	var links = $(parentTable).getElementsByTagName('A');
	for(var i = 0; i < links.length; i++) {
		links[i].className = '';	
	}
	elem.className = 'active';
	
	$(elem.getAttribute('category')).style.display = 'block';
}

function highlightReplay (href) {
	if(href.indexOf('#') != -1) {
		href = href.substring(1, href.length);
		var e = $(href); var className = 'activeReplay';
		if(e)	{	
			var rows = $("replays").getElementsByTagName('tr');
			for (var i=0; i < rows.length; i++) {
				rows[i].className = rows[i].className.replace(className, ' ');
			}
			e.className += (e.className ? ' ' : '') + className;
		}
	}
}

function downloadVideo(videoPath){
	var appVer = navigator.platform.toLowerCase();
	videoPath = "/wwi08/_videos/" + videoPath;
	if (appVer.indexOf("mac") != -1){
		videoPath += ".dmg"
	} else {
		videoPath += ".exe"
	}
	window.location.href = videoPath;
}