﻿//Thumbnail builder for Virtual Eye
//Copyright ARL and Taylormade
//Feb 2008
//All rights reserved
// Written By Clinton Rock Smith
// js reads the directory of the currently highlighted area
// then pulls the directory and displays it in the XAML
// then adds scrollbars if it is bigger than the window can sustain

var currentpage;
var thumbnailXspacing = 75;
var thumbnailYspacing = 75;

//thumbnail filenames go here
var thumbArray=new Array();
thumbArray[0] = "air/gliding/gliding_dvd.wmv";
thumbArray[1] = "air/gliding/gliding_tv1.wmv";
thumbArray[2] = "air/gliding/gliding_tv3.wmv";
thumbArray[3] = "air/gliding/gliding_x.wmv";
thumbArray[4] = "air/gliding/gliding_redbull.wmv";
thumbArray[5] = "land/formula 1/f1driveline.wmv";
thumbArray[6] = "land/formula 1/f1flydowncatalunya.wmv";
thumbArray[7] = "land/formula 1/f1flydownmelbourne.wmv";
thumbArray[8] = "land/formula 1/f1fueladjusted.wmv";
thumbArray[9] = "land/formula 1/f1trackguidecatalunya.wmv";
thumbArray[10] = "land/golf/golf_whatisreal.wmv";
thumbArray[11] = "land/golf/golf_wentworth7th.wmv";
thumbArray[12] = "land/golf/golf_wentworth8th.wmv";
thumbArray[13] = "land/golf/golf_wentworth18th.wmv";
thumbArray[14] = "land/golf/golf02.wmv";
thumbArray[15] = "land/golf/golf_18kapalua1.wmv";
thumbArray[16] = "land/golf/golf_18kapalua2.wmv";
thumbArray[18] = "land/golf/golf_18kapalua3.wmv";
thumbArray[18] = "land/golf/golf06.wmv";
thumbArray[19] = "land/golf/golf_radar2.wmv";
thumbArray[20] = "land/golf/golf_indianwells08_position.wmv";
thumbArray[21] = "land/golf/golf_indianwells08_track.wmv";
thumbArray[22] = "land/golf/golf04.wmv";
thumbArray[23] = "land/golf/golf03.wmv";
thumbArray[24] = "land/golf/golf_liverpool08.wmv";
thumbArray[25] = "land/golf/wentworth_new.wmv";
thumbArray[26] = "land/cricket/cricket_sehwag1.wmv";
thumbArray[27] = "land/cricket/cricket_sehwag2.wmv";
thumbArray[28] = "land/cricket/cricket_sharma.wmv";
thumbArray[29] = "land/cricket/cricket_singh.wmv";
thumbArray[30] = "sea/sailing/sailing_tv3.wmv";
thumbArray[31] = "sea/sailing/sailing_history.wmv";
thumbArray[32] = "sea/sailing/sailing_china.wmv";
thumbArray[33] = "sea/sailing/sailing_handset.wmv";
thumbArray[34] = "sea/sailing/sailing_pre.wmv";
thumbArray[35] = "sea/sailing/sailing_xtra.wmv";

var titleArray = new Array();
titleArray[0] = "2007 NZ AIR GAMES";
titleArray[1] = "2007 GLIDING TV1 NEWS";
titleArray[2] = "2007 GLIDING TV3 NEWS";
titleArray[3] = "2006 NZ AIR GAMES";
titleArray[4] = "2006 RED BULL AIR RACING";
titleArray[5] = "2009 F1 DRIVE LINE";
titleArray[6] = "2009 F1 FLYDOWN CATALUNYA";
titleArray[7] = "2009 F1 FLYDOWN MELBOURNE";
titleArray[8] = "2009 F1 FUEL ADJUSTED";
titleArray[9] = "2009 F1 TRACKGUIDE CATALUNYA";
titleArray[10] = "2008 WENTWORTH: What is Real?";
titleArray[11] = "2008 WENTWORTH: 7th hole";
titleArray[12] = "2008 WENTWORTH: 8th hole";
titleArray[13] = "2008 WENTWORTH: 18th hole";
titleArray[14] = "2007 AUSTRALIAN PGA: 9th hole";
titleArray[15] = "2008 KAPALUA 1: 18th hole";
titleArray[16] = "2008 KAPALUA 2: 18th hole";
titleArray[17] = "2008 KAPALUA 3: 18th hole";
titleArray[18] = "2007 AUSTRALIAN PGA: 18th hole";
titleArray[19] = "2008 RADAR TEST";
titleArray[20] = "2008 INDIAN WELLS 1";
titleArray[21] = "2008 INDIAN WELLS 2";
titleArray[22] = "2007 AUSTRALIAN PGA: 13th hole";
titleArray[23] = "2007 AUSTRALIAN PGA: 12th hole";
titleArray[24] = "2008 LIVERPOOL";
titleArray[25] = "2008 WENTWORTH: NEW";
titleArray[26] = "2008 CRICKET: SEHWAG 1";
titleArray[27] = "2008 CRICKET: SEHWAG 2";
titleArray[28] = "2008 CRICKET: SHARMA";
titleArray[29] = "2008 CRICKET: SINGH";
titleArray[30] = "2007 SAILING TV3 NEWS";
titleArray[31] = "1992-2007 SAILING TIMELINE";
titleArray[32] = "2007 LOUIS VUITTON CUP: broadcast TV";
titleArray[33] = "2007 LOUIS VUITTON CUP: handset TV";
titleArray[34] = "2007 AMERICA'S CUP PROMO";
titleArray[35] = "2007 AMERICA'S CUP EXTRA";

