//Initialize the global DefaultButtonClientId that can be set in all forms
var g_DefaultButtonClientId = null;

function getFieldObject(pstrControlID)
//System only uses a single form therefore the following can be used to work cross browser
{
	return eval("document.forms[0]." + pstrControlID)
}

function openWindowOnTop(url, windowName)
{
    var windowSettings = "location=1,height=550,width=950,resizable=1,scrollbars=1";
    var newwindow = null;    

    if(!windowName)
        windowName = "windowName";

    if(window.focus && newwindow)
        newwindow.focus();    

    //Open the window named windowName to a blank page.  This is done to clear any page that may have been previously loaded in this popup window.
    newwindow = window.open("", windowName, windowSettings);
    newwindow = window.open(url, windowName, windowSettings);
    
    if(window.focus && newwindow)
        newwindow.focus();    
}

function validateControlIsBlank(pstrControlID, pstrControlName)
//If the control passed has a blank value, 
{
	var lobjControl = getFieldObject(pstrControlID);
	var	returnValue = false;

	if(lobjControl != null)
	{
		if (lobjControl.value == "")
			{
				alert(pstrControlName + " cannot be blank.");
				lobjControl.focus();
				returnValue = true;
			}
	}
	
	return returnValue;
}

function notYetImplemented(pobjLink)
{
	alert("Functionality for: '" + getInnerText(pobjLink) + "' is not yet implemented.  \n\nPlease try again later.")
	
	return false;
}

function setControlClass(pobjControl, pstrClassName, pboolAllowChange)
{
	if (pboolAllowChange == null)
		pboolAllowChange = true;

	if (pboolAllowChange)
		pobjControl.className = pstrClassName;
}

function swapControlClass(controlID1, controlID2)
{
  var element1 = document.getElementById(controlID1);
  var element2 = document.getElementById(controlID2);
  var element1Css = element1.className;

  element1.className = element2.className;
  element2.className = element1Css;
}

function redirectBrowser(pstrPath)
{
	document.location = pstrPath;
}

function failValidation(source, arguments)
{
    arguments.IsValid = false;
}

function getRefToDiv(divID,oDoc) {
    if( document.getElementById ) return document.getElementById(divID);
    if( document.all ) return document.all[divID];
    if( !oDoc ) oDoc = document;
    if( document.layers ) {
        if( oDoc.layers[divID] ) return oDoc.layers[divID];
        else {
            //repeatedly run through all child layers, on success return that layer
            for( var x = 0, y; !y && x < oDoc.layers.length; x++ )
                y = getRefToDiv(divID,oDoc.layers[x].document);
            return y; 
        }
    }
    return false;
}

//**********************************
// TELERIK control custom javascript

function expandEndRadPane(splitter)
{
    var endPane = splitter.GetEndPane();

    if (endPane.IsCollapsed()) 
        endPane.Expand(RadSplitterNamespace.RAD_SPLITTER_DIRECTION.Backward);
}

function toggleCollapseRadPane(splitter, radPaneID)
{
    var pane = splitter.GetPaneById(radPaneID);

    if (!pane) return;

    if (pane.IsCollapsed()) 
        pane.Expand();
    else
        pane.Collapse();
}

//Firefox does not use the innertext property
function setInnerText(controlRef, textString)
{
    if(document.all)
        controlRef.innerText = textString;
    else
        controlRef.textContent = textString;
}

//Firefox does not use the innertext property
function getInnerText(controlRef)
{
    var innerText = null;
    
    if(document.all)
        innerText = controlRef.innerText;
    else
        innerText = controlRef.textContent;

    return innerText;
}

function waitCursor()
{
    document.body.style.cursor = 'wait';
}

function playVideo(asset) {
    var width = "240";
    var height = "195";
    var videoHtml = "";

    videoHtml += "<object classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' width='" + width + "' height='" + height + "' codebase='http://www.apple.com/qtactivex/qtplugin.cab'>";
    videoHtml += "<param name='src' value='" + asset + "' />";
    videoHtml += "<param name='controller' value='true' />";
    videoHtml += "<param name='autoplay' value='true' />";
    videoHtml += "<param name='enablejavascript' value='true' />";
    videoHtml += "<param name='scale' value='aspect' />";
    videoHtml += "<param name='kioskmode' value='true' />";
    videoHtml += "<param name='bgcolor' value='#FFFFFF' />";

    videoHtml += "<embed ";
    videoHtml += "src='" + asset + "'";
    videoHtml += "width='" + width + "'";
    videoHtml += "height='" + height + "'";
    videoHtml += "autoplay='true'";
    videoHtml += "scale='aspect'";
    videoHtml += "kioskmode='true'";
    videoHtml += "bgcolor='#FFFFFF'";
    videoHtml += "type='video/quicktime'";
    videoHtml += "pluginspage='http://www.learnalberta.ca/content/quicktimeinstaller_7.6.5.zip'";
    videoHtml += "enablejavascript='true'";
    videoHtml += "controller='true'";
    videoHtml += "></embed>";
    videoHtml += "</object>";

    document.getElementById("videoLayer").innerHTML = videoHtml;
}


