/***********************************************************
 *  bt_window.js
 *----------------------------------------------------------
 *   Készítette        : 2008 Csete Roland, Copyright (C)
 *   E-mail            : csete@fps.hu
 ***********************************************************/

var bt_Widget = 0;
function bt_Window(){
	var Setting;
	var Coords = {mouse:null, mouseOffset:null};
	var dragableObject = null;

	this.open = function(set){
		Setting = set;
		o = this;
		bt_Widget++;
		
		Number.prototype.NaN0 = function(){ return isNaN(this) ? 0 : this; }
		var objBody = document.getElementsByTagName("body").item(0);
		var zIndex = (bt_Widget) ? bt_Widget * 1000 : 1000;
		var pageSize = o.getPageSize();
		var objOverlay = document.createElement("div");

		objOverlay.setAttribute('id','bt_layer_' + bt_Widget );
		objOverlay.style.display = 'none';
		objOverlay.style.position = 'absolute';
		objOverlay.style.top = '0';
		objOverlay.style.left = '0';
		objOverlay.style.zIndex = zIndex;
		objOverlay.style.width = (pageSize.pw + 'px');
		objOverlay.style.minWidth = '100%';
		objOverlay.style.height = (pageSize.ph + 'px');
		objOverlay.style.minHeight = '100%';
		if(Setting.background)
			objOverlay.style.backgroundColor = Setting.background;
		else
			objOverlay.style.backgroundColor = '#000000';
		if(Setting.transparent){
			objOverlay.style.filter = 'alpha(opacity=' + Setting.transparent + ')';
			objOverlay.style.opacity = ( Setting.transparent / 100 );
		}else{
			objOverlay.style.filter = 'alpha(opacity=50)';
			objOverlay.style.opacity = 0.50;
		}
		objBody.appendChild(objOverlay);
		
		var objContent = document.createElement("div");
		objContent.setAttribute('id','bt_content_' + bt_Widget );
		objContent.style.visibility	= 'hidden';
		objContent.style.position = 'absolute';
		objContent.style.top = '0';
		objContent.style.left = '0';
		objContent.style.zIndex = zIndex + 1;	
		objBody.appendChild(objContent);
		
		Setting.winid = 'bt_win_' + bt_Widget;
		var objBox = document.createElement("div");
		objBox.setAttribute('id', Setting.winid);
		if(Setting.winclass) objBox.className = Setting.winclass;
		objContent.appendChild(objBox);
		if(Setting.html) objBox.innerHTML = Setting.html;
		
		if(!Setting.width) Setting.width = objBox.offsetWidth;
		if(!Setting.height) Setting.height = objBox.offsetHeight;
		
		var scroll = o.getPageScroll(0);
		if(Setting.fixtop)
			toppos   = scroll.y + Setting.fixtop;
		else
			toppos   = scroll.y + (pageSize.wh - Setting.height) / 2;
		leftpos	= scroll.x + (pageSize.pw - Setting.width) / 2;
		
		objBox.style.top  = (toppos < 0) ? '0px' : toppos + 'px';
		objBox.style.left = (leftpos < 0) ? '0px' : leftpos + 'px';
		
		objOverlay.style.display = '';
		objContent.style.visibility	= '';
		
		if(Setting.drag){
			var root = document.getElementById(Setting.winid);
			for(var i=0; i<root.childNodes.length; i++){
				var handle = root.childNodes[i];
				if(handle.className == Setting.drag){
					handle.onmousedown = function(e){
						o.mouseDown(this.parentNode, e);
					}
				}
			} // end for
		}
	};
	
	this.reposition = function(){
		var target = document.getElementById('bt_win_' + bt_Widget);
		var pageSize = o.getPageSize();
		var scroll = o.getPageScroll(0);
		
		toppos   = scroll.y + (pageSize.wh - target.offsetHeight) / 2;
		leftpos	= scroll.x + (pageSize.pw - target.offsetWidth) / 2;
		target.style.top  = (toppos < 0) ? '0px' : toppos + 'px';
		target.style.left = (leftpos < 0) ? '0px' : leftpos + 'px';
	};
	
	this.mouseDown = function(root, e){
		e = event(e);
		Coords.mouse = getMouseCoords(e);
		Coords.mouseOffset = getMouseOffset(root,e);
		dragableObject = root;
		
		document.onmousemove = o.mouseMove;
		document.onmouseup = o.mouseUp;
		
		return false;
	};
	
	this.mouseUp = function(){
		dragableObject = null;
	};
	
	this.mouseMove = function(e){
		e = event(e);
		Coords.mouse = getMouseCoords(e);
		
		if(dragableObject){
			dragableObject.style.top  = Coords.mouse.y - Coords.mouseOffset.y + 'px';
			dragableObject.style.left = Coords.mouse.x - Coords.mouseOffset.x + 'px';
		}
		return false;
	};

	this.close = function(){
		
		var activeWidget = bt_Widget;
		
		layerID = 'bt_content_' + activeWidget;
		objElement = document.getElementById(layerID);
		if (objElement && objElement.parentNode && objElement.parentNode.removeChild){
			objElement.parentNode.removeChild(objElement);
		}
		
		layerID = 'bt_layer_' + activeWidget;
		objElement = document.getElementById(layerID);
		if (objElement && objElement.parentNode && objElement.parentNode.removeChild){
			objElement.parentNode.removeChild(objElement);
			bt_Widget--;
		}
	};

	this.getPageSize = function(){
		var scroll = o.getPageScroll(1);
		
		var windowWidth, windowHeight;
		if(self.innerHeight) {	// all except
			windowWidth = self.innerWidth; windowHeight = self.innerHeight;
		}else if(document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict
			windowWidth = document.documentElement.clientWidth; windowHeight = document.documentElement.clientHeight;
		}else if(document.body) { // other
			windowWidth = document.body.clientWidth; windowHeight = document.body.clientHeight;
		}
		
		pageHeight = (scroll.y < windowHeight) ? windowHeight : scroll.y ;
		pageWidth = (scroll.x < windowWidth) ? windowWidth : scroll.x ;

		return {pw:pageWidth, ph:pageHeight, ww:windowWidth, wh:windowHeight};
	};
	
	this.getPageScroll = function(mode){
		var xScroll, yScroll;
		if(mode==1){
			if(window.innerHeight && window.scrollMaxY) {	
				xScroll = document.body.scrollWidth; yScroll = window.innerHeight + window.scrollMaxY;
			}else if(document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth; yScroll = document.body.scrollHeight;
			}else{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth; yScroll = document.body.offsetHeight;
			}
		}else{
			if(self.pageYOffset){
				xScroll = self.pageXOffset; yScroll = self.pageYOffset;
			}else if(document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict
				xScroll = document.documentElement.scrollLeft; yScroll = document.documentElement.scrollTop;
			}else if(document.body){ // other
				xScroll = document.body.scrollLeft; yScroll = document.body.scrollTop;
			}
		}
		return {x:xScroll,y:yScroll};
	};
	
	function getMouseOffset(target, e){
		e = event(e);
		var targPos =  getPosition(target);
		var mousePos = getMouseCoords(e);
		return {x:mousePos.x - targPos.x, y:mousePos.y - targPos.y};
	};
	
	function getMouseCoords(e){
		e = event(e);

		var sL = (document.body.scrollLeft) ? document.body.scrollLeft : 0 ;
		var sT = (document.body.scrollTop) ? document.body.scrollTop : 0 ;
		var cL = (document.body.clientLeft) ? document.body.clientLeft : 0 ;
		var cT = (document.body.clientTop) ? document.body.clientTop : 0 ;

		var mouseX = e.clientX + sL - cL;
		var mouseY = e.clientY + sT - cT;
		return {x:mouseX, y:mouseY};
	};

	function getPosition(target){
		var left = 0;
		var top  = 0;
		do{
			left += target.offsetLeft + (target.currentStyle ? (parseInt(target.currentStyle.borderLeftWidth)).NaN0() : 0);
			top += target.offsetTop + (target.currentStyle ? (parseInt(target.currentStyle.borderTopWidth)).NaN0() : 0);
		}while(target = target.offsetParent);
		return {x:left, y:top};
	};

	function event(e){
		if (typeof e == 'undefined') e = window.event;
		return e;
	};
}

/* wins */
var oneWin = new bt_Window();
function openWindow(){
	oneWin.open({
		winclass : 'fps_window',
		drag : 'fps_windrag',
		html : '<div id="x_wintitle" class="fps_windrag"></div><div id="x_winclose" onclick="closeWindow();" title="Close" class="fps_winclose"></div><div id="loader" class="fps_winloading"></div><div id="x_wincontent" class="fps_wincontent"></div>'
	});
}
function openWindow2(){
	oneWin.open({
		winclass : 'fps_window',
		drag : 'fps_windrag',
		html : '<div id="x_wintitle2" class="fps_windrag"></div><div id="x_winclose2" onclick="closeWindow();" title="Close" class="fps_winclose"></div><div id="loader2" class="fps_winloading"></div><div id="x_wincontent2" class="fps_wincontent"></div>'
	});
}
function closeWindow(){
	oneWin.close();
}