function modulesInit()
{	
	//load POL modules
	updateModules();
	setModuleColumnHeights();
	createSortables();
}
	
//creates the sortable columns
function createSortables()
{
	Sortable.create("module_column_1", {
        containment: ["module_column_1", "module_column_2", "module_column_3"],
        constraint: false,
        tag: 'div',
        only: 'module',
        dropOnEmpty: true,
        handle: 'handle',
        hoverclass: 'block-hover',
        onUpdate: function(container) {
            saveModules();
            setModuleColumnHeights();
        }
    });

	Sortable.create("module_column_2", {
        containment: ["module_column_1", "module_column_2", "module_column_3"],
        constraint: false,
        tag: 'div',
        only: 'module',
        dropOnEmpty: true,
        handle: 'handle',
        hoverclass: 'block-hover',
        onUpdate: function(container) {
            saveModules();
            setModuleColumnHeights();
        }
    });
    
    Sortable.create("module_column_3", {
        containment: ["module_column_1", "module_column_2", "module_column_3"],
        constraint: false,
        tag: 'div',
        only: 'module',
        dropOnEmpty: true,
        handle: 'handle',
        hoverclass: 'block-hover',
        onUpdate: function(container) {
            saveModules();
            setModuleColumnHeights();
        }
    });
}
	
//saves the cookie with module layout and settings
function saveModules()
{	
	var module_layout1=Sortable.sequence('module_column_1');
	var module_layout2=Sortable.sequence('module_column_2');
	var module_layout3=Sortable.sequence('module_column_3');
	var module_open=getOpenModules();
		
	var url="modules/module_ajax_functions.php";
	var params="action=update_modules&module_layout1=" + module_layout1 + "&module_layout2=" + module_layout2 +  "&module_layout3=" + module_layout3 + "&module_open=" + module_open;
	var opt={asynchronous:true,method:"post",parameters:params}
	new Ajax.Request(url,opt);
}
	
//function to reset users homepage by deleting cookie
function resetHomepage()
{
	if(confirm("Are you sure you want to reset your homepage layout?"))
	{
		var url="modules/module_ajax_functions.php";
	  	var params="action=reset_modules";
		var opt={asynchronous:false,method:"post",parameters:params}
	  	new Ajax.Request(url,opt);
		
		window.location='index.php';
	}
}
	
//function to dynamically set column heights to height of the biggest
function setModuleColumnHeights()
{
	autoSetModuleColumnHeights("");
	
	var height1=document.getElementById("module_column_1").clientHeight;
	var height2=document.getElementById("module_column_2").clientHeight;
	var height3=document.getElementById("module_column_3").clientHeight;
	var max_height=height1;
		
	if(height2>max_height)
	{
		max_height=height2;
	}
		
	if(height3>max_height)
	{
		max_height=height3;
	}
	
	document.getElementById("module_column_1").style.height=max_height + "px";
	document.getElementById("module_column_2").style.height=max_height + "px";	
	document.getElementById("module_column_3").style.height=max_height + "px";
}
	
//function to set all column heights to auto so they expand dynamically to fit content
function autoSetModuleColumnHeights(module)
{	
	document.getElementById("module_column_1").style.height="auto";
	document.getElementById("module_column_2").style.height="auto";
	document.getElementById("module_column_3").style.height="auto";
}
	
//function to toggle a modules content visible/hidden
function toggleModule(module_id)
{
	autoSetModuleColumnHeights(module_id);
	Effect.toggle("content_" + module_id,"blind");
	//delay calling functions so the effect can finish rendering
	setTimeout("setModuleColumnHeights();", 1000);
	setTimeout("saveModules();", 1500);
}
	
//function to remove a module from the layout
function removeModule(module_id)
{
	document.getElementById("selected_module_" + module_id).checked=false;
	var module=document.getElementById("module_" + module_id).parentNode;
	module.removeChild(document.getElementById("module_" + module_id));
	setModuleColumnHeights();
	saveModules();
}


/* ########################### POL Modules ############################################# */

var POL_MODULES_PATH = "/pol/"; // should be /pol/

/**
 * Modules
 */
function updateModules() {
  var url = POL_MODULES_PATH + "moduleLogin.do";
  var params = "random=" + (new Date().getTime());
  
  var opt = {
    asynchronous: true,
    method: "post",
    parameters: params,
    // Handle successful response
    onSuccess: function(request) {
      if (request.responseText.match(/\s*true\s*/) != null) {
        updatePOLCalendarModule(null);
        updatePOLFutureProgrammesModule();
        updatePOLActivitiesOfTheMonthModule();
        updatePOLProgrammesOfTheMonthModule();
        setModuleColumnHeights();
      }
      else
      {
    	notLoggedIn(document.getElementById("polCalendarModule"));
    	notLoggedIn(document.getElementById("polFutureProgrammesModule"));
    	notLoggedIn(document.getElementById("polActivitiesOfTheMonthModule"));
    	notLoggedIn(document.getElementById("polProgrammesOfTheMonthModule"));
      }
    }
  }
  
  new Ajax.Request(url, opt); // make a HTTP request to the specified URL
}

/**
 * Calendar Module
 */
