var pendingAction = false;

function initializar_mapa() {
	/* http://maps.google.com.br/maps?hl=pt-BR&ll=-23.558913,-46.720219&spn=0.022738,0.052314&z=15 */
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map_canvas"), {size: new GSize(600, 400), draggableCursor:"move", draggingCursor:"crosshair" });
		map.setCenter(new GLatLng(-23.558913, -46.720219), 15);
		map.setUIToDefault();
		map.enableRotation();
				
		var polygon = new GPolygon([
			new GLatLng(-23.558913, -46.720219 - 0.0085),
			new GLatLng(-23.558913 + 0.0040, -46.720219),
			new GLatLng(-23.558913, -46.720219 + 0.0085),
			new GLatLng(-23.558913 - 0.0040, -46.720219),
			new GLatLng(-23.558913, -46.720219 - 0.0085)
			], "#f33f00", 5, 1, "#ff0000", 0.2
		);
		
		map.addOverlay(polygon);
	}
}

function getDefaultItemInfo(){
	var info = new Object();
	
	info.id_item = parseInt(document.getElementById('defaultitem').value);
	info.id_item_pai = document.getElementById('defaultitem_parent').value;
	info.id_item_pai = (info.id_item_pai != 'null' ? info.id_item_pai : parseInt(info.id_item_pai))
	
	return info;
}

function body_onload() {
	load_dom();
	
	/* A porcaria do internet explorer não tem o método <Array>.indexOf(). */
	if(!Array.indexOf){
		Array.prototype.indexOf = function(obj){
			for(var i=0; i<this.length; i++){
				if(this[i]==obj)
				return i;
			}
			return -1;
		}
	}
	
	dolayout();
	loading_all_hide();
	var info = getDefaultItemInfo();
	dropbox_clicked = info.id_item_pai;
	menu_clicked(info.id_item, null);
}

function body_unload() {
// 	GUnload();
}

function body_onkeydown(event) {
	if(isEscapeKey(event))
		hide_dropdown_menus();
}

function hide_dropdown_menus() {
	for (var c = 0; c < dropdown_contentdivPARSED.length; c++) {
		var dropbox = document.getElementById('menu_item_' + dropdown_contentdivPARSED[c] + '_dropbox');
		if(dropbox != null){
// 			logging.innerHTML += dropbox.id + "<hr class='lightgray' /><br />"
			dropbox.style.display = 'none';
		}
	}
	dropdown_hide_layer.style.display = 'none';
}

function loading_all_hide() {
	loading_all.style.display = 'none';
}

function loading_all_show() {
	loading_all.style.display = 'block';
}

function experimento(menuindex){
	var menu_item_dropbox = document.getElementById('menu_item_' + menuindex + '_dropbox');
	if(menu_item_dropbox != null) {
		var menu_item = document.getElementById('menu_item_' + menuindex);
		var menu_compstyle = myGetComputedStyle(menu, null);
		menu_item_dropbox.style.top = parseInt(menu_item.offsetTop) + parseInt(getElementHeight(menu_compstyle, menu)) + dropdownlist_vertical_offset + 'px';
		menu_item_dropbox.style.left = menu_item.offsetLeft + menu_item.offsetParent.offsetLeft;
		menu_item_dropbox.style.display = 'block';
		dropdown_hide_layer.style.display = 'block';
		return true;
	} else {
		return false;
	}
}

function dropdown_hide_layer_onmousedown(event) {
	hide_dropdown_menus();
}

