﻿/******************************************************
/*
/* Slideshow in Silvelight
/*
/* (c) 2008 PlainConcepts SL 
/* Yamil Hernández & Eduardo Quintás
/*
/*
 */

var SLIDE_PAUSE = 8000; // Wait time between slides in ms.
var SLIDE_NUMBER = 3;   // Number of slides

var mainCanvas;
var currentIndex;
var autoSlide=true;

if (!window.ImageGallery)
	ImageGallery = {};

ImageGallery.Page = function() 
{
}

ImageGallery.Page.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
		this.control = control;
		mainCanvas = control.Content.findName("MainCanvas");
		
		// Sample event hookup:	
		rootElement.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));

		for(var i=0; i< SLIDE_NUMBER; i++)
		   mainCanvas.findName("Image"+i).Source = imageList[i].getUrl(); 
                              
		selectImage(selectRandomIndex());	
	 	
	    setTimeout("nextImage()", SLIDE_PAUSE);	    
	},
	
	// 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();
	}
}

function selectImage(index)
{

 currentIndex = index;

 mainCanvas.findName("SelectedImageView").Source = imageList[index].getUrl(); 
 mainCanvas.findName("TitleTB").Text = imageList[index].getTitle();		
 mainCanvas.findName("DescriptionTB").Text = imageList[index].getDescription();

 for (var i=0; i<imageList.length; i++)
 {
   if (mainCanvas.findName("Rectangle" + i).Visibility = "Visible")
    mainCanvas.findName("Rectangle" + i).Visibility = "Collapsed";
 }
 
 mainCanvas.findName("Rectangle" + index).Visibility = "Visible";
}

function selectRandomIndex()
{
  var r = Math.round(Math.random()*(imageList.length-1));
  if (r<0) r=0;
  if (r>imageList.length-1) r=imageList.length-1;
 
  return r; 
}


function ImageClick(sender, args)
{
 selectImage(parseInt(sender.Name.replace("Image",'')));
 autoSlide = false;
}

function nextImage()
{
   if (autoSlide)
   {
     currentIndex= (currentIndex + 1) % imageList.length;
     selectImage(currentIndex); 
     setTimeout("nextImage()", SLIDE_PAUSE);  
   }
}


function LinkMouseEnter(sender, args)
{
 sender.TextDecorations="Underline";
}

function LinkMouseLeave(sender, args)
{
 sender.TextDecorations="None";
}

function NavigateTo (sender, args)
{
  location.href = imageList[currentIndex].getUrlDestination();
}
