// JavaScript Document

var channel_right_hover='/images/red_arrow_right.png';
var channel_right_on='/images/yellow_arrow_right.png';
var channel_right_off='/images/grey_arrow_right.png';
var channel_left_hover='/images/red_arrow_left.png';
var channel_left_on='/images/yellow_arrow_left.png';
var channel_left_off='/images/grey_arrow_left.png';
var channelitems=Array();
var scrollers=Array();
var scrollerposition=Array();
var curhover_prev=Array();
var curhover_next=Array();
var categorycache=Array();
var positioncache=Array();

function addEvent(obj, evType, fn, useCapture){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}
} 

function start_channel(channel_id) {
	scrollers[channel_id]='scroller_'+channel_id;
	channelitems[channel_id]='items_'+channel_id;
	scrollerposition[channel_id]=0;
	channel_prev(document.getElementById(scrollers[channel_id]+'_prev'),channel_id);
	var caption;
	for (var i=1;i<=3;i++) {
		scroller=document.getElementById('scroller_'+channel_id+'_'+i);
		if (scroller.addEventListener) {
			scroller.addEventListener('click', e_onclick , false);
		} else if (scroller.attachEvent) {
			scroller.attachEvent('onclick', e_onclick);
		}
	}
}

function e_onclick(e) {
	var catid=0;
	if(isSet(this) && isSet(this.getAttribute) && this.getAttribute('category_id')) {
		catid=this.getAttribute('category_id');
	} else {
		e = e || event; 
		var me=(e.srcElement || e.target);
		while ((!isSet(me.id) && isSet(me.parentNode)) || me.id.substr(0,9)!=='scroller_') me=me.parentNode;
		if (me.id.substr(0,8)!=='scroller_') catid=me.getAttribute('category_id');
	}
	if (catid) document.location=categorycache[catid][1];
}
function fetchcategory(channel_id,position,category_id) {
	if (!isSet(categorycache[category_id])) {
//		var tmp1=href.replace(/gallery/,'thread');
		var tmp= new JSONscriptRequest('/FreeFormCMS/getcategorychannel.php?category_id='+parseInt(category_id)+'&position='+parseInt(position)+'&channel_id='+parseInt(channel_id));
		tmp.buildScriptTag();
		tmp.addScriptTag();
	} else {
		showcategory(channel_id,position,category_id);
	}
}

function showcategory(channel_id,position,category_id) {
	var scroller=document.getElementById('scroller_'+channel_id+'_'+position);
	var scrollerimage=document.getElementById('scrollerimage_'+channel_id+'_'+position);
	var caption=document.getElementById('caption_'+channel_id+'_'+position);
	var title=document.getElementById('title_'+channel_id+'_'+position);
	scrollerimage.setAttribute('alt',categorycache[category_id][0]);
	scrollerimage.setAttribute('src',categorycache[category_id][2]);
	title.innerHTML=categorycache[category_id][0];
	caption.innerHTML=categorycache[category_id][3];
	// attach onclick events to jump page to story. categorycache[category_id][1]
	scroller.setAttribute('category_id',category_id);
}

function showchannelblurbs(channel_id) {
	var myitems=window[channelitems[channel_id]];
	var mylen=myitems.length;
	if (mylen) {
		var pos=parseInt(scrollerposition[channel_id]);
		var p=pos;
		var num=1;
		fetchcategory(channel_id,1,myitems[p]);
		p=1+p;
		num=1+num;
		if (p<mylen) {
			fetchcategory(channel_id,2,myitems[p]);
			p=1+p;
			num=1+num;
			if (p<mylen) {
				fetchcategory(channel_id,3,myitems[p]);
			}
		}
	}
}

function channel_prev(me,channel_id) {
	if (!isSet(scrollerposition[channel_id])) return;
	var pos=parseInt(scrollerposition[channel_id])-3;
	var mylen=parseInt(window[channelitems[channel_id]].length);
	if (pos <= 0) {
		pos = 0;
		me.src=curhover_prev[channel_id]=channel_left_off;
		me.style.cursor="auto";
	} else {
		me.src=curhover_prev[channel_id]=channel_left_on;
		me.style.cursor="pointer";
	}
	var them=document.getElementById(scrollers[channel_id]+'_next');
	if (3+pos < mylen) {
		them.src=curhover_next[channel_id]=channel_right_on;
		them.style.cursor="pointer";
	} else {
		them.src=curhover_next[channel_id]=channel_right_off;
		them.style.cursor="auto";
	}
	scrollerposition[channel_id]=pos;
	showchannelblurbs(channel_id);
}

function channel_next(me,channel_id) {
	if (!isSet(scrollerposition[channel_id])) return;
	var pos=3+parseInt(scrollerposition[channel_id]);
	var mylen=parseInt(window[channelitems[channel_id]].length);
	if (pos >= mylen) {
		pos=0-3+mylen;
		me.src=curhover_next[channel_id]=channel_right_on;
		me.style.cursor="pointer";
	} else {
		me.src=curhover_next[channel_id]=channel_right_off;
		me.style.cursor="auto";
	}
	if (pos+1>=mylen) pos=pos-1;
	if (pos+2>=mylen) pos=pos-1;
	var them=document.getElementById(scrollers[channel_id]+'_prev');
	if (pos<=0) {
		pos = 0;
		them.src=curhover_prev[channel_id]=channel_left_off;
		them.style.cursor="auto";
	} else {
		them.src=curhover_prev[channel_id]=channel_left_on;
		them.style.cursor="pointer";
	}
	scrollerposition[channel_id]=pos;
	showchannelblurbs(channel_id);
}

function channel_next_over(me,channel_id) {
	if (!isSet(scrollerposition[channel_id])) return;
	var mylen=parseInt(window[channelitems[channel_id]].length);
	if (3+parseInt(scrollerposition[channel_id])<mylen) {
		me.src=channel_right_hover;
	}
}

function channel_next_out(me,channel_id) {
	if (!isSet(scrollerposition[channel_id])) return;
	var mylen=parseInt(window[channelitems[channel_id]].length);
	if (3+parseInt(scrollerposition[channel_id])<mylen) {
		me.src=channel_right_on;
	} else {
		me.src=channel_right_off;
	}
}

function channel_prev_over(me,channel_id) {
	if (!isSet(scrollerposition[channel_id])) return;
	var mylen=parseInt(window[channelitems[channel_id]].length);
	if (parseInt(scrollerposition[channel_id])>0) {
		me.src=channel_left_hover;
	}
}

function channel_prev_out(me,channel_id) {
	if (!isSet(scrollerposition[channel_id])) return;
	var mylen=parseInt(window[channelitems[channel_id]].length);
	if (parseInt(scrollerposition[channel_id])>0) {
		me.src=channel_left_on;
	} else {
		me.src=channel_left_off;
	}
}