﻿if (!window.Custom)
	window.Custom = {};

var offset = 0;
var timerId = null;

Custom.AccordionMenu = function(container, width, animated) {
    this.container = container;
	this.control = null;
	this.userContext = null;
	this.root = null;
	this.animated = false;
	this.id = 0;	
	this.menuBlocks = new Array();
	this.menuObj = new Array();
	this.menuItem = new Array();
	this.animated = animated;
	this.marginBottom = 40;
	this.accordionBottom = null;
	this.heightBottom = 0;
	this.menuActive = -1;
	this.xamlMenuTab = null;
	this.xamlItem = null;
	this.animated = animated;
	this.width = width;
	//this.timerId = null;
	this.load(container, width, animated, this.marginBottom);
	
}



Custom.AccordionMenu.prototype = {
	load: function(container, width, animated, marginBottom) {
	    this.animated = animated;
	    this.populateMenu(container, width, animated, marginBottom);
	},
	enableTimer: function(){
	    this.timer(0);
    },
    disableTimer: function(){
        if(timerId != null) {
          clearTimeout(timerId);
          timerId  = null;
        }
    },
	handleLoad: function(control, userContext, rootElement) {
		this.control = control;
		this.userContext = userContext;
		this.root = rootElement;		
		var downloader = this.control.createObject("downloader");
		//downloader.addEventListener("downloadProgressChanged", onDownloaderProgressChanged);
		downloader.addEventListener("completed", Silverlight.createDelegate(this, this.onDownloaderCompleted));
		downloader.open("GET", "Controls/ApplicationMenu/AccordionMenu.zip");
		downloader.send();
	},
	onDownloaderProgressChanged: function(sender, eventArgs)   
	{   
		var percentage = Math.floor(sender.downloadProgress * 100);   
	},
	onDownloaderCompleted: function(sender, eventArgs) {
		this.xamlMenuTab = sender.getResponseText("MenuTab.xaml"); 
		this.xamlItem = sender.getResponseText("Item.xaml"); 		
		this.buildMenu();
		this.uniqueAnimationName=0; 
//		this.timer(0);
	},
	timer: function(id){
        var temp;	        
        if(id < this.menuBlocks.length) {
            temp = id+1;
        }
        else {
            temp = 0;
        }
        this.OpenTab(id);  
        var self = this;	      
        timerId = window.setTimeout(function(){ self.timer(temp);},2000);     
	},
	
	populateMenu: function(container, width, animated, marginBottom) {
	
	    var AppMenuService = new DAGSystem.Showcase.Web.UI.AppMenuService();
	   
	    var applications = new Array();
    	AppMenuService.GetAllApplications(1, this.getApplications, null, this);
    },
    
    getApplications: function(applications, accordionMenu) {
        for(var i=0; i <applications.length; i++)
        {
            accordionMenu.menuBlocks[i] = new Object();
            accordionMenu.menuBlocks[i].name = applications[i].Title;
	        accordionMenu.menuBlocks[i].item = new Object();
	        accordionMenu.menuBlocks[i].item.title = applications[i].Title;
	        accordionMenu.menuBlocks[i].item.desc = applications[i].Description;
	        accordionMenu.menuBlocks[i].item.Id = applications[i].Id;
	        accordionMenu.menuBlocks[i].item.link = "application.aspx?Id=" + applications[i].Id;
	        
	        if(applications[i].MainImage == null)
    	        accordionMenu.menuBlocks[i].item.image = "ImagesMedia/logo.jpg";
    	    else
    	        accordionMenu.menuBlocks[i].item.image = applications[i].MainImage;
	    } 
		accordionMenu.totalTabs = accordionMenu.menuBlocks.length;

		document.getElementById(accordionMenu.container).style.width = "100%";
		document.getElementById(accordionMenu.container).style.height = (accordionMenu.totalTabs * 36) + 87 + accordionMenu.marginBottom;
		Silverlight.createObjectEx({
			source: "Controls/ApplicationMenu/AccordionMenu.xaml",
			parentElement: document.getElementById(accordionMenu.container),
			id: "SilverlightControl2",
			properties: {
				width: "100%",
				height: (accordionMenu.totalTabs * 36) + 87 + accordionMenu.marginBottom,
				version: "1.0"
			},
			events: {
				onLoad: Silverlight.createDelegate(accordionMenu, accordionMenu.handleLoad)
		}});    	
		
		//this.buildMenu();
	},
	buildMenu: function() {
		this.numMenu = 0;
		for (var i=0; i < this.menuBlocks.length; i++) {
			this.loadTab(this.menuBlocks[i].name);
		}
	},
	loadTab: function(name) {
		this.menuItem[this.numMenu] = this.control.content.createFromXaml(this.xamlMenuTab, true);
		var accordion = this.root.FindName("accordion");
		var m = accordion.children.add(this.menuItem[this.numMenu]);
		this.menuItem[this.numMenu]["Canvas.Top"] = this.numMenu*36;
		this.menuObj[this.numMenu] = new Custom.AccordionTab(this.control, this, this.menuItem[this.numMenu], this.numMenu, this.menu, this.menuItem, this.animated);
		this.numMenu++;
		if (this.numMenu == this.menuBlocks.length) {
			this.loadItemXAML();
			this.accordionBottom = this.root.FindName("accordionBottom");
			this.heightBottom = 80+this.marginBottom+offset+46;
			this.accordionBottom["Height"] = this.heightBottom;
			this.accordionBottom["Canvas.Top"] = (this.menuBlocks.length)*36;
		}
	},
	loadItemXAML: function(tab, id) {
		var xamlItem = this.xamlItem;
		for (var i=0; i < this.menuBlocks.length; i++) {
			var bg = this.menuItem[i].FindName("bg");
			bg.Height = 80 + 78;
			//bg.Height = 200;
				var container = this.menuItem[i].FindName("tabRoot");
				var item = this.control.content.createFromXaml(xamlItem, true);
				var e = container.children.add(item);
				item["Canvas.Top"] = 50;
				item["Canvas.Left"] = 25;
				var title = item.FindName("title");
				var desc = item.FindName("desc");
				desc.width = this.width-30;
				var thumb = item.FindName("thumb");
				//title.text = this.menuBlocks[i].items[j].title;
				desc.text = this.menuBlocks[i].item.desc;
				thumb.source = this.menuBlocks[i].item.image;
				new Custom.PageItem(this.control, this, item, i, 0, this.menuBlocks[i].item);
			/*
			for (var j=0; j < this.menuBlocks[i].items.length; j++) {
				var container = this.menuItem[i].FindName("tabRoot");
				var item = this.control.content.createFromXaml(xamlItem, true);
				var e = container.children.add(item);
				item["Canvas.Top"] = 50+(j*80);
				item["Canvas.Left"] = 25;
				var title = item.FindName("title");
				var desc = item.FindName("desc");
				desc.width = this.width-82;
				var thumb = item.FindName("thumb");
				//title.text = this.menuBlocks[i].items[j].title;
				desc.text = this.menuBlocks[i].items[j].desc;
				//thumb.source = this.menuBlocks[i].items[j].image;
				new Custom.PageItem(this.control, this, item, i, j, this.menuBlocks[i].items[j]);
			}
	        */
		}
	},
	OpenTab: function(id) {
		this.menuActive = id;
		this.deselectAllTabs();
		if (this.menuBlocks.length != 0) {
			if (id < (this.menuBlocks.length-1)) {
				for (var i=1; i <= id; i++) {
					tabRoot = this.menuItem[i].FindName("tabRoot");
					this.DoubleAnimationKeyFrame(this.control, tabRoot, "tabRoot", "(Canvas.Top)", i*36+offset);
				}
				for (var i=(id+1); i < this.menuBlocks.length; i++) {
					tabRoot = this.menuItem[i].FindName("tabRoot");
					this.DoubleAnimationKeyFrame(this.control, tabRoot, "tabRoot", "(Canvas.Top)", (i*36)+80+this.marginBottom+offset);
				}
			}
			if(id == (this.menuBlocks.length-1)) {
				if(this.menuActive == -1) {
					this.DoubleAnimationKeyFrame(this.control, this.accordionBottom, "accordionBottom", "(Canvas.Top)", (this.menuBlocks.length*36)+80+this.marginBottom+offset);
				} else {
					this.DoubleAnimationKeyFrame(this.control, this.accordionBottom, "accordionBottom", "(Height)", 0);
				}
			} else {
					this.DoubleAnimationKeyFrame(this.control, this.accordionBottom, "accordionBottom", "(Canvas.Top)", (this.menuBlocks.length*36)+80+this.marginBottom+offset);
					this.DoubleAnimationKeyFrame(this.control, this.accordionBottom, "accordionBottom", "(Height)", 80+this.marginBottom+offset+45);
			}
		}
	},
	CloseTab: function(id) {
		var offset = 0;
		if (this.menuBlocks.length != 0) {
			for (var i=(id+1); i < this.menuBlocks.length; i++) {
				tabRoot = this.menuItem[i].FindName("tabRoot");
				this.DoubleAnimationKeyFrame(this.control, tabRoot, "tabRoot", "(Canvas.Top)", (i*36+offset));
			}
			//root.expandPage( (menuBlocks.length*36)+530+offset+90 );
			this.DoubleAnimationKeyFrame(this.control, this.accordionBottom, "accordionBottom", "(Canvas.Top)", (this.menuBlocks.length)*36);
			this.DoubleAnimationKeyFrame(this.control, this.accordionBottom, "accordionBottom", "(Height)", this.heightBottom);
			this.menuActive = -1;
		}
	},
	deselectAllTabs: function() {
		if (this.menuBlocks.length != 0) {
			for (var i=0; i < this.menuBlocks.length; i++) {
				this.menuObj[i].deselectMe();
			}
		}
	},
	DoubleAnimationKeyFrame: function(silverlightControl, target, nameElement, targetProperty, value) {
		this.uniqueAnimationName++;
		var xaml =  "<Canvas>"+
				"<Canvas.Triggers>"+
					"<EventTrigger>"+
						"<EventTrigger.Actions>"+
							"<BeginStoryboard>"+    
								"<Storyboard xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='_Generated_Animation_"+this.uniqueAnimationName+"' BeginTime='0'>"+
									"<DoubleAnimationUsingKeyFrames BeginTime='00:00:00' Storyboard.TargetName='"+nameElement+"' Storyboard.TargetProperty='"+targetProperty+"'>"+
										"<SplineDoubleKeyFrame KeyTime='00:00:00.5' KeySpline='0.5,0,0.5,1' Value='"+value+"'/>"+
									"</DoubleAnimationUsingKeyFrames>"+
								"</Storyboard>"+
							"</BeginStoryboard>"+
						"</EventTrigger.Actions>"+    
					"</EventTrigger>"+
				"</Canvas.Triggers>"+ 
		"</Canvas>";  
		var animationCanvas = silverlightControl.content.createFromXaml(xaml);
		target.children.add(animationCanvas);
		return silverlightControl.content.findName("_Generated_Animation_"+this.uniqueAnimationName);
	}

}

