/*
****************************
tib-slotads.js
****************************
	CUSTOM replacement for $.Accordion plugin
	args (container,header,element,selected,start)
		container: '#menu'
		header:    'dark_head' (default in markup)
		element:   'open_box'
		selected:  'lignt_head' (css changed)
		start:     0-n (index- positive integer starting with zero)
****************************
*/

LeftMenu = function(container,header,element,selected,start) {
	var lbase=this;
	this.cont=container;
	this.head=header;
	this.elem=element;
	this.selected=selected;
	this.current=start;
	this.settings={
		rolltime:10,
		delay:300,
		height:168,
		out_call:'',
		pop_call:function(){ ads_restart() },
		roll_call:'',
		enable_ani:true};
	this.running=false;
	this.fired=false;
	this.hover=-1;
	this.poprate=800;
	this.rolltimer=null;
	this.popdelay=null;
	this.hold=false;
	this.spotimer=null;
	
	//INITIALIZE
	this.len=$(lbase.cont+' [id^=slot]').length;
	//this.len=$(lbase.cont+' .acc_light').length;
	$(this.cont+' .'+this.elem).height(this.settings.height).slideUp(10);
	//$(this.cont+' .'+this.elem).eq(this.current).slideDown(1000);
	$(this.cont+' .'+this.head).eq(this.current).attr('class',this.selected);
	$(this.cont+' .'+this.selected).next().slideDown(1000);
	
	//RANDOMIZE ADS
	var ssorder = [];
	$(this.cont+' [id^=slot] a').each(function(i){ ssorder[i]=[this.innerHTML]; });
	$(this.cont+' .'+this.elem).each(function(i){ ssorder[i][1]=this.innerHTML; });
	ssorder.sort(randOrd);
	$(this.cont+' [id^=slot] a').each(function(i){ this.innerHTML=ssorder[i][0]; });
	$(this.cont+' .'+this.elem).each(function(i){ this.innerHTML=ssorder[i][1]; });
	
	
	//SETUP EVENTS
	$(lbase.cont).hover( function(){
		//clearInterval(lbase.rolltimer);
		//lbase.hold=true;
	}, function(){
		//clearInterval(lbase.rolltimer);
		//lbase.rolltimer=setInterval(lbase.rollNow,lbase.settings.rolltime*1000);
		//clearTimeout(lbase.popdelay);
		
		lbase.hold=false;
		//slothold=false;
		
		if (lbase.settings.out_call) { lbase.settings.out_call() };
	});
	$(lbase.cont+' [id^=slot]').hover( function(){
	//$(lbase.cont+' .acc_light').hover( function(){
		if (lbase.fired) { return };
		lbase.hover=this.getAttribute('id').slice(4);
		clearTimeout(lbase.popdelay);
		lbase.popdelay=setTimeout(lbase.popHere, lbase.settings.delay);
		//lbase.pop(lbase.hover);
	}, function(){
		clearTimeout(lbase.popdelay);
	});
	
	//METHODS
	this.popHere = function() {
		lbase.hold=true;
		//slothold=true;
		lbase.pop(lbase.hover);
		if (lbase.settings.pop_call) { lbase.settings.pop_call() };
	};
	
	this.pop = function(idx) {
		if (lbase.fired) { return };
		if (idx>=0 && idx<lbase.len && idx!=lbase.current) {
			lbase.fired=true;
			
			var x=parseInt(idx);
			//$(lbase.cont+' .'+lbase.selected).next().slideDown(lbase.poprate);
			$(lbase.cont+' .'+lbase.elem).eq(lbase.current).slideUp(lbase.poprate);
			$(lbase.cont+' .'+lbase.selected).attr('class',lbase.head);
			//$(lbase.cont+' [id^=slot]').eq(x).attr('class',lbase.selected);
			$(lbase.cont+' .'+lbase.head).eq(x).attr('class',lbase.selected);
			//$(lbase.cont+' .'+lbase.selected).next().slideUp(lbase.poprate, function() { lbase.fired=false });
			$(lbase.cont+' .'+lbase.elem).eq(x).slideDown(lbase.poprate, function() { lbase.fired=false });
			lbase.current=x;
		};
	};
	
	this.noHold = function() {
		lbase.hold=false;
		//slothold=false;
	};
	
	roll0 = function() {
		var hold = [];
		$('.acc_light a:not(:first)').add('.acc_light a:first').each(function(i){ hold[i]=this.innerHTML; });
		$('.acc_light a').each(function(i){ this.innerHTML=hold[i]; });
		hold = [];
		$('.acc_open:not(:first)').add('.acc_open:first').each(function(i){ hold[i]=this.innerHTML; });
		$('.acc_open').each(function(i){ this.innerHTML=hold[i]; });
		
	};
	
	this.roll = function() {
		var ss = [];
		$(lbase.cont+' [id^=slot] a').each(function(i){ ss[i]=[this.innerHTML]; });
		$(lbase.cont+' .acc_open').each(function(i){ ss[i][1]=this.innerHTML; });
		//$(lbase.cont+' .'+lbase.elem).each(function(i){ ss[i][1]=this.innerHTML; });
		//ss.push(ss.shift());
		ss.unshift(ss.pop());
		$(lbase.cont+' [id^=slot] a').each(function(i){ this.innerHTML=ss[i][0]; });
		$(lbase.cont+' .acc_open').each(function(i){ this.innerHTML=ss[i][1]; });
		//$(lbase.cont+' .'+lbase.elem).each(function(i){ this.innerHTML=ss[i][1]; });
	/*
	{% comment %}
		$(lbase.cont+' [id^=slot] a').each(function(i){ ss[i]=this.innerHTML; });
		//ss.push(ss.shift());
		ss.unshift(ss.pop());
		$(lbase.cont+' [id^=slot] a').each(function(i){ this.innerHTML=ss[i]; });
		ss = [];
		$(lbase.cont+' .'+this.elem).each(function(i){ ss[i]=this.innerHTML; });
		//ss.push(ss.shift());
		ss.unshift(ss.pop());
		$(lbase.cont+' .'+this.elem).each(function(i){ this.innerHTML=ss[i]; });
	{% endcomment %}
	*/
	};
	
	this.rollNow = function() {
		if (lbase.hold) { return };
		//if (slothold) { return };
		if (lbase.current) { lbase.pop(0) };
		
		if (lbase.settings.enable_ani) {
			$(lbase.cont+' .acc_dark').next(lbase.cont+' .acc_open').fadeOut(800, function() {
				lbase.roll();
				$(lbase.cont+' .acc_dark').next(lbase.cont+' .acc_open').fadeIn(1500);
			});
		} else {
			lbase.roll();
		};
		
		if (lbase.settings.roll_call) { lbase.settings.roll_call() };
	};
	
	this.startroll = function() {
		lbase.rolltimer=setInterval(lbase.rollNow,lbase.settings.rolltime*1000);
	};
	
	this.stoproll = function() {
		clearTimeout(lbase.popdelay);
		clearInterval(lbase.rolltimer);
	};
	
	this.restartroll = function() {
		clearTimeout(lbase.popdelay);
		clearInterval(lbase.rolltimer);
		lbase.rolltimer=setInterval(lbase.rollNow,lbase.settings.rolltime*1000);
	
	};
	
	return this;
};


