﻿function carouselItem()
{
    //x:Names
    this._parent = this.params[2];
    this.item = this.control.content.findName(this.name);
    this.solidItem = this.control.content.findName("Item_CM"+this.id);
    this.Scale = this.control.content.findName("Scale_CM"+this.id);
    this.highlight = this.control.content.findName("highlight_CM"+this.id);
    this.highlightReflection = this.control.content.findName("highlightReflection_CM"+this.id);
    //Set Images
    this.image = this.params[0].attributes.getNamedItem("icon").value;
    this.icon = this.control.content.findName("icon_CM"+this.id);
    this.iconReflection = this.control.content.findName("iconReflection_CM"+this.id);
    this.icon.Source = this.image;
    this.iconReflection.Source = this.image;

    //Set variable
    this.angle = this.params[1];
    this.title = this.params[0].attributes.getNamedItem("title").value;
    this.body = this.params[0].attributes.getNamedItem("body").value;
    try
    {
        this.video = this.params[0].attributes.getNamedItem("video").value;
        this.highlight.Visibility = "Visible";
        this.highlightReflection.Visibility = "Visible";
    }
    catch(err)
    {
        this.video = null;
    }
    try
    {
        this.link = this.params[0].attributes.getNamedItem("link").value;
    }
    catch(err)
    {
        this.link = null;
    }
    try
    {
        this.quote = this.params[0].attributes.getNamedItem("quote").value;
    }
    catch(err)
    {
        this.quote = null;
    }
    try
    {
        this.reference = this.params[0].attributes.getNamedItem("reference").value;
    }
    catch(err)
    {
        this.reference = null;
    }
    
    //Functions
    this.setPosition = function(left, top, depth, scale)
    {
        this.item["Canvas.Left"] = left;
        this.item["Canvas.Top"] = top;
        this.item["Canvas.ZIndex"] = depth;
        this.Scale.ScaleX = scale;
        this.Scale.ScaleY = scale;
    }
    
    this.onPress = function(sender, args)
    {
        if(!this._parent.closing)
        {
            this._parent.stop();
            if(this._parent.spinning)
            {
                this._parent.spinning = false;
                this._parent.displayWindow.setDisplay(this.title, this.body, this.quote, this.reference, this.link, this.video, false)
                this._parent.displayWindow.open();
                this._parent.arrowHolder.Visibility = "Visible";
            }
            this._parent.angle = this._parent.normalizeAngle(this._parent.angle);
            var actualItemAngle = this._parent.normalizeAngle(this._parent.angle+this.angle);
            if(actualItemAngle >= Math.PI)
            {
                this._parent.changeAngle = (Math.PI*2)-actualItemAngle;
            }
            else
            {
                this._parent.changeAngle = -actualItemAngle;
            }
            this._parent.startAngle = this._parent.angle;
            this._parent.inc = 0;
            this._parent.selecting = true;
            this._parent.spinInterval = setInterval(preserveScope(this._parent, this._parent.alignCarousel), 10);
        }
    }
    
    this.selectedFirst = function()
    {
        this._parent.spinning = false;
        this._parent.displayWindow.setDisplay(this.title, this.body, this.quote, this.reference, this.link, this.video, true)
        this._parent.displayWindow.open();
        this._parent.arrowHolder.Visibility = "Visible";
    }
    
    this.setDisplay = function()
    {
        this._parent.displayWindow.setDisplay(this.title, this.body, this.quote, this.reference, this.link, this.video, false)
    }
    
    this.solidItem.cursor = "Hand";
    this.solidItem.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.onPress));
    
    //Set Reference of self
    this._parent.addItem(this);
}