/*
 * gaming.js
 *
 * This JavaScript file contains functionality specific to Gaming including:
 * 1) Gaming Online Service outage message handling
 * 2) Open new window functionality, used for opening PDFs and external websites
 * 3) Open video window functionality, used for opening Gaming videos
 *
 */
 
//========================================================================
/* 1. Start: Gaming Online Service outage notice handling
 *
 * The javascript in this section handles presentation of Gaming Online Service outage notices in response
 * to clicks on the 'Online Service' link on the left-hand navigation area of Gaming website pages.
 *
 * To enable the outage notice:
 * Change the javascript below (set gosOutageNoticeEnabled = true; comment or uncomment outage notice
 * Option A, B, or C accordingly; and add outage dates and times) and
 * then upload the revised file to production. Outage notices are usually posted
 * 3-5 days in advance and are taken down the day after the outage.
 *
 * To disable the outage notice:
 * Change the javascript below (set gosOutageNoticeEnabled = false; comment or uncomment outage notice
 * Option A, B, or C accordingly; and remove outage dates and times) and
 * then upload the revised file to production. Outage notices are usually posted
 * 3-5 days in advance and are taken down the day after the outage.
 *
 * References:
 * Related styles can be found in the '/2006_templates/style/gaming.css'.
 * Related code can be found in '/templates/2006_templates-2_col_gpeb.dwt'.
*/

// Set this variable to true if an outage notice is to be displayed, false otherwise.
var gosOutageNoticeEnabled = true;

// Initialize GOS outage notice for javascript enabled users by writing hidden divs to Gaming website pages.
function gosOutageInitialize(url) {
	if(gosOutageNoticeEnabled) {
		document.write('<div id="blanket" style="display: none;"></div>');
		document.write('<div id="popUpDiv" style="display: none;">');
		document.write('  <div id="popUpDivCloseBar">');
		document.write('    <a href="#" onclick="popup(\'popUpDiv\'); scroll(0,getScrollY()); return false;" title="Close.">Close [X]</a>');
		document.write('  </div>');
		document.write('  <div id="popUpDivHeader">');
		document.write('    <h2>Gaming Online Service Alert</h2>');
		document.write('  </div>');
		document.write('  <div id="popUpDivBody">');

		/* START OPTION A: Standard outage notice
		(this is the most commonly used outage notice - the system is down for the whole maintenance period) */
  	document.write('    <p>The Gaming Online Service will be unavailable, due to maintenance, at the following time(s):</p>');
		document.write('    <ul>');
		document.write('      <li>Wednesday, Nov 25/09<br>2:00pm to 5:30pm</li>');
		//document.write('      <li>DayOfWeek, Mmm DD/09<br>H:MMam to H:MMam</li>');
		document.write('    </ul>');
		/* END OPTION A */

		/* START OPTION B: Alternative standard outage notice
		(use this instead of Option A if the service might be up and down during the maintenance period)
  	document.write('    <p>Gaming Online Service users may experience service interruptions or complete system unavailability, due to maintenance, on:</p>');
		document.write('    <ul>');
		document.write('      <li>DayOfWeek, Mmm DD/09<br>H:MMam to H:MMam</li>');
		document.write('      <li>DayOfWeek, Mmm DD/09<br>H:MMam to H:MMam</li>');
		document.write('    </ul>');
  	document.write('    <p>Please do not use the Gaming Online Service during this maintenance period.</p>');
		END OPTION B */

		/* START OPTION C: Payment subsystem outage notice
		(use this for outages where the service is up, but the payment subsystem is down)
  	document.write('    <p>The electronic payment component of the Gaming Online Service will be unavailable, due to maintenance, on:</p>');
		document.write('    <ul>');
		document.write('      <li>DayOfWeek, Mmm DD/09<br>H:MMam to H:MMam</li>');
		document.write('      <li>DayOfWeek, Mmm DD/09<br>H:MMam to H:MMam</li>');
		document.write('    </ul>');
  	document.write('    <p>Gaming event licence applicants who wish to pay application fees with a credit card are advised not to apply during this maintenance period.</p>');
		END OPTION C */

		document.write('    <div id="popUpDivFooter">');
		document.write('      <a href="' + url + '" onclick="popup(\'popUpDiv\')" class="button" title="Continue to online service.">&nbsp;&nbsp;Continue to Online Service&nbsp;&nbsp;</a>');
		document.write('    </div>');
		document.write('  </div>');
		document.write('</div>');
	}
}

function getScrollY() {
    var y = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        // Netscape
        y = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        // DOM
        y = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        // IE6 standards compliant mode
        y = document.documentElement.scrollTop;
    }
    return y;
}

function toggle(div_id) {
	var el = document.getElementById(div_id);
	if ( el.style.display == 'none' ) {	el.style.display = 'block';}
	else {el.style.display = 'none';}
}

function blanket_size(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
	var verticalScrollValue = getScrollY();
	//popUpDiv_height = blanket_height/2-150;//150 is half popup's height
	//popUpDiv.style.top = popUpDiv_height + 'px';
	if (viewportheight > 300) {
		popUpDiv.style.top = (viewportheight * 0.5 - 150 + verticalScrollValue) + 'px';
	} else {
		popUpDiv.style.top = '0px';
	}
	//alert(document.body.clientHeight);
	//alert(window.innerHeight);
	//alert(document.documentElement.clientHeight);
}

