/*News Object
	Remarks:	This file contains javascript objects related to news items on the web site
	Author:		Duncan Abela
	Modification Log: 
*/

//need enumeration here

var NotificationType =
{
    SailingUpdate   : 1,
    Misc		    : 2
}

var NotificationItem=Class.create(
{
	initialize: function(text,category,startDate,endDate)
	{
		this.text=text;
		this.category=category;
		this.startDate=startDate;
		this.endDate=endDate;
		if (this.startDate==null && this.endDate==null)
			this.hasPeriods=false;
		else
			this.hasPeriods=true;
		
		var dNow=new Date();
		this.showItem=false;
		if (this.hasPeriods)
		{
			if (this.endDate==null) //therefore its an item with both a start and an end date
			{
				if (this.startDate.getTime() <=dNow.getTime())
				{
					this.showItem=true;
				}
			}
			else if (this.startDate==null)
			{
				if (dNow.getTime() <= this.endDate.getTime())
				{
					this.showItem=true;
				}
			}
			else //it has both of them
			{
				if (this.startDate.getTime() <=dNow.getTime() && dNow.getTime() <=this.endDate.getTime())
				{
					this.showItem=true;
				}
			}
		}
		else
		{
			this.showItem=true; //Therefore its always shown
		}
	}
});



var NotificationsController=Class.create(
{
	initialize: function(container,items,Buttons,UpHoverImage,DownHoverImage,BaseUrl)
	{
		this.sItems=items;
        this.baseUrl=BaseUrl;
		this.sContainer=$(container);
        this.Animation=null;
        this.UpTopButton=$(Buttons[0]);
        this.DownTopButton=$(Buttons[1]);
        this.UpBottomButton=$(Buttons[2]);
        this.DownBottomButton=$(Buttons[3]);

        this.UpArrowHoverImage=this.baseUrl + UpHoverImage;
        this.DownArrowHoverImage=this.baseUrl + DownHoverImage;

        this.UpArrowImage=this.UpTopButton.src;
        this.DownArrowImage=this.DownTopButton.src;


        Event.observe(this.UpTopButton, 'mouseover', this.OnButtonMouseOver.bindAsEventListener(this,-1,50));
        Event.observe(this.UpTopButton, 'mouseout', this.OnButtonMouseOut.bindAsEventListener(this,-1));
        Event.observe(this.DownTopButton, 'mouseover', this.OnButtonMouseOver.bindAsEventListener(this,1,50));
        Event.observe(this.DownTopButton, 'mouseout', this.OnButtonMouseOut.bindAsEventListener(this,1));
        Event.observe(this.UpBottomButton, 'mouseover', this.OnButtonMouseOver.bindAsEventListener(this,-1,50));
        Event.observe(this.UpBottomButton, 'mouseout', this.OnButtonMouseOut.bindAsEventListener(this,-1));
        Event.observe(this.DownBottomButton, 'mouseover', this.OnButtonMouseOver.bindAsEventListener(this,1,50));
        Event.observe(this.DownBottomButton, 'mouseout', this.OnButtonMouseOut.bindAsEventListener(this,1));

        this.UpButtonShift=0;
        this.DownButtonShift=0;


	},
    OnButtonMouseOut: function(e)
    {
        var data = $A(arguments);
        data.shift();
        var dirUpDown=data[0];
        if (dirUpDown==-1)
        {
            this.UpTopButton.src=this.UpArrowImage;
            this.UpBottomButton.src=this.UpArrowImage;
        }
        else
        {
            this.DownTopButton.src=this.DownArrowImage;
            this.DownBottomButton.src=this.DownArrowImage;
        }
        Event.stop(e);
    },
    OnButtonMouseOver: function(e)
    {
        
        var data = $A(arguments);
        data.shift();
        var dirUpDown=data[0];
        var shiftPixels=data[1];
        if (dirUpDown==-1)
        {
            this.UpTopButton.src=this.UpArrowHoverImage;
            this.UpBottomButton.src=this.UpArrowHoverImage;
        }
        else
        {
            this.DownTopButton.src=this.DownArrowHoverImage;
            this.DownBottomButton.src=this.DownArrowHoverImage;
        }
        var yShift=dirUpDown*shiftPixels;
        this.UpButtonShift +=shiftPixels;
        new Effect.Move(this.sContainer,{   duration: 1.2,  
                                              x: 0, 
                                              y: yShift,
                                              mode: 'relative'
                                        });
       // Event.stop(e);  
    },
	showNotifications: function()
	{
		var sailings=""
		var misc=""
		var htmlString=""
		var numSailingUpdates=0;
		var numMiscUpdates=0;

		this.sItems.each(function(r)
		{
			if (r.category==NotificationType['SailingUpdate'])
			{
				numSailingUpdates++;
				if (r.showItem)
				{
					sailings=sailings + "<li>" + r.text + "</li>";
				}
			}
			else
			{
				numMiscUpdates++;
				misc=misc + "<li>" + r.text + "</li>";
			}
		});
		if (numSailingUpdates>0)
			sailings="<ul>" + sailings + "</ul>";
		if (numMiscUpdates>0)
			misc="<ul>" + misc + "</ul>";
		htmlString=sailings + misc;
		this.sContainer.innerHTML=htmlString;
        //this.scrollNews();	
	}
}); //end NotificationsController
