/**
 * Focus on first text field, if any
 */

function focusFirstInput()
{
	var i, a;
	for(i=0; (a = document.getElementsByTagName('input')[i]); i++)
	{
		if(a.getAttribute("type").indexOf("text") != -1)
		{
			a.focus();
			return true;
		}
	}
}

/**
 * Get all odd rows of a tbody with classname 'zebra' and give them a classname to color them differently
 * Author: Christian ten Hoope
 */

function colorOddRows()
{
  var alltbodies = document.getElementsByTagName("tbody");
  for (var i=0; i < alltbodies.length; i++)
  {
    if (alltbodies[i].className == 'zebra')
    {
      var alltrs = alltbodies[i].getElementsByTagName("tr");
      for (var j=0; j < alltrs.length; j++)
      {
        if (j % 2 == 1)
          alltrs[j].className = "odd";
       }
    }
  }
}

/**
 * Store message box after a few seconds
 * Author: Christian ten Hoope
 */

function storeMessageBox(seconds)
{
	var time = 1000 * seconds;
	setTimeout("disableStyleSheet();",time);
}

function disableStyleSheet()
{
	document.getElementsByTagName('link')[1].disabled = true;
}


/**
 * New window
 */

function newWindow(url, naam, w, h)
{
	props = 'resizable=0,width='+w+',height='+h+',top='+(screen.height-h)/2+',left='+(screen.width-w)/2;
	win = window.open(url, naam, props);
}

/**
 * Implements an expandable menu based on a HTML list
 * Author: Dave Lindquist (http://www.gazingus.org)
 */

// Create click() method for span elements (mozilla)
if (window.HTMLSpanElement && HTMLSpanElement.prototype) {
	HTMLSpanElement.prototype.click = function()
	{
		var evt = this.ownerDocument.createEvent('MouseEvents');
		evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
		this.dispatchEvent(evt);
	}
}

// Simulate mouse click on current menu item
function expandMenu(menuId)
{
    document.getElementById(menuId).click();
}

if (!document.getElementById)
    document.getElementById = function() { return null; }

function initTree(menuId, actuatorId) {
    var menu = document.getElementById(menuId);
    var actuator = document.getElementById(actuatorId);

    if (menu == null || actuator == null) return;

    actuator.parentNode.style.backgroundImage = "url(../gfx/plus.gif)";
    actuator.onclick = function() {
        var display = menu.style.display;
        this.parentNode.style.backgroundImage =
            (display == "block") ? "url(../gfx/plus.gif)" : "url(../gfx/minus.gif)";
        menu.style.display = (display == "block") ? "none" : "block";

        return false;
    }
}

/*
 * Implements an dropdown menu based on a HTML list
 * Author: Dave Lindquist (dave@gazingus.org)
 * Alterator: Kristof
 */

var currentMenu=null;
if (!document.getElementById)document.getElementById=function(){return null;};
function initMenu(menuId, actuatorId){
	var menu=document.getElementById(menuId);
	var actuator=document.getElementById(actuatorId);
	if(menu==null||actuator==null)return;
	//if (window.opera) return;
	actuator.onmouseover=function(){
		if(currentMenu){
			currentMenu.style.visibility='hidden';
			this.showMenu();
		}
	};
	actuator.onclick=function(e){
		if(!e)var e=window.event;
		e.cancelBubble=true;
		if(e.stopPropagation)e.stopPropagation();
		if(currentMenu==null)this.showMenu();
		else{
			currentMenu.style.visibility='hidden';
			currentMenu=null;
		}
		if(this.blur)this.blur();
		return false;
	};
	document.onclick=function(){
		if(currentMenu){
			currentMenu.style.visibility='hidden';
			currentMenu=null;
		}
	};
	actuator.showMenu=function(){
		menu.style.left=this.offsetLeft+'px';
		menu.style.top=this.offsetTop+this.offsetHeight+'px';
		menu.style.visibility='visible';
		currentMenu=menu;
	};
}

/*
 * Styleswitcher
 */

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;
    }
  }
}

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;
}

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;
}

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=/";
}

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;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
  focusFirstInput();
  colorOddRows();

if(document.getElementById('fs_description'))
{
	var oFCKeditor = new FCKeditor( 'fs_description' ) ;
	oFCKeditor.BasePath = "/fckeditor/" ;
	oFCKeditor.Width = 400 ; // 400 pixels
	oFCKeditor.Height = "250" ; // 250 pixels
	oFCKeditor.ToolbarSet = "editor" ;
	oFCKeditor.Config[ "CustomConfigurationsPath" ] = oFCKeditor.BasePath + 'fckconfig_editor.js';
	oFCKeditor.ReplaceTextarea() ;
}
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);