/* 
 * Less critical portion of Web WT/Think Online Web Desktop Toolkit
 * 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/>.

 * Dedicated to the one that has my heart.
 * last change ver. 2009-1021
 */
function set_iframe_cover(win) {
var pane=get_content_pane(win);
var cover=document.createElement('div');
cover.className='cover';
cover.innerHTML='<p style="padding-top:3em">Unfortunately, you are using <b>Internet Explorer</b></p>';
cover.innerHTML+='<p>IE Users have to endure outdated MS IE visual engine</p>';
cover.innerHTML+='<p>Please upgrade to alternatives such as Firefox, Chrome, or Safari</p>';
pane.appendChild(cover);
pane.style.overflow='hidden';
}

function min_win(win) {
	if (all_slave[win.id]) {
		for (var loop in all_slave[win.id]) {
			var alldiv = all_slave[win.id][loop].getElementsByTagName('div');
			for (var x in alldiv) {
				if (alldiv[x] && String(alldiv[x].className).indexOf('resize') != -1) 
					alldiv[x].style.visibility='inherit';
			}
			all_slave[win.id][loop].style.visibility = 'hidden';
		}
		win.className = String(win.className).replace(' backwin', '');
	}
redraw_win(win,'min');
try {
	get_window_element(get_content_pane(win),'cover').style.visibility='inherit';
} catch (e) {;}
var num = win.id.split('_')[1];
document.getElementById('minicon_' + num).style.visibility = 'visible';
document.getElementById('minicon_' + num).style.left = (minicons.length * 122) + 'px';
(document.getElementById('minicon_' + num)).onclick = new Function("restore_win(document.getElementById('" + win.id + "'),this);");
minicons.push(document.getElementById('minicon_' + num));
  
var top_win = 'win_0';
var top_zi = 0;
for (x in all_win) {
  if (all_win[x].style.visibility == 'visible' &&
      all_win[x].style.zIndex > top_zi) {
    all_win[x].style.zIndex;
    top_win= all_win[x];
  }
}
if (top_win != 'win_0') focus_win(top_win);
}

function refresh_minicons() {
  for (var loop = 0; loop < minicons.length; loop++) {
    minicons[loop].style.left = (loop * 122) + 'px';
  }
}

function close_win(win) {
	if (all_slave[win.id]) {
		for (var x in all_slave[win.id]) {
			close_win(all_slave[win.id][x]);
		}
	}
  var temp_all_win = new Array();
  for (var ind in all_win) {
    if (all_win[ind] == win) {
      win.style.visibility='hidden';
      win.parentNode.removeChild(win);
      var minicon = document.getElementById('minicon_' + win.id.split('_')[1]);
      minicon.style.visibility='hidden';
    } else {
      temp_all_win[ind] = all_win[ind];
    }
  }
  all_win=temp_all_win;
  var top_win = 'win_0';
  var top_zi = 0;
  for (x in all_win) {
    if (all_win[x].style.visibility == 'visible' &&
        all_win[x].style.zIndex > top_zi) {
      top_zi = all_win[x].style.zIndex;
      top_win = all_win[x];
    }
  }
  if (top_win != 'win_0') focus_win(top_win);
}

function restore_win(win, icon, coords_top, coords_left, wi, he) {
  if (icon && coords_top == null) {
    var temp = new Array();
    while (minicons.length > 0) {
      var i = minicons.shift();
      if (i != icon) temp.push(i);
    }
    minicons = temp;
  }
  refresh_minicons();
  win.style.visibility = 'visible';
  if (coords_top != null) {
    win.style.top = coords_top;
    win.style.left = coords_left;
    win.style.width=wi;
    win.style.height=he;
    get_window_element(win,'titlebar').onmousedown=new Function("event", "focus_win(this.parentNode); dragStart(event, this.parentNode);");
    try {
		get_window_element(win,'max-butt').onclick=new Function("max_win(document.getElementById('" + win.id + "'));");
	} catch (e) {;}
  }
  var num = win.id.split('_')[1];
  document.getElementById('minicon_' + num).style.visibility = 'hidden';
  focus_win(win);
  redraw_win(win,'reg');
}

