
// make XAML from defineshow

var textList = null;
var collectedXAML = "";
var showAsHTML = false;
var bigImages = {};

function xaml_line(lc,oneLine) {
	if (showAsHTML) {
		var aBr = document.createElement("BR");
		textList.appendChild(aBr);
		var nText = document.createTextNode(lc+" "+oneLine);
		textList.appendChild(nText);
	}
	else collectedXAML += oneLine;
}

function image_clip(lc,pNode,ltwh) {
	xaml_line(lc,'<'+pNode+'.Clip>');
	xaml_line(lc+1,'<RectangleGeometry Rect="'+ltwh.join(",")+'"/>');
	xaml_line(lc,'</'+pNode+'.Clip>');
}

function image_scale(lc,pNode,scale) {
	xaml_line(lc,'<'+pNode+'.RenderTransform>');
	xaml_line(lc+1,'<ScaleTransform ScaleX="'+scale+'" ScaleY="'+scale+'"/>');
	xaml_line(lc,'</'+pNode+'.RenderTransform>');
}

function antimate_clip(lc,cName,pNode,ltwh) {
	xaml_line(lc,'<'+pNode+'.Clip>');
	xaml_line(lc+1,'<PathGeometry>');
	xaml_line(lc+2,'<PathFigure IsClosed="True" StartPoint="'
		+ltwh[0]+','+ltwh[1]+'" Name="'+cName+'PT">');
	xaml_line(lc+3,'<PathFigure.Segments>');
	xaml_line(lc+4,'<LineSegment Point="'+(ltwh[0]+ltwh[2])+','+ltwh[1]+'"/>');
	xaml_line(lc+4,'<LineSegment Point="'+(ltwh[0]+ltwh[2])+','+(ltwh[1]+ltwh[3])+'"/>');
	xaml_line(lc+4,'<LineSegment Point="'+(ltwh[0])+','+(ltwh[1]+ltwh[3])+'"/>');
	xaml_line(lc+3,'</PathFigure.Segments>');
	xaml_line(lc+2,'</PathFigure>');
	xaml_line(lc+1,'</PathGeometry>');
	xaml_line(lc,'</'+pNode+'.Clip>');
}

function antimate_matrix(lc,cName,pNode) {
	xaml_line(lc,'<'+pNode+'.RenderTransform>');
	xaml_line(lc+1,'<MatrixTransform>');
	xaml_line(lc+2,'<MatrixTransform.Matrix>');
	xaml_line(lc+3,'<Matrix Name="'+cName+'MX"/>');
	xaml_line(lc+2,'</MatrixTransform.Matrix>');
	xaml_line(lc+1,'</MatrixTransform>');
	xaml_line(lc,'</'+pNode+'.RenderTransform>');
}

function gradient_stop(lc,sOffset,sColor) {
	xaml_line(lc,'<GradientStop Color="'+sColor+'000000" Offset="'+sOffset+'"/>');
}

function mask_gradient(lc,cName,pNode,mInfo) {
	var mRadius = mInfo.radius;
	var mCenter = mInfo.center.join(",");
	var mStops = mInfo.stops;
	xaml_line(lc,'<'+pNode+'.OpacityMask>');
	xaml_line(lc+1,'<RadialGradientBrush MappingMode="Absolute" RadiusX="'
		+mRadius+'" RadiusY="'+mRadius+'" GradientOrigin="'+mCenter
		+'" Center="'+mCenter+'">');
	for (var ii=0;ii<mStops.length;ii+=2) {
		gradient_stop(lc+2,mStops[ii],mStops[ii+1]);
	}
	xaml_line(lc,'</RadialGradientBrush>');
	xaml_line(lc,'</'+pNode+'.OpacityMask>');
}

function canvas_list(lc,cList) {
	var cInfo;
	for (var ii=0;ii<cList.length;ii++) {
		cInfo = cList[ii];
		canvas(lc,cInfo.name,cInfo.left,cInfo.top,cInfo.opacity);
	}
}

function text_line(lc,tLeft,tTop,tText,tFont) {
	var fInfo = fontList[tFont];
	var tTemp = '<TextBlock';
	tTemp += ' FontFamily="'+fInfo.name+'"';
	tTemp += ' FontSize="'+fInfo.size+'"';
	if (fInfo.style) tTemp += ' FontStyle="'+fInfo.style+'"';
	tTemp += ' Foreground="'+fInfo.color+'"';
	if (tLeft)  tTemp += ' Canvas.Left="'+tLeft+'"';
	tTemp += ' Canvas.Top="'+tTop+'"';
	tTemp += '>';
	xaml_line(lc,tTemp);
	xaml_line(lc+1,tText+'</TextBlock>');
}

