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;
}

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) {
	if (typeof(type) == 'undefined') type = 'general';
	if (typeof(parent) == 'undefined') parent = '';
	
	var section = '?section=' + encodeURIComponent(window.location.pathname);
		
	switch(type) {
   		case 'general':
			popup(path + section, 700, 515);
			break
   		case 'jukebox':
			popup(path + "?view=jukebox&parent=" + parent, 650, 530);
			break
   		default:
   			break
	}
}

function wwefixcontent (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');
		}
		
		if (window.location.pathname.indexOf('shows/wwe247') >= 0) return;
		
		var tables = element.getElementsByTagName('table');
		for (i=0; i<tables.length; i++) {
			var rows = tables[i].getElementsByTagName('tr');
			for (n=0; n<rows.length; n++) {
				rows[n].className = (n % 2)? "odd": "even";
			}
		}
	}
}

function wwefixvideos (e) {
	if (document.getElementById) {
		var hrefs = document.getElementById(e).getElementsByTagName('a');

		for (i=0; i<hrefs.length; i++) {
			if (hrefs[i].protocol == 'javascript:') continue;
			if (hrefs[i].pathname.indexOf('content/media/video/webcast') >= 0) continue;
			
			if (hrefs[i].pathname.indexOf('content/media/video') >= 0) {
				hrefs[i].onclick = function () {
							var pattern = /jukebox/;
							if (pattern.test(this.href)) {
								wwevideoplayer(this.href, 'jukebox');
							} else {
								wwevideoplayer(this.href, 'general');
							}

							return false;
						};
			}
			if (hrefs[i].pathname.indexOf('content/media/wallpapers') >= 0) {
				hrefs[i].onclick = function () {
					popup(this.href, 700, 550);
					return false;
				};
			}
			
		}
	}
}

function insertBodyAd(element, size) {
	var lParaDepth = 5;
	var lContainer = document.getElementById(element);
	var lAd = document.getElementById(size + 'body-ad');
	var lArticle = document.getElementById('article-body');	
	var lParaCount = lArticle.getElementsByTagName('p').length;
	if (lParaCount < lParaDepth) { lParaDepth = lParaCount; }
	var lPara = lArticle.getElementsByTagName('p')[lParaDepth-1];
	
	lContainer.removeChild(lAd);
	lPara.appendChild(lAd);
	lAd.className = 'show';
}

var currentYear = null;
function showTable(year,tableName) {
	document.getElementById(tableName+"-"+currentYear).className = tableName+"-table hide";
	document.getElementById(tableName+"-"+year).className = tableName+"-table show";
	document.getElementById(tableName+"-menu-"+currentYear).className = "";
	document.getElementById(tableName+"-menu-"+year).className = "selected";
	currentYear = year;
}


// 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();


// Wallpaper size detection code
function getWallpaperLink(linkURL) {

	var smallWidth = 800;
	var mediumWidth = 1024;
	var largeWidth = 1280;
	var screenWidth = window.screen.width;
	var currentSize = null;
	var linkString = null;

	if(screenWidth >= largeWidth) {	currentSize = largeWidth; }
	else if(screenWidth >= mediumWidth) { currentSize = mediumWidth; }
	else { currentSize = smallWidth; }
	
	linkString = '<a href="javascript:popup(\'' + linkURL + '?size=' + currentSize + '\',700,550)">Download wallpaper</a>';
	
	return linkString;
}




//Legacy Javascript Functions Below.

// Old function use popupvideo() instead
function popVideo(aShow, aPath, aTitle, aSpeed) {
	popupvideo(aShow, aPath, aTitle, aSpeed);
}

// opens the wwe video player
function popupvideo (show, path, title, speed) {
	var url ="/common/video/popup_video.jsp?file=http://media.wwe.com/" + path + speed + ".wvx" + "&title=" + title + "&show=" + show + "&speed=" + speed + '&section=' + encodeURIComponent(window.location.pathname);
	popup(url,700,515);
}

// opens the wwe media player with automatic wvx
function popwwevideo1 (advert, wmv, title, show, speed) {
	var url ="/common/video/pop_wwevideo.jsp?advert=" + advert +  "&wmv=" + wmv + "&title=" + title + "&show=" + show + "&speed=" + speed + '&section=' + encodeURIComponent(window.location.pathname);
	popup(url,700,515);
}

// opens the wwe jukebox player
function popwwejukebox (date, path) {
	return popupjukebox (date, path);
}

function popupjukebox (date, path) {
	switch (date) {
		case '200501':
			window.location.replace('/subscriptions/247online/mostrequested/');
			break;
		case '200411':
			window.location.replace('/subscriptions/247online/shawnmichaels/');
			break;
		case '200410':
			window.location.replace('/subscriptions/247online/hulkhogan/');
			break;
		case '042005':
			window.location.replace('/subscriptions/247online/divas/');
			break;
		case '032005':
			window.location.replace('/subscriptions/247online/wrestlemania/');
			break;
		case '022005':
			window.location.replace('/subscriptions/247online/february/');
			break;
		default:
			break;
	}
}

