var gPopupMask = null;
var gPopupContainer = null;
var gPopFrame = null;
var gReturnVal = null;
var gTabIndexes = new Array();

// Pre-defined list of tags we want to disable/enable tabbing into
var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");	

// If using Mozilla or Firefox, use Tab-key trap.
if (!document.all) {
	document.onkeypress = keyDownHandler;
}

var videotemplate = "<object data='data:application/x-silverlight-2,' type='application/x-silverlight-2' width='100%' height='100%'>" +
                        "<param name='source' value='video/VideoPlayer.xap'/>" +
                        "<param name='initParams' value='%initParam%'/>" +
                        "<param name='splashScreenSource' value='video/SplashScreen.xaml' />" +
                        "<param name='onSourceDownloadProgressChanged' value='onSourceDownloadProgressChanged' />" +
                        "<param name='onerror' value='onSilverlightError' />" +
                        "<param name='minRuntimeVersion' value='3.0.40624.0' />" +
                        "<param name='autoUpgrade' value='true' />" +
                        "<param name='windowless' value='true' />" +
                        "<param name='background' value='#00ffffff' />" +
                        "<a href='http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0' style='text-decoration: none;'>" +
	                        "<img src='http://go.microsoft.com/fwlink/?LinkId=108181' alt='Get Microsoft Silverlight' style='border-style: none'/>" +
                        "</a>" +
                        "</object>";

function LoadVideo(csID, csDomain) {
    var initParam = "audio=false,caseStudyId=" + csID + ",caseStudyDomain=" + csDomain + ",divid=popupFrame";
    showPopWin(videotemplate.replace(/%initParam%/, initParam), 900, 500);
}

function LoadAudio(csID, csDomain) {
    var initParam = "audio=true,caseStudyId=" + csID + ",caseStudyDomain=" + csDomain + ",divid=popupFrame";
    showPopWin(videotemplate.replace(/%initParam%/, initParam), 900, 500);
}

function initPopUp() {
	// Add the HTML to the body
	theBody = document.getElementsByTagName('BODY')[0];
	popmask = document.createElement('div');
	popmask.id = 'popupMask';
	
	popcont = document.createElement('div');
	popcont.id = 'popupContainer';
	popcont.innerHTML = '' +
		'<div id="popupFrame">' +						
		'</div>';
	
	theBody.appendChild(popmask);
	theBody.appendChild(popcont);
	
	gPopupMask = document.getElementById("popupMask");
	gPopupContainer = document.getElementById("popupContainer");
	gPopFrame = document.getElementById("popupFrame");		
}

function showPopWin(contents, width, height) {
    
	gPopupMask.style.display = "block";
	$('#popupContainer').fadeIn(200);
	
	// calculate where to place the window on screen
	centerPopWin(width, height);
	gPopupContainer.style.width = width + "px";
	gPopupContainer.style.height = height + "px";	
	setMaskSize();

	gPopFrame.style.width = width + "px";
	gPopFrame.style.height = height + "px";
	
	// set the contents
	gPopFrame.innerHTML = contents;		
}

var gi = 0;
function centerPopWin(width, height) {
    if (gPopupContainer != null) {
        if (width == null || isNaN(width)) {
            width = gPopupContainer.offsetWidth;
        }
        if (height == null) {
            height = gPopupContainer.offsetHeight;
        }

        var theBody = document.getElementsByTagName("BODY")[0];

        var scTop = parseInt(getScrollTop(), 10);
        var scLeft = parseInt(theBody.scrollLeft, 10);

        setMaskSize();

        var fullHeight = getViewportHeight();
        var fullWidth = getViewportWidth();

        gPopupContainer.style.top = (scTop + ((fullHeight - (height)) / 2)) + "px";
        gPopupContainer.style.left = (scLeft + ((fullWidth - width) / 2)) + "px";
    }
}

addEvent(window, "resize", centerPopWin);
addEvent(window, "scroll", centerPopWin);
window.onscroll = centerPopWin;

function setMaskSize() {
	var theBody = document.getElementsByTagName("BODY")[0];
			
	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();
	
	// Determine what's bigger, scrollHeight or fullHeight / width
	if (fullHeight > theBody.scrollHeight) {
		popHeight = fullHeight;
	} else {
		popHeight = theBody.scrollHeight;
	}
	
	if (fullWidth > theBody.scrollWidth) {
		popWidth = fullWidth;
	} else {
		popWidth = theBody.scrollWidth;
	}
	
	gPopupMask.style.height = popHeight + "px";
	gPopupMask.style.width = popWidth + "px";
}

function hidePopWin() {
	
	var theBody = document.getElementsByTagName("BODY")[0];
	theBody.style.overflow = "";
	
	if (gPopupMask != null) {
	    gPopupMask.style.display = "none";
	}

	$('#popupContainer').fadeOut(200);
}

function HideBox(o) {    
    hidePopWin();    
}

function ErrorNotNull(e, o) {
    alert('There was an error loading your video.');
    HideBox(o);
}

function keyDownHandler(e) {
    if (typeof (gPopupIsShown) != 'undefined') {
        if (gPopupIsShown && e.keyCode == 9)  return false;
    }
}