 //********************************************************************************
//
//   Copyright (C) 2009, Dynamic Realms, LLC. All rights reserved. 
//
//   Re-use of this software is strictly forbidden, unless granted by Dynamic
//   Realms in writing.
//
//********************************************************************************

var mouse = new JS_Mouse();
var carousel = new JS_ProductCarousel('carousel', 'carousel', mouse, -.8, 695, 150, 317);

function JS_HookEvent (element, eventName, callback)
{
    if (typeof(element) == "string")
        element = document.getElementById(element);

    if(element == null)
        return;

    if(element.addEventListener)
    {
        if(eventName == 'mousewheel')
            element.addEventListener('DOMMouseScroll', callback, false);  

        element.addEventListener(eventName, callback, false);
    }
    else if (element.attachEvent)
        element.attachEvent("on" + eventName, callback);
}

function JS_UnhookEvent (element, eventName, callback)
{
    if (typeof(element) == "string")
        element = document.getElementById(element);
  
    if(element == null)
        return;
    
    if (element.removeEventListener)
    {
        if (eventName == 'mousewheel')
            element.removeEventListener('DOMMouseScroll', callback, false);  
    
        element.removeEventListener(eventName, callback, false);
    }
    else if (element.detachEvent)
        element.detachEvent("on" + eventName, callback);
}

function JS_CancelEvent (event)
{
    event = event ? event : window.event;

    if (event.stopPropagation)
        event.stopPropagation();
  
    if (event.preventDefault)
        event.preventDefault();
  
    event.cancelBubble = true;
    event.cancel = true;
    event.returnValue = false;
  
    return false;
}

function JS_ResetCarousel()
{
    carousel.Stop();
    carousel._items = new Array();
    carousel.AddItem('1', 'storage/carousel/iPhone_dynamicDICE_Carousel.png');
    carousel.AddItem('2', 'storage/carousel/iPhone_LoveDice_Carousel.png');
    carousel.AddItem('3', 'storage/carousel/iPhone_Stripped_Carousel.png');
    carousel.AddItem('4', 'storage/carousel/iPhone_CasinoDice_Carousel.png');
    carousel.AddItem('5', 'storage/carousel/iPhone_DigitalDice_Carousel.png');
    carousel.AddItem('6', 'storage/carousel/iPhone_WorldDice_Carousel.png');
    carousel.AddItem('7', 'storage/carousel/iPhone_ComingSoon_Carousel.png');
    carousel.AddItem('8', 'storage/carousel/iPhone_ComingSoon_Carousel.png');
    carousel.DrawItems();
}

window.onload = function()
{
    try
    {
        if (window.attachEvent)
        {
            window.document.attachEvent("onselectstart", function(Event) { return false; });
        }
    } catch (e) {}

    JS_ResetCarousel();
    carousel.Start();
};

function JS_Vector ( xp, yp )
{
    this.x = xp;
    this.y = yp;

    this.AddValue = function ( val ) { this.x += val; this.y += val; return this; };
    this.SubValue = function ( val ) { this.x -= val; this.y -= val; return this; };
    this.MulValue = function ( val ) { this.x *= val; this.y *= val; return this; };
    this.DivValue = function ( val ) { if (val) { this.x /= val; this.y /= val; } return this; };

    this.AddVector = function ( vec ) { this.x += vec.x; this.y += vec.y; return this; };
    this.SubVector = function ( vec ) { this.x -= vec.x; this.y -= vec.y; return this; };
    this.MulVector = function ( vec ) { this.x *= vec.x; this.y *= vec.y; return this; };
    this.DivVector = function ( vec ) { if (vec.x && vec.y) { this.x /= vec.x; this.y /= vec.y; } return this; };

    this.Copy = function () { return new JS_Vector(this.x,this.y); };

    this.GetIntX = function () { return Math.round(this.x); };
    this.GetIntY = function () { return Math.round(this.y); };

    this.GetPixelX = function () { return Math.round(this.x) + 'px'; };
    this.GetPixelY = function () { return Math.round(this.y) + 'px'; };
}

