// JavaScript Document

function init()
{
	getFontSize();
	new rotatingBanner();
	new listScroll('industryContent', 'industyBar', true);
	new listScroll('companyContent', 'companyBar', true);
}

function genSwf()
{
	var flashvars = {};
	flashvars.path = "./";
	flashvars.xmlPath = "xml/";
	flashvars.imagePath = "./";
	
	var params = {};
	params.allowscriptaccess = "sameDomain";
	params.wmode = "opaque";
	
	var attributes = {};
	
	swfobject.embedSWF("images/swf/home_banner.swf", "homeFlashBanner", 970, 311, "9.0.45", false, flashvars, params, attributes);
}

function rotatingBanner()
{
	var banner = d.getElementById('rotatingBanner');
	var bannerArray = setChildNodes(banner, 'A');
	var total = bannerArray.length;
	var nextClick = 0;
	var timeOut;
	var self = this;
	
	this.setTime = function()
	{
		clearInterval(timeOut);
		
		var timeCompleted = function()
		{
			nextClick ++;
			
			if (nextClick >= total)
			{
				nextClick = 0;
			}
			
			self.changeBanner(nextClick);
		}
		timeOut = setInterval(timeCompleted, 5000);
	}
	
	this.changeBanner = function(id)
	{
		for (var i=0; i<total; i++)
		{
			bannerArray[i].style.display = '';
		}
		bannerArray[id].style.display = 'block';
		startFading(bannerArray[id].getElementsByTagName('img')[0]);
	}
	
	this.setTime();
	this.changeBanner(0);
}

function listScroll(obj, scroll, autoStart)
{
	var self = this;
	
	this.init = function()
	{	
		this.container  = d.getElementById(obj);
		this.div  = this.container.getElementsByTagName('div')[0];
		this.scrollbar  = d.getElementById(scroll);
		this.track = this.scrollbar.getElementsByTagName('div')[0];
		this.scroller = this.scrollbar.getElementsByTagName('img')[0];
		this.timeOutSpeed = 70;
		this.containerH = this.container.offsetHeight;
		//this.trackH = this.track.offsetHeight - this.scroller.offsetHeight;
		this.id = 0;
		
		this.container.parentNode.onmouseover	= function() {self.stopScroll();};
		this.container.parentNode.onmouseout	= function() {if (!self.scrollHit)	self.autoScrollStart();};
		
		addEvent (this.scroller,	'mousedown',		function() {self.scrollStart();});
		addEvent (this.track,		'mousedown',		function() {self.scrollStart();});
		//addEvent (this.container, 	'mousewheel',		function() {self.scrollStart();});
		//addEvent (this.container, 	'DOMMouseScroll',	function() {self.scrollStart();});
		addEvent (this.scroller,	'mouseup',			function() {self.scrollReset();});
		addEvent (d,				'mouseup',			function() {self.scrollReset();});
		
		var scroller  = new Scrolling.Scroller(this.div, this.scrollbar.offsetWidth, this.track.offsetHeight);
		var scrollbar = new Scrolling.Scrollbar(this.scrollbar, scroller, new Scrolling.ScrollTween());
		
		
		
		//Duplicate Content for loop
		var ul = this.container.getElementsByTagName('ul');
		for (var i=0; i<ul.length; i++)
		{
			ul[i].isScroll = ul[i].offsetHeight > this.containerH;
			if (!ul[i].isScroll)	continue;
			
			var str = ul[i].innerHTML;
			ul[i].innerHTML += str;
		}
		
		if (autoStart)	this.autoScrollMode();
		//if (autoStart)	this.changeList(0);
	}
	
	this.scrollStart = function(id)
	{
		self.scrollBarMode();
		self.scrollHit = true;
	}
	
	this.scrollReset = function(id)
	{
		if (self.scrollHit)
		{
			self.autoScrollMode();
			self.scrollHit = false;
		}
	}
	
	this.changeList = function(id)
	{
		self.reset();
		
		var ul = this.container.getElementsByTagName('ul');
		for (var i=0; i<ul.length; i++)
		{
			ul[i].style.display = (i == id)	?	'block'	:	'none';
			if (!ul[i].isScroll)	continue;
			if (i == id)
			{
				var li = ul[i].getElementsByTagName('li');
				var total = li.length;
				var j = 0;
				while (j < total/2)
				{
					li[j].style.display = 'none';
					j ++;
				}
			}
		}
		
		scrollbar.swapContent(this.container);
		
		this.id = id;
		this.autoScrollMode();
	}
	
	this.reset = function()
	{
		this.container.style.top = '0px';
	}
	
	this.autoScrollStart = function()
	{
		this.stopScroll();
		var ul = this.container.getElementsByTagName('ul');
		if (!ul[this.id].isScroll)	return;
		this.timeOut2 = setTimeout(function(){self.autoScroll(1);}, 1000);
		//self.autoScroll(1);
	}
	
	this.autoScroll = function(num)
	{
		this.num = num;
		
		this.stopScroll();
		this.motion(this.div);
	}
	
	this.goScroll = function(num)
	{
		this.num = num;
		
		this.stopScroll();
		this.motion(this.div);
	}
	
	this.motion = function(container)
	{
		var num = Number(container.style.top.replace('px', '')) - this.num;
		var endH = Math.round(this.objH/2);
		if (num < -endH)	num += endH;
		if (num > 0)		num -= endH;
		container.style.top = Math.round(num) + 'px';
		//window.status = container.style.top + '  :  ' + endH + '  :  ' + this.objH;
		
		//this.scroller.style.top = Math.round((-Number(container.style.top.replace('px', ''))/(this.objH/2))*this.trackH) + 'px';
		
		this.timeOut = setTimeout(function() {self.motion(container);}, this.timeOutSpeed);
	}
	
	this.stopScroll = function()
	{
		clearTimeout(this.timeOut);
		clearTimeout(this.timeOut2);
	}
	
	this.autoScrollMode = function(num)
	{
		var ul = this.container.getElementsByTagName('ul');
		if (!ul[this.id].isScroll)
		{
			this.stopScroll();
			return;
		}
		
		var li = ul[this.id].getElementsByTagName('li');
		for (var j=0; j<li.length; j++)
		{
			li[j].style.display = 'block';
		}
		
		//this.objH = this.container.scrollHeight;
		this.objH = this.div.offsetHeight;
		
		this.autoScrollStart();
	}
	
	this.scrollBarMode = function(num)
	{
		var ul = this.container.getElementsByTagName('ul');
		var li = ul[this.id].getElementsByTagName('li');
		var total = li.length;
		var j = 0;
		while (j < total/2)
		{
			li[j].style.display = 'none';
			j ++;
		}
		this.stopScroll();
	}
	
	this.init();
}

