var Schedule = {
	upcomingShowListLength: 3, 
	buildUpcomingList: function(shows, idPrefix){
		var firstShowIndex = 0;
		var totalShowsDisplayed = 0;
		//var today = new Date('2009/01/21');
		var today = new Date();
		today.setToMidnight();
		for(var i = 0; i < shows.length; i++){
			//if show date hasn't passed and we haven't shown 3 shows already
			if((today <= shows[i].date) && (totalShowsDisplayed < Schedule.upcomingShowListLength)){
				$(idPrefix + totalShowsDisplayed).innerHTML = shows[i].toHTML();
				totalShowsDisplayed++;
			}
		}
		//fill in the blanks
		if(totalShowsDisplayed != Schedule.upcomingShowListLength){
			for(var i = totalShowsDisplayed; i < Schedule.upcomingShowListLength; i++){
				$(idPrefix + totalShowsDisplayed).innerHTML = this.emptyShowHTML;
			}
		}
	},
	emptyShowHTML: '<div class="showNextSeason">New shows will <br/>show up here....</div>'
};

//shows
function Show(date, performers){
	this.date = new Date(date);
	this.headliners = [];
	this.hosts = [];
	this.features = [];
	this.openmicers = [];
	this.type = 'Show';
	var comic = false;
	for(var i = 1; i < arguments.length; i++){
		switch(arguments[i].type){
			case 'Comic':
				this.addComic(arguments[i]);
				break;
			case 'Tagline':
				this.tagline = arguments[i];
				break;
		}	
	}
}
Show.prototype.addComic = function(comic){
	if(!comic){return;}
	var list = false;
	switch(comic.role){
		case Role.head:
			list = this.headliners;
			break;
		case Role.host:
			list = this.hosts;
			break;
		case Role.open:
			list = this.openmicers;
			break;
		default:
			list = this.features;
	}
	list.push(comic);
	return;
};
Show.prototype.toHTML = function(){
	var out = '';
	//add date
	out += '<div class="date">'+this.date.toWeekday() +'<span class="details">'+this.date.toMDY()+'</span></div>';
	
	//add optional tagline
	if(this.tagline){
		out += '<div class="tagline">' + this.tagline.text + '</div>';
	}
	
	//add people
	out += '<div class="comedians">';

	//add headliners
	for(var i = 0; i < this.headliners.length; i++){
		out += this.headliners[i].toHTML();
	}
	
	//add host
	out += 'hosted by:';
	for(var i = 0; i < this.hosts.length; i++){
		out += this.hosts[i].toHTML();
	}
	
	//add features
	out += 'with:';
	for(var i = 0; i < this.features.length; i++){
		out += this.features[i].toHTML();
	}
	out += '</div>';
	return out;
};
function Tagline(text){
	this.text = text ? text : '';
	this.type = 'Tagline';
}

//people
function Comic(name, url, role){
	this.name = name;
	this.url = (url && url != '0') ? url : false;
	this.role = role ? role : Role.feat;
	this.type = 'Comic';
}

Comic.prototype.toHTML = function(){
	var displayName = this.url ? '<a href="' + ((this.url.indexOf('http://') == -1)? 'http://' : '') + this.url + '" title="Click to view his/her website">' + this.name + '</a>' : this.name;
	return '<div class="'+this.role+'">' + displayName + '</div>';
};

//comedy show roles
var Role = {
	head : 'headline',
	host : 'host',
	feat : 'feature',
	open : 'openmic'
};

