﻿var cleanables = Array();
var t
function timedCount()
{
 t=setTimeout("timedCount()",2000);
 if (AGLOADED)
 {
    clearTimeout(t);
   var productArray = new Array();
   var plugIn = document.getElementById("SilverlightControlMaster");
   productArray[productArray.length]= new Products("Starter");
   productArray[productArray.length]= new Products("Lite");
   productArray[productArray.length]= new Products("Full");
   productArray[productArray.length]= new Products("Silverlight Facelift");
   new ProductHandler(plugIn,productArray);


 }
}


function StartEvents()
{
   timedCount();
}



Products = function(name) {
 this.Name=name;
 this.Text="";
 this.URI ="";
 
}

ProductHandler = function(plugin,prods)
{
   this.productArray=prods;
   this.menuItems = new Array();
   this.plugin=plugin;
   this.textHolder=null;
   this.productPanel=null;
   this.scrollorigHeight=0;
       // the photos
   this.resourceArray = new Array();
   this.resourceArray[this.resourceArray.length] ="xaml/main/products.xaml";
   this.resourceArray[this.resourceArray.length] ="xaml/main/scroll.xaml";
   //this.resourceArray[this.resourceArray.length] ="xaml/main/scrolltb.xaml";
   this.resourceArray[this.resourceArray.length] ="xaml/main/button.xaml";

   this.currentDownload=0;
    
    // download the products GUI
    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();
    
    
}

ProductHandler.prototype.textdownloadCompleted =function(s,e)
{
    var xamlFragment = s.ResponseText;

   var tb=this.textHolder.findName("ProdTextDisp");
   tb.text=xamlFragment;

  this.downloader.removeEventListener("completed",this.downloadToken);
  this.textHolder.findName("scrollThumbBackground").height=this.scrollorigHeight;
  var scrollThumb = this.textHolder.findName("scrollThumb");
  var scrollThumbBackground = this.textHolder.findName("scrollThumbBackground");
  new scrollbar(scrollThumb, scrollThumbBackground, "#FF6D6D6D", "#FF595959", Silverlight.createDelegate(this, this.onScrollDrag)); 
  this.calculateScrollThumbSize(tb.actualHeight+100);
  
  var sb =this.productPanel.findName("ShowText"); 
  
  sb.stop();
  sb.begin();
  this.textHolder.opacity=1;
 
}

ProductHandler.prototype.onScrollDrag = function(sender, eventArgs) {
   // alert(eventArgs); 
    if (eventArgs <= 10) { 
        sender.findName("scrollThumb")["Canvas.Top"] = 10;
    }
    else if (eventArgs >= 300 - this.thumbSize) {
        sender.findName("scrollThumb")["Canvas.Top"] = 300 - this.thumbSize;
    }
    else {
        sender.findName("scrollThumb")["Canvas.Top"] = eventArgs;
    }
    sender.findName("itemsContainer")["Canvas.top"] = 10 - sender.findName("scrollThumb")["Canvas.Top"];   
}


ProductHandler.prototype.calculateScrollThumbSize = function(_total) {
    var thumbBackground = this.textHolder.findName("scrollThumbBackground");
    thumbScale = this.textHolder.findName("itemsContainer").height / _total;
    
    if (thumbScale > 1.0) {
        thumbBackground.height = this.textHolder.findName("scrollThumbBackground").height;
    }
    else {
        thumbBackground.height = thumbScale * this.textHolder.findName("scrollThumbBackground").height;
    }
    this.thumbSize = thumbBackground.height;
}

ProductHandler.prototype.downloadCompleted =function(s,e)
{
   // Retrieve downloaded XAML content.
    var xamlFragment = s.ResponseText;
    
    switch (s.URI)
    {
      case "xaml/main/products.xaml":
       this.ProductUI = xamlFragment;
       break;
      case "xaml/main/scroll.xaml":
       this.scoll = xamlFragment;
       break; 
       
      case "xaml/main/scrolltb.xaml":
       this.scrolltb = xamlFragment;
       break;
      case "xaml/main/button.xaml":
       this.button = xamlFragment;
       break;         
                 
      
    }


    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);
        // add to the main canvas
        this.AddToMain(); 
    }

}

