/*
 * Company		: Opzet V.O.F..
 * Programmer	: Guus Sprokkereef
 * Email		: guus@opzet.nl
 * 
 * Date			: 01-03-2005
 * Version		: 1.0
 * 
 * History:
 * 
 * 
 */

var sitemapSelectedObject = null;
var sitemapSelectedClass = "";

function selectObject(o)
{
	if (sitemapSelectedObject!=null)
	{
		sitemapSelectedObject.className = sitemapSelectedClass;
		resetClass(sitemapSelectedObject);
	}
	if (sitemapSelectedObject==o)
	{
		o.className = sitemapSelectedClass;
		sitemapSelectedObject = null;
	} else {
		sitemapSelectedObject = o;
		sitemapSelectedClass = o.className;
		o.className = "clickedOver";
	}
}

function changeClass(o)
{
	var c = o.className;
	
	if (c.lastIndexOf("Over")==-1)
	{
		c = c + "Over";
	}
	o.className = c;
}

function resetClass(o)
{
	var c = o.className;
	
	if (c.lastIndexOf("Over")!=-1)
	{
		c = c.substr(0,c.lastIndexOf("Over"));
	}
	o.className = c;
}

function create(url, txt)
{
	if (sitemapSelectedObject==null)
	{
		window.alert("Selecteer een object waaronder de nieuwe "+txt.toLowerCase()+" geplaatst moet worden");
	} else {
		var o = document.forms['sitemap'];

		o.action = url;
		if (sitemapSelectedObject == "new")
		{
			o['parent_id'].value = "";
		} else {
			o['parent_id'].value = sitemapSelectedObject.id;
		}
		o.submit();
	}
}

function edit(url, txt)
{

	var o = document.forms['sitemap'];

	o.action = url;
	o['class_id'].value = txt;

	o.submit();
}

function remove(url, txt)
{
	if (window.confirm("wilt u dit object verwijderen?"))
	{
		edit(url, txt);
	}
}

function strpos(str, ch)
{
	for (var i = 0; i < str.length; i++)
	if (str.substring(i, i + ch.length) == ch) return i;
	return -1;
}


/**
 * Sitemap constructor function
 */
function Sitemap()
{
	// Cookie class var.
	this.cookie = new Array();
}

/**
 * init() method; reads cookie & expands branches.
 */
Sitemap.prototype.init = function()
{
	this.readCookie();
	
	for(var i=0; i<this.cookie.length; i++)
	{
		var id  = (this.cookie[i]);
		if(id != "")
		{	
			this.activate(id, false);
		}
	}
}

/**
 * Read cookie.
 */
Sitemap.prototype.readCookie = function()
{
	var name = 'activated';
	var s = document.cookie.indexOf(name);
	var l = name.length + 1;
	
	if(s != -1)
	{
		var e = document.cookie.indexOf(';', s+l);
		this.cookie = document.cookie.substr( s+l , e - (s+l) ).split(',');
	}
}

/**
 * Save Cookie
 */
Sitemap.prototype.saveCookie = function()
{
	document.cookie = 'activated=' + this.cookie + ';';
}

/**
 * Activate method; expands branches.
 * @param	id		Id of the branch to expand/collapse
 * @param	store	Boolean to store action to cookie
 */
Sitemap.prototype.activate = function(id,store)
{
	var img = document.getElementById('activator_' + id);
	if(img==null)
	{
		// erase the id from the cookie, it doesn't exist anymore
		for (var i=0; i<this.cookie.length; i++)
		{
			if(id == this.cookie[i])
			{
				this.cookie.splice(i,1);
				this.saveCookie();
			}
		}
	} else {
		if(strpos(img.src,'active') > 0) // -> de-activate
		{
		
			try
			{
				// set image
				img.src = img.src.replace('active', 'passive');
				
				// set style
				document.getElementById('group_' + id).style.display = 'none';
				
				// strip from cookie
				if(store) { for(var i=0; i<this.cookie.length; i++) { if(id == this.cookie[i]) this.cookie.splice(i,1);  }; this.saveCookie(); }
			}
			catch(e)
			{
				alert("deactivate activate error");
			}
		}
		else // -> activate
		{
			try
			{
				// set image
				img.src = img.src.replace('passive', 'active');
				
				// set style
				document.getElementById('group_' + id).style.display = 'block';
				
				// add to cookie
				if(store) { this.cookie.push(id); this.saveCookie(); }
			} 
			catch(e)
			{
				alert("activate error");
			}
		}
	}
}

/* CREATE INSTANCE */
var sm = new Sitemap; 