var volumeArray = new Array();
volumeArray[0] = 1;
volumeArray[1] = 1;
volumeArray[2] = 1;
volumeArray[3] = 1;
volumeArray[4] = 1;
volumeArray[5] = 1;
volumeArray[6] = 1;
volumeArray[7] = 1;
volumeArray[8] = 1;
volumeArray[9] = 1;
volumeArray[10] = 1;
volumeArray[11] = 1;
volumeArray[12] = 1;
volumeArray[13] = 1;
volumeArray[14] = 1;
volumeArray[15] = 1;
volumeArray[16] = 1;
volumeArray[17] = 1;
volumeArray[18] = 1;
volumeArray[19] = 1;
volumeArray[20] = 1;
volumeArray[21] = 1;
volumeArray[22] = 1;
volumeArray[23] = 1;
volumeArray[24] = 1;
volumeArray[25] = 1;
volumeArray[26] = 1;
volumeArray[27] = 1;
volumeArray[28] = 1;
volumeArray[29] = 1;
volumeArray[30] = 1;
volumeArray[32] = 1;
volumeArray[33] = 1;
volumeArray[34] = 1;
volumeArray[35] = 1;

//Catagory array
var catArray = new Array();
catArray[0] = "land";
catArray[1] = "sea";
catArray[2] = "air";

//Scrollbar Array
var catScroll = new Array();
catScroll[0] = false;
catScroll[1] = false;
catScroll[2] = false;

//Number of Catagory Thumbs
var catThumbs = new Array();
catThumbs[0] = 0;
catThumbs[1] = 0;
catThumbs[2] = 0;

var subCatagory = new Array();

var newXaml= new Array();
newXaml[0] = "";
newXaml[1] = "";
newXaml[2] = "";

var thumbnailX = new Array();
var thumbnailY = new Array();
thumbnailX[0] = 0;
thumbnailY[0] = 0;
thumbnailX[1] = 0;
thumbnailY[1] = 0;
thumbnailX[2] = 0;
thumbnailY[2] = 0;


//Thumbnail size is 65,65
//Starts at 564,208

