﻿/// <reference name="js/ProductsMain.js" />
var cleanables = Array();
function createSilverlightMain()
{
	var scenes = new Scene();
	Silverlight.createObjectEx({
		source: "xaml/main/Scene.xaml",
		parentElement: document.getElementById("agMain"),
		id: "SilverlightControl",
		properties: {
			width: "997px",
			height: "806px",
			version: "1.0",
			inplaceInstallPrompt: "true", 
			isWindowless: "false",
			background:'#FFFFFFFF'
		},
		events: {
			onLoad: Silverlight.createDelegate(scenes, scenes.handleLoad)
		}
	});
}

if (!window.Sys)
	window.Sys = {};
	
if (!window.Silverlight) 
	window.Silverlight = {};

Silverlight.createDelegate = function(instance, method) {
	return function() {
        return method.apply(instance, arguments);
    }
}

// Scene classes

Scene = function() 
{
 this.productArray = new Array();
 this.productArray[this.productArray.length]= new Products("Starter");
 this.productArray[this.productArray.length]= new Products("Lite");
 this.productArray[this.productArray.length]= new Products("Full");
 this.productArray[this.productArray.length]= new Products("Silverlight Facelift");
 
}

Scene.prototype.handleLoad = function(control, userContext, rootElement)
{
   // show the bugger
   document.getElementById("agMain").style.width="997px";
   document.getElementById("agMain").style.height="806px";
   this.plugIn = control;      // Store the host plug-in
   this.stillAnimating = true;
   
   this.AddMenuHandlers();
   // the photos
   this.resourceArray = new Array();
   this.resourceArray[this.resourceArray.length] ="images/front.png";
   this.resourceArray[this.resourceArray.length] ="images/front1.png";
   this.resourceArray[this.resourceArray.length] ="images/side.png";
   this.resourceArray[this.resourceArray.length] ="images/final.png";
   this.resourceArray[this.resourceArray.length] ="images/watch.png";
   this.resourceArray[this.resourceArray.length] ="images/key copy.png";
   this.currentDownload=0;
   // Animate the laptops
   this.downloadAssets();
   // setup the packages
   new Packages(this.plugIn);
   
   // setup cleanables
   cleanables = new Array();
   cleanables[cleanables.length]= this.plugIn.content.root.findName("Glass");
   cleanables[cleanables.length]= this.plugIn.content.root.findName("Starter");
   cleanables[cleanables.length]= this.plugIn.content.root.findName("Full");
   cleanables[cleanables.length]= this.plugIn.content.root.findName("Lite");
   cleanables[cleanables.length]= this.plugIn.content.root.findName("Upgrades");
      
}
Scene.prototype.downloadAssets = function()
{
    this.downloader = this.plugIn.createObject("Downloader");
    this.downloadToken=this.downloader.addEventListener("completed", Silverlight.createDelegate(this, this.downloadCompleted));
    this.downloader.open("GET", this.resourceArray[this.currentDownload]);
    this.downloader.send();
}
Scene.prototype.downloadCompleted = function(sender, args)
{
    this.currentDownload++;
    if (this.currentDownload < this.resourceArray.length)
    {
        this.downloader.open("GET", this.resourceArray[this.currentDownload]);
        this.downloader.send();
    }
    else
    {
        this.downloader.removeEventListener("completed",this.downloadToken);
        // animate
        this.Animate();
    }
}

Scene.prototype.Animate = function()
{
  // hard coded
  var sb =this.plugIn.content.findName("enterFront");
  // add handler
  this.sbToken=sb.addEventListener("completed",Silverlight.createDelegate(this, this.frontCompleted));
  var front =this.plugIn.content.findName("front");
  front.Visibility="Visible";
  sb.stop();
  sb.begin();
  
}

Scene.prototype.frontCompleted = function(s,e)
{
  //clean
  var sb =this.plugIn.content.findName("enterFront");
  sb.removeEventListener("completed",this.sbToken);
  var front =this.plugIn.content.findName("front");
  front.Visibility="Collapsed";
  // hard coded
  sb =this.plugIn.content.findName("enterfront1");
  // add handler
  this.sbToken=sb.addEventListener("completed",Silverlight.createDelegate(this, this.front1Completed));
  front =this.plugIn.content.findName("front1");
  front.Visibility="Visible";
  sb.stop();
  sb.begin();
  
}

