 function orderThemeBlock ( ul, remove ) {
 	
 	var h = 3;
 	if ( remove == undefined) {
 		remove = false;
 	}
 	$(ul).children("li").css("top","3");
 	
 	 	
 	$(ul).children("li").each ( function ( j, li ) {
 			//$(li).css("top",3);
			var xMin = $(li).position().left;
			var yMin = $(li).position().top;
			var xMax = xMin + $(li).width();
			var yMax = yMin + $(li).height();
			
			var haveMovedAnyPersonal = false;
			
			$(li).siblings("li").each ( function (k, lisiblings) {
				var x = $(lisiblings).position().left;
				var y = $(lisiblings).position().top + 10 ;
				var _x = x + $(lisiblings).width();

				//Check if overlap
				if ( (x >= xMin && x <=xMax && y >=yMin && y <= yMax) || (_x >= xMin && _x <=xMax && y >=yMin && y <= yMax)) {
					
					
					
					if ( remove && $(li).hasClass("personal") && $(lisiblings).hasClass("personalnew") ) {
						$(li).remove();
					} else {
						
						if ( !haveMovedAnyPersonal) {
							var nY = y + 53;
							if ( $(lisiblings).hasClass("personal") ) {
								haveMovedAnyPersonal = true;
							}
						}						
						
						$(lisiblings).css("top", nY);
						
						if ( nY > h) {
							h = nY;
						}
					}
					
					
					
				}
				
			});
			
			if ( $(li).hasClass("personalnew") ) {
				$(li).removeClass("personalnew");
				$(li).addClass("personal");
			}
			$(ul).css("min-height", h+60);
		
	});
}

function chooseSession() {
	var clone = $(this).parent("li").clone();
	clone.addClass("personalnew");
	clone.insertBefore("ul#sessionCommon > br");
	clone.hide();
	clone.fadeIn();
	orderThemeBlock("ul#sessionCommon",true);
	return false;
}
 
 
 
 
 $(document).ready(function(){
	$("#loading").fadeOut('slow');
	
	var c = $('ul#time li').length;
	var wd = c * 120;
	
	$('ul#time').css ( "width", wd);
	$('ul#days').css ( "width", wd + 120);
	$('div#container').css("width",wd);
	$('ul.sessionGroup').css("width",wd);

	
	
	
	/*var ptop = $("body").css("padding-top");
	ptop = Number ( ptop.substr(0,ptop.length-2) ) + 5;
	
	if ( $("ul#sessionCommon").length ) {
		var mheight = $("ul#sessionCommon").css("min-height");
		mheight = Number ( mheight.substr(0,mheight.length-2) ) +2;
		$("body").css("padding-top", ptop+$("ul#sessionCommon").height());
		$("ul#sessionCommon").css({"margin-top":-ptop+"px","min-height":mheight+"px"});
		
	}*/
	

	
		
	var url = "?date="+jQuery.url.param("date");
	var theme = jQuery.url.param("theme");
	
	if (theme) {
		url +="&theme="+theme;
	}
		
	if ( $("ul#days li a").hasClass("active") ) $("ul#days li a").removeClass("active")
	var selLinks = $("uL#days li a[href='" + url + "']");
	
	if ( selLinks.length) {
		selLinks.addClass("active");
	} else {
		$("uL#days li a:first").addClass("active");
	}
	
	
	
	//Make fixed time scroll
	$(window).scroll(function () {
		var hscroll = $(document).scrollLeft( );
		hscroll = (hscroll ? 20-hscroll : "");
		$('ul#time').css ( "left",  hscroll);
		//$('ul#sessionCommon').css ( "left",  hscroll);
		
    });
    
    //Make filter on change theme
    
   $("#themeFilter select").change( function() {
    	var url = "http://" + jQuery.url.attr('host') + jQuery.url.attr('path');
    	var params = jQuery.url.attr('query');

		params = params ? params.split("&") : [];
    	
    	var isfirst = true;
    	
    	for ( var item in params ) {
    		if ( params[item] ) {
	    		
	    		var split = params[item].split('=');
	    		var i = split[0];
	    		var val = split[1];
	    			    		
	    		if ( i != "theme") {
	    			url += (isfirst ? "?" : "&") + i + "=" + val;
	    			isfirst = false;
	    		}
	    	}
    	}
    	
    	var themeVal = Number ( $("#themeFilter select option:selected").val() );
    	
    	if (themeVal) {
	   		url += (isfirst ? "?" : "&") + "theme=" + themeVal;
	   	}
   		
 		window.location = url;
 	});

	//$("ul.sessionGroup:not(#sessionCommon) li a ").click( chooseSession );
    $('a.dayLink').click ( function() {
    	//console.log("Teste");
    	$("#loading").fadeIn("slow");
    	return  true;
    });
    $(".highlight").width ( $(".highlight").width() - 11 );
    
    $("ul.sessionGroup").each( function ( i, ul ) {
		orderThemeBlock(ul);
	});    
    
	//Make sessions fadeIn
	$("#loading").fadeOut('slow');
	$("ul.sessionGroup").css("visibility","visible");
	$("ul.sessionGroup").hide();
	$("ul.sessionGroup").show();
	
	
	if ( $(".highlight").length ) {
		if ( $(".highlight").position().left > $(window).width() ) {
			var xpos = $(".highlight").position().left - Math.round( $(window).width() * .5 );
			$(document).scrollLeft( xpos );
		}
		
		
		var htop = $(".highlight").position().top + $(".highlight").parent().position().top;
		
		
		if ( htop > $(window).height() ) {
			var ypos = $(document).height() - $(window).height();
			$(document).scrollTop( ypos );
		}
	}

	
	
	
	
	
});
