/**
 * @fileoverview:   HSN.TV javascript helper file
 * @author:         Julia Yu
 * @company:        Schematic
**/


/** ------------------------------------------------------
 *	silverlight error helper
 */
function onSilverlightError(sender, args) {

    var appSource = "";
    if (sender != null && sender != 0) {
        appSource = sender.getHost().Source;
    } 
    var errorType = args.ErrorType;
    var iErrorCode = args.ErrorCode;
    
    var errMsg = "Unhandled Error in Silverlight 3 Application " +  appSource + "\n" ;

    errMsg += "Code: "+ iErrorCode + "    \n";
    errMsg += "Category: " + errorType + "       \n";
    errMsg += "Message: " + args.ErrorMessage + "     \n";

    if (errorType == "ParserError") {
        errMsg += "File: " + args.xamlFile + "     \n";
        errMsg += "Line: " + args.lineNumber + "     \n";
        errMsg += "Position: " + args.charPosition + "     \n";
    }
    else if (errorType == "RuntimeError") {
        if (args.lineNumber != 0) {
            errMsg += "Line: " + args.lineNumber + "     \n";
            errMsg += "Position: " + args.charPosition + "     \n";
        }
        errMsg += "MethodName: " + args.methodName + "     \n";
    }
    else if (errorType == "ImageError" && iErrorCode == 4001) {
        return;
    }

    throw new Error(errMsg);
}

/** ------------------------------------------------------
 *	download button clicked, show the next message
 */
function downloadCicked(){
	var content = document.getById("promptContent");
	var clickedContent = document.getById("promptClicked");
	if (content && clickedContent) {
		document.getById("promptContent").setStyle("display", "none");
		document.getById("promptClicked").setStyle("display", "block");
	}
}

/** ------------------------------------------------------
*	update progress for the splash screen
*/
var isSizeCalculated = 0;
function onSourceDownloadProgressChanged(sender, eventArgs) {
    if (isSizeCalculated == 0) {
        isSizeCalculated = 1;
        var winW = 40, winH = 40;

        if (parseInt(navigator.appVersion) > 3) {
            if (navigator.appName == "Netscape") {
                winW = window.innerWidth;
                winH = window.innerHeight;
            }

            if (navigator.appName.indexOf("Microsoft") != -1) {
                winW = document.body.offsetWidth;
                winH = document.body.offsetHeight;
            }
        }

        //var parentCanvas = sender.get_element().content.findName("parentCanvas");
        var parentCanvas = sender.findName("parentCanvas");
        parentCanvas.Width = winW;
        parentCanvas.Height = winH;

        //var contentCanvas = sender.get_element().content.findName("contentCanvas");
        var contentCanvas = sender.findName("contentCanvas");
        contentCanvas.setValue("Canvas.Top", (winH - contentCanvas.Height) / 2);
        contentCanvas.setValue("Canvas.Left", (winW - contentCanvas.Width) / 2);
    }

    sender.findName("LoadingPecentage").Text = "Loading: " + Math.round((eventArgs.progress * 1000)) / 10 + "%";
    sender.findName("ClipRect")["Rect"] = "-142,-142,142," + eventArgs.progress * 142;
}

/** ------------------------------------------------------
 *	embed the object and determine what os user is using
 */
function embedPlugin(){
    var available = Silverlight.isInstalled("3.0");

	var host = document.getById("silverlightControlHost");
	if (!host) {
		return false;
	}

	if (available) {
	    document.getElementById('silverlightInstallHost').style.display = 'none';
	    document.getElementById('silverlightControlHost').style.display = 'block'; 
		
		var objectHTML = '<object data="data:application/x-silverlight," type="application/x-silverlight-2" width="100%" height="100%">';
				objectHTML +=	'<param name="source" value="ClientBin/Schematic.HSN.tv.VideoPlayer.xap"/>';
				objectHTML +=	'<param name="onerror" value="onSilverlightError" />';
				objectHTML +=	'<param name="background" value="white" />';
				objectHTML +=	'<param name="minRuntimeVersion" value="2.0.31005.0" />';
				objectHTML +=   '<param name="autoUpgrade" value="true" />';
				objectHTML +=   '<param name="splashscreensource" value="SplashScreen.xaml"/>';
				objectHTML +=   '<param name="Windowless" value="true" />';
				objectHTML +=   '<param name="enableGPUAcceleration" value="true" />';
				objectHTML +=   '<param name="onSourceDownloadProgressChanged" value="onSourceDownloadProgressChanged" />';
				objectHTML +=	'</object>';
					
		host.innerHTML = objectHTML;
		
		/** the microsoft way doesnt seem to work 
		*/
		// Silverlight.createObject(
		// 	            "Schematic.HSN.tv.VideoPlayer.xap",  // source
		// 	            host,  // parent element
		// 	            "slPlugin",  // id for generated object element
		// 	            {
		// 	                width: "100%", height: "100%", background: "white", 
		// 	                version:"2.0.31005.0"
		// 	            },
		// 	            { onError: onSLError, onLoad: onSLLoad },
		// 	            "param1=value1,param2=value2", 
		// 	            "context"    // context helper for onLoad handler.
		// 	        );

	} else {
	    document.getElementById('silverlightInstallHost').style.display = 'block';
	    document.getElementById('silverlightControlHost').style.display = 'none'; 
	
		var osType = document.getById("osType");
		if (navigator.platform.indexOf("Win")!=-1) {
			osType.innerHTML = "Windows";
		}
		if (navigator.platform.indexOf("MacIntel")!=-1)  {
			osType.innerHTML = "Mac";
		}

		var clickedInstall = document.getById("installBtn");
		if (clickedInstall) {
			clickedInstall.addEventListener("click", downloadCicked, false)
		}
		
	}
}

/** ------------------------------------------------------
 *	Page load 
 */
window.addEventListener("DOMContentLoaded", embedPlugin, false);