function updatePOLCalendarModule(showCalendarMonth) {
	var calendar=document.getElementById("polCalendarModule");

  	if(eval(calendar))
  	{	 	   
	  var url = POL_MODULES_PATH + "moduleCalendar.do";
	  var params = "showCalendarMonth=" + showCalendarMonth + "&random=" + (new Date().getTime());
	  
	  var opt = {
	    asynchronous: true,
	    method: "post",
	    parameters: params,
	    // Handle successful response
	    onSuccess: function(request) {
	      calendar.innerHTML = request.responseText;
        setModuleColumnHeights();
	    }
	  }
	  
	  new Ajax.Request(url, opt); // make a HTTP request to the specified URL
	}
}

/**
 * Future Programmes Module
 */
function updatePOLFutureProgrammesModule() {
	var future=document.getElementById("polFutureProgrammesModule");

	if(eval(future))
  	{
		  var url = POL_MODULES_PATH + "moduleFutureProgrammes.do";
		  var params = "&random=" + (new Date().getTime());
		  
		  var opt = {
		    asynchronous: true,
		    method: "post",
		    parameters: params,
		    // Handle successful response
		    onSuccess: function(request) {
		      future.innerHTML = request.responseText;
          setModuleColumnHeights();
		    }
		  }
		  
		  new Ajax.Request(url, opt); // make a HTTP request to the specified URL
	}
}

/**
 * Activities of the month Module
 */
function updatePOLActivitiesOfTheMonthModule() {
	var aotm=document.getElementById("polActivitiesOfTheMonthModule");

	if(eval(aotm))
  	{
	  var url = POL_MODULES_PATH + "moduleActivitesOfTheMonth.do";
	  var params = "&random=" + (new Date().getTime());
	  
	  var opt = {
	    asynchronous: true,
	    method: "post",
	    parameters: params,
	    // Handle successful response
	    onSuccess: function(request) {
	      aotm.innerHTML = request.responseText;
        setModuleColumnHeights();
	    }
	  }
	  
	  new Ajax.Request(url, opt); // make a HTTP request to the specified URL
	}
}

/**
 * Programmes of the month Module
 */
function updatePOLProgrammesOfTheMonthModule() {
	var potm=document.getElementById("polProgrammesOfTheMonthModule");

	if(eval(potm))
  	{
	  var url = POL_MODULES_PATH + "moduleProgrammesOfTheMonth.do";
	  var params = "&random=" + (new Date().getTime());
	  
	  var opt = {
	    asynchronous: true,
	    method: "post",
	    parameters: params,
	    // Handle successful response
	    onSuccess: function(request) {
	      potm.innerHTML = request.responseText;
        setModuleColumnHeights();
	    }
	  }
	  
	  new Ajax.Request(url, opt); // make a HTTP request to the specified URL
	}
}

/**
 * Not logged in
 */
function notLoggedIn(module)
{
	if(eval(module))
	{
		module.innerHTML="<p>You are currently not registered with Programmes Online. Please <a href='/pol' target='_blank'>click here</a> to register.</p>";
	}
}


/* ########################### Page related functions ############################## */

function activitiesOfTheMonth(section) {
  hide("activityOfTheMonth_Beaver Scouts");
	hide("activityOfTheMonth_Cub Scouts");
	hide("activityOfTheMonth_Scouts");
	hide("activityOfTheMonth_Explorer Scouts");
	hide("activityOfTheMonth_Scout Network");
	if(eval(document.getElementById("activityOfTheMonth_" + section)))
	  show("activityOfTheMonth_" + section);
  setModuleColumnHeights();
}

function programmesOfTheMonth(section) {
  hide("programmeOfTheMonth_Beaver Scouts");
	hide("programmeOfTheMonth_Cub Scouts");
	hide("programmeOfTheMonth_Scouts");
	hide("programmeOfTheMonth_Explorer Scouts");
	hide("programmeOfTheMonth_Scout Network");
	if(eval(document.getElementById("programmeOfTheMonth_" + section)))
	  show("programmeOfTheMonth_" + section);
  setModuleColumnHeights();
}

function searchRecentProgrammes(startDateId) {

	var element=document.getElementById(startDateId);

	if(eval(element))
  	{
	  var url = POL_MODULES_PATH + "ajaxRecentProgrammes.do";
	  var params = "recentProgrammesStartDate=" + element.value;
	  
	  var opt = {
	    asynchronous: true,
	    method: "post",
	    parameters: params,
	    // Handle successful response
	    onSuccess: function(request) {
	      document.getElementById("polFutureProgrammesModule").innerHTML = request.responseText;
	      setModuleColumnHeights();
	    }
	  }
	  
	  new Ajax.Request(url, opt); // make a HTTP request to the specified URL
	}

/*
  var element = window.document.getElementById(startDateId);
  if (element == null)
    return;
  var url = POL_MODULES_PATH + "ajaxRecentProgrammes.do";
  var params = "recentProgrammesStartDate=" + element.value;
  new Ajax.Request(url, {method: "get", parameters: params, onSuccess: function(request) {
	      doucment.getElementById("recentProgrammesDiv").innerHTML = request.responseText;
	      setModuleColumnHeights();
	    }});*/
}


/* ########################### Utils ############################################### */

function show(id) {
  var element = window.document.getElementById(id);
  if (element != null)
    element.style.display = "";
}

function hide(id) {
  var element = window.document.getElementById(id);
  if (element != null)
    element.style.display = "none";
}

function showHide(id) {
  var element = window.document.getElementById(id);
  if (element == null)
    return;
  if (element.style.display == "")
    element.style.display = "none";
  else
    element.style.display = "";
}