RightMenu = function(container,header,element,selected,start) {
	var rbase=this;
	this.rcont=container;
	this.rhead=header;
	this.relem=element;
	this.rselected=selected;
	this.rcurrent=start;
	this.settings={
		rolltime:10,
		delay:300,
		height:168,
		out_call:'',
		pop_call:function(){ ads_restart() },
		roll_call:'',
		enable_ani:true};
	this.rrunning=false;
	this.rfired=false;
	this.rhover=-1;
	this.rpoprate=800;
	this.rrolltimer=null;
	this.rpopdelay=null;
	this.rhold=false;
	this.rspotimer=null;
	
	//INITIALIZE
	this.len=$(rbase.rcont+' [id^=cslot]').length;
	//this.len=$(rbase.rcont+' .acc_light').length;
	$(this.rcont+' .'+this.relem).height(this.settings.height).slideUp(10);
	//$(this.rcont+' .'+this.relem).eq(this.rcurrent).slideDown(1000);
	$(this.rcont+' .'+this.rhead).eq(this.rcurrent).attr('class',this.rselected);
	$(this.rcont+' .'+this.rselected).next().slideDown(1000);
	
	
	//SETUP EVENTS
	$(rbase.rcont).hover( function(){
		//clearInterval(rbase.rrolltimer);
		//rbase.rhold=true;
	}, function(){
		//clearInterval(rbase.rrolltimer);
		//rbase.rolltimer=setInterval(rbase.rrollNow,rbase.settings.rolltime*1000);
		//clearTimeout(rbase.rpopdelay);

		rbase.rhold=false;
		//slothold=false;
		slots.noHold();
		
		//if (rbase.settings.out_call) { rbase.settings.out_call() };
	});
	$(rbase.rcont+' [id^=cslot]').hover( function(){
	//$(rbase.rcont+' .acc_light').hover( function(){
		if (rbase.rfired) { return };
		rbase.rhover=this.getAttribute('id').slice(5);
		clearTimeout(rbase.rpopdelay);
		rbase.rpopdelay=setTimeout(rbase.rpopHere, rbase.settings.delay);
		//rbase.pop(rbase.rhover);
	}, function(){
		clearTimeout(rbase.rpopdelay);
	});
	
	//METHODS
	this.rpopHere = function() {
		rbase.rhold=true;
		//slothold=true;
		rbase.rpop(rbase.rhover);
		//if (rbase.settings.pop_call) { rbase.settings.pop_call() };
	};
	
	this.rpop = function(idx) {
		if (rbase.rfired) { return };
		if (idx>=0 && idx<rbase.len && idx!=rbase.rcurrent) {
			rbase.rfired=true;
			
			var x=parseInt(idx);
			//$(rbase.rcont+' .'+rbase.relem).eq(rbase.rcurrent).slideUp(rbase.rpoprate);
			$(rbase.rcont+' .'+rbase.relem).eq(rbase.rcurrent).slideUp(800);
			$(rbase.rcont+' .'+rbase.rselected).attr('class',rbase.rhead);
			$(rbase.rcont+' .'+rbase.rhead).eq(x).attr('class',rbase.rselected);
			//$(rbase.rcont+' .'+rbase.relem).eq(x).slideDown(rbase.rpoprate, function() { rbase.rfired=false });
			$(rbase.rcont+' .'+rbase.relem).eq(x).slideDown(800, function() { rbase.rfired=false });
			rbase.rcurrent=x;
		};
	};
	
	this.rnoHold = function() {
		rbase.rhold=false;
		//slothold=false;
	};
	
	roll0 = function() {
		var hold = [];
		$('.acc_light a:not(:first)').add('.acc_light a:first').each(function(i){ hold[i]=this.innerHTML; });
		$('.acc_light a').each(function(i){ this.innerHTML=hold[i]; });
		hold = [];
		$('.acc_open:not(:first)').add('.acc_open:first').each(function(i){ hold[i]=this.innerHTML; });
		$('.acc_open').each(function(i){ this.innerHTML=hold[i]; });
		
	};
	
	this.rroll = function() {
		var rss = [];
		$(rbase.rcont+' [id^=cslot] a').each(function(i){ rss[i]=this.innerHTML; });
		$(rbase.rcont+' [id^=cslot] a').each(function(i){ rss[i]=this.innerHTML; });
		//rss.push(rss.shift());
		rss.unshift(rss.pop());
		$(rbase.rcont+' [id^=cslot] a').each(function(i){ this.innerHTML=rss[i]; });
		$(rbase.rcont+' [id^=cslot] a').each(function(i){ this.innerHTML=rss[i]; });
		rss = [];
		$(rbase.rcont+' .'+this.relem).each(function(i){ rss[i]=this.innerHTML; });
		//rss.push(rss.shift());
		rss.unshift(rss.pop());
		$(rbase.rcont+' .'+this.relem).each(function(i){ this.innerHTML=rss[i]; });
		
	};
	
	this.rrollNow = function() {
		if (rbase.rhold) { return };
		//if (slothold) { return };
		if (rbase.rcurrent) { rbase.rpop(0) };
		
		if (rbase.settings.enable_ani) {
			$(rbase.rcont+' .acc_dark').next(rbase.rcont+' .acc_open').fadeOut(800, function() {
				rbase.rroll();
				$(rbase.rcont+' .acc_dark').next(rbase.rcont+' .acc_open').fadeIn(1500);
			});
		} else {
			rbase.rroll();
		};
		
		if (rbase.settings.roll_call) { rbase.settings.roll_call() };
	};
	
	this.rstartroll = function() {
		rbase.rrolltimer=setInterval(rbase.rrollNow,rbase.settings.rolltime*1000);
	};
	
	this.rstoproll = function() {
		clearTimeout(rbase.rpopdelay);
		clearInterval(rbase.rrolltimer);
	};
	
	this.rrestartroll = function() {
		clearTimeout(rbase.rpopdelay);
		clearInterval(rbase.rrolltimer);
		rbase.rrolltimer=setInterval(rbase.rrollNow,rbase.settings.rolltime*1000);
	
	};
	
	return this;
};


