// TTABLE
var global_hours;
var startday;
var endday;
var old_div_id;

$(document).mousemove(function(e){
	$(".balloon").css({
		top: (e.pageY + -30) + "px",
		left: (e.pageX + 15) + "px"
	});
});

(function($) {//Start closure //
	
	$.fn.ttable = function(params) {
		//Set defaul params------------------------//
		var defaults = {};
		var total_acts = $(this).find('.act').length;
		var ttable_id = $(this).attr('id');
		//Get first and last time of the day.
		$(this).find('.act').each(function(i){
			var act = $(this);
			if(i == 0)
			{
				startday = act.attr('starttime');
			}
			if(i == total_acts-1)
			{
				endday = act.attr('endtime');
			}
		});
		$(this).find('.scroller').prepend(create_time_zone());
		set_acts(ttable_id)
	};
	
	function create_time_zone()
	{
		var html = '<div class="ttable_time">'
		global_hours = 17;
		
		for(var i=9; i<26; i++)
		{
			if(i == 25)
			{
				html+= '<div class="time"><p>01:00</p></div>'
			}
			else
			{
				html+= '<div class="time"><p>'+i+':00</p></div>'	
			}
			
		}
		html+= '</div>'
		return html;
	}
	
	function set_acts(t_id)
	{
		
		var old_time = 540; //Beginen altijd om 9 uur
		$('#'+t_id).find('.act').each(function(i){
			
			var act = $(this);
			var act_stime = act.attr('starttime');
			var act_etime = act.attr('endtime');
			var act_title = act.text();
			var act_id = act.attr('id')
		
			var hour_width = 50;
			var s = act_stime.split(':')
			var e = act_etime.split(':')
			if(s[0] == 00) { s[0] = 24;	} 
			if(s[0] == 01) { s[0] = 25;	}//CHECK IF ITS MIDNIGHT
			
			if(e[0] == 00) { e[0] = 24;	}
			if(e[0] == 01) { e[0] = 25;	}
			
			var start_minutes = (s[0]*60) + Number(s[1]);
			var end_minutes = (e[0]*60) + Number(e[1]);
			var duration = end_minutes - start_minutes;
			duration = (duration<0) ? duration*-1 : duration; //Absolute waarde;
			var act_width = ((duration/60)*50)-2; // -2 is for border style
			
			
			var act_margin = start_minutes - old_time;
			var toPx = (act_margin/60)*50;
			
			$('#'+act_id).css({'width':act_width, 'margin-left':toPx +'px'})
			old_time = end_minutes;
		});
	}
	
	$('.act').live('mouseenter', function(){
		$(this).stop().animate({'opacity':1},200);
		var act_id = $(this).attr('id')
		var act_title = $(this).text();
		var act_start = $(this).attr('starttime')
		var act_end = $(this).attr('endtime')
		$('.balloon').html('<h1 style="font-size:22px; margin:0px; padding:0px;">'+act_start+' - '+act_end+' | '+ act_title+'</h1>')
		
		$('.balloon').css({"display":"block", "opacity":0})
		$('.balloon').stop().animate({'opacity':1},200)
		return false;
	})
	
	$('.act').live('mouseleave', function(){
		$(this).stop().animate({'opacity':0.5},200);
		$('.balloon').stop().animate({'opacity':0},200,function(){
			$('.balloon').css({"display":"none"})											
		})
		return false;
	})
	
	$('.act').live('click', function(){
									 
		var div = $(this).parent().parent();
		var _id = $(this).attr('id');
		_id = _id.split('act')
		
		var div_id = div.attr('id')
		div_id = div_id.split('ttable')
		
		$('#act_holder'+div_id[1]).css({'height':'auto', 'display':'block'})
		$.post('pages/get_act.php', {'id':_id[1]},function(data){
			$('#act_holder'+div_id[1]).html(data)
		});
		//WAZIGE WARNING VAN HOSTING2GO.NL
		return false;
	})
	
	$('.cross').live('click',function(){
		var div = $(this).parent();
		div.animate({'height':0},200, function(){
			div.html('')
			div.css({'display':'none'})
		})
		return false;								  
	})
	
	
	  $('.scroller').live('mousemove',function(event){
		  var offset = $(this).parent().offset();
		  var ttableX = offset.left;
		  var div_width = 575;
		  var total_width = 850;
		  var calc = total_width - div_width;
		  var x = event.pageX;
		  var x = event.pageX - this.offsetLeft-(ttableX-17);

		  var percent = x/total_width;
		  var margin = (percent*calc)*-1;
		  $('.scroller').each(function(i){
				$(this).css({'margin-left':margin})
		  })
		  return false;
	  });
	  return false;
	
	
	
	
})(jQuery);//End closure