Date.weekdays = ['Sunday','Monday', 'Tuesday','Wednesday','Thursday','Friday','Saturday'];
Date.weekdaysAbbr = ['Sun','Mon', 'Tues','Wed','Thurs','Fri','Sat'];
Date.months = ['January','February', 'March','April','May','June','July','August','September','October','November','December'];
Date.monthsAbbr = ['Jan','Feb', 'Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];


Date.prototype.toWeekday = function(abbrev){
	return (abbrev)? Date.weekdaysAbbr[this.getDay()] : Date.weekdays[this.getDay()];
};
Date.prototype.toMonth = function(abbrev){
	return (abbrev)? Date.monthsAbbr[this.getMonth()] : Date.months[this.getMonth()];
};
Date.prototype.setToMidnight = function(){
	this.setHours(0,0,0,0);
};
Date.prototype.toMDY = function(abbrev){
	return this.toMonth(abbrev) + ' ' + this.getDate() + ', ' + this.getFullYear();
};

var Browser = {
	IE: (document.all ? true : false),
	getVersion: function(){
		return parseFloat(navigator.appVersion.split('MSIE')[1]);
	},
	Safari:((navigator.userAgent.indexOf('Safari')!= -1)? true : false),
	Firefox:((!document.all&&window.XMLHttpRequest)? true : false),
	Opera:((typeof window.opera != 'undefined')? true : false),
	scrollbarWidth: function(){
		if(typeof this.scrollbarWidthStored != 'undefined'){
			return this.scrollbarWidthStored;
		}
		var wNoScroll = 0;
	    var wScroll = 0;

	    // Outer scrolling div, Start with no scrollbar
	    var a = $$.create('div');
		$$.setStyle(a, 'position', 'absolute', 'top', '-1000px', 'left', '-1000px','width', '100px', 'height', '50px', 'overflow', 'hidden');
	    
	    // Inner content div
	    var b = $$.create('div');
		$$.setStyle(b, 'width', '100%', 'height', '200px');

	    // Put the inner div in the scrolling div
	    $$.addChild(a, b);
	    // Append the scrolling div to the doc
	    $$.addChild(document.body, a);

	    // Width of the inner div sans scrollbar
	    wNoScroll = b.offsetWidth;
	    // Add the scrollbar
	    a.style.overflow = 'auto';
	    // Width of the inner div width scrollbar
	    wScroll = b.offsetWidth;

	    // Remove the scrolling div from the doc
	    document.body.removeChild(a);

	    // Pixel width of the scroller
		this.scrollbarWidthStored = wNoScroll - wScroll;
	    return this.scrollbarWidthStored;
	},
	windowSize: function(){
		//Firefox, mozilla, safari, opera, netscape
		var w = window.innerWidth;
		var h = window.innerHeight;

		if(typeof w == 'undefined'){
			try{
				w = document.documentElement.clientWidth;
				h = document.documentElement.clientHeight;
			}catch(e){
				w = document.body.clientWidth;
				h = document.body.clientHeight;
			}
		}
		return {x: w, y: h};
	},
	pngfix: function (img,w,h){
		if(Browser.IE && (Browser.getVersion() >= 5.5) && (Browser.getVersion() < 7) && (document.body.filters)){
			//only IE less than version 7
			var src = $$.getA(img, 'src');
			img.onload = null;
			if(w && h){
				$$.style(img, 'width', w + 'px', 'height', h + 'px');
			}
			var fimg = img.cloneNode(true);
			$$.setA(fimg, 'src', 'images/blank.gif');
			$$.style(fimg, 'filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='scale')");
			img.parentNode.replaceChild(fimg, img);
		}
	}
};

// DOM object reference
function $(id){return (typeof id == 'string')? document.getElementById(id) : id;}
function $tag(x, tag){return x.getElementsByTagName(tag);}

//DOM global object
var $$ = {
	create: function(t){
		var a = document.createElement(t);
		for(var i=1; i<arguments.length; i+=2){
			switch(arguments[i]){
				case 'class': $$.setClass(a,arguments[i+1]);break;
				case 'valign': a.vAlign=arguments[i+1];break;
				case 'align': a.align=arguments[i+1];break;
				case 'colspan': a.colSpan=arguments[i+1];break;
				case 'rowspan': a.rowSpan=arguments[i+1];break;
				case 'cellpadding': a.cellPadding=arguments[i+1];break;
				case 'cellspacing': a.cellSpacing=arguments[i+1];break;
				case 'valign': a.vAlign=arguments[i+1];break;
				case 'innerText': $$.text(a,arguments[i+1]);break;
				default: $$.setA(a, arguments[i], arguments[i+1]);break;
			}
		}
		return a;
	},
	show: function(){
		var s = '';
		var n = null;
		for(var i=0; i < arguments.length; i++){
			n = $(arguments[i]);
			switch(n.tagName){
				case 'DIV': case 'UL': s = 'block';break;
				case 'TABLE': s = 'table';break;
				case 'TR': s = 'table-row';break;
				case 'TD': s = 'table-cell';break;
				case 'TABLE': s = 'table';break;
				case 'TBODY': case 'THEAD': s = 'table-row-group';break;
				default:
					s = 'inline';
			}
			try{n.style.display = s;}catch(e){n.style.display = 'block';}
		}
	},
	hide: function(){
		for(var i = 0; i < arguments.length; i++){
			$(arguments[i]).style.display = 'none';
		}
	},
	chop: function(){
		var n = '';
		for(var i = 0; i < arguments.length; i++){
			n = $(arguments[i]);
			n.parentNode.removeChild(n);
		}
	},
	toggle: function(n, os, oh){
		//node, onShow, onHide
		n = $(n);
		if(n.style.display == 'none'){
			$$.show(n);
			if(os){os();}
		}else{
			$$.hide(n);
			if(oh){oh();}
		}
	},
	toggleForce: function(n, tf){
		if(tf){
			$$.show(n);
		}else{
			$$.hide(n);
		}
	},
	opacity: function(n, o){
		n = $(n);
		n.style.opacity = o;
		if(Browser.IE){
			n.style.filter = 'alpha(opacity='+(o*100)+')';
			n.style.zoom = "1";//to force hasLayout
		}else{
			n.style.MozOpacity = o;
			n.style['-moz-opacity'] = o;
		}
	},
	setClass: function(n, c){
		$$.setA(n, 'class', c);
		$$.setA(n, 'className', c);
	},
	addClass: function(n, c){
		n = $(n);
		var cs = n.className.split(' ');
		var nc = '';
		for(var i in cs){
			//check if this class is already added
			if(c == cs[i]){
				return;
			}
			nc += cs[i] + ' ';
		}
		$$.setClass(n, nc + ' ' + c);
	},
	removeClass: function(n, c){
		n = $(n);
		var cs = n.className.split(' ');
		var cn = '';
		for(var i in cs){
			if(c !== cs[i]){//check if this class is NOT being removed
				cn += cs[i] + ' ';
			}
		}
		$$.setClass(n, cn);
	},
	setStyle: function(n){
		n = $(n);
		for(var i = 1; i < arguments.length; i += 2){
			n.style[arguments[i]] = arguments[i+1];
		}
	},
	style: function(n){
		n = $(n);
		var l = arguments.length;
		for(var i=1; i < l; i += 2){
			n.style[arguments[i]] = arguments[i + 1];
		}
	},
	addChild: function(n){
		n = $(n);
		for(var i=1; i<arguments.length; i++){
			n.appendChild(arguments[i]);
		}
	},
	text: function(n, t){
		n = $(n);
		while(n.childNodes.length){
			n.removeChild(n.firstChild);
		}
		if(typeof(t) == 'string'){
			n.appendChild(document.createTextNode(t));
		}else{
			n.appendChild(t);
		}
	},
	size: function(n){
		n = $(n);
		return {x: n.offsetWidth, y: n.offsetHeight};
	},
	position: function(n){
		n = $(n);
		var dx = n.offsetLeft;
		var dy = n.offsetTop;
		if(n.offsetParent){
			while( n = n.offsetParent){
				dx += n.offsetLeft;
				dy += n.offsetTop;
			}
		}
		return{x: dx, y: dy};
	},
	//get attribute
	getA: function(n, a){
		return $(n).getAttribute(a);
	},
	//set attribute
	setA: function(n, a, v){
		$(n).setAttribute(a, v);
	},
	//get value
	getV: function(n){
		n = $(n);
		if(n.placeholder && n.value == n.placeholder){
			return '';
		}
		return n.value;
	},
	//set value
	setV: function(n, v){
		n = $(n);
		n.value = v;
		if(n.placeholder){
			Placeholder.blur(n);
		}
	}
};