var contextitem = new Array();

// css
contextitem['csslogo'] 			= new Array('/images/trans.gif', 'Wijzig afbeelding {1}', 'popCSS("{0}","b","{1}")');
contextitem['csslogocustom']		= new Array('/images/trans.gif', 'Wijzig afbeelding {1} voor deze pagina', 'popCSS("{0}","b","{1}",true)');
contextitem['csslogocustomreset']	= new Array('/images/trans.gif', 'Wijzigingen afbeelding {1} voor deze pagina ongedaan maken', 'popCSS("{0}","b","{1}","reset")');
contextitem['cssback']			= new Array('/images/trans.gif', 'Wijzig achtergrond {1} ', 'popCSS("{0}","b","{1}")');
contextitem['cssbackcustom']		= new Array('/images/trans.gif', 'Wijzig achtergrond {1} voor deze pagina', 'popCSS("{0}","b","{1}",true)');
contextitem['cssbackcustomreset']	= new Array('/images/trans.gif', 'Wijzigingen {1} voor deze pagina ongedaan maken', 'popCSS("{0}","b","{1}","reset")');
contextitem['csstxt']			= new Array('/images/trans.gif', 'Wijzig tekstopmaak {1}', 'popCSS("{0}","t","{1}")');
contextitem['csstxtcustom']		= new Array('/images/trans.gif', 'Wijzig tekstopmaak {1} voor deze pagina', 'popCSS("{0}","t","{1}",true)');
contextitem['csstxtcustomreset']	= new Array('/images/trans.gif', 'Wijzigingen tekstopmaak {1} voor deze pagina ongedaan maken', 'popCSS("{0}","t","{1}","reset")');
contextitem['cssmenu']			= new Array('/images/trans.gif', 'Wijzig opmaak {1}', 'popCSSmenu("{0}","m","{1}")');
contextitem['slogan']			= new Array('/images/trans.gif', 'Wijzig tekst', 'popTextarea("{0}","{1}")'); //depricated --GS
contextitem['textedit']			= new Array('/images/trans.gif', 'Wijzig tekst {1}', 'popTextarea("{0}","{1}")');
contextitem['texteditcustom']		= new Array('/images/trans.gif', 'Wijzig tekst {1} voor deze pagina', 'popTextarea("{0}","{1}",true)');
contextitem['texteditcustomreset']	= new Array('/images/trans.gif', 'Wijzigingen tekst {1} voor deze pagina ongedaan maken', 'popTextarea("{0}","{1}","reset")');


// menu onderhouden
contextitem['pageupdate']	= new Array('/images/trans.gif', 'Wijzig deze pagina', 'popContent("{1}");');
contextitem['pagemove']		= new Array('/images/trans.gif', 'Verplaats pagina"s', 'popOrder()');
contextitem['pagenew']		= new Array('/images/trans.gif', 'Voeg een nieuwe pagina toe', 'insertPage("{1}")');
contextitem['pagedelete']	= new Array('/images/trans.gif', 'Verwijder deze pagina', 'deletePage("{1}")');

// algemeen
contextitem['publish']		= new Array('/images/trans.gif', 'Sla vormgeving op', 'publish()');
contextitem['advanced']		= new Array('/images/trans.gif', 'Kies uitgebreide opties', 'setAdvanced("{1}","true")');
contextitem['basic']		= new Array('/images/trans.gif', 'Kies beperkte opties', 'setAdvanced("{1}","false")');
contextitem['template']		= new Array('/images/trans.gif', 'Kies nieuwe vormgeving', 'popTemplate("{1}")');
contextitem['resetcss']		= new Array('/images/trans.gif', 'Annuleer vormgeving', 'cssReset("{1}")');
contextitem['help']		= new Array('/images/trans.gif', 'Help', 'popHelp()');
contextitem['logout']		= new Array('/images/trans.gif', 'Uitloggen', 'uitloggen()');
contextitem['menutitle']	= new Array('/images/trans.gif', '{1}', '//');
contextitem['config']		= new Array('/images/trans.gif', 'Instellingen', 'popConfig()');

contextitem['space'] = '';

//alert(contextitem['textedit']);
function swapScreenshot(o)
{	
	var name = o.name.replace("css_", "")
	var imgName = "img_" + name;
	var img = document.getElementById(imgName);
	img.src = o[o.selectedIndex].value;

	for (var i=0; i<o.form.template.length; i++)
	{
		if (name == o.form.template[i].value)
		{
			o.form.template[i].checked = true;
		}
	}
}





function createNode(position,id) {
	var createUrl = "/modules/structure/edit.php?action=insertNode&siblingId="+ id+ "&position="+ position
		content.location.href = createUrl;
}

function moveNode(dst,id) {
	if (dst == "location") {
		content.location.href = "/modules/structure/move.php?id=" + id;
	} else if (dst != "") {
		window.location = "/modules/structure/edit.php?action=moveNode&dst=" + dst + "&id=" +id;
	}
}	   