function text_list(lc,txtList) {
	var tTop = 0;
	for (var ii=0;ii<txtList.length;ii+=4) {
		tTop += txtList[ii];
		text_line(lc+1,txtList[ii+1],tTop,txtList[ii+2],txtList[ii+3]);
	}
}

function path_list(lc,pthList) {
	var aPath;
	var pTemp;
	var pPoints;
	for (var ii=0;ii<pthList.length;ii++) {
		aPath = pthList[ii];
		pTemp = '<Path';
		if (aPath.stroke) pTemp += ' Stroke="'+aPath.stroke+'"';
		if (aPath.fill) pTemp += ' Fill="'+aPath.fill+'"';
		if (aPath.thickness) pTemp += ' StrokeThickness="'+aPath.thickness+'"';
		if (aPath.opacity) pTemp += ' Opacity="'+aPath.opacity+'"';
		if (aPath.left) pTemp += ' Canvas.Left="'+aPath.left+'"';
		if (aPath.top) pTemp += ' Canvas.Top="'+aPath.top+'"';
		pTemp += ' Data="';
		xaml_line(lc,pTemp);
		pPoints = wordPaths[aPath.name];
		for (var jj=0;jj<pPoints.length;jj++) xaml_line(lc+1,pPoints[jj]);
		xaml_line(lc,'"/>');
	}
}

function propertyElements(cInfo) {
	if (cInfo.clip) return true;
	if (cInfo.aClip) return true;
	if (cInfo.scale) return true;
	if (cInfo.aMatrix) return true;
	if (cInfo.mask) return true;
	if (cInfo.text) return true;
	if (cInfo.paths) return true;
	if (cInfo.canvas) return true;
	return false;
}

function get_sum(nOne,nTwo) {
	var zOne = Number(nOne);
	var zTwo = Number(nTwo);
	if (isNaN(zOne)) zOne = 0;
	if (isNaN(zTwo)) zTwo = 0;
	return (zOne + zTwo);
}

function get_product(nOne,nTwo) {
	var zOne = Number(nOne);
	var zTwo = Number(nTwo);
	if (isNaN(zOne)) zOne = 1;
	if (isNaN(zTwo)) zTwo = 1;
	return (zOne * zTwo);
}

function rectangle(rInfo) {
	var rTemp = '';
	if (rInfo.width) rTemp += ' Width="'+rInfo.width+'"';
	if (rInfo.height) rTemp += ' Height="'+rInfo.height+'"';
	if (rInfo.fill) rTemp += ' Fill="'+rInfo.fill+'"';
	return rTemp;
}

function canvas(lc,cName,cLeft,cTop,cOpacity) {
	var cInfo = defCanvas[cName];
	if (!cInfo) alert("no canvas named "+cName);
	if (cInfo.blank) return;
	if (cInfo.music) return;
	var cTemp = '<Canvas';
	var pNode = 'Canvas';
	if (cInfo.image) {
		cTemp = '<Image Source="'+cInfo.image+'"';
		pNode = 'Image';
	}
	else if (cInfo.rectangle) {
		cTemp = '<Rectangle';
		cTemp += rectangle(cInfo.rectangle);
		pNode = 'Rectangle';
	}
	if (cOpacity == 0) {
		cTemp += ' Name="'+cName+'"';
	}
	var nOpacity = get_product(cInfo.opacity,cOpacity);
	if (nOpacity < 1) {
		cTemp += ' Opacity="'+nOpacity+'"';
	}
	var nLeft = get_sum(cInfo.left,cLeft);
	if (nLeft) {
		cTemp += ' Canvas.Left="'+nLeft+'"';
	}
	var nTop = get_sum(cInfo.top,cTop);
	if (nTop) {
		cTemp += ' Canvas.Top="'+nTop+'"';
	}
	if (propertyElements(cInfo)) {
		cTemp += '>';
		xaml_line(lc,cTemp);
		if (cInfo.canvas) canvas_list(lc+1,cInfo.canvas);
		if (cInfo.text) text_list(lc+1,cInfo.text);
		if (cInfo.paths) path_list(lc+1,cInfo.paths);
		if (cInfo.clip) image_clip(lc+1,pNode,cInfo.clip);
		if (cInfo.scale) image_scale(lc+1,pNode,cInfo.scale);
		if (cInfo.aClip && cOpacity == 0) antimate_clip(lc+1,cName,pNode,cInfo.aClip);
		if (cInfo.aMatrix && cOpacity == 0) antimate_matrix(lc+1,cName,pNode);
		if (cInfo.mask) mask_gradient(lc+1,cName,pNode,cInfo.mask);
		xaml_line(lc,'</'+pNode+'>');
	}
	else {
		cTemp += '/>';
		xaml_line(lc,cTemp);
	}
}