function displayVideo(videoLayerID, image_url, video_url, scrubberVis, fullscreenVis, timeVis) {
	var video = document.createElement("video");
	
	if (typeof(video.canPlayType) != 'undefined' && video.canPlayType('video/mp4; codecs="avc1.42E01E"') == 'probably' && navigator.userAgent.toLowerCase().indexOf('chrome') == -1) {				// detect browsers with  <video> support and H.264 support
	   var overlay = document.createElement("img");
	   overlay.setAttribute("src", image_url);
	   overlay.setAttribute("height", document.getElementById("player2").clientHeight);
	   overlay.setAttribute("width", document.getElementById("player2").clientWidth);
	   overlay.addEventListener("click", function(e) { this.style.display = "none"; video.style.display="block"; video.play(); }, false);
		
	   video.setAttribute("src", video_url);
	   video.setAttribute("preload", "none");
	   video.setAttribute("controls", "");
	   video.setAttribute("height", document.getElementById("player2").clientHeight);
	   video.setAttribute("width", document.getElementById("player2").clientWidth);
	   video.style.display = "none";
	   
	   document.getElementById(videoLayerID).appendChild(overlay);
	   document.getElementById(videoLayerID).appendChild(video);
	}
	else {
		displayFlowPlayerVideo(videoLayerID, image_url, video_url, scrubberVis, fullscreenVis, timeVis);		// default to flash
	}
}

function displayFlowPlayerVideo(videoLayerID, image_url, video_url, scrubberVis, fullscreenVis, timeVis) {
    flowplayer(videoLayerID, { src: "content/flowplayer/flowplayer.commercial.swf", wmode: 'opaque' },
        {
            key: '#$0953d52fe2b15531039',
			debug: false,
            plugins: {
                controls: {
                    scrubber: scrubberVis,
                    fullscreen: fullscreenVis,
                    time: timeVis
                }
            },
            playlist: [
                    image_url,
		        {
		            url: video_url,
		            autoPlay: false,
		            // video will be buffered when splash screen is visible
		            autoBuffering: false
		        }
	        ]
        }
    );
}




function ShowMetadataCorrelationList(collapsiblePanelExtenderClientId, lomGuid, controlToPopulateClientId, imageLoadingControlClientId, interfaceLanguage, showCorrelationProgramLink) 
{
    PageMethods.ShowMetadataCorrelationList(collapsiblePanelExtenderClientId, lomGuid, controlToPopulateClientId, imageLoadingControlClientId, interfaceLanguage, showCorrelationProgramLink, OnShowMetadataCorrelationListSucceeded, OnShowMetadataCorrelationListFailed);
}

function OnShowMetadataCorrelationListSucceeded(resultClientCorrelationControlInfo, userContext, methodName) {
    if (resultClientCorrelationControlInfo) {
        if ($get(resultClientCorrelationControlInfo.ControlToPopulateClientId))
            $get(resultClientCorrelationControlInfo.ControlToPopulateClientId).innerHTML = resultClientCorrelationControlInfo.ControlHtml;

        if ($get(resultClientCorrelationControlInfo.ImageLoadingControlClientId))
            $get(resultClientCorrelationControlInfo.ImageLoadingControlClientId).className = "displayNone";

        if ($find(resultClientCorrelationControlInfo.CollapsiblePanelExtenderClientId)) {
            $find(resultClientCorrelationControlInfo.CollapsiblePanelExtenderClientId).expandPanel();
        }
    }
}

function OnShowMetadataCorrelationListFailed(error, userContext, methodName) 
{
    if(error)
        alert(error.get_message());
}

function showMoreDetailsCorrelations(shortDescriptionClientId, longDescriptionClientId, collapeExtenderClientId, lomGuid, controlToPopulateClientId, imageLoadingControlClientId, interfaceLanguage, showCorrelationProgramLink) {
    swapControlClass(shortDescriptionClientId, longDescriptionClientId);

    //If the more info is not yet loaded, load it once when the request it made
    if ($find(collapeExtenderClientId) && $get(imageLoadingControlClientId)) {
        if ($find(collapeExtenderClientId).get_Collapsed() && $get(imageLoadingControlClientId).className != "displayNone") {
            ShowMetadataCorrelationList(collapeExtenderClientId, lomGuid, controlToPopulateClientId, imageLoadingControlClientId, interfaceLanguage, showCorrelationProgramLink);
        }
        else {
            if ($find(collapeExtenderClientId).get_Collapsed())
                $find(collapeExtenderClientId).expandPanel();
            else
                $find(collapeExtenderClientId).collapsePanel();
        }
    }
}

///////////////////////////////////////////////////////////////////
// var containerId = 'CheckBoxList1.ClientID';
// iterateListControl(containerId);
///////////////////////////////////////////////////////////////////
function iterateListControl(containerId) {
    var containerRef = document.getElementById(containerId);
    var inputRefArray = containerRef.getElementsByTagName('input');

    for (var i = 0; i < inputRefArray.length; i++) {
        var inputRef = inputRefArray[i];

        if (inputRef.type.substr(0, 8) == 'checkbox') {
            if (inputRef.checked == true)
                alert('#' + i + ' (' + inputRef.id + ') is checked');
        }
    }
}