function menu_clicked(menuindex, event) {
	if (pendingAction)
		return;
	
	e = window.event ? window.event : event;
	if (e != null) {
		e.cancelBubble = true;
		if (e.stopPropagation)
			e.stopPropagation();
	}
	
	hide_dropdown_menus();
 	displaying_dropbox = experimento(menuindex);
	
	if (extras_item_selected != null) {
		extras_unselect();
	}
	
	if(!displaying_dropbox) {
		onclick_item(menuindex);
		/* muda-se a aparência do item de menu que acaba de ser selecionado 
		e do que estava selecionado anteriormente */
		if (menu_item_selected != null)
			menu_item_selected.style.backgroundColor = menu_item_unselected_color;
		menu_item_selected = document.getElementById('menu_item_' + menuindex);
		if (menu_item_selected != null) {
			if (!checkVersion())
				menu_item_selected.style.backgroundColor = menu_item_selected_color;
			else
				menu_item_selected.style.backgroundColor = menu_item_selected_color_ie;
		}
		
		/* muda-se o conteúdo que exibido no corpo_nucleo_mural */
		if (corpo_nucleo_mural_conteudo_visible != null)
			corpo_nucleo_mural_conteudo_visible.style.display = 'none';
		corpo_nucleo_mural_conteudo_visible = document.getElementById('corpo_conteudo_' + menuindex);
		if (corpo_nucleo_mural_conteudo_visible != null)
			corpo_nucleo_mural_conteudo_visible.style.display = 'block';
		
		refreshlayout('extras,rodape,log');
		
		if (dropbox_clicked != null) {
			if (isDescendant(document.getElementById('menu_item_' + dropbox_clicked + '_dropbox'), menu_item_selected)) {
// 				alert(1)
				if (!checkVersion())
					document.getElementById('menu_item_' + dropbox_clicked).style.backgroundColor = menu_item_selected_color;
				else
					document.getElementById('menu_item_' + dropbox_clicked).style.backgroundColor = menu_item_selected_color_ie;
				
				if (dropbox_selected != null) {
					document.getElementById('menu_item_' + dropbox_selected).style.backgroundColor = menu_item_unselected_color;
				}
				
				dropbox_selected = dropbox_clicked;
			} else {
				if (dropbox_selected != null)
					document.getElementById('menu_item_' + dropbox_selected).style.backgroundColor = menu_item_unselected_color;
				dropbox_selected = null;
			}
			dropbox_clicked = null;
		} else {
			if (dropbox_selected != null)
				document.getElementById('menu_item_' + dropbox_selected).style.backgroundColor = menu_item_unselected_color;
			dropbox_selected = null;
		}
	} else {
		dropbox_clicked = menuindex
	}
	
	return false;
}

function dropitem_clicked(id_texto){
	if(drop_item_selected != null){
		drop_item_selected.style.backgroundColor = menu_item_unselected_color;
		drop_item_selected = window.event.srcElement;
		if (!checkVersion())
			drop_item_selected.style.backgroundColor = menu_item_selected_color;
		else
			drop_item_selected.style.backgroundColor = menu_item_selected_color_ie;
	}
}

function load_and_show_item_content(id_item){
	var idxItem = itensCarregados.indexOf(parseInt(id_item));
	if (idxItem == -1) {
		loading_all_show();
		pendingAction = true;
		requestOperation('load item text', '&iditem=' + id_item);
	} else {
		showTextDiv(tiposDosItensCarregados[idxItem], id_item, null);
		dolayout();	
	}
}

function onclick_item(id_item) {
	if (pendingAction)
		return;
	/* muda-se a aparência do item de menu que acaba de ser selecionado 
	e do que estava selecionado anteriormente */
	if (menu_item_selected != null)
		menu_item_selected.style.backgroundColor = menu_item_unselected_color;
	if (dropbox_selected != null) {
		document.getElementById('menu_item_' + dropbox_selected).style.backgroundColor = menu_item_unselected_color;
		dropbox_selected = null;
	}
	menu_item_selected = document.getElementById('menu_item_' + id_item);
// 	alert('menu_item_' + id_item + ' = ' + menu_item_selected.toString());
	if (menu_item_selected != null) {
		if (!checkVersion())
			menu_item_selected.style.backgroundColor = menu_item_selected_color;
		else
			menu_item_selected.style.backgroundColor = menu_item_selected_color_ie;
	}
	load_and_show_item_content(id_item);
	hide_dropdown_menus();
}