function filmstrip_images() {
	var lc = 1;
	var iLeft = 8;
	var iTop = 6;
	var nn = 100;
	var cTemp;
	xaml_line(lc,'<Canvas Canvas.Top="0" Name="filmstrip">');
	xaml_line(lc+1,'<Rectangle Width="768" Height="1400" Fill="Gray"/>');
	for (var ii=0;ii<filmStrip.length;ii++) {
		if (iLeft > 700) {
			iLeft = 8;
			iTop += 114;
		}
		cTemp = '<Canvas Width="800" Height="600" Background="Black"';
		cTemp += ' Name="'+(++nn)+filmStrip[ii]+'"';
		cTemp += ' MouseLeftButtonDown="show_big"';
		cTemp += ' Canvas.Left="'+iLeft+'"';
		cTemp += ' Canvas.Top="'+iTop+'">';
		xaml_line(lc+1,cTemp); 
		xaml_line(lc+2,'<Canvas.RenderTransform>');
		xaml_line(lc+3,'<ScaleTransform ScaleX="0.18" ScaleY="0.18"/>');
		xaml_line(lc+2,'</Canvas.RenderTransform>');
		canvas(lc+2,filmStrip[ii],0,0,1);
		xaml_line(lc+1,'</Canvas>');
		iLeft += 152;
	}
	xaml_line(lc,'</Canvas>');
}

function add_some_Xaml(aCanvas, isResource) {
	if (showAsHTML) return;
	var cXaml = slContent.CreateFromXaml(collectedXAML);
	if (isResource) aCanvas.Resources.add(cXaml);
	else aCanvas.children.add(cXaml);
	collectedXAML = "";
}

function make_filmstrip(aCanvas) {
	bigImages = {};
	xaml_line(1,'<Canvas Canvas.Left="768">'); 
	var cTemp = '<Rectangle Width="20" Height="40" Fill="White"';
	cTemp += ' Canvas.Left="5" Canvas.Top="20"';
	cTemp += ' MouseLeftButtonDown="move_up"/>';
	xaml_line(2,cTemp); 
	cTemp = '<Rectangle Width="20" Height="40" Fill="White"';
	cTemp += ' Canvas.Left="5" Canvas.Top="180"';
	cTemp += ' MouseLeftButtonDown="move_down"/>';
	xaml_line(2,cTemp); 
	xaml_line(1,'</Canvas>'); 
	add_some_Xaml(aCanvas);
	collectedXAML = "";
	filmstrip_images();
	add_some_Xaml(aCanvas);
}

function make_one_image(aCanvas, cName) {
	collectedXAML = "";
	var cTemp = '<Canvas Width="800" Height="600" Background="Black" Opacity="0"';
	cTemp += ' Name="'+cName+'"';
	cTemp += ' MouseLeftButtonDown="show_little">';
	xaml_line(1,cTemp); 
	canvas(2,cName,0,0,1);
	xaml_line(1,'</Canvas>');
	add_some_Xaml(aCanvas);
}

function one_button(lc,bText,bAction,bLeft,bTop) {
	var aTemp = '<Canvas MouseLeftButtonDown="'+bAction+'"';
	aTemp += ' MouseEnter="enter_button" MouseLeave="leave_button"';
	aTemp += ' Canvas.Left="'+bLeft+'"';
	aTemp += ' Canvas.Top="'+bTop+'">';
	xaml_line(lc,aTemp);
	aTemp = '<Rectangle Width="300" Height="36"';
	aTemp += ' RadiusX="10" RadiusY="10"';
	aTemp += ' Stroke="Tan" StrokeThickness="3" Fill="Tan"/>';
	xaml_line(lc+1,aTemp);
	aTemp = '<TextBlock Canvas.Left="10" Canvas.Top="10"';
	aTemp += ' FontFamily="Arial" FontSize="16" Foreground="Black"';
	aTemp += ' Text="'+bText+'"/>';
	xaml_line(lc+1,aTemp);
	xaml_line(lc,'</Canvas>');
}

function one_link(lc,bText,bAction,bLeft,bTop) {
	var aTemp = '<Canvas MouseLeftButtonDown="'+bAction+'"';
	aTemp += ' MouseEnter="enter_back_to" MouseLeave="leave_back_to"';
	aTemp += ' Canvas.Left="'+bLeft+'"';
	aTemp += ' Canvas.Top="'+bTop+'">';
	xaml_line(lc,aTemp);
	aTemp = '<TextBlock Canvas.Left="10" Canvas.Top="10"';
	aTemp += ' FontFamily="Arial" FontSize="14" Foreground="Silver"';
	aTemp += ' Text="'+bText+'"/>';
	xaml_line(lc+1,aTemp);
	xaml_line(lc,'</Canvas>');
}

