if (typeof(WWDC) == "undefined") WWDC = {};



WWDC.sections = {
	track: {
		'iPhone': { id:'iphone', title:'iPhone' },
		'Mac': { id:'mac', title:'Mac' },
		'IT': { id:'it', title:'IT' }
	},
	category: {
		'Essentials': { id:'essentials', title:'Essentials' },
		'Getting Started': { id:'started', title:'Getting Started' },
		'Integration': { id:'integration', title:'Integration' },
		'Media': { id:'media', title:'Media' },
		'Tools': { id:'tools', title:'Tools' }
	},
	type: {
		'Lab': { id:'lab', title:'Lab' },
		'Session': { id:'session', title:'Session' }
	}
}

WWDC.room = {
	'Presidio': { id:'presidio', title:'Presidio', type:'session' },
	'Mission': { id:'mission', title:'Mission', type:'session' },
	'Russian Hill': { id:'russianhill', title:'Russian Hill', type:'session' },
	'Marina': { id:'marina', title:'Marina', type:'session' },
	'Soma': { id:'soma', title:'Soma', type:'session' },
	'North Beach': { id:'northbeach', title:'North Beach', type:'session' },
	'Nob Hill': { id:'nobhill', title:'Nob Hill', type:'session' },
	'Pacific Heights': { id:'pacificheights', title:'Pacific Heights', type:'session' },
	'OS X Foundations Lab A': { id:'osxa', title:'OS X Foundations Lab&nbsp;A', type:'lab' },
	'OS X Foundations Lab B': { id:'osxb', title:'OS X Foundations Lab&nbsp;B', type:'lab' },
	'OS X Foundations Lab C': { id:'osxc', title:'OS X Foundations Lab&nbsp;C', type:'lab' },
	'OS X Foundations Lab D': { id:'osxd', title:'OS X Foundations Lab&nbsp;D', type:'lab' },
	'Graphics and Media Lab A': { id:'graphicsmediaa', title:'Graphics and Media Lab', type:'lab' },
	'Graphics and Media Lab B': { id:'graphicsmediab', title:'Graphics and Media Lab', type:'lab' },
	'Graphics and Media Lab C': { id:'graphicsmediac', title:'Graphics and Media Lab', type:'lab' },
	'Information Technologies Lab': { id:'itlab', title:'Information Technologies Lab', type:'lab' },
	'iPhone Lab A': { id:'iphonea', title:'iPhone Lab A', type:'lab' },
	'iPhone Lab B': { id:'iphoneb', title:'iPhone Lab B', type:'lab' },
	'Mac Lab A': { id:'maca', title:'Mac Lab A', type:'lab' },
	'Mac Lab B': { id:'macb', title:'Mac Lab B', type:'lab' },
	'Interface Design Lab A': { id:'interfacedesigna', title:'Interface Design', type:'lab' },
	'Interface Design Lab B': { id:'interfacedesignb', title:'Interface Design', type:'lab' },
	'Compatibility Lab': { id:'compatibility', title:'Compatibility Lab', type:'lab' },
	'Van Ness': { id:'vanness', title:'Van Ness', type:'lab' }
}

WWDC.GetDayFromEnglish = {
	'monday': 1,
	'tuesday': 2,
	'wednesday': 3,
	'thursday': 4,
	'friday': 5,
	'mon': 1,
	'tue': 2,
	'wed': 3,
	'thu': 4,
	'fri': 5
};