function window_pos(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width=window_width/2-165; // Half of the popups width minus 20px for vertical scroll bar
	popUpDiv.style.left = window_width + 'px';
}

function popup(windowname) {
	blanket_size(windowname);
	window_pos(windowname);
	toggle('blanket');
	toggle(windowname);		
}

// 1. End: Gaming Online Service outage notice handling
//========================================================================


//========================================================================
/* 2. Start: openNewWindow(url) - Open external links or PDFs in a fixed size window positioned in the top left of screen
 *
 * This function opens the specified URL in a new browser window. The size and location of the window can be altered
 * by changing the code below. By specifying the size of the new browser window within this file, new windows will
 * open to a consistent size for any links that make use of this function.
 *
 * The number of child windows for each parent window is limited to one. When a user clicks on an external or PDF link
 * in the parent page a new window is opened. For any subsequent external or PDF link clicks from the parent page, where
 * the child window is still open, the page or PDF will be opened in the existing child window. This functionality keeps
 * new windows under control to limit confusion for the user.
 *
 * Note: "| 0" at the end of the var statements truncates the calculated values to integers.
 *
 * The syntax for calling this function in the in an html document is as follows:
 *
 * <a href="http://www.google.ca" target="_blank" onClick="openNewWindow(this.href); return false">Google Canada</a>
 */
 
function openNewWindow(url) {
	var newWindowWidth = 800;
	var newWindowHeight = (screen.availHeight * 0.6) | 0;
	var newWindowLeftPos = 0;
	var newWindowTopPos = 30;
	var newWindowArgs = "toolbar=yes, location=yes, directories=no, status=yes, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=yes";

	// For small screens use 100% window width.
	if(screen.availWidth < 780) {
		newWindowWidth = screen.availWidth;
	}

// Follow this branch if: A child window object (newWin) does not exist nor has one been created since the parent window was opened.		
	if(typeof(newWin) != "object") {
		newWindowArgs = newWindowArgs + ", width=" + newWindowWidth + ", height=" + newWindowHeight + ", left=" + newWindowLeftPos + ", top=" + newWindowTopPos;
		newWin = window.open(url, "_blank", newWindowArgs);
		/*alert(newWindowArgs); //shows values of 'newWindowArgs' for testing purposes*/
	}

	// Follow this branch if: A child window object (newWin) does exist or has been created since the parent window was opened.
	else {
		// Follow this sub-branch if: A child window is currently open.
		if(!newWin.closed) {
			newWin.location.href=url;
		}
		// Follow this sub-branch if: A child window is not currently open.
		else {
			newWindowArgs = newWindowArgs + ", width=" + newWindowWidth + ", height=" + newWindowHeight + ", left=" + newWindowLeftPos + ", top=" + newWindowTopPos;
			newWin = window.open(url, "_blank", newWindowArgs);
			/*alert(newWindowArgs); //shows values of 'newWindowArgs' for testing purposes*/
		}
	}
	newWin.focus();
}

// 2. End: openNewWindow
//========================================================================


//========================================================================
/* 3. Start: openVideoWindow(url) - Open external GPEB video content in a fixed size window positioned in the top left of screen
 *
 * This function opens the specified URL in a new browser window. The size and location of the window can be altered
 * by changing the code below. By specifying the size of the new browser window within this file, new windows will
 * open to a consistent size for any links that make use of this function.
 *
 * The number of child windows for each parent window is limited to one. When a user clicks on an external or PDF link
 * in the parent page a new window is opened. For any subsequent external or PDF link clicks from the parent page, where
 * the child window is still open, the page or PDF will be opened in the existing child window. This functionality keeps
 * new windows under control to limit confusion for the user.
 *
 * Note: "| 0" at the end of the var statements truncates the calculated values to integers.
 *
 * The syntax for calling this function in the in an html document is as follows:
 *
 * <a href="http://www.google.ca" target="_blank" onClick="openNewWindow(this.href); return false">Google Canada</a>
 */

function openVideoWindow(url) {
	var newWindowWidth = 614;
	var newWindowHeight = 585;
	var newWindowLeftPos = 0;
	var newWindowTopPos = 30;
	var newWindowArgs = "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=yes";

// Follow this branch if: A child window object (newWin) does not exist nor has one been created since the parent window was opened.		
	if(typeof(newWin) != "object") {
		newWindowArgs = newWindowArgs + ", width=" + newWindowWidth + ", height=" + newWindowHeight + ", left=" + newWindowLeftPos + ", top=" + newWindowTopPos;
		newWin = window.open(url, "_blank", newWindowArgs);
		/*alert(newWindowArgs); //shows values of 'newWindowArgs' for testing purposes*/
	}

	// Follow this branch if: A child window object (newWin) does exist or has been created since the parent window was opened.
	else {
		// Follow this sub-branch if: A child window is currently open.
		if(!newWin.closed) {
			newWin.location.href=url;
		}
		// Follow this sub-branch if: A child window is not currently open.
		else {
			newWindowArgs = newWindowArgs + ", width=" + newWindowWidth + ", height=" + newWindowHeight + ", left=" + newWindowLeftPos + ", top=" + newWindowTopPos;
			newWin = window.open(url, "_blank", newWindowArgs);
			/*alert(newWindowArgs); //shows values of 'newWindowArgs' for testing purposes*/
		}
	}
	newWin.focus();
}

// 3. End: openVideoWindow
//========================================================================