function make_buttons(aCanvas) {
	var cLeft = 100;
	var cTop = 40;
	collectedXAML = "";
	xaml_line(1,'<Canvas>');
	var aTemp = '<TextBlock Canvas.Left="'+cLeft+'"';
	aTemp += ' Canvas.Top="'+(20+cTop)+'"';
	aTemp += ' FontFamily="Arial" FontWeight="Bold" FontSize="36" Foreground="White"';
	aTemp += ' Text="'+NameOfShow+'"/>';
	xaml_line(2,aTemp);
	one_button(2,"start show in the browser window",
		"start_playing",30+cLeft,100+cTop);
	one_button(2,"start show with full screen background",
		"start_fullscreen_background",30+cLeft,160+cTop);
	one_button(2,"start show with full screen images",
		"start_fullscreen_image",30+cLeft,220+cTop);
	one_link(2,"All Shows","back_to_shows",110+cLeft,400+cTop);
	one_link(2,"D I G ME","back_to_digme",230+cLeft,400+cTop);
	xaml_line(1,'</Canvas>');
	add_some_Xaml(aCanvas);
}

function opacity1(lc,sbName) {
	xaml_line(lc,'<Storyboard Name="'+sbName+'">');
	xaml_line(lc+1,'<DoubleAnimation Storyboard.TargetProperty="Opacity"/>');
	xaml_line(lc,'</Storyboard>');
}
function opacity2(lc,sbName) {
	xaml_line(lc,'<Storyboard Name="'+sbName+'">');
	xaml_line(lc+1,'<DoubleAnimation Storyboard.TargetProperty="Opacity"/>');
	xaml_line(lc+1,'<DoubleAnimation Storyboard.TargetProperty="Opacity"/>');
	xaml_line(lc,'</Storyboard>');
}
function opacity1_mxox(lc,sbName) {
	xaml_line(lc,'<Storyboard Name="'+sbName+'">');
	xaml_line(lc+1,'<DoubleAnimation Storyboard.TargetProperty="Opacity"/>');
	xaml_line(lc+1,'<DoubleAnimation Storyboard.TargetProperty="OffsetX"/>');
	xaml_line(lc,'</Storyboard>');
}
function opacity1_mxm11(lc,sbName) {
	xaml_line(lc,'<Storyboard Name="'+sbName+'">');
	xaml_line(lc+1,'<DoubleAnimation Storyboard.TargetProperty="Opacity"/>');
	xaml_line(lc+1,'<DoubleAnimation Storyboard.TargetProperty="M11"/>');
	xaml_line(lc,'</Storyboard>');
}
function opacity1_points2(lc,sbName) {
	xaml_line(lc,'<Storyboard Name="'+sbName+'">');
	xaml_line(lc+1,'<DoubleAnimation Storyboard.TargetProperty="Opacity"/>');
	xaml_line(lc+1,'<PointAnimation Storyboard.TargetProperty="(PathFigure.Segments)[0].Point"/>');
	xaml_line(lc+1,'<PointAnimation Storyboard.TargetProperty="(PathFigure.Segments)[1].Point"/>');
	xaml_line(lc,'</Storyboard>');
}
function opacity2_points2(lc,sbName) {
	xaml_line(lc,'<Storyboard Name="'+sbName+'">');
	xaml_line(lc+1,'<DoubleAnimation Storyboard.TargetProperty="Opacity"/>');
	xaml_line(lc+1,'<DoubleAnimation Storyboard.TargetProperty="Opacity"/>');
	xaml_line(lc+1,'<PointAnimation Storyboard.TargetProperty="(PathFigure.Segments)[0].Point"/>');
	xaml_line(lc+1,'<PointAnimation Storyboard.TargetProperty="(PathFigure.Segments)[1].Point"/>');
	xaml_line(lc,'</Storyboard>');
}
function opacity2_points4(lc,sbName) {
	xaml_line(lc,'<Storyboard Name="'+sbName+'">');
	xaml_line(lc+1,'<DoubleAnimation Storyboard.TargetProperty="Opacity"/>');
	xaml_line(lc+1,'<DoubleAnimation Storyboard.TargetProperty="Opacity"/>');
	xaml_line(lc+1,'<PointAnimation Storyboard.TargetProperty="StartPoint"/>');
	xaml_line(lc+1,'<PointAnimation Storyboard.TargetProperty="(PathFigure.Segments)[0].Point"/>');
	xaml_line(lc+1,'<PointAnimation Storyboard.TargetProperty="(PathFigure.Segments)[1].Point"/>');
	xaml_line(lc+1,'<PointAnimation Storyboard.TargetProperty="(PathFigure.Segments)[2].Point"/>');
	xaml_line(lc,'</Storyboard>');
}

