﻿if (!window.caseStudyCarousel)
	window.caseStudyCarousel = {};

caseStudyCarousel.Page = function() 
{
}

caseStudyCarousel.Page.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
	    
	    control.content.onFullScreenChange = fullScreenChange;
	
	    //Variables
	    this.xRadius = 250;
	    this.yRadius = 50;
	    this.itemsArray = new Array();
	    this.displayWindow = null;
	    this.spinInterval = null;
	    this.angle = 0;
	    this.delta =.005;
	    this.spinning = true;
	    this.selecting = false;
	    this.closing = false;
	    this.opened = false
	    /////////////////////////////
	    this.startAngle = 0;
	    this.changeAngle = 0;
	    this.totalTime = 50;
	    this.inc = 0;
	    /////////////////////////////
	
		this.control = control;
		this.videoPlayer = new videoPlayer(this.control, this);
		this.itemManager = new CanvasManager("Carousel", "itemHolder");
		this.createItems()
		
		this.player = this.control.content.findName("Player");	
		this.player.addEventListener("MouseMove", Silverlight.createDelegate(this, this.playerMouseMove));
		
		//////////////////////////////////////////////////////////////////////////////
		this.arrowHolder =this.control.content.findName("arrowHolder");
		
		this.leftArrow = this.control.content.findName("leftArrow");
		this.leftArrow.cursor = "Hand";	
		this.leftArrow.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.leftPress));
		
		this.rightArrow = this.control.content.findName("rightArrow");
		this.rightArrow.cursor = "Hand";		
		this.rightArrow.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.rightPress));
	},
	
	createItems: function() 
	{
	    this.itemNum = nodeNumber(xmlCarousel);
	    this.angleInc = (Math.PI*2)/this.itemNum
	    
	    
	    this.itemManager.loadXaml("/global/windowsserver2008/en/us/RichMedia/HVT/Carousel/xaml/displayWindow.xaml", 100, {Left:-180, Top:-260, Invoice:["xaml/displayWindow.deploy", "Segoe.ttf"], File: {init:displayWindow, params:this}});
	    for(var i=1; i<=this.itemNum; i++)
	    {
	        var myNode = sendNodes("item"+i, xmlCarousel);
	        var myAngle = (i*this.angleInc)-this.angleInc;
	        var myScale = .75+Math.cos(myAngle)*.25;
	        var myLeft = Math.sin(myAngle)*this.xRadius;
	        var myTop = Math.cos(myAngle)*(this.yRadius*myScale);
	        var myDepth = parseInt(Math.cos(myAngle)*100+100);
	        
		    this.itemManager.loadXaml("/global/windowsserver2008/en/us/RichMedia/HVT/Carousel/xaml/carouselItem.xaml", myDepth, {Left:myLeft, Top:myTop, ScaleX:myScale, ScaleY:myScale, File: {init:carouselItem, params:[myNode, myAngle, this]}});
		}
	},
	
	addItem: function(key)
	{
	    this.itemsArray.push(key)
	    if(this.itemsArray.length == this.itemNum && this.displayWindow != null && this.spinInterval == null && this.opened == false)
	    {
	        this.opened = true;
	        var myItemArray = this.itemManager.uriToObject("/global/windowsserver2008/en/us/RichMedia/HVT/Carousel/xaml/carouselItem.xaml");
	        myItemArray[0].selectedFirst();
	    }
	},
	
	addWindow: function(key)
	{   
	    this.displayWindow = key
	    if(this.itemsArray.length == this.itemNum && this.displayWindow != null && this.spinInterval == null && this.opened == false)
	    {
	        this.opened = true;
	        var myItemArray = this.itemManager.uriToObject("/global/windowsserver2008/en/us/RichMedia/HVT/Carousel/xaml/carouselItem.xaml");
	        myItemArray[0].selectedFirst();
	    }
	},
	
	alignCarousel: function()
	{   
	    this.angle = this.easeOutSine(this.inc, this.startAngle, this.changeAngle, this.totalTime);
	    this.moveCarousel();
	    if(this.inc >= this.totalTime)
	    {
	        var myItem = this.findFrontItem();
	        myItem.setDisplay();
	        this.selecting = false;
	        this.stop();
	    }
	    else{
	        this.inc++
	    }
	},
	
	rotateCarousel: function()
	{
	   this.angle += this.delta
	   this.moveCarousel(); 
	},
	
	moveCarousel: function()
	{
	    for(i=0; i<this.itemsArray.length; i++)
	    {
	        var myAngle = this.angle+this.itemsArray[i].angle;
	        var myScale = .75+Math.cos(myAngle)*.25;
	        var myLeft = Math.sin(myAngle)*this.xRadius;
	        var myTop = Math.cos(myAngle)*(this.yRadius*myScale);
	        var myDepth = parseInt(Math.cos(myAngle)*100+100);
	        this.itemsArray[i].setPosition(myLeft, myTop, myDepth, myScale);
	    }
	},
	
	playerMouseMove: function(sender, args)
	{
	    if(this.spinning == true)
	    {
	        if(args.getPosition(sender).x >=0 && args.getPosition(sender).x < 120)
	        {
	            this.delta = -.01;
	        }
	        else if(args.getPosition(sender).x >=120 && args.getPosition(sender).x < 240)
	        {
	            this.delta = -.005;
	        }
	        else if(args.getPosition(sender).x >=240 && args.getPosition(sender).x < 300)
	        {
	            this.delta = -.0025;
	        }
	        else if(args.getPosition(sender).x >=300 && args.getPosition(sender).x < 420)
	        {
	            this.delta = 0;
	        }
	        else if(args.getPosition(sender).x >=420 && args.getPosition(sender).x < 480)
	        {
	            this.delta = .0025;
	        }
	        else if(args.getPosition(sender).x >=480 && args.getPosition(sender).x < 600)
	        {
	            this.delta = .005;
	        }
	        else
	        {
	            this.delta = .01;
	        }
	    }
	},
	
	leftPress: function()
	{
	    if(!this.selecting)
	    {
	        this.selecting = true;
	        this.stop();
            this.angle = this.normalizeAngle(this.angle);
            this.changeAngle = -this.angleInc;
            this.startAngle = this.angle;
            this.inc = 0;
            this.spinInterval = setInterval(preserveScope(this, this.alignCarousel), 10)
        }
	},
	
	rightPress: function()
	{
	    if(!this.selecting)
	    {
	        this.selecting = true;
	        this.stop();
            this.angle = this.normalizeAngle(this.angle);
            this.changeAngle = this.angleInc;
            this.startAngle = this.angle;
            this.inc = 0;
            this.spinInterval = setInterval(preserveScope(this, this.alignCarousel), 10)
        }
	},
	
	start: function()
	{
	    this.spinInterval = setInterval(preserveScope(this, this.rotateCarousel), 10);
	},
	
	stop: function()
	{
	    clearInterval(this.spinInterval);
	},
	
	findFrontItem: function()
	{
	    var myItem = null;
	    for(var i=0; i< this.itemsArray.length; i++)
	    {
	        if(myItem == null)
	        {
	            myItem = this.itemsArray[i];
	        }
	        else if(this.itemsArray[i].item["Canvas.ZIndex"] > myItem.item["Canvas.ZIndex"])
	        {
	            myItem = this.itemsArray[i];
	        }
	    }
	    return myItem;
	},
	
	normalizeAngle: function(myAngle)
	{
	    while(myAngle > Math.PI*2 || myAngle < 0)
	    {
	        if(myAngle > Math.PI*2)
	        {
	            myAngle -= Math.PI*2;
	        }
	        else if(myAngle < 0)
	        {
	        myAngle += Math.PI*2;
	        }
	    }
	    return myAngle;
	},
	
	easeOutSine: function(t, b, c, d) 
	{return c * Math.sin(t/d * (Math.PI/2)) + b;}
}