if (document.cookie.indexOf("cms=true") != -1) {
	// write communication iframe
	document.write('<div id="edit_mask" style="position: absolute;"></div>');
	document.write('<div id="edit_logout" class="edit_logout" onmouseover="logout_hover(true);" onmouseout="logout_hover(false);" onclick="location.href=\'/cms/php/login.php?logout=1\';">Quitter</div>');
}

var env = detect_env();
var _g_elem = {};
function init_edit(id_list) {
	if (document.cookie.indexOf("cms=true") == -1) {
		return;
	}

	$('edit_logout').style.display = 'block';

	align_elem('top', 'right', 'edit_logout');
	setInterval("align_elem('top', 'right', 'edit_logout');", 100);

	position_mask(id_list, true);
	var edit_list_string = '[';
	for (var i = 0; i < id_list.length; i++) {
		edit_list_string += "'" + id_list[i] + "',";
	}
	edit_list_string = edit_list_string.substr(0, edit_list_string.length - 1) + ']';
	setInterval("position_mask(" + edit_list_string + ", false);", 100);
}


function position_mask(id_list, create) {
	var elem, id;
	for (var i = 0; i < id_list.length; i++) {
		id = id_list[i];
		elem = $(id);

		if (!elem) {
			break;
		}

		var top = get_pos_y(elem) - 2;
		var left = get_pos_x(elem) - 2;
		var height = elem.offsetHeight + 4;
		var width = elem.offsetWidth + 4;

		if (env.browser.name == "msie") {
			var button_top = top - 20;
			var button_left = left + width - 40;
		}
		else {
			var button_top = top - 18;
			var button_left = left + width - 44;
		}


		if (create) {
			var elem_mask = '<div id="edit_mask_' + id + '" class="edit_mask" style="top: ' + top + 'px; left: ' + left + 'px; width: ' + width + 'px; height: ' + height + 'px;"></div>';
			var elem_edit = '<div id="edit_button_' + id + '" class="edit_button" style="top: ' + top + 'px; left: ' + left + 'px;" onClick="elem_edit(\'' + id + '\');" onMouseOver="elem_hover(\'' + id + '\', true);" onMouseOut="elem_hover(\'' + id + '\', false);">Éditer</div>'

			//$('edit_mask').innerHTML += elem_mask + elem_edit;
			document.write(elem_mask + elem_edit);
		}
		else {
			$('edit_mask_' + id).style.top = top + 'px';
			$('edit_mask_' + id).style.left = left + 'px';
			$('edit_mask_' + id).style.height = height + 'px';
			$('edit_mask_' + id).style.width = width + 'px';

			$('edit_button_' + id).style.top = button_top + 'px';
			$('edit_button_' + id).style.left = button_left + 'px';
		}
	}
}



function elem_edit(id) {
	var elem = $(id);
	var width = elem.offsetWidth;
	var height = elem.offsetHeight;
	var window_size = get_window_size();

	var border = 14;
	if (height > 280) {
		border = 29;
	}

	var window_width = width;
	if (width < 310) {
		window_width = 310;
	}
	else if (width > 700) {
		window_width = 700;
	}
	width = window_width = 700
	
	window_width += border;

	var top = Math.floor((window_size.height - 280) / 2);
	var left = Math.floor((window_size.width - window_width) / 2);
	
	if (id == 'edit_faq') {
		var win = window.open('/cms/php/edit.php?i=' + escape(id) + '&f=' + escape(location.pathname) + '&w=' + width + '&h=' + height, id, "height=600,width=" + window_width + ",directories=no,left=" + left + ",top=" + top + ",location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no");
	}
	else {
		var win = window.open('/cms/php/edit.php?i=' + escape(id) + '&f=' + escape(location.pathname) + '&w=' + width + '&h=' + height, id, "height=600,width=" + window_width + ",directories=no,left=" + left + ",top=" + top + ",location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no");
	}
	win.focus();
}

function logout_hover(over) {
	if (over) {
		$('edit_logout').className = 'edit_logout_over';
	}
	else {
		$('edit_logout').className = 'edit_logout';
	}
}

function elem_hover(id, over) {
	if (over) {
		$('edit_mask_' + id).className = 'edit_mask_over';
		$('edit_button_' + id).className = 'edit_button_over';
	}
	else {
		$('edit_mask_' + id).className = 'edit_mask';
		$('edit_button_' + id).className = 'edit_button';
	}
}

function get_window_size() {
	var size = {
		width	: 0,
		height	: 0
	}

    // Non-IE
    if (window.innerWidth || window.innerHeight) {
        size.width = window.innerWidth;
        size.height = window.innerHeight;
    }
    // IE 6+ in standards compliant mode
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        size.width = document.documentElement.clientWidth;
        size.height = document.documentElement.clientHeight;
    }
    // IE 4 compatible
    else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        size.width = document.body.clientWidth;
        size.height = document.body.clientHeight;
    }
    return size;
}


function get_scroll() {
	var scroll = {
		x : 0,
		y : 0
	};

    // Netscape
    if (window.pageYOffset || window.pageXOffset) {
        scroll.y = window.pageYOffset;
        scroll.x = window.pageXOffset;
    }
    // DOM
    else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        scroll.y = document.body.scrollTop;
        scroll.x = document.body.scrollLeft;
    }
    // IE6
    else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        scroll.y = document.documentElement.scrollTop;
        scroll.x = document.documentElement.scrollLeft;
    }

    return scroll;
}

function get_pos_x(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curleft = obj.x;
	}

	return curleft;
}

function get_pos_y(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) {
		curtop = obj.y;
	}

	return curtop;
}
