/*
Descrição: Criação e manipulação das janelas
Criado em: 22/03/2009
Autor: Hédi Carlos Minin - hedicarlos@gmail.com
*/

var z_index = 3000;
var scroll_top = 0;
var mouse_x = 0;
var mouse_y = 0; 
var dif_x = 0
var dif_y = 0;
var user_height = 0;
var user_width = 0;
var window_id = false;
var window_active = false;
var last_window = false;
var window_position = Array();
var window_mode = 1; //0 = normal, 1 = performance

function moveMouse(e){
    if(document.all){
          mouse_x = event.clientX;
          mouse_y  = event.clientY;
    } else {
          mouse_x = e.clientX;
          mouse_y = e.clientY;
    }
}

function getBodySize(){
		var tamy = document.documentElement.clientHeight;
		var tamx = document.documentElement.clientWidth;
		//topo = document.documentElement.scrollTop;
		
		if (window.opera){ 
			tamy = document.body.clientHeight;
			tamx = document.body.clientWidth;
			//topo = document.body.scrollTop;
		}
		return[tamx,tamy];
}

function Window(){
	this.id = 0;
	this.width = 200;
	this.height = 300;
	this.top = 0;
	this.left = 0;
	this.maximizeButton = true;
	this.minimizeButton = true;
	this.closeButton = true;
	this.inner = '';
	this.title = 'New window';
	this.resize = true;
	this.move = true;
	
	this.Create = function(){
		this.id = 'w' +this.id;
		
		if(!document.getElementById(this.id)){
			
			var criar = document.createElement('div');
			criar.setAttribute('id',this.id);
			document.body.appendChild(criar);
			
			var new_window = document.getElementById(this.id);
			
			new_window.className = 'window';
			
			new_window.onmouseup = function(){
				FocusWindow(this);
			}
			
			//new_window.ondblclick = function(){
			//	MaximizeWindow(this);
			//}
			
			var layout = null;
			
			layout = '<div class="window_top">';
			
				layout += '<div';
				if(this.move == true){
					layout += ' onmousedown="CaptureWindow(this)" title="Mover janela"';	
				}
				layout += ' class="window_title" id="t' +this.id+ '">' +this.title+ '</div>';
				
				layout += '<div class="window_options">';
	
				if(this.minimizeButton == true){
					layout += '<a href="javascript:void(0)" onclick="MinimizeWindow(this)" class="minimize_buttom">&nbsp;</a>';	
				}
	
				if(this.maximizeButton == true){
					layout += '<a href="javascript:void(0)" onclick="MaximizeWindow(this)" class="maximize_buttom" id="m' +this.id+ '">&nbsp;</a>';	
				}
				
				if(this.closeButton == true){
					layout += '<a href="javascript:void(0)" onclick="CloseWindow(this)" class="close_buttom">&nbsp;</a>';	
				}

			layout += '</div>';
			
			layout += '</div>';
			layout += '<div class="window_body">' +this.inner+ '</div>';
			
			if(this.resize == true){
				layout += '<div class="window_bottom" onmousedown="CaptureResizeWindow(this)" title="Redimensionar janela"></div>';
			}
			
			new_window.innerHTML = layout;
			
			
			if(this.top == 0){ this.top = Math.round(Math.random() * 100 + 80); }
			if(this.left == 0){ this.left = Math.round(Math.random() * 100 + 80); }
			
			new_window.style.top = this.top +'px';
			new_window.style.left = this.left +'px';
			
			new_window.style.height = this.height +'px';
			new_window.style.width = this.width +'px';
			
			window_position[this.id] = Array(this.top,this.left,this.height,this.width);
			
			new_window.style.display = 'block';
			
			//cria link barra status
			var linkStatus = '<a href="javascript:void(0)" onclick="RestaureWindow(this)" id="lb' +this.id+ '" title="' +this.title+ '">' +this.title+ '</a>';
			document.getElementById('window_bar').innerHTML += linkStatus;
			
			return new_window;

		}else{
			return document.getElementById(this.id);	
		}
	}
}

function FocusWindow(wd){
	z_index++;
	wd.style.zIndex = z_index;	
	document.getElementById('lb' +wd.getAttribute('id')).style.borderColor = '#999';
	wd.style.visibility = 'visible';
	
	document.title = document.getElementById('t' +wd.getAttribute('id')).innerHTML;
}

function CaptureWindow(wd){
	var main_window = wd.parentNode.parentNode;
	var style = main_window.style;
	var id = main_window.getAttribute('id');
	
	FocusWindow(main_window);

	if((parseInt(style.top) != 29) || (parseInt(style.left) != 0)){	

		window_id = id;
		if(window_mode == 0){
			dif_y = mouse_y - parseInt(style.top);	
			dif_x = mouse_x - parseInt(style.left);	
			window_active = main_window;
		}else{
			dif_y = mouse_y - (parseInt(style.top) - 29);	
			dif_x = mouse_x - parseInt(style.left);	
			window_active = CloneWindow(main_window);	
		}
		
		document.onmousemove =  MoveWindow;
	}
	
	document.onmousedown = function(){
		return false;
	}	
    document.onselectstart = function(){
	 	return false;  
   }	
}

function CloneWindow(obj){
	var div = obj.cloneNode(false);
	div.innerHTML = '';
	div.setAttribute('id','temp');
	document.body.appendChild(div);
	
	z_index++;
	div.style.zIndex = z_index;
	div.style.background = 'none';
	div.style.cursor = 'default';
	div.style.top = (parseInt(div.style.top) - 29) +'px';
	div.style.height = (parseInt(div.style.height) + 35) +'px';
	div.style.border = '2px dashed #999';
	div.style.backgroundImage = 'url(msnnovo/css/background_move.gif)';
	return div;
}	