// opens the wwe photo gallery
function popupgallery (url) {
	popup(url,351,438);
}


/************* Start Splash Bubble Code ********************/

var max = 0;
var timeout = null;
var splashDivArray = null;
var previousIndex = 0;
var nextIndex = 0;
var currentDOMObject;
var nextDOMObject;
var firstTime=true;
var insertionIndex = 0;
var currentDiv = null;
var nextDiv = null;
var debug = false;
var disabled = false;
var firstMove = true;
var isPlaying = false;
var pauseButton = null;
var playButton = null;
var buttonsvisible = false;

/************* Player Controls ********************/

function wweplay(){
	if(disabled){
		if(debug){
			alert('play called on disabled player');
		}
		return;
	}

	if(firstTime){
		init();
	}else{
		if(isPlaying){
			progressIndices();
		}
		
		resetDivs();
		showNextDiv();
	}
	
	divTimeout();
	
	if(!isPlaying){
		togglePausePlay();
	}
	if(!buttonsvisible){
		wweshowButtons();
	}
}

function wweshowButtons(){
	var buttons = document.getElementById("buttons1");
	buttons.style.display="inline";
	buttonsvisible = true;
	
}

function wwepause(){
	if(disabled){
		if(debug){
			alert('pause called on disabled player');
		}
	return;
	}
	
	if(timeout != null){
		window.clearTimeout(timeout);
		timeout = null;
		firstMove=true;
		if(isPlaying){
			togglePausePlay();
		}
	}
}

function wweback(){
	if(disabled){
		if(debug){
			alert('back called on disabled player');
		}
		return;
	}

	wwepause();
	regressIndices();
	resetDivs();
	showNextDiv();
}

function wweforward(){
	if(disabled){
		if(debug){
			alert('forward called on disabled player');
		}
		return;
	}

	wwepause();
	progressIndices();
	resetDivs();
	showNextDiv();

}

function init(){
	firstTime=false;
	resetDivs();
	getPausePlayButtons();
}

/**************************** Splash Helper Functions ***************************/
function addSplashDiv(splashDiv){
	splashDivArray[insertionIndex] = splashDiv;
	insertionIndex++;
}

function getPausePlayButtons(){
	pauseButton = document.getElementById("pause");
	playButton = document.getElementById("play");
}

function togglePausePlay(){
	if(!isPlaying){
		pauseButton.style.display="inline";
		playButton.style.display="none";
		isPlaying = true;
	}else{
		playButton.style.display="inline";
		pauseButton.style.display="none";
		isPlaying = false;
	}
}

function progressIndices(){
	previousIndex = nextIndex;
	if((previousIndex + 1) < max){
		nextIndex = previousIndex + 1;
	}else{
		nextIndex = 0;
	}
}

function resetDivs(){
	nextDiv = splashDivArray[nextIndex];
	previousDiv = splashDivArray[previousIndex];
}
  
function divTimeout(){
	var delay = nextDiv.delay;
	if(isNaN(delay)){
		if(debug){
			alert('delay for timeout is not a number');
		}
		disable();
		return;
	}

	timeout = window.setTimeout("wweplay()",delay);
}

function regressIndices(){
	previousIndex = nextIndex;

	if((previousIndex - 1) >= 0){
		nextIndex = previousIndex - 1;
	}else{
		nextIndex = max -1;
	}
}

function showNextDiv(){
	var previousDivSortOrder = previousDiv.sortOrder;
	var nextDivSortOrder = nextDiv.sortOrder;
  	
	if(isNaN(previousDivSortOrder) || isNaN(nextDivSortOrder)){
		if(debug){
			alert('sort order returned by div is not a number');
		}
		disable();
		return;
  	}
  	
  	previousDOMObject = document.getElementById("bubble_left_" + previousDivSortOrder);
  	nextDOMObject = document.getElementById("bubble_left_" + nextDivSortOrder);
  	
  	if(previousDOMObject == null || nextDOMObject == null){
  		if(debug){
  			alert('unable to retrieve one of the DOM Objects');
  		}
  		disable();
  		return;
  	}
    
    previousDOMObject.className = "left offscreen";
    nextDOMObject.className = "left";
}

function disable(){
	disabled = true;
	if(timeout != null){
		window.clearTimeout(timeout);
	}
}

/************************* Helper Object Encapsulates properties for each Div*******************/

function splashDiv(delay,sortOrder){
	var defaultDelay = 3;
	if(delay == "-1"){
		delay = defaultDelay;
	}
	
	this.delay = delay * 1000;
	this.sortOrder = sortOrder;
}

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;
}