var storyInfo = [
	"op1n",opacity1,
	"op2n",opacity2,
	"op1m11n",opacity1_mxm11,
	"op1pt2n",opacity1_points2,
	"op2pt2n",opacity2_points2,
	"op2pt4n",opacity2_points4,
	"op1moxn",opacity1_mxox,
];

var storyNames = {};

function make_storyboards(aCanvas) {
	var baseName, numNeeded, newName;
	var cXaml;
	storyNames = {};
	for (var ii=0;ii<storyInfo.length;ii+=2) {
		baseName = storyInfo[ii];
		if (storyNeeded[baseName]) {
			numNeeded = storyNeeded[baseName];
			storyNames[baseName] = new Array();
			while (numNeeded) {
				newName = baseName+numNeeded;
				storyNames[baseName].push(newName);
				collectedXAML = "";
				storyInfo[ii+1](1,newName);
				add_some_Xaml(aCanvas,"Resource");
				numNeeded--;
			}
		}
	}
}

function make_media(lc,mName) {
	var mInfo = defCanvas[mName];
	var mTemp = '<MediaElement Source="'+mInfo.music+'"';
	mTemp += ' Name="'+mName+'" AutoPlay="false"/>';
	xaml_line(lc,mTemp);
}

function make_one_element(aCanvas, cName) {
	if (!cName) return false;
	var cInfo = defCanvas[cName];
	if (!cInfo) alert("no def for "+cName);
	if (cInfo.blank) return false;
	if (cInfo.done) return false;
	collectedXAML = "";
	if (cInfo.music) make_media(1,cName);
	else canvas(1,cName,0,0,0);
	add_some_Xaml(aCanvas);
	cInfo.done = true;
	return true;
}

function clear_image_done() {
	var cInfo;
	var cXaml;
	for (var cName in defCanvas) {
		cInfo = defCanvas[cName];
		if (cInfo.done) cInfo.done = false;
	}
}

function make_show_images(aCanvas) {
	var cInfo;
	var cXaml;
	for (var cName in defCanvas) {
		cInfo = defCanvas[cName];
//		if (cInfo.canvas) continue;
		if (cInfo.blank) continue;
		if (cInfo.music) continue;
		collectedXAML = "";
		canvas(1,cName,0,0,0);
		add_some_Xaml(aCanvas);
	}
}

function show_as_html() {
	textList = document.getElementById('showxaml');
	showAsHTML = true;
	show_length();
	filmstrip_images();
	make_buttons();
	make_storyboards();
	make_show_images();
}

function show_length() {
	var curAct = 0;
	var sumAct = 0;
	while (curAct < actions.length) {
		sumAct += actions[curAct];
		curAct +=4;
	}
	var sumMinutes = sumAct/600;
	xaml_line(0,"show length is "+sumMinutes.toFixed(3)+" minutes");
}


// support show XAML

var sP = {
	p0: {story:undefined},
	p1: {story:undefined},
	p2: {story:undefined},
	p3: {story:undefined},
	p4: {story:undefined}
};

var nextTime = null;
var scaleTime = null;
var curAct = 0;


function start_actions() {
	switch_views("show");
	curAct = 0;
	var aWait = actions[curAct];
	if (aWait) nextTime = setTimeout(next_action,aWait*100);
	else next_action();
}

function next_action() {
	var aWait,aFunc;
	while (curAct < actions.length) {
		aFunc = actions[curAct+1];
		if (!aFunc) break;
		aFunc.apply(sP["p"+actions[curAct+2]],actions[curAct+3]);
		curAct +=4;
		aWait = actions[curAct];
		if (aWait) {	
			nextTime = setTimeout(next_action,aWait*100);
			return;
		}
	}
	slContent.FullScreen = false;
	switch_views("button");
}

function music(mName,anAction) {
	var music;
	if (this.sPlaying) {
		music = slContent.findName(this.sPlaying);
		music.Stop();
		this.sPlaying = null;
	}
	if (anAction != "play") return;
	music = slContent.findName(mName);
	music.Play();
	this.sPlaying = mName;
}

function stop_story(aPath) {
	if (aPath.sCur) aPath.sCur.Stop();
	aPath.sName = "";
	aPath.iName = "";
	aPath.sPrev = null;
	aPath.sCur = null;
}

function pstop() {
	var aPath = this;
	if (aPath.sCur) aPath.sCur.Stop();
	aPath.sName = "";
	aPath.iName = "";
	aPath.sPrev = null;
	aPath.sCur = null;
}