function buildThumbnails(sender, eventArgs) {

//Build canvas based on thumbnailPage, make it collapsed
newXaml[0] = '<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="landcanvas" Visibility="Collapsed" Canvas.Left="0" Canvas.Top="0" Width="294" Height="XXXreplaceHeightXXX">';
newXaml[1] = '<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="seacanvas" Visibility="Collapsed" Canvas.Left="0" Canvas.Top="0" Width="294" Height="XXXreplaceHeightXXX">';
newXaml[2] = '<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="aircanvas" Visibility="Collapsed" Canvas.Left="0" Canvas.Top="0" Width="294" Height="XXXreplaceHeightXXX">';

//Checker for creation of scrollbars
	
for (checkScroll = 0; checkScroll <= thumbArray.length; checkScroll++) {
	parts = thumbArray[checkScroll] + "";
	bits = parts.split("/");
	mainCatagory = bits[0];
	for (quickfix = 0; quickfix <=2; quickfix++) {
		if (mainCatagory == catArray[quickfix]) catThumbs[quickfix] ++;
		}
	}

//Flag the Scrollbars
for (quickfix = 0; quickfix <=2; quickfix++) {
	if (catThumbs[quickfix] >= 9) {
		catScroll[quickfix] = true;
	}
}



for (cat = 0; cat <= 2; cat++) {
	//for(nextCat = 0; nextCat <= subCatagory.length; nextCat++){

	for (subCat=0; subCat<=thumbArray.length; subCat++) {
	
	//SPLIT the array into bits
	sectionz = thumbArray[subCat] + "";
	var section = sectionz.split("/");
	mainCatagory = section[0];
	subCatagoryRead = section[1];
	filename = section[2];
				
	if (mainCatagory == catArray[cat]) {
	
		//
		//sub catagory printing.
		//Read the subcatagory value, then cycle to see if it's been done before.
		//If so, it is not printed
		//
		var subExists = false;
		for (checker = 0; checker <= subCatagory.length; checker++) {
			if (subCatagory[checker] == subCatagoryRead) {
			subExists = true;
			}
		}
		if (!subExists) {
		puller = subCatagoryRead + "";
		pullerz = puller.toUpperCase();
		//sender.findName("block").setValue("Text", pullerz);
		if (thumbnailX[cat] >= 20) thumbnailY[cat] += thumbnailYspacing;
		newXaml[cat] += '<TextBlock Width="288" Height="18" Canvas.Left="0" Canvas.Top="' + thumbnailY[cat] + '" Text="' + pullerz + '" TextWrapping="Wrap" Foreground="#FFFFFFFF" FontSize="12" />';
		thumbnailX[cat] = 0;
		thumbnailY[cat] += 18;
		subCatagory.push(subCatagoryRead);
		}
	
		//Build Thumbnails
		newXaml[cat] += '<Canvas x:Name="mediathumb' + subCat + '" Canvas.Left="' + thumbnailX[cat] + '" Canvas.Top="' + thumbnailY[cat] + '" Width="65" Height="65" MouseEnter="thumbnailEnter" MouseLeave="thumbnailExit" MouseLeftButtonDown="videomainTrigger" Cursor="Hand" Opacity="0.75" RenderTransformOrigin="0.5,0.5">';
		newXaml[cat] += '<Canvas Width="65" Height="65">';
		newXaml[cat] += '<Path Width="65" Height="65" Canvas.Left="0" Canvas.Top="0" Stretch="Fill" Fill="#3431BFFF" Data="F1 M 18.6072,0L 0,21.8334L 0,65.0912L 65.0922,65.0912L 65.0922,0L 18.6072,0 Z "/>';
		newXaml[cat] += '<MediaElement Loaded="forceThumb" Source="thumbnails/' + thumbArray[subCat] + '" Width="60" Height="60" Canvas.Left="3" Canvas.Top="3" Volume="0" x:Name="mediathumb' + subCat + 'element" Clip="M41.754921,58.053888 L58.054929,38.930692 L58.054929,1.0372899 L17.685308,1.0372899 L1.03723,20.571496 L1.03723,58.053888 z" IsMuted="True" AutoPlay="False"/>';
        //newXaml[cat] += '<MediaElement Width="60" Height="60" Canvas.Left="3" Canvas.Top="3" Volume="0" x:Name="mediathumb' + subCat + 'element" Clip="M41.754921,58.053888 L58.054929,38.930692 L58.054929,1.0372899 L17.685308,1.0372899 L1.03723,20.571496 L1.03723,58.053888 z" IsMuted="True" AutoPlay="False"/>';
		newXaml[cat] += '</Canvas>';
		newXaml[cat] += '</Canvas>';		
		thumbnailX[cat] += thumbnailXspacing;
			
		if (thumbnailX[cat] >= 250) {
			thumbnailX[cat] = 0;
			thumbnailY[cat] += thumbnailYspacing;
			} //end if		
		} //end of mainCatagory if
	} //end of subCat For
//}//end of nextCat for statement
newXaml[cat] += '</Canvas>';

thumbnailY[cat] +=90;

//Update Height
sendXAML = newXaml[cat].replace("XXXreplaceHeightXXX" , thumbnailY[cat] + "")

//Create Scrollbars
//Width="308" Height="222" x="564" y="204"
makeScrollBars(sender, catArray[cat] + "", 558, 207, 305, 217, "v", sendXAML, thumbnailY[cat] + "");
} //end of Cat
collapseThumbs(sender);
uncollapseThumbs(sender);

sender.findName("landcanvas").Visibility = "Visible";
sender.findName("seacanvas").Visibility = "Visible";
sender.findName("aircanvas").Visibility = "Visible";


} //End of function