Custom.PageItem = function(control, page, target, menuId, id, menuItem) {
	this.control = control;
	this.page = page;
	this.id = id;
	this.menuId = menuId;
	this.target = target;
	this.menuItem = menuItem;
	
	this.enter = this.target.findName("animMouseEnter");
	this.leave = this.target.findName("animMouseLeave");
	this.down = this.target.findName("mouseDown");
	var SeeDetails = this.target.findName("linkDetails");
	var SeePhoto = this.target.findName("linkPhoto");
	
	SeeDetails.cursor = "hand";
	SeePhoto.cursor = "hand";

	SeePhoto.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleChangeSlideShowSource));
	SeeDetails.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
	
	this.target.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.handleMouseEnter));
	this.target.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleMouseLeave));
//	this.target.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
//	this.target.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMouseUp));
}

Custom.PageItem.prototype =
{
    handleLoad: function(control, userContext, rootElement) {
    },
    handleMouseEnter: function(sender, eventArgs) {
        this.enter.begin();
    },

    handleMouseLeave: function(sender, eventArgs) {
        this.leave.begin();
    },
    handleMouseDown: function(sender, eventArgs) {
        this.down.Visibility = "Visible";
        document.location.href = this.menuItem.link;
    },
    handleMouseUp: function(sender, eventArgs) {
        this.down.Visibility = "Collapsed";
    },
    handleChangeSlideShowSource: function(sender, eventArgs) {
    window.open("ApplicationMediaPopUp.aspx?Id=" + this.menuItem.Id, null, 'width=1030, height=750, status=no, resizable= yes, scrollbars= yes, toolbar= no,location= no, menubar= no');
    }
}