function choose_story(aPath,stName) {
	var newName;
	var storyList = storyNames[stName];
	for (var ii = 0; ii <= storyList.length; ii++) {
		if (ii >= storyList.length) {
			alert("no free storyList");
			break;
		}
		newName = storyList[ii];
		if (newName == sP.p1.sName) continue;
		if (newName == sP.p2.sName) continue;
		if (newName == sP.p3.sName) continue;
		if (newName == sP.p4.sName) continue;
		break;
	}
	aPath.sPrev = aPath.sCur;
	aPath.sCur = slContent.findName(newName);
	aPath.sName = newName;
//	alert(newName);
}

function change_animate(aPath,fs,image,aTo,aFrom,aIn) {
	var animation = aPath.sCur.children.getItem(fs);
	animation.setValue("Storyboard.TargetName",image);
	animation.setValue("From",aFrom);
	animation.setValue("To",aTo);
	animation.setValue("Duration",aIn);
}

function finish_action(aPath,image,zIndex,fadeTo) {
	if (zIndex != "s") {
		var imgXaml = slContent.findName(image);
		if (!imgXaml) alert('did not find '+image);
		if (zIndex == "f") aPath.zIndex += 2;
		else if (zIndex == "b") aPath.zIndex -= 2;
		else aPath.zIndex = zIndex;
		imgXaml.setValue("Canvas.ZIndex",aPath.zIndex);
	}
	aPath.to = fadeTo;
	aPath.iName = image;
	aPath.sCur.Begin();
	if (aPath.sPrev) aPath.sPrev.Stop();
}

function fade(image,zIndex,fadeTo,fadeIn) {
	var fadeTime = "0:0:2";
	if (typeof fadeIn != "undefined") fadeTime = "0:0:"+fadeIn;
	if (zIndex == "f") {
		choose_story(this,"op2n");
		change_animate(this,0,image,fadeTo,0,fadeTime);
		change_animate(this,1,this.iName,this.to,this.to,"0:0:0");
	}
	else if (zIndex == "s") {
		choose_story(this,"op1n");
		change_animate(this,0,image,fadeTo,this.to,fadeTime);
	}
	else if (zIndex == "b") {
		choose_story(this,"op2n");
		change_animate(this,0,this.iName,0,this.to,fadeTime);
		change_animate(this,1,image,fadeTo,fadeTo,"0:0:0");
	}
	else {
		choose_story(this,"op1n");
		change_animate(this,0,image,fadeTo,0,fadeTime);
	}
	finish_action(this,image,zIndex,fadeTo);
};

function change_ptanimate(aPath,fs,point,pFrom,pTo,aIn) {
	var animation = aPath.sCur.children.getItem(fs);
	animation.setValue("Storyboard.TargetName",point);
	animation.setValue("Duration",aIn);
	animation.setValue("From",pFrom);
	animation.setValue("To",pTo);
}

function flip(image,zIndex,fadeTo) {
	choose_story(this,"op1m11n");
	change_animate(this,0,image,1,1,"0:0:0");
	change_animate(this,1,image+"MX",0,1,"0:0:1");
	finish_action(this,image,"s",fadeTo);
};

function flip2(image,zIndex,fadeTo) {
	var id = image_dimensions(image);
	if (zIndex == "s") {
		choose_story(this,"op1pt2n");
		change_animate(this,0,image,this.to,this.to,"0:0:0");
		change_ptanimate(this,1,image+"PT",id.iw+",0","0,0","0:0:1");
		change_ptanimate(this,2,image+"PT",id.iw+","+id.ih,"0,"+id.ih,"0:0:1");
	}
	else {
		choose_story(this,"op2pt2n");
		change_animate(this,0,image,this.to,this.to,"0:0:0");
		change_animate(this,1,this.iName,this.to,this.to,"0:0:0");
		change_ptanimate(this,2,image+"PT",id.iw+",0","0,0","0:0:1");
		change_ptanimate(this,3,image+"PT",id.iw+","+id.ih,"0,"+id.ih,"0:0:1");
	}
	finish_action(this,image,"s",fadeTo);
};

function smallbig(image,zIndex,fadeTo) {
	var id = image_dimensions(image);
	choose_story(this,"op2pt2n");
	change_animate(this,0,this.iName,this.to,this.to,"0:0:0");
	change_animate(this,1,image,1,1,"0:0:0");
	change_ptanimate(this,2,image+"PT","0,0",id.iw+",0","0:0:2");
	change_ptanimate(this,3,image+"PT","0,"+id.ih,id.iw+","+id.ih,"0:0:2");
	finish_action(this,image,"f",fadeTo);
};

function bigsmall(image,zIndex,fadeTo) {
	var id = image_dimensions(this.iName);
	choose_story(this,"op2pt4n");
	change_animate(this,0,image,fadeTo,fadeTo,"0:0:0");
	change_animate(this,1,this.iName,1,1,"0:0:0");
	change_ptanimate(this,2,this.iName+"PT","0,0",id.hw+",0","0:0:2");
	change_ptanimate(this,3,this.iName+"PT",id.iw+",0",id.hw+",0","0:0:2");
	change_ptanimate(this,4,this.iName+"PT",id.iw+","+id.ih,id.hw+","+id.ih,"0:0:2");
	change_ptanimate(this,5,this.iName+"PT","0,"+id.ih,id.hw+","+id.ih,"0:0:2");
	finish_action(this,image,"b",fadeTo);
};