function max_win(win) {
  var restore_coords = new Array();
  get_titlebar(win).style.cursor='default';
  restore_coords[0]=win.style.top;
  restore_coords[1]=win.style.left;
  if (win.style.width) restore_coords[2]=win.style.width;
  else restore_coords[2]='530px';
  if (win.style.width) restore_coords[3]=win.style.height;
  else restore_coords[3]='260px';
  var all_div = win.getElementsByTagName('div');
  var max_button;
  try {
	var max_button=get_window_element(win,'max-butt');
	max_button.onclick = new Function("get_titlebar(this.parentNode).style.cursor='move';restore_win(document.getElementById('" + win.id + "'), document.getElementById('minicon_" + win.id.split('_')[1] + "'), '" + restore_coords[0] + "', '" + restore_coords[1] + "', '" + restore_coords[2] + "', '" + restore_coords[3] + "');");
} catch (e) {;}
get_titlebar(win).onmousedown='';
  if (!(magic['max_top'])) magic['max_top'] = '0px';
  if (!(magic['max_right'])) magic['max_right'] = '0px';
  if (!(magic['max_left'])) magic['max_left'] = '0px';
  if (!(magic['max_bottom'])) magic['max_bottom'] = '0px';
	  win.style.top=magic['max_top'];
  win.style.right=magic['max_right'];
  win.style.left=magic['max_left'];
  win.style.bottom=magic['max_bottom'];

  try {
    win.style.width=window.innerWidth-(parseInt(magic['max_left'])+parseInt(magic['max_right']+5));
    win.style.height=window.innerHeight-parseInt(magic['max_bottom']);
  } catch (x) {
    try {
      win.style.width=document.body.offsetWidth-(parseInt(magic['max_left'])+parseInt(magic['max_right']+5));
      win.style.height=document.body.offsetHeight-parseInt(magic['max_bottom']);
    } catch (e) {
      try {
        win.style.width=document.documentElement.clientWidth-(parseInt(magic['max_left'])+parseInt(magic['max_right']+5));
        win.style.height=document.documentElement.clientHeight-parseInt(magic['max_bottom']);
      } catch (c) { 
        alert('No viewport data; maximizing will likely to malfunction');
        win.style.width=763;
        win.style.height=580;
      }
    }
  }

  redraw_win(win,'max');
}

function set_content(win,content) {
	if (win.new_pane==false) return win;
	else win.new_pane=false;
get_content_pane(win).innerHTML=content;
get_content_pane(win).style.backgroundImage='';
return win;
}