Custom.AccordionTab = function(control, page, tab, id, animated) {
	this.control = control;
	this.page = page;
	this.tab = tab;
	this.id = id;
	this.menuBlocks = page.menuBlocks;
	this.menuItem = page.menuItem;
	this.animated = animated;
	
	this.open = false;
	
	var title = tab.FindName("title");
	if (this.menuBlocks.length != 0) {
		title.text = this.menuBlocks[id].name;
	}
	
	this.down = tab.FindName("tabDown");
	this.tabOpen = tab.FindName("tabOpen");
	this.reflex = tab.FindName("reflex");
	this.puce = tab.FindName("puce");
	
	if(animated==true) {
		this.puce.Visibility = "Collapsed";
	}
	
	this.chevronOpen = tab.FindName("chevronOpen");
	this.chevronClose = tab.FindName("chevronClose");
	
	this.target = tab.FindName("tabContainer");
	
	this.target.cursor = "hand";
	
	this.target.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.handleMouseEnter));
	this.target.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleMouseLeave));
	this.target.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
	this.target.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMouseUp));
}

Custom.AccordionTab.prototype = {
	handleMouseEnter: function(sender, eventArgs) {
		this.tab.findName("animTabEnter").Begin();
	},
	handleMouseLeave: function(sender, eventArgs) {
		this.tab.findName("animTabLeave").Begin();
	},
	handleMouseDown: function(sender, eventArgs) {
		this.down.Visibility = "Visible";
	},
	handleMouseUp: function(sender, eventArgs) {
		this.down.Visibility = "Collapsed";
		if (this.open) {
			this.page.CloseTab(this.id);
			this.tabOpen.Visibility = "Collapsed";
			this.reflex.Visibility = "Collapsed";
			if(this.animated == true) {
				this.chevronOpen.Visibility = "Visible";
				this.chevronClose.Visibility = "Collapsed";
			}
			this.open = false;
		} else {
			this.page.OpenTab(this.id);
			this.tabOpen.Visibility = "Visible";
			this.reflex.Visibility = "Visible";
			if(this.animated == true) {
				this.chevronOpen.Visibility = "Collapsed";
				this.chevronClose.Visibility = "Visible";
			}
			this.open = true;
		}
	},
	deselectMe: function() {
		this.tabOpen.Visibility = "Collapsed";
		this.reflex.Visibility = "Collapsed";
		if(this.animated == true) {
			this.chevronOpen.Visibility = "Visible";
			this.chevronClose.Visibility = "Collapsed";
		}
		this.page.CloseTab(this.id);
		this.open = false;
	}
}
