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 showBlock(e) {
	setDisplay(e, 'block');
}
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 overflow(e) {
	return $(e).style.height != 'auto';
}
function setHeight(e, height) {
	$(e).style.height = height;
}
function auto(e) {
	setHeight(e, 'auto');
}
function notauto(e) {
	setHeight(e, '');
}
function toggleContent(e, f, anchor, collapsed, expanded) {
	if(visible(e)) {
		hide(e);
		show(f);
		anchor.innerHTML = collapsed;
	} else {
		show(e);
		hide(f);
		anchor.innerHTML = expanded;
	}
}
function toggleOverflow(e, anchor, collapsed, expanded) {
	if(overflow(e)) {
		auto(e);
		anchor.innerHTML = collapsed;
	} else {
		notauto(e);
		anchor.innerHTML = expanded;
	}
}
function swap(e,f) {
	hide(e);
	showBlock(f);
}
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(id, type, data, width, height, params, fallbackContent, createElementFunc) {
	var createElementFunc = createElementFunc || createElement;
	var obj = createElementFunc('object', {
		'type': type,
		'data': data,
		'width': width,
		'height': height
	});
	if(id)
		obj.setAttribute('id', id);
	if(params)
		for(var i = 0, pair; pair = params[i]; i++)
			obj.appendChild(createElementFunc('param', {
				'name': pair[0],
				'value': pair[1]
			}));
	if(fallbackContent)
		obj.appendChild(fallbackContent);
	return obj;
}

function createElementStr(name, attrs) {
	return new NodeStr(name, attrs);
}

// used for DOM-like creation of object elements in IE
var NodeStr = function(name, attrs) {
	this.name = name;
	if(attrs)
		this.attrs = attrs;
	else
		this.attrs = {};
	this.childNodes = [];
}
NodeStr.prototype = {
	appendChild : function(node) {
		this.childNodes.push(node);
		return node;
	},
	setAttribute : function(name, value) {
		this.attrs[name] = value;
	},
	toString : function() {
		var str = '<' + this.name;
		if(this.attrs)
			for(attr in this.attrs)
				str += ' ' + attr + '="' + this.attrs[attr] + '"';
		str += '>';
		for(child in this.childNodes)
			str += this.childNodes[child];

		return str + '</' + this.name + '>';
	}
}

function setFlash(target, id, data, width, height, params, fallback, containsAgeGate) {
	var FLASH_MEDIA_TYPE = 'application/x-shockwave-flash';
	var targetNode = $(target);

	if(containsAgeGate)
		AgeGate.addListenerId(id);

	// DOM manipulation and styling of OBJECT tags is not available in IE
	if(Browser.ie) {
		if(typeof fallback != 'string')
			fallback = fallback.innerHTML;
		targetNode.innerHTML = createObject(id, FLASH_MEDIA_TYPE, data, width, height, params, fallback, createElementStr);
	} else {
		if(typeof fallback == 'string')
			fallback = document.createTextNode(fallback);
		removeChildren(targetNode);
		targetNode.appendChild(createObject(id, FLASH_MEDIA_TYPE, data, width, height, params, fallback));
	}
	return targetNode.firstChild;
}

function loadScript(src, id) {
	var head = document.getElementsByTagName('head')[0];
	var script = createElement('script', {
		'type': 'text/javascript',
		'src': src
	});
	if(id) {
		var old = document.getElementById(id);
		if(old)
			old.parentNode.removeChild(old);
		script.id = id;
	}
	head.appendChild(script);
}

function jsonCallback(result) {
	// ageAppropriate is global
	var tmp = result.ageAppropriate;

	if(tmp == null)
		ageAppropriate = null;
	else if(typeof tmp == 'string')
		ageAppropriate = tmp == "true";
	else
		ageAppropriate = !!tmp;
}

var AgeGate = {
	FORM_ID: 'ageGateForm',
	day: '',
	month: '',
	year: '',
	listenerIds: [],
	verify: function() {
		if(ageAppropriate == null)
			return null;
		return ageAppropriate;
	},
	setYear: function(v) {
		AgeGate._set('year', v);
	},
	setMonth: function(v) {
		AgeGate._set('month', v);
	},
	setDay: function(v) {
		AgeGate._set('day', v);
	},
	addListenerId: function(id) {
		AgeGate.listenerIds.push(id);
	},
	setCookies: function(cookies) {
		jsonCallback(cookies);
		for(index in AgeGate.listenerIds) {
			//alert('notifying listener: ' + AgeGate.listenerIds[index] + ', ageAppropriate = ' + ageAppropriate);
			$(AgeGate.listenerIds[index]).passedAgeGate(ageAppropriate);
		}
	},
	verifyRedirect: function(loc) {
		var validated = AgeGate.verify();
		if(validated)
			window.location = loc
		else if(validated == false)
			alert('not old enough');
		else
			alert('show age gate');
	},
	_set: function(name, value) {
		var form = $(AgeGate.FORM_ID);
		if(form) { // full site age gate
			form[name].value = value;
			if(form.year.value != '' &&
					form.month.value != '' &&
					form.day.value != '')
				form.submit();
		} else { // per asset age gate
			AgeGate[name] = value;
			if(AgeGate.year != '' && AgeGate.month != '' && AgeGate.day != '')
				loadScript(jsRoot + 'agegate.xml?callback=AgeGate.setCookies&year=' + AgeGate.year +
						'&month=' + AgeGate.month +	'&day=' + AgeGate.day, 'cookiejson');
		}
	}
}

// Flash GetURL Workaround for standard linkage on the main site (avoid Root)
function getFlashLink(flashurl,absolute) {
	var rellink = jsRoot;
	if (absolute == false) {
		rellink = "";
	}
	if (flashurl.substr(0,1) == "?") {
		window.location.href += flashurl;
	} else {
		window.location.href = rellink + flashurl;
	}
}
function loadVideo (data, mapname) {
	path = 'http://media.wow-europe.com/stream/regional_finals08/';
	var ctx = "/rf08/";
	setFlash('flash','flash-video', ctx + '_flash/video_loader.swf', 560, 342, [ 
	['movie', ctx + '_flash/video_loader.swf'],
	['base', ctx + '_flash/'],
	['allowFullScreen', 'true'],
	['wmode', 'transparent'],
	['salign', 't1'],
	['quality', 'best'],
	['flashvars', 'filename=' + path + '' + data + '&skipintro=true']
	], noflash);
	$('map-name').innerHTML = mapname;
}