function JS_Mouse ()
{
    this.isBtnDown = false;

    this.curPosition = new JS_Vector(0, 0);
    this.prevPosition = new JS_Vector(0, 0);
    this.offset = new JS_Vector(0, 0);
    this.delta = 0;
    this.count = 0;
    this.acceleration = new JS_Vector(0, 0);

    var This = this;

    JS_HookEvent('carousel','mousewheel', function(Event) { This.Wheel(Event); });
    JS_HookEvent('carousel','mousedown', function(Event) { This.isBtnDown = true; carousel.MouseDown(); });
    JS_HookEvent('carousel','mouseup', function(Event) { This.isBtnDown = false; });
    JS_HookEvent('carousel','mousemove', function(Event) { This.Move(Event);  carousel.Drag(); });

    this.Wheel = function(event)
    {
        var delta = 0;

        if (!event)
            event = window.event;
    
        if (event.wheelDelta)
        {
            delta = event.wheelDelta/120; 
        
            if (window.opera)
                delta = -delta;
        }
        else if (event.detail)
            delta = -event.detail/3;

        if (delta)
            carousel.Spin(delta);

        return JS_CancelEvent(event);
    }

    this.Move = function(Event)
    {
        ++this.count;

        this.prevPosition = this.curPosition.Copy();
        this.curPosition = new JS_Vector(Event.clientX + window.document.body.scrollLeft, Event.clientY + window.document.body.scrollTop);

        this.offset.AddVector(this.curPosition.Copy().SubVector(this.prevPosition));
        this.acceleration = this.offset.Copy().DivValue(this.count);
    };

    this.Reset = function()
    {
        this.offset.x = 0;
        this.offset.y = 0;

        this.count = 0;

        this.acceleration.x = 0;
        this.acceleration.y = 0;
    };
}

function JS_CarouselItem ( Parent, HREF, ImageSRC )
{
	this.parent = Parent;
	this.elemID = this.parent.elemID + 'Item' + this.parent.items.length;

	this.hRef = HREF;
	this.element = null;

	this.curPosition = new JS_Vector(0, 0);
	this.itemSize = this.parent.itemSize.Copy();

	this.image = new Image();
	this.image.src = ImageSRC;

	this.GetElement = function ()
	{
		if ( !this.element )
			this.element = window.document.getElementById(this.elemID);

		return this.element;
	};

	this.GetMarkup = function ()
	{
		var screenSize = this.GetScreenSize();
		var screenPosition = this.GetScreenPosition();

//		return '<a href="javascript:' + this.parent.elemID + '.ClickItem(\'' + this.hRef + '\');">' +
 //      '  <img id="' + this.elemID + '" style="position: absolute; ' +
 //      'left: ' + screenPosition.GetPixelX() + '; ' +
 //      'top: ' + screenPosition.GetPixelY() + '; ' +
 //      'border: none; z-index: ' + this.GetZIndex() + ';" ' +
 //      'src="' + this.image.src + '" ' +
 //      'width="' + screenSize.GetIntX() + '" ' +
 //      'height="' + screenSize.GetIntY() + '" ' +
 //      'alt="" title="" /></a>';
 
 return '<img id="' + this.elemID + '" style="position: absolute; ' + 
        'left: ' + screenPosition.GetPixelX() + '; ' +
        'top: ' + screenPosition.GetPixelY() + '; ' +
        'border: none; z-index: ' + this.GetZIndex() + ';" ' +
        'src="' + this.image.src + '" ' +
        'width="' + screenSize.GetIntX() + '" ' +
        'height="' + screenSize.GetIntY() + '" ' +
        'alt="" title="" />';
 
//              '<a href="javascript:' + this.parent.elemID +
//				'.ClickItem(\'' + this.hRef + '\');">\n' +
//				'    <img id="' + this.elemID + '" style="position: absolute; ' +
//				'left: ' + screenPosition.GetPixelX() + '; ' +
//				'top: ' + screenPosition.GetPixelY() + '; ' +
//				'border: none; z-index: ' + this.GetZIndex() + ';" ' +
//				'src="' + this.image.src + '" ' +
//				'width="' + screenSize.GetIntX() + '" ' +
//				'height="' + screenSize.GetIntY() + '" ' +
//				'alt="" title="" /></a>\n';
    };

	this.GetScreenPosition = function ()
	{
		var returnVal = this.parent.itemPosition.Copy();
		var offset = (this.parent.itemSize.x - this.itemSize.x) / 2;

		returnVal.x += this.curPosition.x * (returnVal.x + offset) + offset;
		returnVal.y = 50 - 7*returnVal.y*this.curPosition.y;

		return returnVal;
	};

	this.GetScreenSize = function ()
	{
		this.itemSize = this.parent.itemSize.Copy();
		this.itemSize.MulValue((this.curPosition.y + 1.5) / 2.5);

		return this.itemSize;
	};

	this.GetZIndex = function ()
	{
		return Math.round((this.curPosition.y + 2) * 10000);
	};

	this.Update = function()
	{
		this.parent.Rotate(this.curPosition);

		var screenSize = this.GetScreenSize();
		var screenPosition = this.GetScreenPosition();

		if (this.GetElement())
		{
			this.element.style.left = screenPosition.GetPixelX();
			this.element.style.top = screenPosition.GetPixelY();
			this.element.width = screenSize.GetIntX();
			this.element.height = screenSize.GetIntY();

			this.element.style.zIndex = this.GetZIndex();
		}
	};
}

