var csPage = null;

function csapiPage() {
	this.colorTheme = null;
	this.colorNum = null;
	this.menu = null;
	this.banner = null;
	this.splash = null;
	this.onload = new Array();
	this.services = new Array();
	this.arVersion = navigator.appVersion.split("MSIE");
	this.version = parseFloat(this.arVersion[1]);
	this.bannerDom;
	
	this.setColorTheme = function(clr,num) {
		clr = ""+clr;
		this.colorTheme = clr.toLowerCase();
		this.colorNum = num;
	}
	
	this.setSplashRotator = function(imgCnt) {
		this.splash = new csapiSplash(imgCnt);
	}
	
	this.setSplashTextItem = function(str) {
		if ( !this.splash ) { return; }
		this.splash.setTextListItem(str);
	}
	
	this.showServices = function() {
		this.bannerDom = document.getElementById('banner_innertext');
		this.rotateServices();
	}
	
	this.rotateServices = function() {
		var id = this.getRandomId(7);
		var src = 'cswebbin/images/ban_txt'+id+'.png';
		this.bannerDom.innerHTML = this.fixPNG(src);
		window.setTimeout("csPage.rotateServices()",5000);
	}

	this.fixPNG = function(src) {
		if ( (this.version >= 5.5) && (this.version < 7) && (document.body.filters) ) {
			var html = "<span style=\"" + "width:400px;height:30px;display:inline-block;"
		                  + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		                  + "(src=\'" + src + "\', sizingMethod='scale');\"></span>";
		} else {
			var html = "<img src=\""+src+"\" height=\"30\" width=\"400\"/>";
		}
		return html
	}

	
	this.splashRotate = function() {
		if ( !this.splash ) { return; }
		var _args = this.splashRotate.arguments;
		if ( _args.length > 1 ) {
			// new call, change divs
			this.splash.setDivs(_args[0],_args[1]);
		}
		this.splash.rotate();
	}
	
	this.setMenuItem = function(mnuText,mnuUrl,mnuClass) {
		if ( !this.menu ) { this.menu = new csapiMenu(); }
		this.menu.setMenuItem(mnuText,mnuUrl,mnuClass);
	}
	
	this.getRandomId = function(max) {
		var n = max;
    return (Math.floor(Math.random() * n))+1;
	}
	
	this.setBanner = function(txt) {
		this.banner = new csapiBanner(this.colorTheme,this.colorNum,this.setBanner.arguments);
	}
	
	this.showBanner = function(clr,dir) {
		var bnr = new csapiBanner(clr,dir);
	}
	/**
	 * Pass in the timestamp [new Date()] and the format
	 * @param {Date} timestamp The timestamp as a date/time object
	 * @param {String} format The format to specify
	 * @return {String} String representation of date according to format.
	 */
	this.formatDate = function() {
		var _args = this.formatDate.arguments;
		var ts = (_args.length>0?_args[0]:null);
		var fmt = (_args.length>1?_args[1]:null);
		if ( ts == null || typeof(ts) != "object" ) { return ""; }
		if ( fmt == null || fmt == "" ) { return ts.toLocaleString(); }
		var dy = new Array("","Su|Sun|Sunday","Mo|Mon|Monday","Tu|Tue|Tuesday","We|Wed|Wednesday","Th|Thu|Thursday","Fr|Fri|Friday","Sa|Sat|Saturday");
		var mn = new Array("","Jan|January","Feb|February","Mar|March","Apr|April","May|May","Jun|June","Jul|July","Aug|August","Sep|September","Oct|October","Nov|November","Dec|December");
		var d = ""+ts.getDate();
		var dd = (parseInt(d)<10?"0":"")+parseInt(d);
		var m = ""+parseInt(parseInt(ts.getMonth())+1);
		var mm = (parseInt(m)<10?"0":"")+parseInt(m);
		var mns = mn[parseInt(m)];
		var mnArr = mns.split("|");
		var mmm = mnArr[0];
		var mmmm = mnArr[1];
		var yy = ""+ts.getFullYear();
		var y = yy.substring(2,2);
		var w = ""+parseInt(parseInt(ts.getDay())+1);
		var dyArr = dy[parseInt(w)];
		var dys = dyArr.split("|");
		var ww = dys[0];
		var www = dys[1];
		var wwww = dys[2];
		var h2 = ""+ts.getHours();
		var hh2 = (parseInt(h2)<10?"0":"")+parseInt(h2);
		var h = ""+(parseInt(h2)<=12?h2:parseInt(h2)-12);
		var hh = (parseInt(h)<10?"0":"")+parseInt(h);
		var n = ""+ts.getMinutes();
		var nn = (parseInt(n)<10?"0":"")+parseInt(n);
		var s = ""+ts.getSeconds();
		var ss = (parseInt(s)<10?"0":"")+parseInt(s);
		var a = (parseInt(h2)<=11?"A":"P")+"M";
		var rtn = "";
		for(i=0;i<fmt.length;i++) {
			switch( fmt.charAt(i) ) {
				// lowercase Ante or Post meridiem
				case "a"	: rtn += a.toLowerCase(); break;
				// uppercase Ante or Post meridiem
				case "A"	: rtn += a; break;
				// day of month, 2 digits with leading zero
				case "d"	: rtn += dd; break;
				// Textual representation of day, three letters
				case "D"	: rtn += www; break;
				// full textual representation of month
				case "F"	: rtn += mmmm; break;
				// 12-hour format of hour w/o leading zero
				case "g"	: rtn += h; break;
				// 24-hour format of hour w/o leading zero
				case "G"	: rtn += h2; break;
				// 12-hour format of hour w/ leading zero
				case "h"	: rtn += hh; break;
				// 24-Hour format of hour w/ leading zero
				case "H"	: rtn += hh2; break;
				// Minutes w/ leading zero
				case "i"	: rtn += nn; break;
				// Day of month w/o leading zero
				case "j"	: rtn += d; break;
				// Full textual representation of day of week (Sunday through Saturday)
				case "l"	: rtn += wwww; break;
				// Whether it's a leap year (1 if is, 0 if not)
				//case "L"	: s += dateIsLeapYear(timestamp); break;
				// Numeric representation of month w/ leading zero
				case "m"	: rtn += mm; break;
				// Short textual representation of month, three letter
				case "M"	: rtn += mmm; break;
				// Numeric representation of month w/o leading zero
				case "n"	: rtn += m; break;
				// Seconds w/ leading zero
				case "s"	: rtn += ss; break;
				// English ordinal suffix for day of month, 2 char (st, nd, rd, th)
				//case "S"	: s += monthOrdSuffix(timestamp); break;
				// Number of days in the given month (28 through 31)
				//case "t"	: s += dateGetMonthDays(timestamp); break;
				// Seconds since the Unix Epoch (January 1, 1970 00:00:00 GMT)
				case "U"	: rtn += ""+ts.getTime(); break;
				// Numeric representation of the day of week (1 for Sun, 7 for Sat)
				case "w"	: rtn += w; break;
				//case "W"	: s += ""; break; // Week number of year
				// Full numeric representation of year, 4 digits
				case "Y"	: rtn += yy; break;
				// Two digit representation of year
				case "y"	: rtn += y; break;
				//case "z"	: s += ""; break; // Day of year (starting from 0) (0 through 365)
				default		: rtn += fmt.charAt(i); break; // leave alone, let it pass through
			}
		}
		return rtn;
	}
	
	this.todaysDate = function() { document.write(this.formatDate(new Date(),"l F j, Y")); }
	
	this.addOnloadFunction = function(func) {
		this.onload.push(func);
	}
	
	this.doOnLoad = function() {
		for(var i=0; i<this.onload.length; i++) {
			try {
				eval(this.onload[i]);
			} catch(e) {}
		}
	}
	
	this.menu_toggle = function(_this,div_id,img_dir) {
		var dom = document.getElementById(div_id);
		if ( !dom ) { return; }
		var status = _this.getAttribute('status');
		if ( status == 'expanded' ) {
			// collapse
			_this.setAttribute('alt','Click to expand');
			_this.setAttribute('status','collapsed');
			_this.setAttribute('src',img_dir+'/menu_expand.gif')
			dom.style.display = 'none';
		} else {
			// expand
			_this.setAttribute('alt','Click to collapse');
			_this.setAttribute('status','expanded');
			_this.setAttribute('src',img_dir+'/menu_collapse.gif')
			dom.style.display = 'block';
		}
	}

}

