function Timeline(year,month,day){
	this.year = year;
	this.month = month;
	this.day = day;
	this.category = 0;
	//this.setCategory = setCategory;
	this.view = 'timeline';
	//this.setView = setView;
	this.tableCnt = document.getElementById("timeline");
	this.headerCnt = document.getElementById("timeline_day");
	this.sortby = 'EventStartTS';
	//this.setSortBy = setSortBy;
	this.sortorder = 'ASC';
	//this.setSortOrder = setSortOrder;
	//this.load_timeline = load_timeline;
	this.create_ajax = create_ajax;
}
function create_ajax(){
	with(this)
	var xmlHttp;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e)
		{
		try
		  {
		  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		catch (e)
		  {
		  alert("Your browser does not support AJAX!");
		  return false;
		  }
		}
	  }
		return xmlHttp;
}
function getMonthNameT(month){
	switch(month){
	case 0:
		return 'January';
	break;
	case 1:
		return 'February';
	break;
	case 2:
		return 'March';
	break;
	case 3:
		return 'April';
	break;
	case 4:
		return 'May';
	break;
	case 5:
		return 'June';
	break;
	case 6:
		return 'July';
	break;
	case 7:
		return 'August';
	break;
	case 8:
		return 'September';
	break;
	case 9:
		return 'October';
	break;
	case 10:
		return 'November';
	break;
	case 11:
		return 'December';
	break;
	
	}
}
function getDayNameT(dow){
	switch(dow){
		case 0:
			return 'Sunday';
		break;
		case 1:
			return 'Monday';
		break;
		case 2:
			return 'Tuesday';
		break;
		case 3:
			return 'Wednesday';
		break;
		case 4:
			return 'Thursday';
		break;
		case 5:
			return 'Friday';
		break;
		case 6:
			return 'Saturday';
		break;
	}
}
Timeline.prototype.load_timeline = function(){
	var xmlHttp = this.create_ajax();
	xmlHttp.onreadystatechange=function()
    {
		if(xmlHttp.readyState==1){
			//display loading animation
			if(!document.getElementById("timeline_tbody")) {
				var temp_tbody = document.createElement("TBODY");
				temp_tbody.id = "timeline_tbody";
				var temp_timeline = document.getElementById("timeline");
				temp_timeline.appendChild(temp_tbody);
			}
			var this_tbody = document.getElementById("timeline_tbody");
				//remove all current rows from tbody
						while(this_tbody.hasChildNodes()){
							this_tbody.removeChild(this_tbody.firstChild);
						}
				//remove all tbodies that apply to categories
						jQuery(".cat").remove();
				//post error message as first line in table
				//var this_table = document.getElementById("timeline");
				var this_tbody = document.getElementById("timeline_tbody");
				var this_tr = document.createElement("TR");
				var this_td = document.createElement("TD");
					this_td.className = 'td_error';
					this_td.innerHTML = 'Loading...';
					this_td.setAttribute('colspan','4');
				this_tr.appendChild(this_td);
				this_tbody.appendChild(this_tr);
				//this_table.appendChild(this_tbody);
			
		}
		if(xmlHttp.readyState==4)
		{
			$('#timeline_tbody').show();
			var response = xmlHttp.responseText;
			//alert(response);
			var return_array = new Array();
			return_array = response.split('~');
			if(return_array[0] == '1'){
				
				var this_tbody = document.getElementById("timeline_tbody");
				//remove all current rows from tbody
						while(this_tbody.hasChildNodes()){
							this_tbody.removeChild(this_tbody.firstChild);
						}
				//remove all tbodies that apply to categories
						jQuery(".cat").remove();
				//good return - process data
				var a_data = new Array();
				//alert(return_array[1]);
				a_data = return_array[1].split('|');
				for(x=0;x<a_data.length;x++){
					//loop through activities in list
					var this_split = a_data[x];
					//alert(this_split);
					var this_activity = this_split.split('^^');
					/* title/location/instructor/instructor_id/starttime/endtime/desc/price/contact/cat_name/cat_id/upc1/upc2/upc3/upc4 */
					var this_title = this_activity[0];
					var this_location = this_activity[1];
					var this_instructor = this_activity[2];
					var this_instructor_id = this_activity[3];
					var this_starttime = this_activity[4];
					var this_endtime = this_activity[5];
					var this_desc = this_activity[6];
					var this_price = this_activity[7];
					var this_contact = this_activity[8];
					var this_cat_name = this_activity[9];
					var this_cat_id = this_activity[10];
					var this_flag_time = this_activity[11];
					var this_flag_location = this_activity[12];
					var this_flag_instructor = this_activity[13];
					var this_flag_cancelled = this_activity[14];
					var this_signup = this_activity[15];
					var this_staff_url = this_activity[18];
					var this_upc = new Array();
					this_upc = this_activity[16].split('##');
					var this_activity_id = this_activity[19];
					
					if((typeof(this_activity[17]) != 'undefined') && (this_activity[17] != 'undefined') && (this_title != 'TBD')){
						var this_category = this_activity[17];
						//check to see if tbody already exists
						var tbody_name = 'cat_' + this_category.replace(' ','_');
						if(document.getElementById(tbody_name) != null){
							var this_tbody = document.getElementById(tbody_name);
							//var num_e_name = 'num_events_' + this_category;
							//var num_events = parseInt(document.getElementById(num_e_name).innerHTML);
							//num_events++;
							//document.getElementById(num_e_name).innerHTML = num_events;
						}else{
							//create tbody and append it to table
							var this_tbody = document.createElement("TBODY");
								this_tbody.id = tbody_name;
								this_tbody.className = 'cat';
								var cat_tr = document.createElement("TR");
								this_tbody.appendChild(cat_tr);
									var cat_th = document.createElement("TH");
										//cat_th.id = 'cat_th_' + this_category;
										//cat_th.setAttribute('colspan','4');
										cat_th.setAttribute('scope','col');
										var cat_th_html = this_category + ' <em><span id="num_events_' + this_category + '"></span> <!--1 Event(s)--></em>';
										cat_th.innerHTML = cat_th_html;
									cat_tr.appendChild(cat_th);
							
							var cat_table = document.getElementById("timeline");
								cat_table.appendChild(this_tbody);
								jQuery('.cat th').attr('colspan','4');
						}
					}else{
						//var this_table = document.getElementById("timeline");
						//var this_tbody = document.createElement("TBODY");
						var this_tbody = document.getElementById("timeline_tbody");
					}

					//check for failed import
					if(this_title != 'N/A'){
					
					//fill header row
					var this_header_row = document.createElement("TR");
						if(this_flag_cancelled == '1'){
							this_header_row.className = 'cancel';
						}
						var activity_td = document.createElement("TD");
							activity_td.className = 'activity';
							if(this_flag_cancelled == '1'){
								activity_td.innerHTML = '<em class="cancel">Canceled</em> ' + this_title;
							}else if(this_flag_cancelled == '0' && (this_flag_time == '1' || this_flag_location == '1' || this_flag_instructor == '1')){
								activity_td.innerHTML = '<em class="update">Updated</em> ' + this_title;
							} else {
								activity_td.innerHTML = this_title;
							}
						//var activity_td_anchor = document.createElement("A");
						//activity_td_anchor.href = '#';
						//activity_td_anchor.innerHTML = this_title;
						//activity_td.appendChild(activity_td_anchor);
						this_header_row.appendChild(activity_td);
					
						var location_td = document.createElement("TD");
						if(this_flag_location == 1) {
							location_td.className = 'highlight';
						}
							location_td.innerHTML = this_location;
						this_header_row.appendChild(location_td);
						
						var trainer_td = document.createElement("TD");
							trainer_td.className = 'trainer';
								if(this_staff_url != "") {
									var trainer_td_anchor = document.createElement("A");
									if(this_flag_instructor == 1) {
										trainer_td_anchor.className = 'highlight';
									}
									trainer_td_anchor.href = '/index.php/fitness-sports/staff/' + this_staff_url + '/';
									trainer_td_anchor.innerHTML = this_instructor;
									trainer_td.appendChild(trainer_td_anchor);
								} else {
									var trainer_td_anchor =	document.createElement("SPAN");
									trainer_td_anchor.innerHTML = " ";//this_instructor;
									trainer_td.appendChild(trainer_td_anchor);
								}
						this_header_row.appendChild(trainer_td);
						
						var time_td = document.createElement("TD");
						if(this_flag_time == 1) {
							time_td.className = 'time_highlight';
						} else {
							time_td.className = 'time';
						}
							time_td.innerHTML = this_starttime + ' - ' + this_endtime;
						this_header_row.appendChild(time_td);
						
						//append this_header_row to tbody
						this_tbody.appendChild(this_header_row);
						
					//fill details row
					var this_details_row = document.createElement("TR");
						this_details_row.className = 'moreInfo';
							var this_details_td = document.createElement("TD");
								//this_details_td.setAttribute('colspan','4');
									var this_details_ul = document.createElement("UL");
										this_details_ul.className = 'sum';
											var price_li = document.createElement("LI");
												price_li.className = 'price';
												price_li.innerHTML = 'Price ' + this_price;
										this_details_ul.appendChild(price_li);
										if(this_contact != "--") {
											var contact_li = document.createElement("LI");
												contact_li.className = 'contact';
												contact_li.innerHTML = '<h4>Contact:</h4>' + this_contact;
											this_details_ul.appendChild(contact_li);
										}
											var category_li = document.createElement("LI");
												category_li.className = 'category';
												category_li.innerHTML = '<h4>Category:</h4>' + this_cat_name;
										this_details_ul.appendChild(category_li);
											var notify_li = document.createElement("LI");
												notify_li.className = 'notify';
												notify_li.innerHTML = '<a href="/ebc/calendar-reminder/' + this_activity_id + '">Sign up to receive updates for this class</a>';
										this_details_ul.appendChild(notify_li);
								this_details_td.appendChild(this_details_ul);
							
									var this_details_div = document.createElement("DIV");
										this_details_div.className = 'desc';
											var this_desc_header = document.createElement("H4");
												this_desc_header.innerHTML = 'Description:';
										this_details_div.appendChild(this_desc_header);
											var this_desc_desc = document.createElement("P");
												this_desc_desc.innerHTML = this_desc;
										this_details_div.appendChild(this_desc_desc);
										if(this_upc[1] != "") {
											var this_desc_header2 = document.createElement("H4");
												this_desc_header2.innerHTML = 'Class Schedule:';
											this_details_div.appendChild(this_desc_header2);
											var this_desc_upcs = document.createElement("TABLE");
											var this_desc_upcs_tbody = document.createElement("TBODY");
											this_desc_upcs.appendChild(this_desc_upcs_tbody);
											this_desc_upcs.className = 'classSched';
												for(z=0;z<this_upc.length;z++){
													var this_desc_upcs_tr = document.createElement("TR");
													this_desc_upcs_tbody.appendChild(this_desc_upcs_tr);
													if(z%2==0) {
														this_desc_upcs_tr.className = "alt";
													}
													
													jQuery(this_desc_upcs_tr).html(this_upc[z]);
												}
											this_details_div.appendChild(this_desc_upcs);
											this_desc_upcs.setAttribute("cellspacing","0");
										}
										/*if(this_signup != 'N/A') {
											var this_signup_link = document.createElement("DIV");
												this_signup_link.className = 'signUp';
												this_signup_link.innerHTML = '<a href="' + this_signup + '">Register</a>';
											this_details_div.appendChild(this_signup_link);
										}*/
								this_details_td.appendChild(this_details_div);
						this_details_row.appendChild(this_details_td);
						
						//append this_details_row to tbody
						this_tbody.appendChild(this_details_row);
						jQuery('.moreInfo > td').attr('colspan','4');
						//append tbody to table
						//this_table.appendChild(this_tbody);
					}
				}
			}else{
				//bad return - alert error
				//alert('Data processing error, please contact support');
				
				var error_msg = return_array[1];
				
				var this_tbody = document.getElementById("timeline_tbody");
				//remove all current rows from tbody
						while(this_tbody.hasChildNodes()){
							this_tbody.removeChild(this_tbody.firstChild);
						}
				//remove all tbodies that apply to categories
						jQuery(".cat").remove();
				//post error message as first line in table
				//var this_table = document.getElementById("timeline");
				var this_tbody = document.getElementById("timeline_tbody");
				var this_tr = document.createElement("TR");
				var this_td = document.createElement("TD");
					this_td.className = 'td_error';
					this_td.innerHTML = error_msg;
					this_td.setAttribute('colspan','4');
				this_tr.appendChild(this_td);

				this_tbody.appendChild(this_tr);
				//this_table.appendChild(this_tbody);
				jQuery("#timeline_tbody > td").attr('colspan','4');
			}

			calTool();
			if(this_activity && (this_activity[17] != 'undefined') && (typeof(this_activity[17]) != 'undefined') && (this_title != 'TBD')){
				category_row();
			}
			
			jQuery('li.notify a').click(function() {
				if(jQuery('div.profileWin').length == 0) {
					var toLoad = jQuery(this).attr('href') + ' div.emailWindow';
					// append load icon
					jQuery('#ajaxStaffLoad').stop().after('div id="loadIcon">loading</div>').stop().load(toLoad, function() {
						jQuery('#loadIcon').fadeOut('fast', function() { $(this).remove(); });
						
						// number of pixels browser viewport from top
						var staffTop = $(window).scrollTop();

						jQuery('div.emailWindow').css('top',  (staffTop+140) + 'px').hide().fadeIn('slow').append('<div class="closeProfile"><a href="#">Close Window</a></div>');

						jQuery('div.closeProfile').click(function() {
							jQuery('div.emailWindow').fadeOut('fast').remove();
							return false;
						})
					});
				}
				return false;
			});
			
			// add lightbox effect to trainer links
			jQuery('td.trainer a').click(function() {  
				if (jQuery('div.profileWin').length == 0) {
					var toLoad = jQuery(this).attr('href')+ ' #viewStaff'; 
					// append load icon
					jQuery('#ajaxStaffLoad').stop().after('<div id="loadIcon">loading</div>').stop().load(toLoad, function() {
						jQuery('#loadIcon').fadeOut('fast', function() { $(this).remove(); });
						
						// number of pixels browser viewport from top
						var staffTop = $(window).scrollTop();

						jQuery('#viewStaff').css('top',  (staffTop+140) + 'px').hide().fadeIn('slow').append('<div class="closeProfile"><a href="#">Close Window</a></div>');

						jQuery('div.closeProfile').click(function() {
							closeContent();
							return false;
						})
					});
				}
				return false;  
			});

/*
			jQuery('td.trainer a').click(function() {
				if (jQuery('div.profileWindow').length == 0) {
					var toLoad = jQuery(this).attr('href')+ ' #viewStaff'; 
					// append load icon
					jQuery('#ajaxStaffLoad').stop().after('<div id="loadIcon">loading</div>').stop().load(toLoad, function hideLoader() {
						jQuery('#loadIcon').fadeOut('fast').remove();
						jQuery('#viewStaff').append('<div class="closeProfile"><a href="#">Close Window</a></div>');
						jQuery('div.closeProfile').click(function() {
							closeContent();
							return false;
						})
					});
				}

				return false;  
			});
*/	
		    function closeContent() {
				jQuery('#viewStaff').fadeOut('fast').remove();
			}
		}
	}

  var da = new Date();
  var timestamp = da.getTime();
  
  //set correct date
			var date_set = new Date(this.year,(this.month-1),this.day);
			var date_string = getDayNameT(date_set.getDay()) + ', ' + getMonthNameT(date_set.getMonth()) + ' ' + date_set.getDate();
			var tl_object = document.getElementById("timeline_day");
			tl_object.innerHTML = date_string;
  
  var posturl = '/ajax/get_timeline.php?category=' + this.category;
  posturl += '&view=' + this.view;
  posturl += '&sortby=' + this.sortby;
  posturl += '&sortorder=' + this.sortorder;
  posturl += '&year=' + this.year;
  posturl += '&month=' + this.month;
  posturl += '&day=' + this.day;
  posturl += '&ts=' + timestamp;
  xmlHttp.open("POST",posturl,true);
  xmlHttp.send(null);
}
Timeline.prototype.setView = function(view){
	this.view = view;
	if(view == 'category'){
		this.sortby = 'EventStartTS';
		this.setCategory(0);

		jQuery('li#catView > a').text('all categories');
		jQuery('th a').hide();
	}else{
		this.load_timeline();
		jQuery('th a').show();
	}
}
Timeline.prototype.setCategory = function(category){
	this.category = parseInt(category, 10);
	if(category != 0){
		this.setView('timeline');
		jQuery('#timeView').css('backgroundPosition','15px 100%');
		jQuery('#timeView > a').text('timeline view');
	}else{
		this.load_timeline();
	}
}
Timeline.prototype.setSortBy = function(sortby){
	this.sortby = sortby;
	this.load_timeline();
}
Timeline.prototype.setSortOrder = function(sortorder){
	this.sortorder = sortorder;
	this.load_timeline();
}