function menuequipe_clicked(menuequipeindex) {
	if (menuequipe_item_selected != null) {
		menuequipe_item_selected.style.backgroundColor = menuequipe_item_unselected_color;	
		corpoequipe_conteudo_visible.style.display = 'none';
	}
	
	var menuequipeitem_clicked = document.getElementById('menuequipe_item_' + menuequipeindex);
	
	if (menuequipe_item_selected == menuequipeitem_clicked) {
		menuequipe_unselect();
	} else {
		menuequipe_item_selected = document.getElementById('menuequipe_item_' + menuequipeindex);
		menuequipe_item_selected.style.backgroundColor = menuequipe_item_selected_color;
		corpoequipe_conteudo_visible = document.getElementById('corpoequipe_conteudo_' + menuequipeindex);
		corpoequipe_conteudo_visible.style.display = 'block';
	}
	
	refreshlayout('extras,rodape,log');
}

function extras_clicked(extrasindex) {
	if (extras_item_selected != null) {
		extras_item_selected.style.backgroundColor = extras_item_unselected_color;	
		corpo_nucleo_conteudo_visible.style.display = 'none';
	}
	
	var extrasitem_clicked = document.getElementById('extras_item_' + extrasindex);
	
	if (extras_item_selected == extrasitem_clicked) {
		extras_unselect();
	} else {
		menu_item_selected.style.backgroundColor = menu_item_unselected_color;
		extras_item_selected = document.getElementById('extras_item_' + extrasindex);
		extras_item_selected.style.backgroundColor = extras_item_selected_color;
		corpo_nucleo_mural.style.display = 'none';
		mural.style.display = 'none';
		parceiros.style.display = 'none';
		corpo_nucleo_conteudo_visible = document.getElementById('extrasbody_conteudo_' + extrasindex);
		corpo_nucleo_conteudo_visible.style.display = 'block';
		corpo_nucleo.style.display = 'block';
	}
	
	dolayout();
}

function extras_unselect() {
	if(!checkVersion())
		menu_item_selected.style.backgroundColor = menu_item_selected_color;
	else
		menu_item_selected.style.backgroundColor = menu_item_selected_color_ie;
	
	if (extras_item_selected != null) {
		extras_item_selected.style.backgroundColor = extras_item_unselected_color;	
		corpo_nucleo_conteudo_visible.style.display = 'none';
		corpo_nucleo.style.display = 'none';
		corpo_nucleo_mural.style.display = 'block';
		mural.style.display = 'block';
		parceiros.style.display = 'block';
		
	}
	corpo_nucleo_conteudo_visible = null;
 	extras_item_selected = null;
}

function menuequipe_unselect() {
	if (menuequipe_item_selected != null) {
		menuequipe_item_selected.style.backgroundColor = menuequipe_item_unselected_color;	
		corpoequipe_conteudo_visible.style.display = 'none';
	}
	corpoequipe_conteudo_visible = null;
 	menuequipe_item_selected = null;
}

function onmouseover_menuitem(menuindex) {
	var item = document.getElementById('menu_item_' + menuindex);
// 	if(item != menu_item_selected)
	if (item != menu_item_selected && item != document.getElementById('menu_item_' + dropbox_selected))
		if (!checkVersion())
			item.style.backgroundColor = menu_item_mouseover_color;
		else
			item.style.backgroundColor = menu_item_mouseover_color_ie;
// 	else if(extras_item_selected != null)
// 		item.style.backgroundColor = menu_item_selected_color;
}

function onmouseout_menuitem(menuindex) {
	var item = document.getElementById('menu_item_' + menuindex);
	if (item != menu_item_selected && item != document.getElementById('menu_item_' + dropbox_selected))
		item.style.backgroundColor = menu_item_unselected_color;
}

