window.addEvent('domready', function(){
	// Color picker for menu font-color, and background-color
	if ($('style_color')){
		new MooRainbow('style_color',{
			id			:	'mooRainbow_1',
			imgPath		:	'http://brini-maskinfabrik.dk/cms/js/mooRainbow/images/',
			wheel		:	true,
			onChange	:	function(color){
				$('style_color_field').value = color.hex;
				$('menu_style_demo').setStyle('color', color.hex);
			}
		});
	}
	if ($('style_background_color')){
		new MooRainbow('style_background_color',{
			id			:	'mooRainbow_2',
			imgPath		:	'http://brini-maskinfabrik.dk/cms/js/mooRainbow/images/',
			wheel		:	true,
			onChange	:	function(color){
				$('style_background_color_field').value = color.hex;
				$('menu_style_demo').setStyle('backgroundColor', color.hex);
			}
		});
	}
});

// Modal window control functions
function modalwindow_show(elm){
	//var elm_overlay = modalwindow_overlay();
	//elm_overlay.fade(0.1);
	
	if (typeof(elm) == 'string'){
		elm = $(elm);
	}
	elm.setStyle('display', 'block');
	elm.setStyle('visibility', 'visible');
	elm.setStyle('opacity', 0.0);
	elm.fade(1.0);
}
function modalwindow_hide(elm){
	//var elm_overlay = modalwindow_overlay();
	//elm_overlay.fade(0.0);
	
	if (typeof(elm) == 'string'){
		elm = $(elm);
	}
	//elm.fade(0.0);
	elm.setStyle('display', 'none');
}
function modalwindow_overlay(){
	if ($('modalwindow_overlay')){
		var elm = $('modalwindow_overlay');
	}
	else{
		var viewport_height = browser.client_height() + browser.scroll_top();
		
		var elm = new Element('div',{
			id			:	'modalwindow_overlay',
			styles		:	{
				position	:	'absolute',
				top			:	'0px',
				left		:	'0px',
				width		:	'100%',
				height		:	viewport_height+'px',//'100%',
				background	:	'#000',
				cursor		:	'pointer',
				opacity		:	0.0
			},
			events		:	{
				click		:	function(){
					$$('.modalwindow').each(function(mw){
						modalwindow_hide(mw);
					});
				}
			}
		});
		
		elm.injectInside($(document.body));
	}
	
	return elm;
}
function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	
	return arrayPageSizeWithScroll;
}
// Modal window control functions

function addOption(selectId, txt, val){
	var objOption = new Option(txt, val);
	document.getElementById(selectId).options.add(objOption);
}

function showHide(id){
	if (document.getElementById && document.getElementById(id)){
		if (document.getElementById(id).style.visibility == "visible"
			|| document.getElementById(id).style.display == "block"){
			// hide it
			document.getElementById(id).style.visibility = "hidden";
			document.getElementById(id).style.display = "none";
		}else{
			// show it
			document.getElementById(id).style.visibility = "visible";
			document.getElementById(id).style.display = "block";
		}                      
	}
}

function ChangeImage(imgId, newSrc, id){
	document.getElementById(imgId).src=newSrc;
	document.getElementById(imgId).className=id;
}

// Password generation functions
function generate_password(length){
	var charSet = '';
		charSet+= '0123456789';
		//charSet+= 'abcdefghjklmnpqrstuvwxyz';
		charSet+= 'ABCDEFGHJKLMNPQRSTUVWXYZ';
		//charSet+= '!@#$()-_=+;:?';
	
	var password = '';
	
	if (length > 0){
		password = password + charSet.charAt((Math.floor(Math.random() * (charSet.length - 0)) + 0));
		for (var idx = 1; idx < length; ++idx) {
			password = password + charSet.charAt((Math.floor(Math.random() * (charSet.length - 0)) + 0));
		}
	}
	
	return password;
}

// Client browser functions
function f_clientWidth(){
	return f_filterResults(
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
		);
}
function f_clientHeight(){
	return f_filterResults(
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
		);
}
function f_scrollLeft(){
	return f_filterResults(
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
		);
}
function f_scrollTop(){
	return f_filterResults(
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
		);
}
function f_filterResults(n_win, n_docel, n_body){
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel))){
		n_result = n_docel;
	}
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

// Open popup window
function popup(url, title, width, height){
	var screenY = 0;
	var screenX = (f_clientWidth() / 2) - (width / 2);
	
	var params = 'width='+width+',height='+height+',screenY='+screenY+',screenX='+screenX+',location=no,menubar=no,resizeable=yes,scrollbars=yes';
	window.open(url, title, params, false);
}

