// JavaScript Document

var count = 0;
var timeDelay = 4000;
var newsItems = null;
var newsCount = null;

function setupTicker() {
	newsItems = document.getElementById("news-items").getElementsByTagName("div");
	newsCount = newsItems.length;
	interval = window.setInterval("updateTicker()", timeDelay)
	updateTicker();
}

function updateTicker() {
	var newsItem = newsItems[count].getElementsByTagName("span");
	document.getElementById("news-title").innerHTML = "<span>" + newsItem[0].innerHTML + "</span>";
	document.getElementById("news-description").innerHTML = "<span>" + newsItem[1].innerHTML + "</span>";
	count++;
	if(count >= newsCount) { count = 0; }
}

function toggleMenu(node) {
	
	var lNodes = node.parentNode.parentNode.childNodes;
	
	for (var i = 0; i < lNodes.length; i++) {
		if(lNodes[i].className == 'expand') { lNodes[i].className = 'collapse'; }
	}
	
	switch (node.parentNode.className) {
		case 'expand':		node.parentNode.className = 'collapse'; break
		case 'collapse':	node.parentNode.className = 'expand'; break
	}
}

function wwefiximages (e) {
	if (document.getElementById) {
		var element = document.getElementById(e)
		
		var imgs = element.getElementsByTagName('img');
		for (i=0; i<imgs.length; i++) {
			imgs[i].className  = imgs[i].getAttribute('align');
		}
	}
}

function popup(url, width, height, resizable, location, statusbar, menubar, toolbar, scrollbars) {
	if (!width || !height) {
		// open a full browser window
		window.open(url);
	} else {
		// open a popup with options
		var day = new Date();
		var id = day.getTime();

		var params = '';
	
		if (!width) width = 0;
		if (!height) height = 0;
		if (!resizable) resizable = 0;
		if (!location) location = 0;
		if (!statusbar) statusbar = 0;
		if (!menubar) menubar = 0;
		if (!toolbar) toolbar = 0;
		if (!scrollbars) scrollbars = 0;
		
		if (height >= (window.screen.availHeight - 40)) {
			height = window.screen.availHeight - 40;
			width = width + 20;
			scrollbars = 1;
		}
		
		if (width >= (window.screen.availWidth - 20)) {
			width = window.screen.availWidth - 20;
			scrollbars = 1;
		}
		
		params += 'width=' + width + ',';
		params += 'height=' + height + ',';
		params += 'resizable=' + resizable + ',';
		params += 'location=' + location + ',';
		params += 'statusbar=' + statusbar + ',';
		params += 'menubar=' + menubar + ',';
		params += 'toolbar=' + toolbar + ',';
		params += 'scrollbars=' + scrollbars + ',';
		
		window.open(url, id, params);
	}
}

function wwevideoplayer(path, type, parent) {
	var show = 'wwe';
	if (typeof(type) == 'undefined') type = 'general';
	if (typeof(parent) == 'undefined') parent = '';
	
	if (window.location.pathname.indexOf('shows/raw') >= 0) {
		show = 'raw';
	} else if (window.location.pathname.indexOf('shows/smackdown') >= 0) {
		show = 'smackdown';
	} else if (window.location.pathname.indexOf('shows/247') >= 0) {
		show = '247';
	} else if (window.location.pathname.indexOf('community/bytethis') >= 0) {
		show = 'bytethis';
	}

	switch(type) {
   		case 'general':
   			popup(path + '?show=' + show, 700, 515);
   			break
   		case 'jukebox':
   			popup(path + "?view=jukebox&parent=" + parent, 650, 530);
   			break
   		default:
   			break
	}
}

function wwecookie (document, name, hours, path, domain, secure) {
	this.$document = document;
	this.$name = name;
	if (hours) {
		this.$expiration = new Date((new Date()).getTime( ) + hours*3600000);
	} else {
		this.$expiration = null;
	}
	if (path) this.$path = path; else this.$path = null;
	if (domain) this.$domain = domain; else this.$domain = null;
	if (secure) this.$secure = true; else this.$secure = false;
}

wwecookie.prototype.store = function () {
	var cookieval = "";
	for(var prop in this) {
		if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function') || (prop == '')) continue;
		if (cookieval != "") cookieval += '&';
		cookieval += prop + ':' + escape(this[prop]);
	}

	var cookie = this.$name + '=' + cookieval;
	if (this.$expiration) cookie += '; expires=' + this.$expiration.toGMTString();
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    if (this.$secure) cookie += '; secure';

    this.$document.cookie = cookie;
}

wwecookie.prototype.load = function() {
	var allcookies = this.$document.cookie;
	if (allcookies == "") return false;

	var start = allcookies.indexOf(this.$name + '=');
	if (start == -1) return false;
	start += this.$name.length + 1;
	var end = allcookies.indexOf(';', start);
	if (end == -1) end = allcookies.length;
	var cookieval = allcookies.substring(start, end);

	var a = cookieval.split('&');
	for(var i=0; i < a.length; i++) {
		a[i] = a[i].split(':');
	}

	for(var i = 0; i < a.length; i++) {
		this[a[i][0]] = unescape(a[i][1]);
	}

	return true;
}

wwecookie.prototype.remove = function() {
	var cookie;
	cookie = this.$name + '=';
	if (this.$path) cookie += '; path=' + this.$path;
	if (this.$domain) cookie += '; domain=' + this.$domain;
	cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

	this.$document.cookie = cookie;
}

// Setup Global Cookie
var host;
if (window.location.host.toString() != 'localhost') host = '.wwe.com';
var visitordata = new wwecookie(document, ".visitor", 24*356, '/', host);
if (!visitordata.load()) visitordata.store();

function countrySelect(country) {
		//set country cookie
		if (countrydata == null) {
			var countrydata = new wwecookie(document, ".country", 24*356, '/', host);
		}
		countrydata.country = country;
		countrydata.store();
		document.location.href='?country='+country;
}