/**
	name			ShowHideDivs
	type			function
	params			show: name of element which should be shown
					hide: name of element which should be hidden
	description		shows an element and hides another element by changing CSS display property.
**/
function ShowHideDivs(show,hide){
	var obj_divnaam = document.getElementById(hide);
	obj_divnaam.style.display='none';
	
	var obj_divnaam = document.getElementById(show);
	obj_divnaam.style.display='block';
}

/**
	name			ChangeSorttype
	type			function
	params			-
	description		changes the sorttype in the (advanced) search screen from descending to ascending and vice versa.
**/
function ChangeSorttype() {
	if (document.getElementById("sortfield").value == 'publishdate'){
		document.getElementById("sorttype").selectedIndex = 1;
	}
	else {
		document.getElementById("sorttype").selectedIndex = 0;
	}
}

/**
	name			RIS_checkboxes
	type			function
	params			n: number of checkboxes to check
	description		loops through all the RIS checkboxes and fills a hidden text field, so that the
					search tag understands the query.
**/
function RIS_checkboxes(n) {
	var s = "";
	var check = true;
	
	for (var i = 1; i <= n; i++) {
		if (document.getElementById("doc" + i).checked) {
			if (s != "") s += ", ";
			s += document.getElementById("doc" + i).value;
		} else {
			check = false;
		}
	}
	
	if (!check) {
		document.getElementById("dsmrisdocumentsoort").value = s;
	} else {
		document.getElementById("dsmrisdocumentsoort").value = "";	
	}
}

/**
	name			changeTab
	type			function
	params			active: the name of the tab which should be active
					tabs_array: an array of tab names (strings)
					active_class: the name of the CSS class for an active tab
					inactive_class: the name of the CSS class for an inactive tab
	description		used in tabbed interfaces to set active tabs and show its contents while hiding the
					content of other tabs.
**/
function changeTab(active, tabs_array, active_class, inactive_class) {
	for (var i = 0; i < tabs_array.length; i++) {
		if (active == tabs_array[i]) {
			document.getElementById(tabs_array[i]).className = active_class;
			document.getElementById(tabs_array[i] + "_blade").style.display = "block";
		} else {
			document.getElementById(tabs_array[i]).className = inactive_class;
			document.getElementById(tabs_array[i] + "_blade").style.display = "none";
		}
	}
}

//################################### TODO: embed stylesheet switching functions in a single object

/**
	name			setActiveStyleSheet
	type			function
	params			title: ??
	description		??
**/
function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

/**
	name			getActiveStyleSheet
	type			function
	params			-
	description		??
**/
function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

/**
	name			getPreferredStyleSheet
	type			function
	params			-
	description		??
**/
function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

/**
	name			createCookie
	type			function
	params			name: ??
					value: ??
					days: ??
	description		??
**/
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

/**
	name			readCookie
	type			function
	params			name: ??
	description		??
**/
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

/**
	name			styleSwitchLoad
	type			function
	params			e: event
	description		function is called on window.load and reads cookie information to set 
					previously selected stylesheet
**/
function styleSwitchLoad(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
  if (title=="Groter"){
  	document.getElementById("set_normal").style.display="block";
  	document.getElementById("set_bigger").style.display="none";
  	}
  else{
  	document.getElementById("set_bigger").style.display="block";
  	document.getElementById("set_normal").style.display="none";	
	}
}