// Get elements with a certain class-name
function getElementsByClassName(strClassName, obj){
	var ar = arguments[2] || new Array();
	var re = new RegExp("\\b" + strClassName + "\\b", "g");
	
	if (re.test(obj.className)){
		ar.push(obj);
	}
	for ( var i = 0; i < obj.childNodes.length; i++ ){
		getElementsByClassName(strClassName, obj.childNodes[i], ar);
	}
	
	return ar;
}

// Show/hide an element
function display(elm, mode, show, hide){
	if (show == '' || show == undefined){
		show = 'block';
	}
	if (hide == '' || hide == undefined){
		hide = 'none';
	}
	
	if ($(elm)){
		elm = $(elm);
	}
	else{
		return false;
	}
	
	switch (mode){
		case 'show':
			elm.setStyle('display', show);
		break;
		case 'hide':
			elm.setStyle('display', hide);
		break;
		default:
			if (elm.getStyle('display') == show){
				elm.setStyle('display', hide);
			}
			else{
				elm.setStyle('display', show);
			}
		break;
	}
}

// Show/hide all elements with a certain class-name
function display_class(class_name, mode, show, hide){
	if (show == '' || show == undefined){
		show = 'block';
	}
	if (hide == '' || hide == undefined){
		hide = 'none';
	}
	
	var elements = getElementsByClassName(class_name, document.body);
	var elm = '';
	
	for (i = 0; i < elements.length; i++){
		elm = $(elements[i]);
		
		switch (mode){
			case 'show':
				elm.setStyle('display', show);
			break;
			case 'hide':
				elm.setStyle('display', hide);
			break;
			default:
				if (elm.getStyle('display') == show){
					elm.setStyle('display', hide);
				}
				else{
					elm.setStyle('display', show);
				}
			break;
		}
	}
}

// Check all elements with a certain class-name
function check_all(class_name){
	var elements = getElementsByClassName(class_name, document.body);
	
	for (i = 0; i < elements.length; i++){
		elements[i].checked = true;
	}
}

// Un-check all elements with at certain class-name
function uncheck_all(class_name){
	var elements = getElementsByClassName(class_name, document.body);
	
	for (i = 0; i < elements.length; i++){
		elements[i].checked = false;
	}
}


// Browser class
var browser = {
	client_width : function(){
		if (
			typeof(window.innerHeight) != 'undefined' &&
			window.innerHeight != 0
			){
			return window.innerWidth;
		}
		else if (
			typeof(document.documentElement) != 'undefined' &&
			typeof(document.documentElement.clientWidth) != 'undefined' &&
			document.documentElement.clientWidth != 0
			){
			return document.documentElement.clientWidth;
		}
		else if (
			typeof(document.body) != 'undefined' &&
			typeof(document.body.clientWidth) != 'undefined' &&
			document.body.clientWidth != 0
			){
			return document.body.clientWidth;
		}
		else{
			return document.getElementsByTagName('body')[0].clientWidth;
		}
		
		return 0;
	},
	
	client_height : function(){
		if (
			typeof(window.innerHeight) != 'undefined' &&
			window.innerHeight != 0
			){
			return window.innerHeight;
		}
		else if (
			typeof(document.documentElement) != 'undefined' &&
			typeof(document.documentElement.clientHeight) != 'undefined' &&
			document.documentElement.clientHeight != 0
			){
			return document.documentElement.clientHeight;
		}
		else if (
			typeof(document.body) != 'undefined' &&
			typeof(document.body.clientHeight) != 'undefined' &&
			document.body.clientHeight != 0
			){
			return document.body.clientHeight;
		}
		else{
			return document.getElementsByTagName('body')[0].clientHeight;
		}
		
		return 0;
	},
	
	scroll_left : function(){
		if (typeof(window.scrollMaxX) != 'undefined'){
			return window.scrollMaxX;
		}
		else if (typeof(document.body) != 'undefined'){
			if (typeof(document.body.scrollWidth) != 'undefined'){
				return document.body.scrollWidth - this.client_width();
			}
			else if (typeof(document.body.offsetWidth) != 'undefined'){
				return document.body.offsetWidth - this.client_width();
			}
		}
		
		return 0;
	},
	
	scroll_top : function(){
		if (typeof(window.scrollMaxY) != 'undefined'){
			return window.scrollMaxY;
		}
		else if (typeof(document.body) != 'undefined'){
			if (typeof(document.body.scrollHeight) != 'undefined'){
				return document.body.scrollHeight - this.client_height();
			}
			else if (typeof(document.body.offsetHeight) != 'undefined'){
				return document.body.offsetHeight - this.client_height();
			}
		}
		
		return 0;
	}
}