for (type in WWDC.sections.type) {
	var type = WWDC.sections.type[type];
	type.day = {
		1: { index:1, id:'monday', date:new Date('June 9, 2008'), title:'Monday', time:{} },
		2: { index:2, id:'tuesday', date:new Date('June 10, 2008'), title:'Tuesday', time:{} },
		3: { index:3, id:'wednesday', date:new Date('June 11, 2008'), title:'Wednesday', time:{} },
		4: { index:4, id:'thursday', date:new Date('June 12, 2008'), title:'Thursday', time:{} },
		5: { index:5, id:'friday', date:new Date('June 13, 2008'), title:'Friday', time:{} }
	};

	for (day in type.day) {
		type.day[day].time.morning = {
			id: 'morning',
			title: 'Morning',
			bounds: [{ lower:'9:00', upper:'13:30' }],
			increments: [
				{ title:'9:00 AM - 10:15 AM', bounds:[{ lower:'9:00', upper:'10:30' }] },
				{ title:'10:30 AM - 11:45 AM', bounds:[{ lower:'10:30', upper:'12:00' }] },
				{ title:'Noon - 1:45 PM', bounds:[{ lower:'12:00', upper:'13:30' }] }
			]
		};
		if (type.id == 'session') type.day[day].time.morning.increments[2].listing = true;
		if (type.id == 'lab') {
			type.day[day].time.morning.bounds[0].upper = '14:00';
			type.day[day].time.morning.increments[2].bounds[0].upper = '14:00';
		}

		type.day[day].time.afternoon = {
			id: 'afternoon',
			title: 'Afternoon',
			bounds: [{ lower:'14:00', upper:'20:00' }],
			increments: [
				{ title:'2:00 PM - 3:15 PM', bounds:[{ lower:'14:00', upper:'15:30' }] },
				{ title:'3:30 PM - 4:45 PM', bounds:[{ lower:'15:30', upper:'17:00' }] },
				{ title:'5:00 PM - 6:15 PM', bounds:[{ lower:'17:00', upper:'18:30' }] },
				{ title:'Evening', bounds:[{ lower:'18:30', upper:'20:00' }] }
			]
		};
		if (type.id == 'session') type.day[day].time.afternoon.increments[3].listing = true;
		

		for (time in type.day[day].time) {
			type.day[day].time[time].bounds[0].lower = new Date(type.day[day].date.toDateString()+' '+type.day[day].time[time].bounds[0].lower).getTime();
			type.day[day].time[time].bounds[0].upper = new Date(type.day[day].date.toDateString()+' '+type.day[day].time[time].bounds[0].upper).getTime();

			type.day[day].time[time].increments.each(function(increment) {
				increment.bounds[0].lower = new Date(type.day[day].date.toDateString()+' '+increment.bounds[0].lower).getTime();
				increment.bounds[0].upper = new Date(type.day[day].date.toDateString()+' '+increment.bounds[0].upper).getTime();
			});
		}
	}
}

WWDC.schedules = {};
Object.extend(WWDC.schedules, WWDC.sections.type['Lab'].day);




WWDC.RoundToHalfHour = function(time) {
	var halfhour = 1800000;
	time = Math.ceil(time/halfhour)*halfhour;
	return time;
};




WWDC.TimeToString = function(time, debug) {
	var formatTime = function(date) {
		var hours = parseInt(date.getHours(), 10);
		var minutes = date.getMinutes();
		if (minutes.toString().length == 1) minutes += '0';
		var meridiem = (hours<12) ? 'AM' : 'PM';
		if (hours>12) hours -= 12;

		return hours+':'+minutes+' '+meridiem;
	}

	var string = '';

	var oneTime = function(date, i) {
		if (Object.isString(date.lower) && date.lower.match(/-/)) date.lower = date.lower.replace(/-/g, '/');
		if (Object.isString(date.upper) && date.upper.match(/-/)) date.upper = date.upper.replace(/-/g, '/');

		date.lower = new Date(date.lower);
		date.upper = new Date(date.upper);

		var day = WWDC.schedules[date.lower.getDay()].title.substring(0, 3)+', ';
		var lower = formatTime(date.lower);
		var upper = formatTime(date.upper);

		if (!string.match(day)) {
			string += day;
			string += lower+' - '+upper+'; ';
		} else {
			string = string.replace(/ - \d*:\d* \w*; /, ' - '+upper+'; ');
		}

	}

	if (time) time.each(oneTime)
	string = string.substring(0, string.length-2);

	var meridiems = string.match(/(\wM)/g);
	for (var i=0; i<meridiems.length; i++) {
		if (i%2 == 1) {
			if (meridiems[i-1] == meridiems[i]) {
				string = string.replace(meridiems[i], '');
			}
		}
	}
	
	return string;
};




var isLessThanSafari3 = navigator.userAgent.match(/Safari/) && !navigator.userAgent.match(/Version/);

WWDC.UpdateUrl = function(data, evt) {
	if (evt) var link = evt.findElement('a');

	for (name in data) {
		var value = data[name];

		// what is the hash right now
		var hash = {};
		if (document.location.hash) var hash = document.location.hash.replace('#', '').toQueryParams();
		if (link && link.href) var hash = link.href.replace(/.*#/, '').toQueryParams();

		// add the current one to the hash object
		if (!hash[name] || name != 'filter') {
			hash[name] = value;
		} else {
			if (Object.isArray(hash[name])) {
				if (hash[name].indexOf(value)>=0) {
					hash[name] = hash[name].without(value);
				} else {
					hash[name].push(value);	
				}
			} else if (hash[name] == value) {
				hash[name] = null;
			} else {
				hash[name] = [ value, hash[name] ];
			}
		}

		// turn the object into a string
		hash = Object.toQueryString(hash);

		// set the document.location, or the link href (to prevent safari2 constant loading state)
		if (isLessThanSafari3) {
			if (link) link.href = '#'+hash;
		} else {
			document.location.hash = hash;
		}
	}
};