/**
	name			styleSwitchUnload
	type			function
	params			e: event
	description		function is called on window.unload and stores selected stylesheet in cookie
**/
function styleSwitchUnload(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

/**
	set stylesheet switch function to window.load and window.unload events
**/
Event.observe(window, "load", styleSwitchLoad, false);
Event.observe(window, "unload", styleSwitchUnload, false);

/**
	some global vars for stylesheet switching
**/
var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();

/**
	global var for discussion tree open/close code
**/
var elementstring = "";

/**
	name			ShowHide
	type			function
	params			divname: ??
	description		does something to show or hide replies in a discussion view
**/
function ShowHide(divname){
	var obj_divname = document.getElementById(divname);
	var elementindex;
	var elementlength;
	
	elementindex = elementstring.indexOf(';' + divname + ';');
	elementlength = divname.length+2;
		
	if (elementindex ==-1){
		obj_divname.style.display='block';
		elementstring += ';'+divname+';';
	}else{
		obj_divname.style.display='none';
		elementstring = elementstring.substr(0,elementindex)+elementstring.substr(elementindex+elementlength,elementstring.length);
		
	}
}

/**
	name			ShowAllReactions
	type			function
	params			-
	description		does something to show all replies in a discussion view
**/
function ShowAllReactions(){
	 for (i = 1; i < NumberOfReactions+1; i++){
	 	var divitem;
	 	divitem = "reaction_"+i;
	 	
	 	var obj_divitem = document.getElementById(divitem);
	 	
	 	obj_divitem.style.display='block';
	 	
	 	elementstring += ';'+divitem+';';
	 }
	
}

/**
	name			HideAllReactions
	type			function
	params			-
	description		does something to hide all replies in a discussion view
**/
function HideAllReactions(){
	 for (i = 1; i < NumberOfReactions+1; i++){
	 	var divitem;
	 	divitem = "reaction_"+i;
	 	
	 	var obj_divitem = document.getElementById(divitem);
	 	
	 	obj_divitem.style.display='none';
	 	
	 	elementstring = "";
	 }
	
}

/**
	name			gvGMap
	type			class
	description		represents a Google Maps widget
**/
var gvGMap = Class.create();
gvGMap.prototype = {
	initialize: function(obj, pars, input) {
		this.pars = pars;
		this.map = new GMap2(obj);
		this.map.setCenter(new GLatLng(this.pars["lat"], this.pars["lng"]), this.pars["zoom"], this.pars["mode"]);
		
		if (this.pars["shownav"]) this.map.addControl(new GSmallMapControl());
		if (this.pars["showmode"]) this.map.addControl(new GMapTypeControl());
		
		this.inputfield = input;
		this.inputfield.style.display = "none";
		
		this.marker = null;
		
		if (this.inputfield.value != "") {
			var pars = this.inputfield.value.split(",");
			if (pars && pars.length == 2) {
				this.marker = new GMarker(new GLatLng(pars[0], pars[1]), {draggable: true, bouncy: false});
				this.map.addOverlay(this.marker);
				this.map.setCenter(new GLatLng(pars[0], pars[1]));
			}
		}
		
		if (this.marker == null) {
			this.inputfield.value = "";
		}
		
		GEvent.bind(this.map, "click", this, this.handleClick);
	}, 
	
	handleClick: function(marker, point) {
		if (marker) {
			this.map.removeOverlay(marker);
			this.marker = null;
			this.inputfield.value = "";
		}
		
		if (this.marker) {
			this.map.removeOverlay(this.marker);
		}
		
		this.marker = new GMarker(point, {draggable: true, bouncy: false});
		this.map.addOverlay(this.marker);
		this.inputfield.value = this.marker.getPoint().lat() + "," + this.marker.getPoint().lng();
	}
}

/**
	name			loadGoogleMap
	type			function
	params			-
	description		adds Google Maps widget to the page in each div with CSS class "gMap". This function is dependent on the 
					Google Maps API.
**/
function loadGoogleMaps() {
	if (GBrowserIsCompatible()) {
		var el = document.getElementsByClassName("gmap");
	
		el.each(function(node) {
			var t = document.getElementsByClassName("gmap_flags", node);
			
			if (t[0] && t[0].innerHTML) {
				var pars;
				eval("pars = " + t[0].innerHTML);
				
				var w = $(node.id.substring(3, node.id.length));
				if (pars && w) {
					var m = new gvGMap(node, pars, w);
				}
			}
		});
	}
}

/**
	name			loadHandlers
	type			function
	params			-
	description		searches the DOM tree for elements with CSS class "gvFlag". The innerHTML of these elements 
				is used to see if a specific javascript handler should be loaded.
				Also checks if the GVMenu is used and initializes the menu items.
**/
function loadHandlers() {
	if (initMenu) {
		initMenu();
	}
	
	var el = document.getElementsByClassName("gvflag");
	
	el.each(function(node) {
		if (node.innerHTML == "GV_LOADFORMGMAPS") { //load Google maps for FormDesigner
			Event.observe(window, "unload", GUnload, false);
			loadGoogleMaps();
		}
	});
}

Event.observe(window, "load", loadHandlers, false);
