function motigo_toggle_image(elm) {
	domain = $(elm).src.match( /:\/\/(www\.)?([^\/:]+)/ );
	domain = domain[2]?domain[2]:'';
	if($(elm).src.indexOf("open")!=-1) {
		$(elm).src="http://" + domain + "/images/" + elm + "_closed.gif";
	} else if($(elm).src.indexOf("closed")!=-1) {
		$(elm).src="http://" + domain +"/images/" + elm + "_open.gif";
	}
}

function motigo_toggle_class(elm) {
	if($(elm).className=='') {
		$(elm).className='m_menu_open';
	} else  {
		$(elm).className='';
	}
}

function motigo_set_cookie(name, value, expires, path, domain, secure) {
	document.cookie= name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

function motigo_set_menu_cookie(name, elm) {
	if($(elm).className=='') {
		motigo_set_cookie(name, 0, null, "/");
	} else  {
		motigo_set_cookie(name, 1, null, "/");
	}
}

function motigo_create_cookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	// If we are operating in a frame, then set the cookie for the frame
	if (parent.frames.length > 0) {
        window.self.document.cookie = name+"="+value+expires+"; path=/; domain=.motigo.com";
	} else {
    	document.cookie = name+"="+value+expires+"; path=/; domain=.motigo.com";
	}
}

function motigo_read_cookie(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);
	}
	// check if the cookie is to be found in a frame instead
	if (typeof(parent.frames)=="object") {
    	for (var i=0; i < parent.frames.length; i++) {
        	var nameEQ = name + "=";
        	var ca = parent.frames[i].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;
}

function motigo_parse_menu_cookie(cookieName) {
	var motigo_menu = motigo_read_cookie(cookieName);
	var values = new Array();
	if (motigo_menu != null) {
	   values = motigo_menu.split('-');
	}
	return values;
}

function motigo_convert_array_to_string(elementArray) {
    var string = '';
    for (i = 0; i < elementArray.length; i++) {
        if (elementArray[i] != 'null') {
            if (string == '') {separator = '';} else {separator = '-';}
            string = string + separator + elementArray[i];
        }
    }
    return string;
}

function motigo_toggle_menu_element(elementID, class_name, controllerName) {
    var cookieName = 'm_menu_' + controllerName;
    var menus_open = motigo_parse_menu_cookie(cookieName);
    if (class_name == 'm_menu_open') {
        menus_open.push(elementID);
    } else {
        menus_open = removeElementFromArray(elementID, menus_open);
    }
    var menus_open_string = motigo_convert_array_to_string(menus_open);
    motigo_set_cookie(cookieName, menus_open_string, null, '/');

}

function removeElementFromArray(nameOfElement, elementArray) {
    var newArray = new Array();
    for (i = 0; i < elementArray.length; i++) {
        if (!(elementArray[i] == nameOfElement)) {
            newArray.push(elementArray[i]);
        }
    }
    return newArray;
}

function motigo_set_language(dropdown) {
	var myindex  = dropdown.selectedIndex;
	var language = dropdown.options[myindex].value;
	motigo_create_cookie("m_lang", language, 1000);
	if ( ! motigo_read_cookie("m_lang") )
    	{ alert("Please enable cookies to make language selection work."); }
	else
	{ window.location.reload( true ); };
}

function motigo_open_toggled_menus (controllerName) {
    var cookieName = 'm_menu_' + controllerName;
    var menus_open = motigo_parse_menu_cookie(cookieName);
    for (i = 0; i < menus_open.length; i++) {
        motigo_toggle_class(menus_open[i]);
        Element.show(menus_open[i] + '_1');
    }
}

function motigo_toggle_menu(name, menustring) {

    values_before = motigo_parse_menu_cookie(name);
	var service_before = values[0];
	var level1_before = values[1].split(',');
	var level2_before  = values[2].split(',');
	values_now = menustring.split('|');
	var service_now = menustring[0];
	var level1_now = menustring[1].split(',');
	var level2_now  = menustring[2].split(',');
	if (service_before == service_now) {
//		if level1_before.indexOf(level1_now)
//		level2_before.each(function(s) {
//			alert(s);
//		});
	}
}

function motigo_erase_cookie(name) {
	motigo_create_cookie(name,"",-1);
}

function motigo_toggle_display(elm) {
	var elms = elm.split(",");
	for (var i=0;i<elms.length;i++) {
		Element.toggle(elms[i]);
	}
}

var bHideError = false, aErrors = [], iHide = 0;
function motigo_login_error(elm) {
	var elms = elm.split(",");
	aErrors.length = 0;
	for (var i=0;i<elms.length;i++) {
		var el = $(elms[i]);
		el.style.display = "block";
		aErrors.push( el );
		bHideError = true;
	}
	var oForm = $('loginForm'), el = oForm.elements['loginPassword'];
	el.focus();
	Event.observe(document, 'click', hideErrorTimer);
	Event.observe(oForm, 'submit', hideError);
	Event.observe(oForm.loginUsername, 'blur', hideErrorTimer);
	Event.observe(oForm.loginPassword, 'blur', hideErrorTimer);
	Event.observe(oForm.loginUsername, 'focus', hideErrorCancel);
	Event.observe(oForm.loginPassword, 'focus', hideErrorCancel);
}
function hideErrorCancel(){
	clearTimeout(iHide);
	bHideError = false;
}
function hideErrorTimer(ev){
	var e=ev||event, t=e.target||e.srcElement;
	if(e.type=="click" && t.tagName=="INPUT") return;
	bHideError = true;
	iHide = setTimeout(hideError,120);
}
function hideError(){
	if(!bHideError) return;
	for(var i=0, il=aErrors.length; i<il; i++){
		aErrors[i].style.display = "none";
	}
	bHideError = false;
}

function motigo_hide(elm) {
		alert(elm);
		Element.hide(elm);
		motigo_hide_observer = motigo_hide.bindAsEventListener(this);
		return false;
}

function change_stylepreview_image(type, path) {
    if (!path) {var path = '/images/stylepreview/';}

    var value = document.stylepreview_form.style.options[document.stylepreview_form.style.options.selectedIndex].value;

    if (document.getElementById) {
		stylepreview_image = document.getElementById('stylepreview_image');
	} else if (window.opener.document.all) {
		x = document.all['stylepreview_image'];
	}
    Element.hide('stylepreview_image');
	stylepreview_image.src = path + type + '_' + value + '.gif';
    Effect.Appear('stylepreview_image', {duration: 0.2});
}

function show_colorpicker(field) { // coded by maurice
		var width = 200;
		var height = 180;
		var left = (screen.width / 2) - width / 2;
		var top = (screen.height / 2) - height / 2;
		colorpicker = window.open("/index.php?module=forums&controller=forums&action=misc_colorpicker&field=" + field, "colorpicker", "left="+left+",top="+top+",width="+width+",height="+height+",dependent=yes,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no");
		colorpicker.focus();
		return;
}