﻿if (!window.XboxLiveMarketplace)
	window.XboxLiveMarketplace = {};

XboxLiveMarketplace.EpixController = function(xaml) 
{
    this.xaml = xaml;
    this.isMouseCaptured = false;
    this.mouseVerticalPosition = -1;
    this.mouseHorizontalPosition = -1;

    this.debug = this.xaml.findName("t");
    this.handle = this.xaml.findName("Handle");
    this.background = this.xaml.findName("Background");
    this.arrowLeft = this.xaml.findName("arrowLeft");
    this.arrowRight = this.xaml.findName("arrowRight");
    this.center = this.xaml.findName('center');
    this.left = this.xaml.findName('left');
    this.farleft = this.xaml.findName('farleft');
    this.right = this.xaml.findName('right');
    this.farright = this.xaml.findName('farright');

    this.dispatch = true;
    this.direction = null;
    
    this.HANDLE_CENTER = this.background.Width/2 - this.handle.Width/2;
    this.handle.AddEventListener("MouseMove", Silverlight.createDelegate(this, this.onMouseMove)); 
    this.handle.AddEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.onMouseDown)); 
    this.handle.AddEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.onMouseUp)); 
    this.arrowLeft.AddEventListener("MouseEnter", Silverlight.createDelegate(this, this.onArrowMouseEnter));  
    this.arrowLeft.AddEventListener("MouseLeave", Silverlight.createDelegate(this, this.onArrowMouseLeave));  
    this.arrowRight.AddEventListener("MouseEnter", Silverlight.createDelegate(this, this.onArrowMouseEnter));  
    this.arrowRight.AddEventListener("MouseLeave", Silverlight.createDelegate(this, this.onArrowMouseLeave));  
    this.arrowLeft.Opacity = this.arrowRight.Opacity = .3;
}

XboxLiveMarketplace.EpixController.prototype =
{
    init: function()
    {
        this.centerHandle();
    },
    onArrowMouseEnter:function(sender, args)
    {
        sender.Opacity = 1;
    },
    onArrowMouseLeave:function(sender, args)
    {
        sender.Opacity = .3;
    },
    centerHandle: function()
    {
        this.debug.Text = '';
        this.handle["Canvas.Left"] = this.HANDLE_CENTER;
        this.xaml.findName('center').Visibility = "Visible";
        this.xaml.findName('left').Visibility = "Collapsed";
        this.xaml.findName('farleft').Visibility = "Collapsed";
        this.xaml.findName('right').Visibility = "Collapsed";
        this.xaml.findName('farright').Visibility = "Collapsed";
        this.arrowLeft.Opacity = .3;
        this.arrowRight.Opacity = .3;
    },
    onMouseUp: function(sender, args)
    {
      this.direction = null;
      this.handleMode = null;
      var item = sender;
      this.isMouseCaptured = false;
      item.ReleaseMouseCapture();
      this.mouseVerticalPosition = -1;
      this.mouseHorizontalPosition = -1;
      this.dispatch = true;
      this.centerHandle();
      this.DispatchReleaseEvent();
    },
    onMouseDown: function(sender, args)
    {
      var item = sender;
      this.mouseVerticalPosition = args.getPosition(null).y;
      this.mouseHorizontalPosition = args.getPosition(null).x;
      this.isMouseCaptured = true;
      item.CaptureMouse();
    },
    onMouseMove: function(sender, args)
    {
      var item = sender;
      if (this.isMouseCaptured) 
      {

        // Calculate the current position of the object.
        var deltaV = args.getPosition(null).y - this.mouseVerticalPosition;
        var deltaH = args.getPosition(null).x - this.mouseHorizontalPosition;
        var newTop = deltaV+ item["Canvas.Top"];
        var newLeft = deltaH+ item["Canvas.Left"];

        // Set new position of object.
        //item["Canvas.Top"] = newTop;
        if(newLeft > this.background["Canvas.Left"] && newLeft < this.background.Width - this.handle.Width) item["Canvas.Left"] = newLeft;
        if(this.direction == 'left' && (this.handle["Canvas.Left"] > this.HANDLE_CENTER))
        {
            this.dispatch = true;
            this.DispatchReleaseEvent();
        }
        else if(this.direction == 'right' && (this.handle["Canvas.Left"] < this.HANDLE_CENTER))
        {
            this.dispatch = true;
            this.DispatchReleaseEvent();
        }
        this.handleMode = 'center';
        if(this.direction == 'left')
        {
            this.arrowLeft.Opacity = 1;
            this.arrowRight.Opacity = .3;
            if(this.handle["Canvas.Left"] < this.HANDLE_CENTER/2)
            {
                this.handleMode = 'farleft';
            }
            else
            {
                this.handleMode = 'left';
            }
        }
        else if(this.direction == 'right')
        {
            this.arrowLeft.Opacity = .3;
            this.arrowRight.Opacity = 1;
            if(this.handle["Canvas.Left"] > parseInt(this.HANDLE_CENTER+this.HANDLE_CENTER/2))
            {
                this.handleMode = 'farright';
            }
            else
            {
                this.handleMode = 'right';
            }
        }
        this.xaml.findName('center').Visibility = "Collapsed";
        this.xaml.findName('left').Visibility = "Collapsed";
        this.xaml.findName('farleft').Visibility = "Collapsed";
        this.xaml.findName('right').Visibility = "Collapsed";
        this.xaml.findName('farright').Visibility = "Collapsed";
        //
        this.xaml.findName(this.handleMode).Visibility = "Visible";
        // Update position global variables.
        this.mouseVerticalPosition = args.getPosition(null).y;
        this.mouseHorizontalPosition = args.getPosition(null).x;
        if(this.dispatch == true)
        {
            this.direction = (this.handle["Canvas.Left"] > this.HANDLE_CENTER) ? 'right' : 'left';


            this.dispatch = false;
            this.DispatchChangeEvent(this, this.direction);
        }
      }
    },
    DispatchChangeEvent:function(sender, position)
    {
        if(sender.onChange)
            sender.onChange(position);
    },
    DispatchReleaseEvent:function()
    {
        if(this.onRelease)
            this.onRelease();
    }
}