/***** fyc.on.ca/lasers/GrandBendOpen/ - Developed by ScriptReaction - www.scriptreaction.com *****/

/********** shell items **********/
var shell = {
	init: function (){
		<!-- browser specific -->
		if(window.XMLHttpRequest){
			if(window.ActiveXObject){ // IE 7
				shell.browser = "ie";
				shell.browserId = "ie7";
				document.write('<link href="assets/css/global_fixIE7.css" rel="stylesheet" type="text/css" />');
				return;
			}else{ // Opera, Safari, Firefox
				shell.browser = "gecko";
				shell.browserId = "gecko";
				return;
			}
		}else{ //IE 6 and below
			shell.browser = "ie";
			shell.browserVersion = 6;
			shell.browserId = "ie6";
			document.write('<link href="assets/css/global_fixIE6.css" rel="stylesheet" type="text/css" />');
		}
	},
	news: {
		divId: "newsItems", //target div
		delay: 3000, //display time for each item
		speed: 50, //fade step speed
		count: 0, //active item number
		total: 0, //total news items
		launch: function (){
			this.update();
		},
		update: function (){
			this.total = active.news.items.length;
			
			if(typeof(this.timer_in) != "undefined") clearTimeout(this.timer_in);
			if(typeof(this.timer_out) != "undefined") clearTimeout(this.timer_out);
			if(typeof(this.timer) != "undefined") clearTimeout(this.timer);
			
			this.count++;
			this.count = (this.count > this.total) ? 1 : this.count;
			
			var display = active.news.items[this.count-1];
			var output = this.get(display[0], display[1], display[2]);
			
			if(!this.firstRun){ //remove welcome item
				active.news.items.splice(0,1);
				this.count--;
				this.firstRun = true;
			}
			
			global.setDivContent(this.divId, output);
			var dsp = document.getElementById(this.divId);
			global.setAlpha(dsp, 0);
			
			this.fadeIn();
		},
		fadeIn: function (){
			var dsp = document.getElementById(this.divId);
			var alpha = Math.round( dsp.style.opacity * 100 );
			if(alpha < 100){
				global.setAlpha(dsp, alpha += 10);
				this.timer_in = setTimeout("shell.news.fadeIn()", this.speed);
			}else{
				global.setAlpha(dsp, 100);
				if(this.total > 1){ //do not fade if only 1 item
					this.timer = setTimeout("shell.news.fadeOut()", this.delay);
				}
			}
		},
		fadeOut: function (){
			var dsp = document.getElementById(this.divId);
			var alpha = Math.round( dsp.style.opacity * 100 );
			if(alpha > 0){
				global.setAlpha(dsp, alpha -= 10);
				this.timer_out = setTimeout("shell.news.fadeOut()", this.speed);
			}else{
				global.setAlpha(dsp, 0);
				this.update();
			}
		},
		get: function (display, href, target){
			if(href){
				if(!target) target = "_top";
				return '<a href="' + href + '" target="' + target + '" onmouseover="shell.news.cancel()" onmouseout="shell.news.reopen()">' + display + '</a>';
			}else{
				return display;
			}
		},
		cancel: function (){
			if(typeof(this.timer) != "undefined") clearTimeout(this.timer);
		},
		reopen: function (){
			if(typeof(this.timer) != "undefined") clearTimeout(this.timer);
			if(this.total > 1){ //do not fade if only 1 item
				this.timer = setTimeout("shell.news.fadeOut()", this.delay - (this.delay/2));
			}
		}
	},
	count: {
		//2008: "June 28, 2008"
		//2007: "August 11, 2007"
		date: "June 28, 2008", //set event date here
		launch: function (){
			var eventDate = this.date;
			this.date = new Date(eventDate + " 05:00:00 GMT");
			//this.update();
			this.init();
		},
		//divId, numberSize
		countDivs:Array(
			["sec", 2],
			["min", 2],
			["hrs", 2],
			["day", 3]
		),
		init: function (){
			//populate each countDiv with numbers
			for(var i=0; i<this.countDivs.length; i++){
				var divId = this.countDivs[i][0];
				var numberSize = this.countDivs[i][1];
				this.populate(divId, numberSize);
			}
			this.update();
		},
		populate: function (divId, numberSize){
			var output = "";
			//populate countDiv with 0-9 for each digit position
			for(var digitPos=1; digitPos<=numberSize; digitPos++){
				for(var digitCount=0; digitCount<=9; digitCount++){
					output += '<img id="digit_' + divId + '_' + digitPos + "_" + digitCount + '" src="assets/gfx/count/' + digitCount + '.gif" style="display:none" />';
				}
			}
			global.setDivContent(divId, output);
		},
		update: function (){
			var date = new Date();
			var time = Math.floor( ( this.date.getTime() - date.getTime() ) /1000);
			if(time <= 0){ //is event
				//set all countDivs to zero
				for(var i=0; i<this.countDivs.length; i++){
					var divId = this.countDivs[i][0];
					this.set(divId, 0);
				}
				return;
			}else{
				this.set("sec", time%60);
				time = Math.floor(time/60);
				this.set("min", time%60);
				time = Math.floor(time/60);
				this.set("hrs", time%24);
				time = Math.floor(time/24);
				this.set("day", time);
			
				setTimeout("shell.count.update()", 250);
			}
		},
		set: function (divId, count){
			
			//retrieve numberSize based on requested divId
			var numberSize = 0;
			for(var i=0; i<this.countDivs.length; i++){
				var id = this.countDivs[i][0];
				if(divId == id){
					numberSize = this.countDivs[i][1];
					break;
				}
			}
			
			var count = count.toString();
			
			//format count to proper size
			while(count.length < numberSize) count = "0" + count;
			
			//get individual digit values
			var count1 = count.slice(0,1);
			var count2 = count.slice(1,2);
			var count3 = count.slice(2,3);
			
			//reset all numbers in requested countDiv
			for(var digitPos=1; digitPos<=numberSize; digitPos++){
				for(var digitCount=0; digitCount<=9; digitCount++){
					var digit = document.getElementById("digit_" + divId + "_" + digitPos + "_" + digitCount);
					digit.style.display = "none";
				}
			}
			
			//display new count digits
			for(var digitPos=1; digitPos<=numberSize; digitPos++){
				var digit = document.getElementById("digit_" + divId + "_" + digitPos + "_" + eval("count" + digitPos));
				digit.style.display = "block";
			}
			
		}
	},
	tabs: {
		//********** UPDATE TOTAL when switching between SCHEDULE and RESULTS
		total: 1,
		start: 2,
		launch: function (){
			var tabHolder = document.getElementById("eventTabs");
			var tabItems = tabHolder.getElementsByTagName("a");
			
			for(var i=0; i<this.total; i++){
				var whichItem = tabItems[i];
				
				whichItem.onclick = function (){
					this.blur();
					return false;
				}
			}
			
			if(this.start > this.total) this.start = this.total;
			this.ck(this.start);
		},
		menuOn: 0,
		ck: function (x){ //tab click
			if(x != this.menuOn){
				this.menuOn = x;
				this.menuActive = "tab" + x;
				this.rs();
				var thetab = document.getElementById(this.menuActive);
				with(thetab){
					with(style){
						backgroundImage 	= "url(assets/gfx/tab_grad.gif)";
						color				= "#ffff00";
						height				= "22px";
						borderBottomWidth	= "0px";
						cursor 				= "default";
					}
				blur();
				}
				var dsp = document.getElementById("tabDsp" + x);
				with(dsp.style){
					display = "block";
				}
			}
		},
		rs: function (){ //tab reset
			for(var i=1; i<=this.total; i++){
				var thetab = document.getElementById("tab" + i);
				with(thetab.style){
					backgroundImage 	= "";
					color				= "";
					height				= "";
					borderBottomWidth	= "";
					cursor 				= "";
				}
				var dsp = document.getElementById("tabDsp" + i);
				with(dsp.style){
					display = "none";
				}
			}
		}
	},
	swap: {
		results: function (toYear){
			top.location.href = "results/" + toYear;
		},
		photos: function (toYear){
			top.location.href = "photos/" + toYear;
		}
	},
	blank: function (){
		return;	
	},
	winClose: function (){
		top.location = 'javascript:shell.blank()';
		top.window.close();
	}
}
function launch_count (){
	shell.count.update();
}

/********** shell init **********/
shell.init();