Scene.prototype.front1Completed = function(s,e)
{
  //clean
  var sb =this.plugIn.content.findName("enterfront1");
  sb.removeEventListener("completed",this.sbToken);
  var front =this.plugIn.content.findName("front1");
  front.Visibility="Collapsed";
  // hard coded
  sb =this.plugIn.content.findName("enterSide");
  // add handler
  this.sbToken=sb.addEventListener("completed",Silverlight.createDelegate(this, this.sideCompleted));
  front =this.plugIn.content.findName("side");
  front.Visibility="Visible";
  sb.stop();
  sb.begin();
  
}
Scene.prototype.sideCompleted = function(s,e)
{
  //clean
  var sb =this.plugIn.content.findName("enterSide");
  sb.removeEventListener("completed",this.sbToken);
  var front =this.plugIn.content.findName("side");
  front.Visibility="Collapsed";
  // hard coded
  sb =this.plugIn.content.findName("ShowLaptop");
  // add handler
  this.sbToken=sb.addEventListener("completed",Silverlight.createDelegate(this, this.showCompleted));
  front =this.plugIn.content.findName("Laptop");
  front.Visibility="Visible";
  sb.stop();
  sb.begin();
  this.stillAnimating=false;
}

Scene.prototype.showCompleted = function(s,e)
{
  //clean
  var sb =this.plugIn.content.findName("ShowLaptop");
  sb.removeEventListener("completed",this.sbToken);
  sb =this.plugIn.content.findName("showMain");
  sb.stop();
  sb.begin();
  var sb1=this.plugIn.content.findName("showElements");
  sb1.stop();
  sb1.begin();
  
 
}


Scene.prototype.AddMenuHandlers = function()
{
  // find home
 
  var tab = this.plugIn.content.findName("Home");
  new MenuLinkItem(tab,Silverlight.createDelegate(this, this.onMenuItemClicked));
  
  var prod = this.plugIn.content.findName("Products");
  new MenuLinkItem(prod,Silverlight.createDelegate(this, this.onMenuItemClicked));
 
  var serv = this.plugIn.content.findName("Services");
  new MenuLinkItem(serv,Silverlight.createDelegate(this, this.onMenuItemClicked));
 
  var ab = this.plugIn.content.findName("AboutUs");
  new MenuLinkItem(ab,Silverlight.createDelegate(this, this.onMenuItemClicked));


}

Scene.prototype.cleanup = function()
{
  for (var i=0; i<cleanables.length; i++){
    
    this.plugIn.content.root.children.remove(cleanables[i]);
  }
}
Scene.prototype.onMenuItemClicked = function(s,e)
{
  //alert(s.Name);
  if (this.stillAnimating)
    return;
  switch (s.Name)
  {
    case "Home":
     this.cleanup();
     // 
     top.location="http://" + top.location.host + "/" + "default.aspx" ;
     break;
    case "Products":
     // go to products page 
      this.cleanup();
      cleanables=new Array();
      new ProductHandler(this.plugIn,this.productArray);

     break;
    case "Services":
      top.location="http://" + top.location.host + "/" + "gallery.aspx" ;
     break;
    case "AboutUs":
     if (top.location.toString().toLowerCase()!= "http://" + top.location.host + "/" + "aboutus.aspx")
      top.location="http://" + top.location.host + "/" + "aboutus.aspx" ;  
     break  
  }
  
}

// Button Class

MenuLinkItem = function(button,handler)
{
      this.xamlElement=button;
      this.clickHandler=handler;
      this.Name=button.Name;
      // add handlers
      this.xamlElement.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.btn_MouseLeftButtonUp));
	  this.xamlElement.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.btn_MouseLeftButtonDown));
	  this.xamlElement.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.btn_MouseEnter));
	  this.xamlElement.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.btn_MouseLeave));

}

MenuLinkItem.prototype.btn_MouseEnter = function(sender, args) 
{
    var plugin = sender.getHost();

	var txt = sender.Name + "_Over";
	var ov= plugin.content.findName(txt);
	ov.Visibility="Visible";
	var sb = plugin.content.findName("MouseEnter");
	sb.stop();
	sb["Storyboard.TargetName"] =  txt;
	sb.begin();
}

MenuLinkItem.prototype.btn_MouseLeave = function(sender, args)
{
    var plugin = sender.getHost();
	var txt = sender.Name;
	var sb = plugin.content.findName("MouseLeave");
	sb.stop();
	sb["Storyboard.TargetName"] =  sender.Name + "_Over";
	sb.begin();
}

MenuLinkItem.prototype.btn_MouseLeftButtonDown = function(sender, args)
{
    var plugin = sender.getHost();
	var txt = sender.Name;
	var sb = plugin.content.findName("MouseEnter");
	sb.stop();
	sb["Storyboard.TargetName"] =  sender.Name + "_Over";
	sb.begin();
}	

MenuLinkItem.prototype.btn_MouseLeftButtonUp = function(sender, args)
{
     var plugin = sender.getHost();
     var txt = sender.Name;
     if (this.clickHandler) {
	        this.clickHandler(this);
	 }
	
	var sb = plugin.content.findName("MouseEnter");
	sb.stop();
	sb["Storyboard.TargetName"] =  sender.Name + "_Over";
	sb.begin();
	
	sb = plugin.content.findName("MouseLeave");
	sb.stop();
	sb["Storyboard.TargetName"] =  sender.Name + "_Over";
	sb.begin();
}