﻿
function cbr(obj,reference,container,count,current,interval,sticky){

	this.obj = obj;
	this.reference = reference;
	this.container = container;
	this.count = count;
	this.current = current - 1;
	this.interval = interval;
	this.sticky = sticky;
	this.bnrNfo = new Array(count);
	this.btnNextCss = '';
	this.btnPrevCss = '';
	this.btnNextMoCss = '';
	this.btnPrevMoCss = '';
	this.btnNrCss = '';
	this.btnNrSelCss = '';
	this.btnNrMoCss = '';
	this.prevSel = -1;
	this.started = false;
	this.preloaded = false;
	this.autoloop = null;
	
	if(document.getElementById(this.obj + 'Ctrls')){
		document.getElementById(this.obj + 'Ctrls').style.visibility = 'visible';
	}

	function Start(){
		if(!this.preloaded){this.Preload(this.current);}
		this.Stop();
		this.autoloop = setInterval(this.obj + '.NextAuto()', this.interval);
		this.started = true;
		this.SwitchButton();
	}
	
	function Stop(){
		if(this.started){
			clearInterval(this.autoloop);
			this.autoloop = null;
			this.started = false;
		}
	}
	
	function NextAuto(){
		this.CountUp();
		this.SwitchBanner();
		this.SwitchButton();
	}

	function Next(e){
		this.Stop();
		this.CountUp();
		this.SwitchBanner();
		this.Start();
	}
	
	function Prev(e){
		this.Stop();
		this.CountDown();
		this.SwitchBanner();
		this.Start();
	}
	
	function Sel(e,s){
		if(s - 1 != this.current){
			this.Stop();
			this.prevSel = this.current;
			this.current = s - 1;
			this.SwitchBanner();
			this.SwitchButton();
			this.Start();
		}
	}
	
	function SwitchBanner(){
		document.getElementById(this.container).innerHTML = this.bnrNfo[this.current][0];
		this.Stick();
	}
	
	function SwitchButton(){
		if(document.getElementById(this.obj + 'nrBtn' + (this.current + 1))){
			if(this.btnNrCss == ''){
				this.btnNrCss = document.getElementById(this.obj + 'nrBtn' + (this.current + 1)).className;
				if(this.btnNrSelCss == ''){this.btnNrSelCss = this.btnNrCss;}
			}
			if(this.prevSel > -1){document.getElementById(this.obj + 'nrBtn' + (this.prevSel + 1)).className = this.btnNrCss;}
			if(this.btnNrSelCss != ''){document.getElementById(this.obj + 'nrBtn' + (this.current + 1)).className = this.btnNrSelCss;}
		}
	}
	
	function CountUp(){
		this.prevSel = this.current;
		this.current ++;
		if(this.current > this.count - 1){this.current = 0;}
	}
	
	function CountDown(){
		this.prevSel = this.current;
		this.current --;
		if(this.current < 0){this.current = this.count - 1;}
	}
	
	function Stick(){
		if(this.sticky > 0){
			var expDate = new Date();
			expDate.setTime(expDate.getTime() + (this.sticky*24*60*60*1000));
			var stickyCookie = 'cbr' + this.reference + "=" + this.bnrNfo[this.current][2] + "; expires=" + expDate.toGMTString() + "; path=/";
			document.cookie = stickyCookie;
		}
	}
	
	function NrOver(e,s){
		if(this.btnNrCss == ''){this.btnNrCss = this.GetTarget(e).className;}
		if(s - 1 != this.current ){this.GetTarget(e).className = this.btnNrMoCss;}
	}
	
	function NrOut(e,s){
		if(s - 1 == this.current){
			if(this.btnNrSelCss != ''){this.GetTarget(e).className = this.btnNrSelCss;}
		}
		else
		{
			if(this.btnNrCss != ''){this.GetTarget(e).className = this.btnNrCss;}
		}
	}
	
	function NextOver(e){
		if(this.btnNextCss == ''){this.btnNextCss = this.GetTarget(e).className;}
		this.GetTarget(e).className = this.btnNextMoCss;
	}
	
	function NextOut(e){
		if(this.btnNextCss != ''){this.GetTarget(e).className = this.btnNextCss;}
	}
	
	function PrevOver(e){
		if(this.btnPrevCss == ''){this.btnPrevCss = this.GetTarget(e).className;}
		this.GetTarget(e).className = this.btnPrevMoCss;
	}
	
	function PrevOut(e){
		if(this.btnPrevCss != ''){this.GetTarget(e).className = this.btnPrevCss;}
	}
	
	function GetTarget(e){
		if(e.target != null){return e.target;}
		return e.srcElement;
	}
	
	function Preload(c){
		var imgObjs = new Array();
		for(var i = 0; i < this.bnrNfo.length; i++){
			if(this.bnrNfo[i][1] != '' && c != i){
				var imgObj = new Image;
				imgObjs.push(imgObj);
				imgObj.src = this.bnrNfo[i][1];
			}
		}
		this.preloaded = true;
	}
	
	this.Start = Start;
	this.Stop = Stop;
	this.NextAuto = NextAuto;
	this.Next = Next;
	this.Prev = Prev;
	this.Sel = Sel;
	this.SwitchBanner = SwitchBanner;
	this.SwitchButton = SwitchButton;
	this.CountUp = CountUp;
	this.CountDown = CountDown;
	this.Stick = Stick;
	this.NrOver = NrOver;
	this.NrOut = NrOut;
	this.NextOver = NextOver;
	this.NextOut = NextOut;
	this.PrevOver = PrevOver;
	this.PrevOut = PrevOut;
	this.GetTarget = GetTarget;
	this.Preload = Preload;

}