﻿if (!window.halo3_banner)
	halo3_banner = {};

halo3_banner.Page = function() 
{
}

halo3_banner.Page.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
		this.control = control;
		
		// Sample event hookup:	
		rootElement.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
	},
	
	// Sample event handler
	handleMouseDown: function(sender, eventArgs) 
	{
		// The following line of code shows how to find an element by name and call a method on it.
		// this.control.content.findName("Storyboard1").Begin();
	}
}
var root,gototalTime,backtotalTime;
xamlimg=new Array();
xamlimg.push("image2,image2.png");
xamlimg.push("believe,believe.png");
xamlimg.push("go_wmv,go.wmv");
xamlimg.push("back_wmv,back.wmv");

//解析秒處成為00:00:00
function setTimes(sec){
	getHour=Math.floor((sec/60)/60);
	nowsec=sec-((getHour*60)*60);
	getNum=Math.floor(nowsec/60);
	nowsec=nowsec-(getNum*60);
	nowsec=Math.floor(nowsec*10);
	nowsec=nowsec/10;
	
	if(getHour<10){
		getHour="0"+getHour;
	}
	if(getNum<10){
		getNum="0"+getNum;
	}
	if(nowsec<10){
		nowsec="0"+nowsec;
	}
	
	var dfTime=getHour+":"+getNum+":"+nowsec;
	return dfTime;
}

function onDownloadProgressChanged(sender, eventArgs)
{
    var percentage = Math.floor(sender.downloadProgress * 100);
    sender.findName("progressText").text = percentage + "%";
   sender.findName("progressRectangle").width = percentage * 2; 
}
//設定完成讀取之後檔案配置
function onCompleted(sender, eventArgs)
{
   sender.findName("loaderMov").Visibility="Collapsed";
    for(sourceNum=0;sourceNum<xamlimg.length;sourceNum++){
    	getValue=xamlimg[sourceNum]
    	theArray=getValue.split(",");
        var nowSetImage = sender.findName(theArray[0]);
        nowSetImage.setSource(sender, "source/"+theArray[1]);
    }
}
function media_openState(sender,e){
	sender.Stop();
	if(sender["Name"]=="go_wmv"){
		gototalTime=sender.NaturalDuration.Seconds;
	}
	if(sender["Name"]=="back_wmv"){
		backtotalTime=sender.NaturalDuration.Seconds;
	}
}
function page_mouseEnter(sender,e){
	if(root.findName("back_wmv").CurrentState!="Playing"){
		root.findName("go_wmv").Position="0:0:0";
		root.findName("go_wmv").Play();
		root.findName("back_hide").begin();
	}else{
		getbacktime=root.findName("back_wmv").Position.Seconds;
		thestarttime=gototalTime-getbacktime;
		root.findName("go_wmv").Position=setTimes(thestarttime);
		root.findName("go_wmv").Play();
		root.findName("back_hide").begin();
	}
	//root.findName("back_wmv").Position="0:0:0";
	root.findName("back_wmv").Pause();
}
function page_mouseleave(sender,e){
	if(root.findName("go_wmv").CurrentState!="Playing"){
		root.findName("back_wmv").Position="0:0:0";
		root.findName("back_wmv").Play();
		root.findName("back_show").begin();
	}else{
		getgotime=root.findName("go_wmv").Position.Seconds;
		thestarttime=backtotalTime-getgotime;
		root.findName("back_wmv").Position=setTimes(thestarttime);
		root.findName("back_wmv").Play();
		root.findName("back_show").begin();
	}
	//root.findName("go_wmv").Position="0:0:0";
	root.findName("go_wmv").Pause();
	root.findName("days")["Opacity"]=1;
	root.findName("believe")["Opacity"]=1;
	root.findName("daysAmi").Begin();
}
function media_backend(sender,e){
	root.findName("go_wmv").Position="0:0:0";
	root.findName("daysAmi").Stop();
	root.findName("days")["Opacity"]=0;
	root.findName("believe")["Opacity"]=0;
}
function media_goend(sender,e){
	root.findName("back_wmv").Position="0:0:0";
	root.findName("days")["Opacity"]=1;
	root.findName("believe")["Opacity"]=1;
	root.findName("daysAmi").Begin();
}
function getURL(sender,e){
	location.href="http://www.xbox.com/zh-TW/games/splash/h/halo3/";
}
function setXaml(sender,e){
	root=sender;
	root.findName("go_wmv").AddEventListener("MediaOpened",media_openState);
	root.findName("back_wmv").AddEventListener("MediaOpened",media_openState);
	root.findName("back_wmv").AddEventListener("MediaEnded",media_backend);
	root.findName("go_wmv").AddEventListener("MediaEnded",media_goend);
	root.findName("back_wmv")["Opacity"]=0;
	root.findName("days")["Opacity"]=0;
	root.findName("believe")["Opacity"]=0;
	root.AddEventListener("MouseEnter",page_mouseEnter);
	root.AddEventListener("MouseLeave",page_mouseleave);
	root.AddEventListener("MouseLeftButtonUp",getURL);
	root["Cursor"]="Hand";
	
	var slPlugin = sender.getHost();
	var downloader = slPlugin.createObject("downloader");
		
	downloader.addEventListener("downloadProgressChanged", onDownloadProgressChanged);
    downloader.addEventListener("completed", onCompleted);
         
    downloader.open("GET", "index_silverlight_banner/source.jpg");
    downloader.send();
}