// JavaScript Document
	var isClickHandlerActive=true;
	var isAnimationActive=false;	//variable prevents double-click
	var hasBeenMouseover = false;
	
//$(document).ready(function(){
$(window).load(function() {
	$('table.timetable td[id]').disableTextSelect();

	$('table.timetable > tbody > tr:not([class])').click(function(e) {	//was one of the table rows with the sessions clicked?
		if (!isClickHandlerActive) {return;}
		var session_cell = $(e.target);								//delegate click event (to avoid binding events to each single cell)
		if (session_cell.data('isAnimationActive')) {return;}
		open_session_box(session_cell);
		
		//e.preventDefault();
	});
	
	$('button.close_button').live('click', function() {
		var info_div = $(this).parent().parent().parent();
		var hidden_rows = info_div.parent().parent().parent().children('tr[class]');	//cache the corresponding hidden table rows
		var top_row=hidden_rows.eq(0);
		var info_row=hidden_rows.eq(1);
		var session_cell = hidden_rows.parent().children().eq(0).children('.selected');
		session_cell.toggleClass('selected');session_cell.parent().removeData('last_selected_sid');
		var track_class=session_cell.attr('class').split(' ').slice(0)[0];
		session_cell.removeClass(track_class).addClass(track_class.substring(0,track_class.length-4));
		info_div.animate({height:0},100,function() {info_row.hide();top_row.hide();session_cell.removeData('isAnimationActive');});
		top_row.children().first().animate({paddingTop:0},100);
	});
	

	$('table.timetable > tbody > tr:not([class])').mouseover(function(e) {	//was one of the table rows with the sessions clicked?
		var session_cell = $(e.target);								//delegate click event (to avoid binding events to each single cell)
		if(session_cell.is('td')) {									//was it a td and not a th (header cell)?
			var session_cell_id = session_cell.attr('id');			//extract the element id
			if (session_cell_id.substring(0,4) == 'sid_') {			//is it an element with a sid_?
				if (!session_cell.hasClass('selected')) {
					var track_class=session_cell.attr('class').split(' ').slice(0)[0];
					if (!track_class.substr(0,3)=='tra') {alert(track_class);}
					session_cell.removeClass(track_class).addClass(track_class + '_alt');
					hasBeenMouseover=true;
				}
			}
		}									  
	}).mouseout(function(e) {
		var session_cell = $(e.target);								//delegate click event (to avoid binding events to each single cell)
		if(session_cell.is('td')) {									//was it a td and not a th (header cell)?
			if (!hasBeenMouseover) {return;}
			var session_cell_id = session_cell.attr('id');			//extract the element id
			if (session_cell_id.substring(0,4) == 'sid_') {			//is it an element with a sid_?
				if (!session_cell.hasClass('selected')) {
					var track_class=session_cell.attr('class').split(' ').slice(0)[0];
					if (!track_class.substr(0,3)=='tra') {alert(track_class);}
					session_cell.removeClass(track_class).addClass(track_class.substring(0,track_class.length-4));
					//alert(session_cell.attr('class'));
				}
			}
		}	
	});
	
	var open_session_string= $.query.get('open');
	if (open_session_string) {
		var session_list=open_session_string.split(',');
		$.each(session_list,function(){open_session_box($("#" + this));});
	}
});