function default_template() {
	var result=document.createElement('div');
    var titlebar=document.createElement('div');
    titlebar.id='title_0';
    titlebar.innerHTML='Template Window';
    titlebar.style.position='absolute';
titlebar.className='titlebar';
    titlebar.style.width='auto';
	titlebar.style.top='5px';
	titlebar.style.left='5px';
	titlebar.style.right='5px';
    titlebar.style.height='2em';
    titlebar.style.zIndex='1';
	if (magic['bg_colour'])titlebar.style.backgroundColor=magic['bg_colour'];
    result.appendChild(titlebar);
	var close_button=document.createElement('div');
	close_button.style.fontSize='x-small';
	close_button.style.position='absolute';
	close_button.style.right='12px';
	close_button.style.top='8px';
	close_button.style.width='14px';
	close_button.style.height='14px';
	close_button.style.backgroundImage='url(images/be-close.gif)';
	close_button.style.zIndex=20;
	close_button.className='close-butt';
	close_button.onmouseover = new Function("this.style.backgroundImage='url(images/be-close-on.gif)'");
	close_button.onmouseout = new Function("this.style.backgroundImage='url(images/be-close.gif)'");
    result.appendChild(close_button);
	var max_button=document.createElement('div');
	max_button.style.fontSize='x-small';
	max_button.style.position='absolute';
	max_button.style.right='30px';
	max_button.style.top='8px';
	max_button.style.width='14px';
	max_button.style.height='14px';
	max_button.style.backgroundImage='url(images/be-max.gif)';
	max_button.style.zIndex=20;
	max_button.className='max-butt';
	max_button.onmouseover = new Function("this.style.backgroundImage='url(images/be-max-on.gif)'");
	max_button.onmouseout = new Function("this.style.backgroundImage='url(images/be-max.gif)'");
    result.appendChild(max_button);
	var min_button=document.createElement('div');
	min_button.style.fontSize='x-small';
	min_button.style.position='absolute';
	min_button.style.right='48px';
	min_button.style.top='8px';
	min_button.style.width='14px';
	min_button.style.height='14px';
	min_button.style.backgroundImage='url(images/be-min.gif)';
	min_button.style.zIndex=20;
	min_button.className='min-butt';
	min_button.onmouseover = new Function("this.style.backgroundImage='url(images/be-min-on.gif)'");
	min_button.onmouseout = new Function("this.style.backgroundImage='url(images/be-min.gif)'");
    result.appendChild(min_button);
    var pane = document.createElement('div');
    pane.innerHTML='template';
pane.className='pane';
    pane.style.position='absolute';
    pane.style.top='30px';
    pane.style.left='5px';
pane.style.right='5px';
pane.style.bottom='5px';
if (magic['bg_lighter']){
	pane.style.backgroundColor=magic['bg_lighter'];
}
pane.style.overflow='scroll';
pane.style.height='auto';
pane.style.width='auto';
    result.appendChild(pane);
result.style.width='400px';
result.style.height='300px';
result.style.position='absolute';
var resizer_se = document.createElement('div');
resizer_se.className='resize-se';
resizer_se.onmousedown=new Function("focus_win(this.parentNode);resize_start(event, this.parentNode, 's');");
var resizer_sw = resizer_se.cloneNode(true);
resizer_sw.className='resize-sw';
var resizer_nw = resizer_se.cloneNode(true);
resizer_nw.className='resize-nw';
var resizer_ne = resizer_se.cloneNode(true);
resizer_ne.className='resize-ne';
var resizer_n = resizer_se.cloneNode(true);
resizer_n.className='resize-n';
var resizer_w = resizer_se.cloneNode(true);
resizer_w.className='resize-w';
var resizer_e = resizer_se.cloneNode(true);
resizer_e.className='resize-e';
var resizer_s = resizer_se.cloneNode(true);
resizer_s.className='resize-s';
result.appendChild(resizer_n);
result.appendChild(resizer_s);
result.appendChild(resizer_e);
result.appendChild(resizer_w);
result.appendChild(resizer_se);
result.appendChild(resizer_sw);
result.appendChild(resizer_ne);
result.appendChild(resizer_nw);
return result;
}

function set_slave_to(master_id, slave_win) {
var mb = get_window_element(slave_win, 'max-butt');
mb.parentNode.removeChild(mb);
mb = get_window_element(slave_win, 'min-butt');
mb.parentNode.removeChild(mb);
mb = get_window_element(slave_win, 'close-butt');
mb.parentNode.removeChild(mb);
get_titlebar(slave_win).style.fontSize = 'x-small';
var cp = get_content_pane(slave_win);
cp.style.top = (parseInt(cp.style.top) - 10) + 'px';
cp.style.height = (parseInt(cp.style.height) + 10) + 'px';
if (!(all_slave[master_id])) all_slave[master_id] = new Array();
all_slave[master_id].push(slave_win);
slave_win.setAttribute('master', master_id);
}

function set_sticky(window) {
	all_sticky.push(window);
	return window;
}

function get_title(obj) {
	if (obj.className=='window') return get_title(get_titlebar(obj));
	else if (obj.nodeType==3)return obj.nodeValue;
	else {
		var result='';
		for (var x=0;x<obj.childNodes.length;x++) {
			result=result+get_title(obj.childNodes[x]);
		}
		return result;
	}
}