function MoveWindow(e){
	moveMouse(e);
	if(window_id != false){
		var style = window_active.style;
		
		style.top = (mouse_y - dif_y) +'px';
		style.left = (mouse_x - dif_x) +'px';
	}
}

function ReleaseWindow(){
	if(window_id != false){
		if(window_mode != 0){
			var window_move = window_active.style;
			var main_window = document.getElementById(window_id);
			
			main_window.style.top = (parseInt(window_move.top) + 29) +'px';	
			main_window.style.left = parseInt(window_move.left) +'px';	
			main_window.style.width = parseInt(window_move.width) +'px';	
			main_window.style.height = (parseInt(window_move.height) - 35) +'px';	
			
			var style = main_window.style;
			window_position[window_id] = Array(parseInt(style.top),parseInt(style.left),parseInt(style.height),parseInt(style.width));
			
			document.body.removeChild(window_active);
			FocusWindow(main_window);
		}else{
			var style = window_active.style;
			window_position[window_id] = Array(parseInt(style.top),parseInt(style.left),parseInt(style.height),parseInt(style.width));
		}
	}
	
	window_id = false;	
	document.onmousedown = function(){
		return true;
	}
    document.onselectstart = function(){
	 	return true;  
   }	
}

function CloseWindow(obj){

	var main_window = obj.parentNode.parentNode.parentNode;

	StopObjectPlayer(main_window.getAttribute('id'));

	RemoveWindowBar(main_window.getAttribute('id'));
	document.body.removeChild(main_window);	
	
	window_id = false;
	last_window = false;
	window_active = false;
	document.title = 'Chat espirita lema';
	
}

function CloseWindowById(id){
	
	if(document.getElementById('w' +id)){
		var main_window = document.getElementById('w' +id);
		
		StopObjectPlayer('w' +id);
	
		RemoveWindowBar('w' +id);
		document.body.removeChild(main_window);	
		
		
		window_id = false;
		last_window = false;
		window_active = false;
		document.title = 'Chat espirita Lema';
	}
}

function StopObjectPlayer(id){
	if(document.getElementById('MPOBJ' +id)){
		document.getElementById('MPOBJ' +id).controls.stop();	
	}
}

function RemoveWindowBar(id){
	var window_bar = document.getElementById('lb' +id);	
	document.getElementById('window_bar').removeChild(window_bar);	
}

function CaptureResizeWindow(wd){
	var main_window = wd.parentNode;
	var style = main_window.style;
	var id = main_window.getAttribute('id');
	
	FocusWindow(main_window);

	if((parseInt(style.top) != 29) || (parseInt(style.left) != 0)){	
		
		if(window_mode == 0){
			dif_y = mouse_y - parseInt(style.height);	
			dif_x = mouse_x - parseInt(style.width);	
			window_active = main_window;
		}else{
			dif_y = mouse_y - (parseInt(style.height) + 35);	
			dif_x = mouse_x - parseInt(style.width);	
			window_active = CloneWindow(main_window);	
		}
		
		document.onmousemove =  ResizeWindow;

		window_id = id;
	}
	
	document.onmousedown = function(){
		return false;
	}	
    document.onselectstart = function(){
	 	return false;  
   }
}

function ResizeWindow(e){
	moveMouse(e);
	if(window_id != false){
		var style = window_active.style;
		
		var ry = mouse_y - dif_y;
		if(ry > 220){
			style.height = ry +'px';
		}
		var rx = mouse_x - dif_x;
		if(rx > 220){
			style.width = rx +'px';
		}
	}
}

function MaximizeWindow(obj){
	var main_window = obj.parentNode.parentNode.parentNode;
	var style = main_window.style;
	var body_size = getBodySize();
	//var obj = document.getElementById('mb' +main_window.getAttribute('id'));
	
	if((parseInt(style.top) == 29) && (parseInt(style.left) == 0)){
		var temp = window_position[main_window.getAttribute('id')];
		style.top = temp[0] +'px';
		style.left = temp[1] +'px';	
		style.height = temp[2] +'px';
		style.width = temp[3] +'px';
		
		obj.style.backgroundImage = 'url(msnnovo/css/maximize.gif)';
		obj.title = 'Maximizar';
	}else{
		var bar_pos = findPos(document.getElementById('window_bar'));
		
		style.top = 29 +'px';
		style.left = 0 +'px';
		//style.height = (body_size[1] - 38) +'px';
		style.height = (bar_pos[1] - 37) +'px';
		style.width = (body_size[0]- 2) +'px';
		
		obj.style.backgroundImage = 'url(msnnovo/css/restaure.gif)';
		obj.title = 'Restaurar';
	}
	
}


function MinimizeWindow(obj){
	var main_window = obj.parentNode.parentNode.parentNode;
	main_window.style.visibility = 'hidden';	
}

function RestaureWindow(obj){
	var id = obj.getAttribute('id').replace('lbw','');
	var main_window = getWindow(id);
	main_window.style.visibility = 'visible';
	
	FocusWindow(main_window);
}


function getWindow(id){
	if(document.getElementById('w' +id)){
		return document.getElementById('w' +id);
	}
	return false;
}


function setWindowTitle(id,title){
	if(document.getElementById('t' +id)){
		document.getElementById('t' +id).innerHTML = title;
	}	
}

//posição elemento
function findPos(obj) {
	var curleft = 0;
	var curtop = 0;
	if(obj.offsetParent) {
		do{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}while(obj = obj.offsetParent);
	}	
	return [curleft,curtop];
}

document.onmousemove = MoveWindow;
document.onmouseup = ReleaseWindow;