function csapiSplash(imgCnt) {
	this.txtDom = null;
	this.imgDom = null;
	this.imgCount = imgCnt;
	this.txtLst = new Array();
	this.rotateTime = 5;
	
	this.setTextListItem = function(str) { this.txtLst.push(str); }
	
	this.setDivs = function(imgDiv,txtDiv) {
		this.setImageDiv(imgDiv);
		this.setTextDiv(txtDiv);
	}
	
	this.setImageDiv = function(id) {
		try {
			this.imgDom = document.getElementById(id);
			if ( this.imgDom ) { return; }
		} catch(e) {}
		this.imgDom = null;
	}
	
	this.setTextDiv = function(id) {
		try {
			this.txtDom = document.getElementById(id);
			if ( this.txtDom ) { return; }
		} catch(e) {}
		this.txtDom = null;
	}
	
	this.setImageCount = function(num) { this.imgCount = num; }
	
	this.getRandomId = function(type) {
		var n = 0;
		switch(type) {
			case 'image'	: n = this.imgCount; break;
			case 'text'		: n = this.txtLst.length; break;
			default: return 1;
		}
    return (Math.floor(Math.random() * n))+1;
	}
	
	this.rotate = function() {
		if ( !this.imgDom || !this.txtDom ) { return; }
		var txtIdx = this.getRandomId('text');
		var imgIdx = this.getRandomId('image');
		var url = 'cswebbin/images/splash_'+imgIdx+'.gif';
		this.txtDom.innerHTML = this.txtLst[txtIdx-1];
		this.imgDom.style.backgroundImage = "url("+url+")";
		setTimeout("csPage.splashRotate()",this.rotateTime*1000);
	}
}

