/*******************************************************************************
# JS CLASS FOR HANDLING BACK-END FILTERING OF PREVIA ACTIVTIES
# 2006-12-21
*******************************************************************************/

function activities() {
	
	this.subjectParams = "";
	this.locationParam = "Hela Sverige";
	this.isLoading = false;
	
	this.init = function() {
		this.subject_list = $('subjectList').getElementsByTagName("input");
		this.ajaxloader = $('ajaxloader');
		this.norrland1 = $('norrlandLocationContainer');
		this.svealand1 = $('svealandLocationContainer');
		this.gotaland1 = $('gotalandLocationContainer');
		this.locationText = $('locationText');
		this.TriggerLoad();
	}
	
	this.TriggerLoad = function() {
		this.DisplayLoader(true);
		this.LoadSubjectParams();
		//alert(this.locationParam);
		//alert(this.params_subject);
		ActivitiesStartPage.GetActivities(this.subjectParams, this.locationParam, Activities.GetActivities_callback);
	}
	
	this.LoadSubjectParams = function() {
		this.subjectParams = "";
		for(var i = 0;i < this.subject_list.length;i++) {
			if(this.subject_list[i].checked)
				this.subjectParams += ":" + this.subject_list[i].value;
		}
			
	}
	
	this.DisplayLoader = function(display) {
		if(display)
			this.ajaxloader.style.display = "block";
		else
			this.ajaxloader.style.display = "none";
	}

	this.SetLocation = function(location) {
		this.locationParam = location;
		this.locationText.innerHTML = location;
		this.TriggerLoad();
	}

	this.GetActivities_callback = function(res) {
		$('activitiesList').innerHTML = res.value;
		//alert(res.value);
		Activities.DisplayLoader(false);
	}
	
	this.GetRelevantLocations_callback = function(res) {
		this.isLoading = false;
		//alert(res.value);
		bla = res.value.split("|");

		/*************************************
		Fill norrland with citys 
		***/
		
		$('norrlandLocationContainer').innerHTML = bla[0];
		
		// If only one location add rowbreak to prevent box arrow of sliding out
		if(bla[0].split("</a>").length == 2)
			$('norrlandLocationContainer').innerHTML += "<br />";
		
		// If empty, set no location text
		if(bla[0] == "")
			$('norrlandLocationContainer').innerHTML = noregionsavailableMsg;
		
		/*************************************
		Fill svealand with citys 
		***/
		
		$('svealandLocationContainer').innerHTML = bla[1];
		
		// If only one location add rowbreak to prevent box arrow of sliding out
		if(bla[1].split("</a>").length == 2)
			$('svealandLocationContainer').innerHTML += "<br />";
		
		// If empty, set no location text
		if(bla[1] == "")
			$('svealandLocationContainer').innerHTML = noregionsavailableMsg;
		
		/*************************************
		Fill götaland with citys 
		***/
		
		$('gotalandLocationContainer').innerHTML = bla[2];
		
		// If only one location add rowbreak to prevent box arrow of sliding out
		if(bla[2].split("</a>").length == 2)
			$('gotalandLocationContainer').innerHTML += "<br />";
		
		// If empty, set no location text
		if(bla[2] == "")
			$('gotalandLocationContainer').innerHTML = noregionsavailableMsg;
		
		Activities.DisplayLoader(false);
	}
	
	this.hovering = false;
	this.active_hover_box = null;
	this.DisplayHoverBox = function(id, x, y) {
		this.hovering = true;
		$('chbox_1216').focus();
		$(id).style.left = x + "px";
		$(id).style.top = y + "px";

		if(this.active_hover_box) {
			this.active_hover_box.style.display = 'none';
		}

		$(id).style.display = 'block';
		this.active_hover_box = $(id);
		
		this.isLoading = true;
		this.DisplayLoader(true);
		this.LoadSubjectParams();
		ActivitiesStartPage.GetRelevantLocations(this.subjectParams, id, Activities.GetRelevantLocations_callback);
		//this.TriggerLoad();

	}

	this.HideHoverBox = function() {
		if(this.active_hover_box) {
			this.hovering = false;
			setTimeout("Activities.DelayedHideHoverBox()", 300);
		}
	}


	this.DelayedHideHoverBox = function() {
		if(!this.hovering)
			this.active_hover_box.style.display = 'none';
	}	
	
	this.active_type_link = null;
	this.Sort = function(sortby, linkObj) {
		
		var type_utb = " ";
		if($('typeUtb')) type_utb = $('typeUtb').innerHTML;
		var type_sem = " ";
		if($('typeSem')) type_sem = $('typeSem').innerHTML;
		var type_opn = " ";
		if($('typeOpn')) type_opn = $('typeOpn').innerHTML;
		
		if(this.active_type_link != null)
			this.active_type_link.style.fontWeight = "normal";
		
		$(linkObj).style.fontWeight = "bold";
		this.active_type_link = $(linkObj);
		
		
		if(sortby == "Utbildningar") {
			if($('typeUtb')) {
				$('activitiesList').innerHTML = "";
				
				$('activitiesList').innerHTML += "<div id=\"typeUtb\">" + type_utb + "</div>";
				$('activitiesList').innerHTML += "<div id=\"typeSem\">" + type_sem + "</div>";
				$('activitiesList').innerHTML += "<div id=\"typeOpn\">" + type_opn + "</div>";
			}
		} else if(sortby == "Seminarier") {
			if($('typeSem')) {
				$('activitiesList').innerHTML = "";
				
				$('activitiesList').innerHTML += "<div id=\"typeSem\">" + type_sem + "</div>";
				$('activitiesList').innerHTML += "<div id=\"typeUtb\">" + type_utb + "</div>";
				$('activitiesList').innerHTML += "<div id=\"typeOpn\">" + type_opn + "</div>";
			}
		
		} else if(sortby == "Oppet hus") {
			if($('typeOpn')) {
				$('activitiesList').innerHTML = "";
				
				$('activitiesList').innerHTML += "<div id=\"typeOpn\">" + type_opn + "</div>";
				$('activitiesList').innerHTML += "<div id=\"typeSem\">" + type_sem + "</div>";
				$('activitiesList').innerHTML += "<div id=\"typeUtb\">" + type_utb + "</div>";
			}
		}
	}
	
	
}


function GetActivities() {
	ActivitiesStartPage.GetActivities(GetActivities_callback);
}

var Activities =  new activities();
addEvent(window, "load", function(){Activities.init();})