function image_dimensions(image) {
	var imgXaml = slContent.findName(image);
	return {iw: imgXaml.Width,
		ih: imgXaml.Height,
		hw: imgXaml.Width/2,
		hh: imgXaml.Height/2};
}

function pointfull(image,zIndex,fadeTo) {
	var id = image_dimensions(image);
	choose_story(this,"op2pt4n");
	change_animate(this,0,this.iName,this.to,this.to,"0:0:0");
	change_animate(this,1,image,1,1,"0:0:0");
	change_ptanimate(this,2,image+"PT",id.hw+","+id.hh,"0,0","0:0:2");
	change_ptanimate(this,3,image+"PT",id.hw+","+id.hh,id.iw+",0","0:0:2");
	change_ptanimate(this,4,image+"PT",id.hw+","+id.hh,id.iw+","+id.ih,"0:0:2");
	change_ptanimate(this,5,image+"PT",id.hw+","+id.hh,"0,"+id.ih,"0:0:2");
	finish_action(this,image,"f",fadeTo);
};

function movex(image,zIndex,fadeTo) {
	var iScale = windowScale;
	var imgXaml = slContent.findName(image);
	var wTot = slContent.actualWidth;
	var wClip = imgXaml.getValue("Canvas.Left");
	var wBorder = wTot/(2*iScale) - 400;
	var mStart = 0 - 800 - wBorder;
	var mEnd = 800 - wClip + wBorder;
	choose_story(this,"op1moxn");
	change_animate(this,0,image,fadeTo,0,"0:0:0");
	change_animate(this,1,image+"MX",mEnd,mStart,"0:0:6");
	finish_action(this,image,zIndex,fadeTo);
}

// make show XAML

var curMake;
var timeMake;
var canvasMake;
var allMusic;

function start_makeXAML(vImages) {
	canvasMake = vImages;
	allMusic = Array();
	var inTime = 100;
	var actTime = 0;
	var minAct = 0;
	var pName = "";
	clear_image_done();
	for (var ii=0;ii<actions.length;ii+=4) {
		actTime += actions[ii];
		aName = actions[ii+3][0];
		if (!aName) continue;
		if (aName == pName) continue;
		pName = aName;
		inTime += 20;
		actTime += actions[ii];
		if (actTime < inTime) minAct = ii;
	}
	var aName;
	for (curMake=0;curMake<actions.length;curMake+=4) {
		if (curMake > minAct) break;
		aName = actions[curMake+3][0];
		make_one_element(canvasMake,aName);
		if (actions[curMake+1] == music) allMusic.push(aName);
//		alert("make action "+aName);
	}
	timeMake = setTimeout(next_makeXAML,2000);
}

var errCount = 4;

function next_makeXAML() {
	var aName;
	while (curMake < actions.length) {
		if (!actions[curMake+1]) break;
		aName = actions[curMake+3][0];
		anAction = actions[curMake+1];
//		if (errCount-- > 0) alert(aName);
		curMake += 4;
		if (make_one_element(canvasMake,aName)) {
			timeMake = setTimeout(next_makeXAML,2000);
			if (anAction == music) allMusic.push(aName);
			return;
		}
	}
}

function goto_webpage(newURL) {
	if (timeMake) {
		clearTimeout(timeMake);
		timeMake = null;
	}
	var iMusic;
	for (var ii=0;ii<allMusic.length;ii++) {
		iMusic = slContent.findName(allMusic[ii]);
		iMusic.Source = "";
	}
	location.href = newURL;
}


// on load of XAML

var slControl = null;
var slContent = null;

function onLoad(plugin,userContext,sender) {
	slControl = plugin;
	slContent = slControl.content;
	showAsHTML = false;
	var vButtons = slContent.findName("buttons");
	if (vButtons) {
		make_buttons(vButtons);
		var vTop = slContent.findName("topcanvas");
		make_storyboards(vTop);
		var vImages = slContent.findName("viewshow");
//		make_show_images(vImages);
		start_makeXAML(vImages);
		slContent.onFullScreenChange = full_screen_change;
		slContent.onResize = screen_resize;
	}
	else {
		var vImages = slContent.findName("viewimages");
		make_filmstrip(vImages);
	}
}

//***** environment functions ******


function start_playing(sender,mouseEventArgs) {
	maxScale = false;
	get_window_scale();
	resize_to_scale();
	start_actions();
}

