/**
 * Banners
 */

function SetHtmlContent()
{
	var Menu = $('Banner').getElement('.Banner-Menu-Content');
	var MenuHeight = 286;
	Menu.style.overflow = 'hidden';
	Menu.style.height = MenuHeight+'px';
	MenuScroll = new Fx.Scroll(Menu,{velocity: 0.2});
	MenuScroll.Menu = Menu;
	MenuScroll.Current = 0;
	MenuScroll.Items = Menu.getElements('.Item');
	MenuScroll.Items.each(function(el,i){ 
		el.id = 'Banner-Menu-Item-'+i;
		el.Active = false;
		el.addEvent('mouseenter',function() { this.addClass('Item-On'); });
		el.addEvent('mouseleave',function() { if(!this.Active) this.removeClass('Item-On'); });
		el.y = el.getSize().y;
		el.A = el.getElement('A');
		el.A.Link = el.A.href;
		el.A.Img = el.A.getProperty('onmouseover')+'';
		el.A.Img = el.A.Img.substring(el.A.Img.indexOf("Image='")+7,el.A.Img.lastIndexOf("'"));
		el.A.LinkTarget = el.A.getProperty('target')+'';
		el.A.removeProperty('href');
		el.A.onmouseover=function(){};
		el.A.removeProperty('onmouseover');
		el.A.style.cursor = 'pointer';
		el.A.MenuView = function(){ MenuView(this.parentNode.parentNode,this.Link,this.Img,this.LinkTarget); };
		el.A.onclick = function(){ this.MenuView(); };
	});
	
	//spacer
	Menu.grab( new Element('div', {'style' : 'height:130px;'}) );

	//the up/down on needs to be enough clicks
	//so last item becomes visible at bottom of
	//menu box
		MenuScroll.Total = 0;
	
		//Total height
		var MenuItemsHeight = 0;
		for(var i=0; i<MenuScroll.Items.length; i++)
		{
			var el = $('Banner-Menu-Item-'+i);
			MenuItemsHeight += el.y;
		}
		
		//Trailing height
		var MenuTrailingHeight = MenuItemsHeight - MenuHeight;
		
		//Number items trailing over end
		MenuScroll.TrailingTotal = 0;
		if(MenuTrailingHeight > 0)
		{
			var MenuTrailingStackHeight = 0;
			for(var i=MenuScroll.Items.length-1; i>=0; i--)
			{
				var el = $('Banner-Menu-Item-'+i);
				MenuTrailingStackHeight += el.y;
				MenuScroll.TrailingTotal++;
				if(MenuTrailingStackHeight > MenuTrailingHeight)
					break;
			}
		}
		MenuScroll.Total = MenuScroll.TrailingTotal;
	//

	MenuScroll.ScrollDown = function() {
		if(MenuScroll.Current + 1 <= MenuScroll.Total)
		{
			MenuScroll.Current++;
			this.toElement('Banner-Menu-Item-'+MenuScroll.Current);
			this.buttonDown.style.backgroundPosition='left top';
		}
		MenuScroll.SetButtons();
	};

	MenuScroll.ScrollUp = function() {
		if(MenuScroll.Current - 1 >= 0)
		{
			MenuScroll.Current--;
			this.toElement('Banner-Menu-Item-'+MenuScroll.Current);
		}
		MenuScroll.SetButtons();
	};

	MenuScroll.SetButtons = function() {
		if(MenuScroll.Current == 0)//at start
		{
			this.buttonDown.style.backgroundPosition='left top';
			this.buttonDown.style.cursor = 'pointer';
			this.buttonUp.style.backgroundPosition='left bottom';
			this.buttonUp.style.cursor = 'default';
		}
		else if(MenuScroll.Current >= MenuScroll.Total)//at end
		{
			this.buttonDown.style.backgroundPosition='left bottom';
			this.buttonDown.style.cursor = 'default';
			this.buttonUp.style.backgroundPosition='left top';
			this.buttonUp.style.cursor = 'pointer';
		}
		else//middle
		{
			this.buttonDown.style.backgroundPosition='left top';
			this.buttonDown.style.cursor = 'pointer';
			this.buttonUp.style.backgroundPosition='left top';
			this.buttonUp.style.cursor = 'pointer';
		}
	};
	
	function MenuView(Box,Link,Src,Tgt)
	{
		Box.Active = true;
		Box.addClass('Item-On');

		for(var i=0; i<MenuScroll.Items.length; i++)
		{
			if(MenuScroll.Items[i] != Box)
			{
				MenuScroll.Items[i].removeClass('Item-On');
				MenuScroll.Items[i].Active = false;
			}
		}
		
		var Img = $('Banner').getElement('.Banner-Image');
		if(Img)
		{
			Img.src = Src;
			Img.Link = Link;
			Img.LinkTarget = Tgt;
			Img.style.cursor = 'pointer';
			Img.onclick = function() { 
				if(!this.LinkTarget)
					location.href = this.Link;
				else
					window.open(this.Link,this.LinkTarget);
			};
		}
	}

	var navDown = new Element('div', {
		'class' : 'Banner-Menu-Control-Down'
	});
	navDown.onclick = function() { MenuScroll.ScrollDown(); }
	var navUp = new Element('div', {
		'class' : 'Banner-Menu-Control-Up'
	});
	navUp.onclick = function() { MenuScroll.ScrollUp(); }

	$('Banner-Menu-Controls').grab( navDown );
	$('Banner-Menu-Controls').grab( navUp );

	MenuScroll.buttonDown = $('Banner').getElement('.Banner-Menu-Control-Down');
	MenuScroll.buttonUp = $('Banner').getElement('.Banner-Menu-Control-Up');

	MenuScroll.toTop();
	MenuScroll.SetButtons();
	MenuScroll.Items[0].A.MenuView();
}


