/*
 * Think Online Web Desktop/Think Desktop scripts; portion Mike Hall of Brainjar.com
 * Copyright (C) 2009 Andy Tsai 薒昭安

 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.

 * Web Menu Toolkit/Think Desktop script; inspired by Mike Hall of Brainjar.com but use <div> and <span> instead of <a>
 * Use of it must include this notice and acknowledgement of derivative work
 * Dedicated to the one that has my heart.
 * last change ver. 2009-1021
 */
var menu_top = new Array();
var all_menu = new Array();
var clicked=false;
var bar;
var locked;

try {
	document.addEventListener("mousedown", pageMousedown, true);
} catch (e) { document.onmousedown = pageMousedown; }

function pageMousedown(event) {
  var el;
  if (clicked==false) return;

  try { el = window.event.srcElement; }
  catch (e) { el = (event.target.tagName ? event.target : event.target.parentNode);}

  try { if (el.parentNode == bar) return; }
  catch (e) {;}

  // If the element is not part of a menu, reset and clear the active
  // button.
  if (el.className != 'dropdown' && el.parentNode.className != 'dropdown') {
    for (x in all_menu) {
      if (all_menu[x]!=el) {
        try {
          all_menu[x].menu.style.visibility='hidden';
        } catch (e) {;}
        all_menu[x].style.backgroundColor=bar.style.backgroundColor;
        all_menu[x].style.color='black'
      }
    }
	clicked=false;
  }
}
var magic = new Array();
magic['menu_colour']='#64a460';
function set_as_menubar(top) {
	bar = top;
	bar.style.position = 'absolute';
	bar.style.left = '0px';
	bar.style.top = '0px';
	if (magic['menu_colour']) {
		bar.style.border = magic['menu_colour']+' 1px outset';
		bar.style.backgroundColor=magic['menu_colour'];
	} else {
		bar.style.border = 'yellow 1px outset';
		bar.style.backgroundColor='yellow';
	}
	bar.style.padding = '5px';
	bar.style.width = '100%';
}
function getPageOffsetLeft(el) {
  var x;
  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);
  return x;
}

function getPageOffsetTop(el) {
  var y;
  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);
  return y;
}

function set_tearable(menuid_text) {
	var target = document.getElementById(menuid_text + '_slide');
	var txt = '';
	for (var x = 0; x < 30; x++) txt += ' &nbsp;';
	add_menu(target.id + '_tear', txt,document.getElementById(menuid_text));
	document.getElementById(target.id + '_tear').className='tear';
	var txt = target.innerHTML;
	var cb = new Function("action_default();var win = set_sticky(show_win_by_paneid('" + menuid_text + "' + '_tore', '" + document.getElementById(menuid_text).innerHTML + "' + ' Menu'));tear(document.getElementById('" + menuid_text + "' + '_slide'),win);");
	document.getElementById(target.id + '_tear').onclick = cb;
}

function tear(menu_slide, win) {
	get_content_pane(win).style.backgroundImage='';
	var butt = get_window_element(win, 'max-butt');
	butt.parentNode.removeChild(butt);
	var butt = get_window_element(win, 'min-butt');
	butt.parentNode.removeChild(butt);
	win.style.left=menu_slide.style.left;
	get_content_pane(win).style.overflow='auto';
	win.style.width='200px';
	win.style.top=parseInt(menu_slide.style.top)+15+'px';
	var txt = '';
	var all = menu_slide.getElementsByTagName('span');
	for (var loop = 1; loop < all.length; loop++) {
		if (all[loop].innerHTML == 'more...') continue;
		else {
			var div = document.createElement('div');
			div.className = 'button';
			if (all[loop].innerHTML != undefined) {
				div.innerHTML = all[loop].innerHTML;
				div.onclick = all[loop].onclick;
				get_content_pane(win).appendChild(div);
			}
		}
	}
	redraw_win(win, 'reg');
}