function JS_ProductCarousel(ID, LayerID, JS_Mouse, InitSpeed, CarouselWidth, ItemWidth, ItemHeight)
{
    this.initSpeed = InitSpeed;
    this.elemID = ID;
    this.layerID = LayerID;
    this.interval = '';
    this.items = new Array();
    this.carouselWidth = CarouselWidth;
    this.itemSize = new JS_Vector(ItemWidth, ItemHeight);
    this.itemPosition = new JS_Vector((this.carouselWidth - this.itemSize.x) / 2, 10);
    this.rotSpeed = this.initSpeed;
    this.cosRot = 0;
    this.sinRot = 0;
    this.mouse = JS_Mouse;

    this.AddItem = function(HREF, ImageSRC)
    {
        this.items[this.items.length] = new JS_CarouselItem(this, HREF, ImageSRC);
    };

    this.DrawItems = function()
    {
        var layer = window.document.getElementById(this.layerID);
        if (layer)
        {
            layer.style.width = this.carouselWidth + 'px';
            layer.style.height = (this.itemPosition.y * 2 + this.itemSize.y) + 'px';

            this.SetRotate((2 * Math.PI) / this.items.length);

            var itemPosition = new JS_Vector(0, 1);
            var markup = '';
            var link;

//            layer.innerHTML = '';
            
            for (var count = 0; count < this.items.length; ++count)
            {
                this.items[count].element = null;
                this.items[count].curPosition = itemPosition.Copy();
                link = 'javascript:' + this.elemID + '.ClickItem(\'' + this.items[count].hRef + '\')';
                
//               markup += '<a href="';
//                markup += link;
//                markup += '" >';
                markup += this.items[count].GetMarkup();
//                markup += '</a>';
                
                layer.innerHTML = markup;
                
                this.Rotate(itemPosition);
            }

 //           layer.innerHTML = markup;
              
        }
    };

    this.Animate = function()
    {
        this.Decelerate();

        for (var count = 0; count < this.items.length; ++count)
            this.items[count].Update();

        if (this.rotSpeed >= 0 && this.rotSpeed < 0.1 ||
            this.rotSpeed < 0 && this.rotSpeed > -0.1)
            this.Stop();
    };

    this.Start = function()
    {
        if (this.interval == '')
            this.interval = setInterval(this.elemID + '.Animate()', 10);
    };

    this.Stop = function()
    {
        if (this.interval != '')
            clearInterval(this.interval);

        this.interval = '';
    };

    this.SetRotate = function(Angle)
    {
        this.cosRot = Math.cos(Angle);
        this.sinRot = Math.sin(Angle);
    };

    this.Rotate = function(Vector)
    {
        var clone = Vector.Copy();
        Vector.x = this.cosRot * clone.x - this.sinRot * clone.y;
        Vector.y = this.sinRot * clone.x + this.cosRot * clone.y;
    };

    this.Decelerate = function()
    {
        if ( this.initSpeed == 0 )
            this.rotSpeed *= .9;
        this.SetRotate((this.rotSpeed * Math.PI) / 500);
    };

    this.MouseDown = function()
    {
        this.initSpeed = 0;
        this.rotSpeed = 0;
    };

    this.Drag = function()
    {
        if (this.mouse.isBtnDown)
        {
            this.initSpeed = 0;
            this.rotSpeed = -this.mouse.acceleration.x;

            this.mouse.Reset();
            this.Start();
        }
    };

    this.Spin = function(delta)
    {
            this.initSpeed = 0;
            this.rotSpeed = 10*delta;

            this.mouse.Reset();
            this.Start();
    };

    this.ClickItem = function(ItemID)
    {
        if (this.rotSpeed == 0 && !this.mouse.isBtnDown)
        {
            switch (ItemID)
            {
                default:    break;
				case '1':	window.location = document.getElementById('AboutUs');	break;
				case '2':	window.location = document.getElementById('LoveDice');	break;
				case '3':	break;
				case '4':	break;
				case '5':	break;
				case '6':	break;
				case '7':	window.location = document.getElementById('dynamicDICE');   break;
		    }
		}
    };

}