function open_session_box(session_cell) {
if(session_cell.is('td')) {									//was it a td and not a th (header cell)?
			var session_cell_id = session_cell.attr('id');			//extract the element id
			var session_box_row=session_cell.parent();
			if (session_cell_id.substring(0,4) == 'sid_') {			//is it an element with a sid_?
				session_cell_id=session_cell_id.substr(4);			//only retain the pure numerical session id for further purposes (database access)
				var track_class=session_cell.attr('class').split(' ').slice(0)[0];
				if (track_class.substring(track_class.length-4)!='_alt') session_cell.removeClass(track_class).addClass(track_class + '_alt');
				//alert(session_cell.attr('class'));
				hasBeenMouseover=true;
				session_cell.toggleClass('selected');				//if it is first clicked on apply "selected" class, otherwise "unselect"
				var hidden_rows = session_box_row.parent().children('tr[class]');	//cache the corresponding hidden table rows
				var top_row=hidden_rows.eq(0);
				var info_row=hidden_rows.eq(1);
				var info_div = info_row.children('td').children('div');		//cache the corresponding "curtain" div layer
				var info_inner_div = info_div.children('div');				//cache the corresponding inner "content" div layer
				if (session_cell.hasClass('selected')) {					//if the session has been click-opened, "raise the curtain" and load the content
					if (session_box_row.data('last_selected_sid')) {				//in case another session on the same row was opened before, unselect it
						var last_selected_cell = $('#'+session_box_row.data('last_selected_sid'));
						//alert(last_selected_cell.attr("class"));
						last_selected_cell.removeClass('selected');
						
						var track_class=last_selected_cell.attr('class').split(' ').slice(0)[0];
						//alert(track_class);
						//alert(last_selected_cell.attr("class"));
						last_selected_cell.removeClass(track_class).addClass(track_class.substring(0,track_class.length-4));
						//alert(last_selected_cell.attr("class"));
					}	
					top_row.children('td').css('background-color',session_cell.css('background-color'));	//apply the selected session's background color to the info cell
					info_row.show();								//display the previously hidden row
					top_row.show();
					top_row.children().first().animate({paddingTop:15},100);
					if(!session_cell.data('session_content')) {		//isn't content of this session already cached?
						if(!window.is_json_data_loaded) {
							isClickHandlerActive=false;				//prohibit other sessions of being click-opened while we're loading data
							info_inner_div.html("<center><img src='ajax-loader.gif'></center>");
							info_div.css({height:info_inner_div.outerHeight(true)});
							load_json_data(function(){
													var new_session_content=create_session_content(session_cell_id);
													session_cell.data('session_content',new_session_content);
													info_div.html(new_session_content);
													info_inner_div = info_div.children('div');
													isClickHandlerActive=true;
													session_cell.data('isAnimationActive',true);
													info_div.animate({height:info_inner_div.outerHeight(true)},100,function(){session_cell.removeData('isAnimationActive');});
													});
						} else {		//bulky json data has previously been loaded via Ajax, but session content must be created for the first time
							session_cell.data('session_content',create_session_content(session_cell_id));
						}
					} else {										//load cached session content
					}
					if (window.is_json_data_loaded) {
						info_div.html(session_cell.data('session_content'));
						info_inner_div = info_div.children('div');
						session_cell.data('isAnimationActive',true);
						info_div.animate({height:info_inner_div.outerHeight(true)},100,function(){session_cell.removeData('isAnimationActive');});	//animate the curtain openeing
					}
					session_box_row.data('last_selected_sid','sid_'+session_cell_id);		//save the last selected session in this row (time slot)
				} else {													//if element is already open and thus now being closed:
					session_cell.data('isAnimationActive',true);
					info_div.animate({height:0},100,function() {info_row.hide();top_row.hide();session_cell.removeData('isAnimationActive');});
					top_row.children().first().animate({paddingTop:0},100);
					session_box_row.removeData('last_selected_sid');
				}
			}
		}	
}

function create_session_content(_sid) {
	var ts=$.grep(json_data.sessions,function(i){return (i.sid==_sid);})[0];
	if (ts==null) {return "<div>t.b.d.</div>";}
	var new_session_content = "<div><div class='session_content'>";
	//var session_primary_title = $('#sid_'+_sid).attr('title');
	new_session_content+="<h4>"+ts.start_time+"-"+ts.end_time+" | "+ts.sroom+"</h4><h3>"+ts.strack+"</h3><h1>"+ts.stitle+"</h1><h6>Chair: "+ts.schair+", <i>"+ts.schair_aff+"</i></h6>";
	if (ts.stext.length>0) new_session_content+="<div class=\"additional_information\">"+ts.stext+"</div>";
	new_session_content+="<ol>";
	$.each($.grep(json_data.papers,function(i){return (i.sid==_sid);}),function(){
		var ssrn_addition="";
		new_session_content+="<li><h2>"+this.ptitle+"</h2>";
		if (this.no_download==0) {
			if (this.ssrn!=null) {ssrn_addition="&s="+this.ssrn+"\" target=\"_blank";}
			new_session_content+=" <a href=\"http://efa2010.unicp.net/provide_paper.php?pid="+this.pid+ssrn_addition+"\">PDF</a>";}
		new_session_content+="<ul><li>"+this.authors+", <i>"+this.author_aff+"</i></li><li>Discussant: "+this.discussant+", <i>"+this.discussant_aff+"</i></li></ul></li>";
	});
	new_session_content+="</ol></div><div class='button_container'><button class='close_button'><img src='button_up.gif'></button></div><br style='clear:both;'></div>";
	return new_session_content;
}

function load_json_data(do_on_success) {
	$.ajax({
	type: 'GET',
	url: 'http://efa2010.unicp.net/session_content.php',
	dataType: 'json',
	cache:false,
	timeout: 20000,
	success: function(data) {
		window.json_data=data;
		window.is_json_data_loaded=true;
		do_on_success();
	}
	});
}

  /**
	*  http://chris-barr.com/entry/disable_text_selection_with_jquery/
	*  modified to be “$” safe by Dakkar Daemor [www.imaginific.com]
	*/
	(function($) {
	  $.fn.disableTextSelect = function() {
		  return this.each(function(){
			if($.browser.mozilla){//Firefox
			  $(this).css('MozUserSelect','none');
			}else if($.browser.msie){//IE
			  $(this).bind('selectstart',function(){return false;});
			}else{//Opera, etc.
			  $(this).mousedown(function(){return false;});
			}
		  });
	  }
	})(jQuery); 
