if (!window.mic1146_Frog)
	window.mic1146_Frog = {};

mic1146_Frog.Page = function() 
{
}

mic1146_Frog.Page.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
		this.control = control;
		control.content.findName("frogBreathe").Begin();
		control.content.findName("frogBlink").Begin();  
	    
		// 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("Timeline1").Begin();
	}
}

var plugin;
var debug;
var loaded = false;

function handlePageLoad(sender, args){
    plugin = sender.getHost().content;
    //debug = plugin.findName("debug");
    
    // Create a Downloader object.
    var downloader = sender.getHost().createObject("downloader");

    // Add DownloadProgressChanged and Completed events.
    downloader.addEventListener("downloadProgressChanged", onDownloadProgressChanged);
    downloader.addEventListener("completed", onCompleted);
    downloader.open("GET", "frog_images.zip");	
    // Execute the Downloader request.
    downloader.send();    
   
}

var currently_animating = new Array();
//add listener
function grassRollOver(sender, args){

	var which_grass = sender.Name.split("Hotspot")[0];
	
	if(currently_animating.length>0)	//	else join() will throw an error
	{
		var exploded_array = currently_animating.join(" ");	// could do an array search....

		//check that it's not already playing     
		if(exploded_array.indexOf(which_grass) == -1){
		//	not aniamting
			sender.AddEventListener("MouseMove", "grassMove");
		}

	}
	else	//	not animating by default
		sender.AddEventListener("MouseMove", "grassMove");

	
	/*
	if(currently_animating.indexOf(which_grass) == -1){
		sender.AddEventListener("MouseMove", "grassMove");
	    }
	*/
	

	
    //debug.Text += "grassRollOver - " + currently_animating + "\n";
    
}
// detect direction
var mouseX = "";

function grassMove(sender, args){

	var which_grass = sender.Name.split("Hotspot")[0];
    //debug.Text += "grassMove - " + sender.Name + " x: " + mouseX + "\n";
    if(mouseX == ""){
        mouseX = args.getPosition(null).x;
    } else {
        var direction;
        if(args.getPosition(null).x < mouseX){ //moving left
            direction = "Right";
        } else {
            direction = "Left";
        }
        //plugin.findName(which_grass+""+direction).Stop();
		if(which_grass != "loading_block" && loaded)	//	cos if rollover too quickly, sender.name can be 'loading_block' which will throw an error at findName or 'which_grass' not found yet
		{
			plugin.findName(which_grass+""+direction).Begin();
			currently_animating.push(which_grass);
			//tidy up & remove listener
			mouseX = ""; //reset mouseX
			sender.RemoveEventListener("MouseMove", "grassMove");
		}
	}
    
}

//end of storyboard
function grassStoryboardEnd(sender, args){
    
		
	var which_grass = sender.Name.substr(11);	
	var array_loc;	
	
	for(i=0;i++;i<currently_animating.length)
	{
		if(currently_animating[i] == which_grass)
			array_loc = i;
	}	

    currently_animating.splice(array_loc, 1);
	
	
	/*
	var which_grass = sender.Name.substr(11);
    var array_loc = currently_animating.indexOf(which_grass);
    currently_animating.splice(array_loc, 1);
*/
	}

// Event handler for the Completed event.
function onCompleted(sender, args){

    // Retrieve the Image object & set the Source property to the contents of the downloaded object,
    // In this case, since the downloaded content represents a single image file, promo.png,
    // the part parameter is set to an empty string.
   // sender.findName("movingGrass_16Image").setSource(sender, "grass_d.png");
     //sender.findName("movingGrass_17Image").setSource(sender, "grass_2.png");
    
    // start fade in storyboard

    plugin.findName("loading_complete").Begin();
	loaded = true;
	
}

// called by storyboard loading_complete
function toggleVisibility(sender, args){
     plugin.findName("loading_block").Visibility = "Collapsed";
}

function onDownloadProgressChanged(sender, args){
    // Calculate the downloaded percentage.
    var percentage = Math.floor(sender.downloadProgress * 100);

    // Update the Rectangle and TextBlock objects of the visual progress indicator.
    plugin.findName("loading_text").text = "Loading " + percentage + "%";
}