function csapiBanner(clr,dir) {
	this.color = clr;
	this.color_hex = null;
	this.img_dir = dir;
	this.imgs = "";
	
	this.init = function() {
		this.setColor();
		this.setImages();
	}
	
	this.setImages = function() {
		var img_1 = this.img_dir+"/"+this.color+"_img"+this.getRandomNum(23)+".gif";
		var img_2 = this.img_dir+"/"+this.color+"_img"+this.getRandomNum(23)+".gif";
		var img_3 = this.img_dir+"/"+this.color+"_img"+this.getRandomNum(23)+".gif";
		var img_4 = this.img_dir+"/"+this.color+"_img"+this.getRandomNum(23)+".gif";
		var main_img = this.img_dir+"/ban_"+this.color+".gif";
		var dom = document.getElementById('banner');
		if ( !dom ) { return; }
		dom.style.backgroundColor = this.color_hex;
		dom.style.backgroundImage = "url("+main_img+")";
		var dom = document.getElementById('banner_image1');
		if ( dom ) {
			dom.style.backgroundImage = "url("+img_1+")";
		}
		var dom = document.getElementById('banner_image2');
		if ( dom ) {
			dom.style.backgroundImage = "url("+img_2+")";
		}
		var dom = document.getElementById('banner_image3');
		if ( dom ) {
			dom.style.backgroundImage = "url("+img_3+")";
		}
		var dom = document.getElementById('banner_image4');
		if ( dom ) {
			dom.style.backgroundImage = "url("+img_4+")";
		}
	}
	
	this.setColor = function() {
		clr = ""+this.color;
		switch(clr.toLowerCase()) {
			case 'blue'		:
			case 'blu'		:
				this.color = 'blu';
				this.color_hex = '#628db6';
				break;
			case 'green'	:
			case 'grn'		:
				this.color = 'grn';
				this.color_hex = '#6fa96f';
				break;
			case 'purple'	:
			case 'prp'		:
				this.color = 'prp';
				this.color_hex = '#9774a4';
				break;
			case 'gold'		:
			case 'gld'		:
				this.color = 'gld';
				this.color_hex = '#bc9622';
				break;
			case 'red'		:
				this.color = 'red';
				this.color_hex = '#ae0000';
				break;
			default:
				this.color = null;
				this.color_hex = null;
				break;
		}
	}
		
	this.getRandomNum = function(max) {
		var n = max;
		var i = max;
		do {
	    i = ""+((Math.floor(Math.random() * n))+1);
		} while ( this.imgs.indexOf("|"+i+"|") >= 0 );
	  this.imgs += "|"+i+"|";
	  return parseInt(i); 
	}

	this.init();
}

function csapiMenu() {
	this.menuItems = new Array();
	this.ie = false;
	
	this.setMenuItem = function(txt,ref) {
		var _args = this.setMenuItem.arguments;
		var cls = null;
		switch(_args.length) {
			case 3:
				cls = _args[2];
				break;
			default:
		}
		this.menuItems.push(new csapiMenuItem(txt,ref,cls));
	}

	this.setMenuProps = function(div,item) {
		var m_over = "this.className='"+item.getClassName()+"_hover';";
		var m_out = "this.className='"+item.getClassName()+"';";
		var loc_ref = "location.href='"+item.getLinkRef()+"';";
		if ( this.ie ) {
			if ( !div ) { div = ""; }
			div += "<div class=\""+item.getClassName()+"\" onmouseover=\""+m_over+"\" onmouseout=\""+m_out+"\" onclick=\""+loc_ref+"\">"+item.getLinkText()+"</div>";
		} else {
			div.className = item.getClassName();
			div.setAttribute('onmouseover',m_over);
			div.setAttribute('onmouseout',m_out);
			div.setAttribute('onclick',loc_ref);
			div.innerHTML = item.getLinkText();
		}
		return div;
	}

}

function csapiMenuItem(txt,href,cls) {
	this.menuText = txt;
	this.linkRef = href;
	this.className = cls;
	this.getClassName = function() { return this.className; }
	this.getLinkRef = function() { return this.linkRef; }
	this.getLinkText = function() { return this.menuText; }
}


// set the page attributes
csPage = new csapiPage();
