
/*

*/

var article_browser = {
	num : 0,
	curr : 0,
	old : null,
	t1 : null,
	active : 1,
	load : function(id) {
		var pg = document.getElementById(id);
		if (!pg) return;
		this.pg = pg;
		this.feed = pg.getElementsByTagName('div');
		this.img = [];
		for (var i = 0; i < this.feed.length; ++i) {
			this.img[i] = {};
			this.img[i].href = this.feed[i].getElementsByTagName('a')[0].href;
			this.img[i].hd = this.feed[i].getElementsByTagName('h2')[0].innerHTML;
			this.img[i].bg = this.feed[i].getElementsByTagName('img')[0].src;
			this.img[i].p = this.feed[i].getElementsByTagName('p')[0].innerHTML;
			var target = (this.img[i].href.indexOf('.onthesnow.') > -1) ? '_parent' : '_blank';
			target = (this.img[i].href.indexOf('oascentral.') > -1) ? '_blank' : target;
			//this.feed[i].getElementsByTagName('h2')[0].innerHTML = '<a href="' + this.img[i].href + '" target="' + target + '">' + this.img[i].hd + '</a>';
			this.img[i].html = this.feed[i].innerHTML;
		}
		this.tLen = this.img.length;
		pg.innerHTML = '<div id="teasers" class="teasers"></div><div id="controls" class="controls"></div>';
		this.obj = document.getElementById('teasers');
		if (!this.obj) return;
		var s = '';
		for (var i = 0; i < this.img.length; ++i) {
			s += '<div id="teaser' + i + '" style="display:none;">';
			s += '<b style="background-image:url(' + this.img[i].bg + ');"><a href="' + this.img[i].href + '"></a></b>';
			s += '<span class="teaser">';
			//s += this.img[i].html;
			s += '<h2><a href="' + this.img[i].href + '" target="' + target + '">' + this.img[i].hd + '</a></h2>';
			s += '<p>' + this.img[i].p + '</p>';
			s += '</span><span class="marker"></span></div>';
		}
		this.obj.innerHTML = s;
		if (this.tLen > 1) this.makeNav();
		pg.style.display = 'block';
		this.setData();
	},
	makeNav : function() {
		var html = '';
		var ow = (this.pg.className.indexOf('2') > -1) ? 645 : 465;
		var w = Math.floor((ow - (this.tLen * 5)) / this.tLen);
		var c = (this.tLen == 2) ? 50 : (this.tLen == 4) ? 30 : 38;
		for (var i = 0; i < this.tLen; ++i) {
			var bg = this.img[i].bg.replace('_md.', '_sm.');
			var target = (this.img[i].href.indexOf('www.onthesnow.') > -1) ? '_parent' : '_blank';
			target = (this.img[i].href.indexOf('oascentral.') > -1) ? '_blank' : target;
			var hd = (this.img[i].hd.length > c) ? this.img[i].hd.substring(0, (c - 1)) + '...' : this.img[i].hd;
			html += '<a id="btn' + i +'" style="width:' + w + 'px;" href="' + this.img[i].href + '" onmouseover="article_browser.gotoNav(' + i + ');" target="' + target + '"><img src="' + bg + '" width="30" alt="" border="0" /><span>' + hd + '</span></a> ';
		}
		var nav = document.getElementById('controls');
		if (!nav) return;
		nav.innerHTML = html;
	},
	setData : function() {
		clearTimeout(this.t1);
		if (this.curr > (this.tLen - 1)) {
			this.curr = 0;
		}
		if (this.curr < 0) {
			this.curr = (this.tLen - 1);
		}
		var oto = document.getElementById('teaser' + this.old);
		if (oto) oto.style.display = 'none';
		var to = document.getElementById('teaser' + this.curr);
		if (to) to.style.display = 'block';
		var oldelm = document.getElementById('btn' + this.old);
		if (oldelm) oldelm.className = '';
		var currelm = document.getElementById('btn' + this.curr);
		if (currelm) currelm.className = 'active';
		if (this.tLen > 1) {
			this.fadeIn();
		}
	},
	gotoNav : function(v) {
		this.doPause();
		this.old = this.curr;
		this.curr = parseInt(v);
		this.setData();
	},
	doPlay : function() {
		this.active = 1;
		this.fadeIn();
	},
	doPause : function() {
		clearTimeout(this.t1);
		this.active = 0;
	},
	doNav : function(v) {
		if (this.num > 0 && this.num < 100) return;
		this.active = 0;
		this.old = this.curr;
		this.curr = parseInt(v);
		this.fadeOut();	
	},
	doNext : function() {
		this.old = this.curr;
		this.curr++;
		this.fadeOut();
	},
	doPrev : function() {
		this.old = this.curr;
		this.curr--;
		this.fadeOut();
	},
	fadeIn : function() {
		if (this.num < 100) {
			this.setFade(this.num);
			this.num += 5;
			setTimeout('article_browser.fadeIn()', 10);
		} else {
			this.num = 100;
			this.setFade(100);
			if (this.active == 1) {
				this.t1 = setTimeout('article_browser.doNext()', 5000);
			}
		}
	},
	fadeOut : function() {
		if (this.num > 0) {
			this.setFade(this.num);
			this.num -= 5;
			setTimeout('article_browser.fadeOut()', 10);
		} else {
			this.num = 0;
			this.setFade(0);
			this.setData();
		}
	},
	setFade : function(opacity) {
		this.obj.style.opacity = (opacity / 100);
		this.obj.style.MozOpacity = (opacity / 100);
		this.obj.style.KhtmlOpacity = (opacity / 100);
		this.obj.style.filter = 'alpha(opacity=' + opacity + ')';
	}
}
article_browser.load('articleBrowser');