function forceThumb(sender) {
sender.Position = "00:00:00";
}



function volumeAdjust (sender, eventArgs) {

filename = sender.findName("videomain").Source;
var section = filename.split("/");
playingVideo = section[3];
var rightVolume = "1";

for (cat = 0; cat<=thumbArray.length; cat++) {
	sectionz = thumbArray[cat];
	//sectionw = sectionz + "";
	var sectiony = sectionw.split("/");
	filenamey = sectiony[3];
	sender.findName("block").setValue("Text", "" + playingVideo + " : " + filenamey + " : " + sectionz);
	
	if (filenamey == playingVideo)
		{
		rightVolume = volumeArray[cat];
		
	

//Read slider value
currentVolume = sender.findName("volume")["Canvas.Left"];
absoluteVolume = ((currentVolume - 83)/83) + 1;
tempVolume = ((rightVolume - 83)/83) + 1;
startVolume = (absoluteVolume - 1) * tempVolume;

//Set Volume
sender.findName("block").setValue("Text", "" + startVolume + " : " + currentVolume + " : " + absoluteVolume + " : " + tempVolume  + " : " + rightVolume + " : " + volumeArray[cat]  + " : " + cat);
//sender.findName("videomain").Volume = "" + startVolume;

//Update slider
//sender.findName("volume")["Canvas.Left"] = "" + (83 * startVolume);

        }// end of if filenamey == playing Video
    }// end of cat for loop
}






















//Loads in the assets for each of the thumbnails after
//downloading has completed
function setupAssets(sender, eventArgs){
//land = 0;
//sea = 0;
//air = 0;

	for (assets=0;assets<=thumbArray.length; assets++) {
	theCanvas ='mediathumb' + assets + 'element';
	theFile = "" + sender.findName(theCanvas).source;
	//sender.findName(theCanvas + "").setSource(dl,theFile + "");
	sender.findName("block").setValue("Text", "" + theFile);
	}
}


function displayDescription(sender, eventArgs, buttonName) {

//sender.findName("description").setValue("Text", "" + buttonName)

thumbnailNumber = buttonName.replace("mediathumb", "");
sender.findName("description").setValue("Text", "" + titleArray[thumbnailNumber]);
}

function displayDescription2(sender, eventArgs, buttonName) {

//sender.findName("description").setValue("Text", "" + buttonName)

thumbnailNumber = buttonName.replace("mediathumb", "");
sender.findName("description2").setValue("Text", "" + titleArray[thumbnailNumber]);
}


var beginX;
var beginY;
var isMouseDown=false;

function sliderMouseDown(sender, mouseEventArgs) {
	// Set the beginning position of the mouse
    beginX = mouseEventArgs.getPosition(null).x;
    beginY = mouseEventArgs.getPosition(null).y;
	
	//ensures that the mouse is pressed
	isMouseDown = true;
	
	// Ensure this object is the only one receiving mouse events
	sender.captureMouse();
}

// Reposition object during drag and drop operation.
function sliderMouseMove(sender, mouseEventArgs) {
//Settings for current Canvas
StringcurrX = sender["Canvas.Left"];
StringcurrY = sender["Canvas.Top"];

sliderHorizontalMinimum = 1;
sliderHorizontalMaximum = 83;


// Determine whether the mouse button is down.
// If so, move the object.
if (isMouseDown == true){
		// Retrieve the current position of the mouse.
		var currX = mouseEventArgs.getPosition(null).x;
		var currY = mouseEventArgs.getPosition(null).y;
		
		
		value = (StringcurrX + (currX-beginX));
		if (value >= sliderHorizontalMaximum) value = sliderHorizontalMaximum;
		if (value <= sliderHorizontalMinimum) value = sliderHorizontalMinimum;
		
		volume = ((value - sliderHorizontalMaximum)/sliderHorizontalMaximum) + 1;
		
        //Change Volume
        sender.findName("videomain").Volume = "" + volume;		
		
		sender["Canvas.Left"] = value;
		// Update the beginning position of the mouse.
		beginX = currX;
		beginY = currY;
		
	}
}

// Stop drag and drop operation.
function sliderMouseUp(sender, mouseEventArgs)
{
	isMouseDown = false;
	
	// Allow all objects to receive mouse events.
	sender.releaseMouseCapture();
}