var windowScale = 0;
var maxScale = false;

function start_fullscreen_background(sender,mouseEventArgs) {
	maxScale = false;
	get_window_scale();
	slContent.FullScreen = true;
	start_actions();
}

function start_fullscreen_image(sender,mouseEventArgs) {
	maxScale = true;
	slContent.FullScreen = true;
	start_actions();
}

function switch_views(newView) {
	stop_story(sP.p1);
	stop_story(sP.p2);
	stop_story(sP.p3);
	stop_story(sP.p4);
	var vButton = slContent.findName("buttons");
	var vImage = slContent.findName("viewshow");
	if (newView == "button") {
		if (nextTime) clearTimeout(nextTime);
		nextTime = null;
		vImage.Visibility = "Collapsed";
		vButton.Visibility = "Visible";
	}
	else if (newView == "show") {
		vButton.Visibility = "Collapsed";
		vImage.Visibility = "Visible";
	}
}	

function stop_show() {
	music.apply(sP.p0,[sP.p0.nowPlaying,"stop"]);
	stop_story(sP.p1);
	stop_story(sP.p2);
	stop_story(sP.p3);
	stop_story(sP.p4);
	switch_views("button");
}

function full_screen_change() {
	if (slContent.fullScreen) {
		if (maxScale) get_window_scale();
		resize_to_scale();
	}
	else {
		stop_show();
	}
}

function screen_resize() {
	if (scaleTime) clearTimeout(scaleTime);
	scaleTime = setTimeout(scale_images,500);
}

function scale_images() {
	scaleTime = null;
	get_window_scale();
	resize_to_scale();
	return;
}

function resize_to_scale() {
	var width = slContent.actualWidth;
	var height = slContent.actualHeight;
	var aMatrix = slContent.findName("scaleshow");
	var offx = (width - (800 * windowScale))/2;
	var offy = (height - (600 * windowScale))/2;
	if (offx < 0) offx = 0;
	if (offy < 0) offy = 0;
	aMatrix.OffsetX = offx;
	aMatrix.OffsetY = offy;
	aMatrix.M11 = windowScale;
	aMatrix.M22 = windowScale;
}

function get_window_scale() {
	var width = slContent.actualWidth;
	var height = slContent.actualHeight;
	if (maxScale) {
		windowScale = width / 800;
		if ((600 * windowScale) > height) windowScale = height / 600;
		return;
	}
	for (windowScale = 1.0; windowScale > 0.5; windowScale -= 0.2) {
		if ((800 * windowScale) > width) continue;
		if ((600 * windowScale) > height) continue;
		return;
	}
}

// button page functions

function enter_back_to(sender, mouseEventArgs) {
	sender.children.getItem(0).Foreground="White"
}
function leave_back_to(sender, mouseEventArgs) {
	sender.children.getItem(0).Foreground="Silver"
}
function back_to_shows(sender, mouseEventArgs) {
	goto_webpage("../index.html");
//	alert("go to all shows");
}
function back_to_digme(sender, mouseEventArgs) {
	goto_webpage("../../Welcome.html");
//	alert("go to digme");
}
function enter_button(sender, mouseEventArgs) {
	sender.children.getItem(0).Stroke="Wheat"
}
function leave_button(sender, mouseEventArgs) {
	sender.children.getItem(0).Stroke="Tan"
}

// filmstrip functions

function move_up(sender, mouseEventArgs) {
	var aStrip = slContent.findName("filmstrip");
	var sTop = Number(aStrip.getValue("Canvas.Top"));
	if (sTop >= 0) return;
	aStrip.setValue("Canvas.Top",String(sTop+114));
}
function move_down(sender, mouseEventArgs) {
	var aStrip = slContent.findName("filmstrip");
	var sTop = Number(aStrip.getValue("Canvas.Top"));
	if (sTop < -700) return;
	aStrip.setValue("Canvas.Top",String(sTop-114));
}

var curBig;

function show_big(sender, mouseEventArgs) {
	var sOne = slContent.findName("oneimage");
	curBig = sender.Name.substr(3);
	if (!bigImages[curBig]) {
		make_one_image(sOne,curBig);
		bigImages[curBig] = true;
	}
	var aBig = slContent.findName(curBig);
	aBig.Opacity = 1;
	sOne.Visibility = "Visible";
	var aStrip = slContent.findName("viewimages");
	aStrip.Visibility = "Collapsed";
}

function show_little(sender, mouseEventArgs) {
	var aStrip = slContent.findName("viewimages");
	aStrip.Visibility = "Visible";
	var sOne = slContent.findName("oneimage");
	sOne.Visibility = "Collapsed";
	var aBig = slContent.findName(curBig);
	aBig.Opacity = 0;
}