/*
****************************
*
*	SPONSOR object - handles firing sponsor changes - and hover events
*
*/
function DoSponsors() {
	
	this.enable_ani=true;
	this.holdspon=false;
	this.spon_idx = [0,0,0];
	this.nnspon = [0,0,0];
	this.spon_type = sponsor_type;
	this.pop_call2 = function(){ ads_restart() };
	var base2=this;
	
	//INITIALIZE
	for (var i=0;i<3;i++){
		base2.nnspon[i] = $('#sponsors_'+sponsor_type[i]+' > div').size();
	};
	
	//SETUP EVENTS
	$('[id^=sponsors]').hover( function(){
		//clearInterval(spotimer2);
		base2.holdspon=true;
		
	}, function(){
		//spotimer2 = setInterval(fire_sponsors,slots.settings.rolltime*1000);
		//slots.restart();
		base2.holdspon=false;
		base2.pop_call2();
		
	});
	
	//METHODS
	
//	this.togglehold = function() {
//		if (base2.holdspon) { base2.holdspon=false } else { base2.holdspon=true };
		
//	};
	
	//FIRE SPONSORS
	
	this.rollspon = function() {
		if (base2.holdspon) { return };
		
		if (base2.enable_ani) {
			$('#sponsors_'+base2.spon_type[0]+' > div').eq(base2.spon_idx[0]).fadeOut(1000);
			$('#sponsors_'+base2.spon_type[1]+' > div').eq(base2.spon_idx[1]).fadeOut(1000);
			$('#sponsors_'+base2.spon_type[2]+' > div').eq(base2.spon_idx[2]).fadeOut(1000, function() {
				for (var i=0;i<3;i++){
					$('#sponsors_'+base2.spon_type[i]+' > div').eq(base2.spon_idx[i]).css('display','none');
					base2.spon_idx[i]++;
					if (base2.spon_idx[i]==base2.nnspon[i]) { base2.spon_idx[i]=0 };
					//$('#sponsors_'+sponsor_type[i]+' > div').eq(sponsor_idx[i]).fadeOut(0);
					$('#sponsors_'+base2.spon_type[i]+' > div').eq(base2.spon_idx[i]).css('display','block');
				};
			});
		} else {
			for (i=0;i<3;i++){
				$('#sponsors_'+base2.spon_type[i]+' > div').eq(base2.spon_idx[i]).css('display','none');
				base2.spon_idx[i]++;
				if (base2.spon_idx[i]==base2.nnspon[i]) { sponsor_idx[i]=0 };
				$('#sponsors_'+base2.spon_type[i]+' > div').eq(base2.spon_idx[i]).css('display','block');
			};
		};
		
	};
	
	//var roll = this.rollspon;
	
	return this;
};