// Add to the main canvass
ProductHandler.prototype.AddToMain =function()
{
    // Add downloaded XAML content to the root Canvas plugin.
    var rootCanvas = this.plugin.content.findName("Scene");
    var productUI=this.plugin.content.createFromXaml(this.ProductUI,true);
    productUI["Canvas.Top"]=380;
    productUI["Canvas.Left"]=41.413;
    // add the menu items to this product UI
    var initTop=50;
    for (var i=0; i<this.productArray.length; i++){ 
     var menulinkItem= new MenuItem(this.plugin,Silverlight.createDelegate(this, this.clickHandler),this.menuItems);  
     menulinkItem.XamlString=this.button;
     menulinkItem.Product=this.productArray[i];
     menulinkItem.SetLabel(this.productArray[i].Name);
     menulinkItem.SetCoords(50,(i*menulinkItem.GetHeight()) + initTop);
     productUI.children.add(menulinkItem.xamlElement);
     this.menuItems[this.menuItems.length]=menulinkItem;
    }

     rootCanvas.children.add(productUI);
     
     // add the scroll box
     var prodTextHolder=productUI.findName("TextHolder");
     
     var scrBar = this.plugin.content.createFromXaml(this.scoll,true);
    
     prodTextHolder.children.add(scrBar);
     cleanables[cleanables.length]=productUI;
     this.textHolder=scrBar;
     this.scrollorigHeight=scrBar.findName("scrollThumbBackground").height;
     this.productPanel=productUI;
}

ProductHandler.prototype.clickHandler = function(sender)
{
  // display the text
  switch (sender.Name)
  {
    case "Starter":
      // download the products GUI
      this.downloader = this.plugin.createObject("Downloader");
      this.downloadToken=this.downloader.addEventListener("completed", Silverlight.createDelegate(this, this.textdownloadCompleted));
      this.downloader.open("GET", "texts/starter.txt");
      this.downloader.send();
    break;
    case "Lite":
      // download the products GUI
      this.downloader = this.plugin.createObject("Downloader");
      this.downloadToken=this.downloader.addEventListener("completed", Silverlight.createDelegate(this, this.textdownloadCompleted));
      this.downloader.open("GET", "texts/lite.txt");
      this.downloader.send();
    break;
    case "Full":
      // download the products GUI
      this.downloader = this.plugin.createObject("Downloader");
      this.downloadToken=this.downloader.addEventListener("completed", Silverlight.createDelegate(this, this.textdownloadCompleted));
      this.downloader.open("GET", "texts/full.txt");
      this.downloader.send();
    break;  
    case "Silverlight Facelift":
      // download the products GUI
      this.downloader = this.plugin.createObject("Downloader");
      this.downloadToken=this.downloader.addEventListener("completed", Silverlight.createDelegate(this, this.textdownloadCompleted));
      this.downloader.open("GET", "texts/upgrade.txt");
      this.downloader.send();
    break;          
  
  }  
     this.textHolder.opacity=0;
     this.textHolder.findName("Title").text= sender.Name;
}


MenuItem = function(plugin,clickHandler,menItems) {
 this.menuItems=menItems;
 this.plugin=plugin;
 this.xamlElement=null;
 this.Top=0;
 this.Left=0;
 this.NormalScaleX=1;
 this.NormalScaleY=1;
 
 this.Label="";
 this.XamlString="";
 this.clickHandler=clickHandler;
 this.Name="";
 this.Product=null;
}