function onmouseover_menu_subitem(event) {
// 	alert('onmouseover_menu_subitem');
	var e = (event == null ? window.event : event);
	if(e != null) {
		if(e.target)
// 			e.target.style.backgroundColor = 'rgba(0,0,0,0.15)';
			e.target.style.backgroundColor = 'rgb(50,50,100)';
		else if (e.srcElement)
// 			e.srcElement.style.backgroundColor = 'rgba(0,0,0,0.15)';
			e.srcElement.style.backgroundColor = 'rgb(50,50,100)';
	}
}

function onmouseout_menu_subitem(event) {
	var e = window.event ? window.event : event;
	if(e != null) {
		if(e.target) {
			if (e.target != menu_item_selected)
				e.target.style.backgroundColor = 'transparent';
		} else if (e.srcElement) {
			if (e.srcElement != menu_item_selected)
				e.srcElement.style.backgroundColor = 'transparent';
		}
	}
}

function onmouseover_menuequipeitem(menuequipeindex) {
	var item = document.getElementById('menuequipe_item_' + menuequipeindex);
	if(item != menuequipe_item_selected)
		item.style.backgroundColor = menuequipe_item_mouseover_color;
}

function onmouseover_extrasitem(extrasindex) {
	var item = document.getElementById('extras_item_' + extrasindex);
	if(item != extras_item_selected)
		item.style.backgroundColor = extras_item_mouseover_color;
}

function onmouseout_extrasitem(extrasindex) {
	var item = document.getElementById('extras_item_' + extrasindex);
	if(item != extras_item_selected)
		item.style.backgroundColor = extras_item_unselected_color;
}

function onmouseout_menuequipeitem(menuequipeindex) {
	var item = document.getElementById('menuequipe_item_' + menuequipeindex);
	if(item != menuequipe_item_selected)
		item.style.backgroundColor = menuequipe_item_unselected_color;
}

function control_onchange(property) {
	if(property == 'wcorpo') {
		wcorpo = parseInt(document.getElementById('wcorpo').value);
		dolayout();
	} else if(property == 'wmural') {
		wmural = parseInt(document.getElementById('wmural').value);
		dolayout();
	} else if(property == 'pagetop') {
		pagetop = parseInt(document.getElementById('pagetop').value);
		dolayout();
	} else if(property == 'pageleft') {
		pageleft = parseInt(document.getElementById('pageleft').value);
		dolayout();
	} else if(property == 'blockhspacing') {
		blockhspacing = parseInt(document.getElementById('blockhspacing').value);
		dolayout();
	} else if(property == 'blockvspacing') {
		blockvspacing = parseInt(document.getElementById('blockvspacing').value);
		dolayout();
	} else if(property == 'pagehmargin') {
		pagehmargin = parseInt(document.getElementById('pagehmargin').value);
		dolayout();
	} else if(property == 'pagevmargin') {
		pagevmargin = parseInt(document.getElementById('pagevmargin').value);
		dolayout();
	}
}

function _onload(onload) {
// 	logging.innerHTML += 'loaded' + "<hr class='lightgray' /><br />"
	CKeditorIsLoaded = true;
	onload();
}

function loadCKeditor(onload){
	var headID = document.getElementsByTagName("head")[0];         
	var newScript = document.createElement("script");
	newScript.type = "text/javascript";
	newScript.src = "http://localhost/cursinhodapsico/ckeditor/ckeditor.js";
	newScript.onload = _onload;
	headID.appendChild(newScript);
}

function onLogin() {
	if(!CKeditorIsLoaded)
		loadCKeditor();
}

function commonHighlight(idIndex, onOff){
 	var elemento = document.getElementById('mural_item_' + idIndex);
	var estilo = myGetComputedStyle(elemento, null);
	var cores = estilo.backgroundColor;
  	cores = cores.substring(cores.indexOf('(') + 1, cores.indexOf(')'));
	cores = cores.split(',');
	
}