function goDeleteTree(id) {
	if (confirm('Weet u het zeker?')) {
		window.location = '/modules/structure/edit.php?action=delete&id='+id
	}
}

/* * * * * * * * * * * * * * * * * * *\
 *  SUweller.com - MIT licence	   *
 *  mail: webdesign@suweller.com	 *
 *  keep license intact and in place *
\* * * * * * * * * * * * * * * * * * */
function createContextmenu(e,id,items) { // legacy wrapper
	context_menu.create(e,id,items);
}
var context_menu = {
	duration: 0.5,
	delay: 0.5,
	id: 'contextPopupFrame',
	current: null,
	create: function(e, id, items) {
		if (!context_menu.current || context_menu.current != new_menu) {
			new_menu = new Element('ul', {'id': context_menu.id, 'style': 'display:none;z-index:2147483647;'});
			new_menu.insert({ bottom: context_menu.content(id, items)});
			$(e).insert({ after: new_menu});
			context_menu.show_menu( new_menu);
		}
	},
	show_menu: function(new_menu) {
		if (context_menu.current) {
			var old_menu = context_menu.current;
			return new Effect.Parallel([
				new Effect.Fade( old_menu, {
					duration: context_menu.duration,
					afterFinish: function() {
						old_menu.up('.editCssBtn').toggleClassName('this');
						old_menu.remove();
					}
				}),
				new Effect.Appear( new_menu, {
					duration: context_menu.duration,
					beforeStart: function() {
						new_menu.up('.editCssBtn').toggleClassName('this');
					},
					afterFinish: function() {
						context_menu.current = new_menu;
						Event.observe(document, 'click', context_menu.hide );
					}
				}), {
				duration: context_menu.duration,
				delay: context_menu.delay,
				queue: { position: 'end', scope: 'suweller_context_menu', limit: 1 }
				}]);
		} else {
			return new Effect.Appear( new_menu, {
				duration: context_menu.duration,
				queue: { position: 'end', scope: 'suweller_context_menu', limit: 1 },
				beforeStart: function() {
					new_menu.up('.editCssBtn').toggleClassName('this');
				},
				afterFinish: function() {
					context_menu.current = new_menu;
					Event.observe(document, 'click', context_menu.hide );
				}
			});
		}
	},

	hide: function() {
		context_menu.hide_menu(context_menu.current);
	},

	hide_menu: function(menu) {
		return new Effect.Fade( menu, {
			duration: context_menu.duration,
			queue: { position: 'end', scope: 'suweller_context_menu', limit: 1 },
			afterFinish: function() {
				menu.up('.editCssBtn').toggleClassName('this');
				menu.remove();
				context_menu.current = null;
				Event.stopObserving(document, 'click', context_menu.hide );
			}
		});
	},

	content: function(id, items) {
		content = "";
		$A(items).each( function(item) {
			content += '<li>';
			switch (item) {
				case "space": content += "<hr/>"; break;
				case "menutitle": content += context_menu.content_item( context_menu.item_name(id, item)); break;
				default:
					icon  = contextitem[item][0];
					link   = stringFormat(contextitem[item][2], id[0], id[1]);

					content += context_menu.content_item( context_menu.item_name(id, item), link, icon);
				break;
			}
			content += '</li>';
		});
		return content;
	},
	item_name: function(id, item) {
		if (id.length ==3) {
			return stringReplace( contextitem[item][1], id[2]);
		} else {
			return contextitem[item][1];
		}
	},
	content_item: function( name) {
		if (arguments[1]) { // has a link
			if (arguments[2]) { // has an icon TODO should be a css class
				return "<a href='javascript:"+ arguments[1]+ "'><img src='"+ arguments[2]+ "'/> "+ name+ "</a>";
			} else {
				return "<a href='javascript:"+ arguments[1]+ "'> "+ name+ "</a>";
			}
		} else {
			return "<h6> "+ name+ "</h6>";
		}
	}
}


function timeOutVisibleContextmenu() {
	if (!keepContextPopup) removeContextmenu();
}

function initTimer() {
	clearTimeout(contextPopupTimer);
	contextPopupTimer = setInterval("timeOutVisibleContextmenu()", 3000);
	//contextPopupTimer = setInterval("timeOutVisibleContextmenu()", 1200);
}

function stringReplace(str) {
	var i = 0;
	for (var x=1; x<arguments.length; x++ )
	{
		str = str.replace("{"+x+"}",arguments[x].toLowerCase());
	}
	return str;
}
function stringFormat(strInput) {
	var idx = 0;
	for (var i=1; i<arguments.length; i++) {
		while ((idx = strInput.indexOf('{' + (i - 1) + '}', idx)) != -1) {
			strInput = strInput.substring(0, idx) + arguments[i] + strInput.substr(idx + 3);
		}
	}
	return strInput;
}