MenuItem.prototype.GetHeight = function()
{ 
   if (this.xamlElement!=null)
  {
    return this.xamlElement.height;
  }

}
MenuItem.prototype.SetCoords = function(x,y)
{
  if (this.xamlElement!=null)
  {
    this.xamlElement["Canvas.Top"]=y;
    this.xamlElement["Canvas.Left"]=x;
  }
}

MenuItem.prototype.xamlPath = function(sPath) 
{
 // path of xaml, so download it
 this.downloader=this.plugin.createObject("Downloader");
 this.remToken=this.downloader.addEventListener("completed", Silverlight.createDelegate(this, this.downloadCompleted));
 this.downloader.open("GET",sPath);
 this.downloader.send();
}

MenuItem.prototype.downloadCompleted = function(s,e)
{
  this.downloader.removeEventListener("completed",this.remToken);
  this.XamlString=s.getResponseText("");
}

MenuItem.prototype.SetLabel = function(sLabel)
{
  this.Name=sLabel;
  this.XamlString =this.XamlString.replace(/Button/g,sLabel);
  this.xamlElement = this.plugin.content.createFromXaml(this.XamlString,true);
  if (this.xamlElement==null)
  {
   alert("XAML not downloaded!");
   return;
  }
  var btnText=this.xamlElement.findName("btnText")
  btnText.text=sLabel;
  // measure width and try to center
  btnText["Canvas.Left"] = (this.xamlElement.width/2) - (btnText.ActualWidth/2);
  // add the effect 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));
  // todo: remove these handlers
  
  var sTransform = this.xamlElement.findName("scale");
  sTransform.scaleX=this.NormalScaleX;
  sTransform.scaleY=this.NormalScaleY;
}

MenuItem.prototype.btn_MouseEnter = function(sender, args) 
{
	var txt = sender.Name;
	var sb = this.xamlElement.findName("sbHigh");
	sb.stop();
	sb["Storyboard.TargetName"] ="light";
	sb.begin();
	
	// which item is hovered?
	var hovered=0;
	for (var i=0; i<this.menuItems.length; i++){
	  if (sender.Name==this.menuItems[i].Name)
	  {
	    hovered=i;
	    break;
	  }
	}
	var scaleFactor= 1.0;
	for (var j=0; j<this.menuItems.length; j++){
	  scaleFactor= 1.0
	  if (j<hovered)
	  {
	    scaleFactor=((j+1)/(hovered+1))*1.2;
	  }
	  if (j>hovered)
	  {
	    scaleFactor=((hovered+1)/(j+1))*1.2;
	  }
	  // scale the things
	  var otherItem = this.menuItems[j].xamlElement;
	  otherItem.findName("InitX").value=otherItem.findName("FinalX").value;
	  otherItem.findName("InitY").value=otherItem.findName("FinalY").value;
	  otherItem.findName("FinalX").value=scaleFactor * this.menuItems[j].NormalScaleX;
	  otherItem.findName("FinalY").value=scaleFactor * this.menuItems[j].NormalScaleY;
	  var zoom = otherItem.findName("sbgoBig");
	  zoom.stop();
	  zoom.begin();
	}

}

MenuItem.prototype.btn_MouseLeave = function(sender, args)
{
	var txt = sender.Name;
	var sb = this.xamlElement.findName("sbOff");
	sb.stop();
	sb["Storyboard.TargetName"] ="light";
	sb.begin()
}

MenuItem.prototype.btn_MouseLeftButtonDown = function(sender, args)
{
	var txt = sender.Name;
	var sb = this.xamlElement.findName("sbOff");
	sb.stop();
	sb["Storyboard.TargetName"] ="light";
	sb.begin()
}	

MenuItem.prototype.btn_MouseLeftButtonUp = function(sender, args)
{
   
     if (this.clickHandler) {
	        this.clickHandler(this);
	 }
	var txt = sender.Name;
	var sb = this.xamlElement.findName("sbHigh");
	sb.stop();
	sb["Storyboard.TargetName"] ="light";
	sb.begin();
}
