function StopBubblingEvent(e){
	if(!$chk(e)) return ;
	try{
		e.stop();
	} catch(err){}
}

var TwitterObj = {
	CurrentText: 1,
	GetContainerWidth: function(){
		if($chk(this.ContainerWidth))
			return this.ContainerWidth;
		return (this.ContainerWidth = $('DivTwitterTextContainer').getSize().x);
	}, GetMsgCount: function(){
		if($chk(this.MsgCount))
			return this.MsgCount;
		return (this.MsgCount = $$('.DivTwitterText').length);
	}, SetNextMsg: function(){
		if(++this.CurrentText>this.GetMsgCount())
			this.CurrentText = 1;
	}, GetReadTime: function(width, truncate, reduce){
		if((truncate)&&(width>this.GetContainerWidth())){
			width = this.GetContainerWidth();
			if(($chk(reduce))&&(reduce))
				width = width/2;
		}
		var ReadTime = (width/100).round(3);
		if(ReadTime<=0) ReadTime = 1;
		return ReadTime*2000; // for each 100px width, takes about 2 seconds of reading
	}, StartAnimation: function(){
		this.CurrentTextObj = $('DivTwitterText'+this.CurrentText);
		this.MouseOver = false;
		this.MouseOut = false;
		this.FXTrans = "";
		this.CurrentFX = null;
		if(!$chk(this.CurrentTextObj)) return ;
		this.CurrentTextObj.setStyles({'display': 'block', 'opacity': '0', 'left': '0'});
		new Fx.Tween(this.CurrentTextObj, {duration:1200, onComplete: function(){
			this.CurrentFX = this.EndAnimation.delay(this.GetReadTime(this.CurrentTextObj.getSize().x, true, true), this);
			this.CurrentTextObj.addEvent('mouseover', function(e){
				if(this.MouseOver) return;
				this.MouseOver = true;
				$clear(this.CurrentFX);
			}.bind(this));
			this.CurrentTextObj.addEvent('mouseout', function(e){
				if(this.MouseOut) return;
				this.MouseOut = true;
				this.EndAnimation();
			}.bind(this));
		}.bind(this)}).start('opacity', '1');
	}, EndAnimation: function(){
		this.CurrentTextObj.removeEvents(); 
		this.MouseOver = false;
		this.MouseOut = false;
		if(this.CurrentTextObj.getSize().x>this.GetContainerWidth()){
			var diff = this.CurrentTextObj.getSize().x - this.GetContainerWidth();
			this.CurrentFX = new Fx.Tween(this.CurrentTextObj, {duration:this.GetReadTime(diff, false), onComplete: function(){
				this.FXTrans = "stop";
				this.CurrentTextObj.removeEvents(); 
				this.CurrentFX = this.HideText.delay(3000, this);
				this.CurrentTextObj.addEvent('mouseover', function(e){
					if(this.MouseOver) return;
					this.MouseOver = true;
					$clear(this.CurrentFX);
				}.bind(this));
				this.CurrentTextObj.addEvent('mouseout', function(e){
					if(this.MouseOut) return;
					this.MouseOut = true;
					this.HideText();
				}.bind(this));
			}.bind(this), transition:Fx.Transitions.linear}).start('left', diff*-1);
			this.CurrentTextObj.addEvent('mouseover', function(e){
				if(this.FXTrans!="") return ;
				this.FXTrans = "stop";
				if($chk(this.CurrentFX))
					this.CurrentFX.pause();
				this.FXTrans = "pause";
			}.bind(this));
			this.CurrentTextObj.addEvent('mouseout', function(e){
				if(this.FXTrans!="pause") return ;
				this.FXTrans = "stop";
				if($chk(this.CurrentFX))
					this.CurrentFX.resume();
				this.FXTrans = "";
			}.bind(this));
		} else
			this.HideText();
	}, HideText: function(){
		this.CurrentTextObj.removeEvents(); 
		new Fx.Tween(this.CurrentTextObj, {duration:1200, onComplete: function(){
			this.CurrentTextObj.setStyle('display','none');
			this.SetNextMsg();
			this.StartAnimation.delay(100, this);
		}.bind(this)}).start('opacity', '0');
	}
};

var MainMenu = {

	MenuCount: 0,
	CurrentMenu: null,
	MapLinks: function(){
		$$('.LinkMainMenu').each(function(el){
			this.MenuCount++;
			el.addEvent('mouseover', function(e){
				MainMenu.ShowMenuMarker(this);
			});
		}.bind(this));
		$$('.LinkMainMenuSub').each(function(el){
			this.MenuCount++;
			el.addEvent('mouseover', function(e){
				MainMenu.ShowMenuMarker(this);
			});
			el.addEvent('click', function(e){
				e = new Event(e);
				e.stop();
				el.fireEvent('mouseover', e);
			});
		}.bind(this));
	}, ShowMenuMarker: function(el){

		var container = el.getParent();
		var container_size = container.getSize();
		var container_pos = container.getPosition($('DivHeaderMenuBack'));

		this.HideMenuMarker();
		this.CurrentMenu = el;

		$('DivHMBackSel').setPosition({x: container_pos.x-10});
		$('DivHMBContent').setStyle('width', container_size.x+'px');
		$('DivHMBackSel').setStyles({'display':'block', 'width':(container_size.x+20)+'px'});
		el.addClass("SelectedLink");

		var Submenu = $('DivHMSub_'+this.FilterMenuNumber(el.get("id")));

		if($chk(Submenu)){
			Submenu.setPosition({x: container_pos.x-10});
			Submenu.getFirst().setStyle('width', container_size.x+'px');
			var IdealWidth = ($('DivHMBackSel').getSize().x*1.5).toInt();
			Submenu.setStyle('display','block');
			if(Submenu.getSize().x<IdealWidth)
				Submenu.setStyle('width', IdealWidth+"px");
		}

	}, HideMenuMarker: function(el){
		if(!$chk(el)){
			if($chk(this.CurrentMenu)){
				el = this.CurrentMenu;
				this.CurrentMenu = null;
			} else
				return ;
		}
		$('DivHMBackSel').setStyle('display','none');
		el.removeClass("SelectedLink");
		if($chk($('DivHMSub_'+this.FilterMenuNumber(el.get("id")))))
			$('DivHMSub_'+this.FilterMenuNumber(el.get("id"))).setStyle('display','none');
	}, FilterMenuNumber: function(id){
		var splitstr = id.split("_");
		return splitstr[1].toInt();
	}

};

document.addEvent('click', function(e){
	MainMenu.HideMenuMarker();
});

window.addEvent('domready', function(e){
	TwitterObj.StartAnimation();
	MainMenu.MapLinks();
});
