// JavaScript Document

//BScrollPosts
var BScrollPosts = function (elem)
{
	elem = $(elem);
	if (!elem)
		return;
	
	this.box = elem;
	
	this.posts = [];
	var items = this.box.getChildren();
	for (var i=0; i<items.length; i++)
	{
		var ob = {};
		ob.item = items[i];
		ob.fx = new Fx.Style(ob.item, 'opacity', {duration:800});
		this.posts.push(ob);
	}
	
	this.curr_post = items.length - 1;
}

BScrollPosts.prototype.set_post = function (no)
{
	if (0 > no || no >= this.posts.length)
		return;

	this.box.adopt(this.posts[no].item);
	this.posts[no].item.style.display = 'block';
	this.curr_post = no;
}

BScrollPosts.prototype.show_post = function (no)
{
	if (0 > no || no >= this.posts.length || no == this.curr_post)
		return;
		
	var self = this;
	this.posts[this.curr_post].fx.stop();
	this.box.adopt(this.posts[no].item);
	this.posts[no].fx.set(0);
	this.posts[no].fx.start(0, 1);
	this.curr_post = no;
}

BScrollPosts.prototype.add_imgs = function (imgs_arr)
{
	var self = this;
	var loaded_imgs_arr = new Array();
	var on_load = function ()
	{
		loaded_imgs_arr.push(this);
		if (loaded_imgs_arr.length == imgs_arr.length)
		{
			self.box.style.background = '';
			
			for (var i=0; i<loaded_imgs_arr.length; i++)
				self.posts[i].item.firstChild.appendChild(loaded_imgs_arr[i]);
				
			self.on_ready();
		}
	};
	
	var imgs = new Array();
	for (var i=0; i<imgs_arr.length; i++)
	{
		imgs[i] = new Image();
		imgs[i].onload = on_load;
		imgs[i].onerror = on_load;
		imgs[i].src = imgs_arr[i];
	}
}

BScrollPosts.prototype.on_ready = function ()
{}

//BScrollPostsController
var BScrollPostsController = function (elem, sp)
{
	elem = $(elem);
	if (!elem || !sp)
		return null;

	var self = this;
	this.scroll_post = sp;
	this.box = elem;
	var as = elem.getElementsByTagName('a');
	this.contrs = new Array();

	as[0].onclick = function ()
	{
		self.previous();
		this.blur();
		return false;
	};

	as[as.length-1].onclick = function ()
	{
		self.next();
		this.blur();
		return false;
	};

	for (var i=1; i<as.length-1; i++)
	{
		as[i].setAttribute('no', this.contrs.length);
		as[i].onclick = function ()
		{
			self.select_post(this.getAttribute('no'));
			this.blur();
			return false;
		}
		this.contrs.push(as[i]);
	}

	this.curr_post = 0;
	this.timer = 0;
	this.select_post(0);
	
	elem.style.display = 'block';
}

BScrollPostsController.prototype.select_post = function (no)
{
	this.contrs[this.curr_post].className = '';
	this.contrs[no].className = 'on';
	this.scroll_post.show_post(no);
	this.curr_post = no;
}

BScrollPostsController.prototype.next = function ()
{
	var n = (this.curr_post + 1) % this.contrs.length;
	this.select_post(n);
}

BScrollPostsController.prototype.previous = function ()
{
	var n = this.curr_post - 1 >=0 ? (this.curr_post - 1) % this.contrs.length : this.contrs.length-1;
	this.select_post(n);
}

BScrollPostsController.prototype.automatic = function ()
{
	this.next.periodical(5000, this);
}

var BMainMenu = function (elem)
{
	elem = $(elem);
	var self = this;
	this.box = elem;
	this.menus = this.box.getElementsByTagName('a');
	this.curr_bar = null;
	this.orgi_bar = null;
	this.curr_drop = null;

	for (var i=0; i<this.menus.length; i++)
	{
		if ('on' == this.menus[i].className)
			this.orgi_bar = this.menus[i];
		
		var a = $(this.menus[i]);
		a.addEvent('mouseenter', function ()
		{
			self.rollover(this);
		});

		a.addEvent('mouseleave', function (e)
		{
			var related_target = e ? e.relatedTarget : window.event.fromElement;
			if (/^bm_drop.*/.test(related_target.className) || /^menu_title.*/.test(related_target.className))
				return;
			else
				self.drop_menu_rollout();
		});

		a.onclick = function ()
		{
			this.blur();
			return !/#$/.test(this.href);
		};
	}
}

BMainMenu.prototype.rollover = function (menu)
{
	if (this.orgi_bar)
		this.orgi_bar.className = '';
	
	if (this.curr_bar)
		this.curr_bar.className = '';
	menu.className = 'hover';
	this.curr_bar = menu;

	var id = menu.getAttribute('rid');

	if (!id)
	{
		if (this.curr_drop)
			this.curr_drop.style.display = 'none';
		
		return false;
	}
	
	var drop = $(id);
	if (!drop)
	{
		if (this.curr_drop)
			this.curr_drop.style.display = 'none';
			
		return false;
	}
		
	if (drop != this.curr_drop)
	{
		if (this.curr_drop)
			this.curr_drop.style.display = 'none';
	}
	
	drop.style.display = 'block';
	this.curr_drop = drop;
	
	drop.removeEvents('mouseleave');
	drop.addEvent('mouseleave', this.drop_menu_rollout.bindWithEvent(this));

	return false;
}

BMainMenu.prototype.drop_menu_rollout = function (menu)
{
	this.curr_bar.className = '';
	
	if (this.curr_drop)
		this.curr_drop.style.display = 'none';
		
	if (this.orgi_bar)
		this.orgi_bar.className = 'on';
}



var BScrollNews = function (elem)
{
	elem = $(elem);
	if (!elem)
		return;

	var self = this;
	this.posts = [];
	var items = elem.getChildren();
	for (var i=0; i<items.length; i++)
	{
		var ob = {};
		ob.item = items[i];
		ob.fx = new Fx.Style(ob.item, 'opacity', {duration:450});
		this.posts.push(ob);
	}
	
	var motion_over = function ()
	{
		document.title = this;
	};
	
	motion_over.bind(this);
	
	setInterval(function ()
	{
		var news = self.posts.pop();
		news.fx.set(0);
		elem.adopt(news.item);
		news.fx.start(0, 1);
		self.posts.unshift(news)
	}, 4500);
}

function set_mainmenu_heightlight (page)
{
	var menus = $('bm_bar').getElementsByTagName('a');
	for (var i=0; i<menus.length; i++)
	{
		if (page == menus[i].innerHTML)
		{
			menus[i].className = 'on';
			return ;
		}
	}
}