function add_menu(id_text, item_text, onto) {
	// No "onto" assume the bar
	if (onto == null) onto = bar;
	var item = document.createElement('span');
	item.style.backgroundColor = bar.style.backgroundColor;
	item.className = 'menu';
	item.onmouseover = new Function("this.style.backgroundColor='#000080';this.style.color='white';set_menu_active(this);");
	item.onmouseout = new Function("if (!clicked) { this.style.backgroundColor=bar.style.backgroundColor;this.style.color='black'}");
	item.innerHTML = item_text;
	item.style.cursor = 'default';
	item.id = id_text;
	if (onto == bar) {
		onto.appendChild(item);
		var slide_x = item.offsetLeft;
		var slide_y = item.offsetTop + item.offsetHeight-1;
		var slide = document.createElement('div');
		slide.className = 'dropdown';
		slide.id = id_text + '_slide';
		slide.style.top = slide_y+'px';
		slide.style.left = slide_x + 'px';
		if (magic['menu_colour']) slide.style.borderColor = magic['menu_colour'];//bar.style.border;
		slide.style.borderWidth = '1px';
		slide.style.borderStyle = 'outset';
		slide.style.backgroundColor = bar.style.backgroundColor;
		item.menu=slide;
		item.onclick = new Function("clicked=!clicked;if (clicked==true) this.menu.style.visibility='visible'; else this.menu.style.visibility='hidden';");
		bar.parentNode.appendChild(slide);
		menu_top[id_text] = item;
	} else if (onto.parentNode.className=='dropdown') {
		if (onto.menu) {
		} else {
			onto.innerHTML = onto.innerHTML+'<span style="color:red;text-align:right;margin-left:10px">more...</span>';
			var slide = document.createElement('div');
			slide.className='dropdown';
			slide.style.backgroundColor = bar.style.backgroundColor;
			slide.style.border = bar.style.border;
			slide.id=onto.id+"_slide";
			slide.root=onto.id;
			slide.style.left=getPageOffsetLeft(onto)+parseInt(onto.offsetWidth)-4+'px';
			slide.style.top=getPageOffsetTop(onto)+'px';
			slide.submenu='yes';
			slide.style.visibility='hidden';
			bar.parentNode.appendChild(slide);
			onto.menu=slide;
			slide.style.position='absolute';
		}
		onto.menu.appendChild(item);
		item.onclick=menu_alert;
		item.onmouseover = new Function("set_highlight(this, this.parentNode);document.getElementById(this.parentNode.root).style.color='white';document.getElementById(this.parentNode.root).style.backgroundColor='#000080'");
		item.onmouseout = new Function("");
		item.style.margin='0px';
		item.style.display='block';
		onto.onmouseover = new Function("set_highlight(this, this.menu)");
	} else {
		item.style.display='block';
		item.onclick=menu_alert;
		onto.menu.appendChild(item);
		item.onmouseover = new Function("set_highlight(this)");
		item.onmouseout = new Function("");
		item.style.margin='0px';
	}
	all_menu[id_text]=item;
}

function register_action(id_text, callback_fn) {
	all_menu[id_text].onclick = callback_fn;
}

function togglelock(id) {
  if (docked[id]) {
    docked[id]=false;
    document.getElementById(id).onmousedown = dragStart;
  } else {
    docked[id]=true;
    document.getElementById(id).onmousedown = null;
  }
}

function set_menu_active(el) {
	for (x in menu_top) {menu_top[x].menu.style.visibility='hidden';}
	if (clicked) {
		el.menu.style.visibility='visible';
		for (x in all_menu) {
			if (all_menu[x]!=el) {
				try {
					all_menu[x].menu.style.visibility='hidden';
				} catch (e) {;}
				all_menu[x].style.backgroundColor=bar.style.backgroundColor;
				all_menu[x].style.color='black'
			}
		}
	} else el.menu.style.visibility='hidden';
}

function set_highlight(el, lo) {
	if (locked) locked.style.visibility='visible';
	locked = lo;
	for (x in all_menu) {
		if (all_menu[x].parentNode != bar) {
			all_menu[x].style.color='black';
			all_menu[x].style.backgroundColor=bar.style.backgroundColor;
			try {
				if (all_menu[x].menu != locked) all_menu[x].menu.style.visibility='hidden';
			} catch (e) {;}
		}
	}
	el.style.color='white';
	el.style.backgroundColor='#000080';
	try {
		el.menu.style.visibility='visible';
		locked.style.visibility='visible';
	} catch (e) {;}
}

function menu_alert() {
	action_default();
	alert(this.innerHTML);
}

function action_default() {
	for (x in all_menu) {
		all_menu[x].style.color='black';
		all_menu[x].style.backgroundColor=bar.style.backgroundColor;
		try {
			all_menu[x].menu.style.visibility='hidden';
		} catch (e) {
		}
	}
	locked=false;
	clicked=false;
}

function insert_separator(id_text) {
document.getElementById(id_text).style.borderBottom='black 4px double';
}

function all_top_id() {
var result='';for (var x in menu_top) result = result+x+' ';return result;
}

function all_menu_id() {
var result='';for (var x in all_menu) result = result+x+' ';return result;
}

function span_exists(div, el) {
var all=div.getElementsByTagName('span');
for (var y in all) if (all[y]==all) return true;
return false;
}
