window.addEvent('domready', function(){
	
	$$('.colorpicker').each(function(el){
		$(el).adopt(new Element('img', { 'src': 'images/moorainbow/rainbow.png', 'height': 16, 'width': 16, 'alt': '' }));
		var field_name = el.id.replace('colorpicker-', '');
		
		new MooRainbow(el.id, {
			'id': 'rainbow-' + field_name,
			'startColor': $(field_name).value != '' ? new Color($(field_name).value, 'RGB') : [0,0,0],
			'onChange': function(color) {
				$('cd_'+field_name).setStyle('background-color', color.hex);
				$(field_name).value = color.hex.replace('#','');
			}
		});
	});

	$(document).getElements('fieldset.collapsible legend').each(function(el){

		var fieldset = $(el).getParent('fieldset.collapsible');
		var text = el.get('html');

		$(el).empty().adopt(new Element('a', { 'href': '#', 'html': text })).addEvent('click', function() {
			if ($(fieldset).hasClass('collapsed')) {
				$(fieldset).getElement('.fieldset-wrapper').setStyle('display', 'block');
				$(fieldset).removeClass('collapsed');
			}
			else {
				$(fieldset).getElement('.fieldset-wrapper').setStyle('display', 'none');
				$(fieldset).addClass('collapsed');
			}
			return false;
		});
		
		if ($(fieldset).hasClass('collapsed')) {
			$(fieldset).getElement('.fieldset-wrapper').setStyle('display', 'none');
		}
	});
});



	function set_focus () {
		if (document.forms[0]) {
			for (i = 0; i < document.forms[0].length; i++) {
				if (document.forms[0][i].name != 'search_string' && document.forms[0][i].name != 'username' && document.forms[0][i].name != 'password' && (document.forms[0][i].type == 'text' || document.forms[0][i].type == 'textarea' || document.forms[0][i].type == 'password')) {
					//document.forms[0][i].focus();
					break;
				}
			}
		}
	}

	function unload() {
		if (document.input_changes && document.forms[document.input_name]) {
			if (confirm("Save changes?")) {
				for (i = 0; i < document.forms[document.input_name].length; i++) {
					if (document.forms[document.input_name][i].type == 'submit') {
						document.forms[document.input_name][i].click();
						break;
					}
				}
			}
		}
	}


	function init() {
		set_focus();
	}



	var pop_window = null;

	function popup(url, w, h) {

		var winl = 0;
		var wint = 0;
		w = w + 50;
		h = h + 50;
		if (screen) {
			var winl = (screen.width - w) / 2;
			var wint = (screen.height - h) / 2;
		}
		winprops = 'height='+h+', width='+w+', top='+wint+', left='+winl+', scrollbars=0';

		if (pop_window && !pop_window.closed && pop_window.location) {
			pop_window.close();
		}
		pop_window = window.open(url, "member", winprops);
	}

	function popup_scroll(url, w, h) {

		var winl = 0;
		var wint = 0;
		w = w + 50;
		h = h + 50;
		if (screen) {
			var winl = (screen.width - w) / 2;
			var wint = (screen.height - h) / 2;
		}
		winprops = 'height='+h+', width='+w+', top='+wint+', left='+winl+', scrollbars=1';

		if (pop_window && !pop_window.closed && pop_window.location) {
			pop_window.close();
		}
		pop_window = window.open(url, "member", winprops);
	}


	function box_lists_add (form_obj, left, right) {
		box_lists_change(form_obj, left, right);
		box_lists_update(form_obj, left, right);
	}

	function box_lists_remove (form_obj, left, right) {
		box_lists_change(form_obj, right, left);
		box_lists_update(form_obj, left, right);
	}


	function box_lists_update (form_obj, left, right) {
		form_obj[left + '_' + right].value = '';

		for (i = 0; i < form_obj[right].length; i++) {
			if (i == form_obj[right].length - 1)
				form_obj[left + '_' + right].value = form_obj[left + '_' + right].value + form_obj[right][i].value;
			else
				form_obj[left + '_' + right].value = form_obj[left + '_' + right].value + form_obj[right][i].value + ',';
		}
		document.changed = true;
	}

	function box_lists_change (form_obj, left, right) {
		var
			opt_value, opt_text;

		for (;form_obj[left].selectedIndex > -1;) {
			opt_value = form_obj[left][form_obj[left].selectedIndex].value;
			opt_text = form_obj[left][form_obj[left].selectedIndex].text;
			form_obj[left][form_obj[left].selectedIndex] = null;
			form_obj[right][(form_obj[right].length)] = new Option(opt_text, opt_value);
		}
	}
	
	function box_expand (form_obj, area) {
		if (document.getElementById(area)) {
			if (form_obj.checked)
				document.getElementById(area).style.display = 'none';
			else
				document.getElementById(area).style.display = 'block';
		}
	}

	function box_expand_radio (form_obj, area) {
		if (document.getElementById(area)) {
			if (form_obj.value == 0)
				document.getElementById(area).style.display = 'none';
			else
				document.getElementById(area).style.display = 'block';
		}
	}

	function box_expand_flip (form_obj, area) {
		if (document.getElementById(area)) {
			if (!form_obj.checked)
				document.getElementById(area).style.display = 'none';
			else
				document.getElementById(area).style.display = 'block';
		}
	}
		
	var faq_current = '';
	
	function faq_expand (area) {
		if (document.getElementById(area)) {
			if (document.getElementById(area).style.display == 'block') {
				document.getElementById(area).style.display = 'none';
				faq_current = '';
			}
			else {
				document.getElementById(area).style.display = 'block';
				if ( (faq_current != area) && (faq_current != '') )
					faq_expand(faq_current)
				faq_current = area;
			}
		}
	}
	
	// mutually excludes one ore more form fields
	function mutualExclude() { // argument list = names of mutually exclusive fields (onchange trigger is the first in list)
		var fields = arguments;
		
		var disabled = true;
				
		if (!document.forms[0][fields[0]].value || document.forms[0][fields[0]].value == "0") {
			disabled = false;
		}
		
		for(i=1; i<arguments.length; i++) {
			document.forms[0][fields[i]].disabled = disabled;
		}
	
	}
	
	function inputFocus(ele, txt) {
		if (ele.value == txt) {
			ele.value = "";
		}
	}

	function inputBlur(ele, txt) {
		if (ele.value == "") {
			ele.value = txt;
		}
	}
	
	function template_forms_multi (form_obj, area, val) {
		if (document.getElementById(area)) {
			if (form_obj.value == val)
				document.getElementById(area).style.display = 'none';
			else
				document.getElementById(area).style.display = 'block';
		}
	}

	function printpage () {
		if (window.print) {
			window.print();
		}
	}
	
	function shop_step_validate(obj, max) {
			if (obj.value > max) {
				alert('Der er kun ' + max + ' på lager');
				obj.value = max;
			}
	}

	function kontent_lang_paste (table, id, field, field_paste, path) {
		var translation = new Request({url: path + 'kadmin/get_translation.php', method: 'get', onComplete: function (txt) {
			document.forms.edit_form[field_paste].value = txt;
			//tinyMCE.updateContent(field_paste);
			//tinyMCE.activeEditor.load(tinyMCE.get(field_paste));
			var editor = tinyMCE.get(field_paste);

			if (editor) {
				//editor.load();
				editor.setContent(txt.replace(/\n/gi, '<br />'));
			}
		}}).send('table=' + table + '&id=' + id + '&field=' + field);
	}



	if (!window.console || !console.firebug) {
		var names = new Array("log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd");
		window.console = {};
		for (var i = 0; i < names.length; ++i) {
			window.console[names[i]] = function() {}
		}
	}

	function toggle(id) {
		if (document.getElementById(id)) {
			if (document.getElementById(id).style.display == 'block') {
				document.getElementById(id).style.display = 'none';
			}
			else {
				document.getElementById(id).style.display = 'block';
			}
		}
	}
	
	