/*
  Author: Techocraft, Uroš Renko
  Dependencies: Prototype library 1.6+
*/




//
// getPageScroll()
// Returns array with x,y page scroll values.
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	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;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

var TcWindow = Class.create(
{
  
  minWidth:          100,
  width:             200,
  minHeight:         20,
  height:            300,
  opacity: 0.5,
  overlayColor: "#000000",

  resizable:         true,
  closable:          true,
  draggable:         true,
  recenterAuto:      true,
  debug:             true,

	initialize: function(_selector)
  {	

		if (!document.getElementsByTagName)
    {
      return;
    }

    if (!_selector)
    {
      _selector = ".GRID_FORM";
    }

		var _window = $$(_selector);
    if (_window=="")
    {
      return;
    } else
    {
      _window = _window[0];
    }
    
    var _body = $(document.getElementsByTagName("body").item(0));

		var _overlay = $(document.createElement("div"));
		_overlay.setAttribute('id','tc-window-overlay');
    _overlay.show();
		_body.appendChild(_overlay);

    if (this.debug)
    {
      var _debug = $(document.createElement("div"));
		  _debug.setAttribute('id','tc-window-debug');
      _debug.show();
  		_overlay.appendChild(_debug);
    }

		var _container = $(document.createElement("div"));
		_container.setAttribute('id','tc-window-container');
    _container.show();
		_body.appendChild(_container);

		var _box = $(document.createElement("div"));
		_box.setAttribute('id','tc-window-box');
    //_box.setStyle('position: absolute; left: 150px; top: 100px; width: auto; height: auto; border: 2px solid #444444;');
    _box.setStyle('margin:20px auto; width:900px; border: 2px solid #444444;');
    //_box.setStyle('position: absolute; left: 600px; width:900px; border: 2px solid #444444;');
		_container.appendChild(_box);
    
    var _new_window = Element.remove(_window);
    _box.appendChild(_new_window);
    _new_window.show();

    this.body = _body;
    this.overlay = _overlay;
    this.container = _container;
    this.box = _box;
    this.window = _new_window;
    if (this.debug)
    {
      this.debug = _debug;
    }

    this._Create();
  },

  _Create: function()
  {
    objPageSize = getPageSize();
    var pWidth = objPageSize[0];
    var pHeight = objPageSize[1];
    var pageWidth = document.viewport.getWidth();
    var pageHeight = document.viewport.getHeight();
    if (this.debug)
    {
      this.debug.innerHTML = 'PAGE width=' + pageWidth + ', height=' + pageHeight;
      this.debug.innerHTML += ' PAGESIZE ' + objPageSize[0] + ', ' + objPageSize[1] + ', ' + objPageSize[2] + ', ' + objPageSize[3];
    }
    var ScrollOffsets = document.viewport.getScrollOffsets();
    var scrollLeft = ScrollOffsets[0];
    var scrollTop = ScrollOffsets[1];
    if (this.debug)
    {
      this.debug.innerHTML += ' -- SCROLL left=' + scrollLeft + ', top=' + scrollTop;
    }

    this.overlay.setStyle('position: absolute; left: 0px; top: 0px; width: 100%; height: ' + pHeight + 'px; background-color: ' + this.overlayColor + '; z-index: 90; -ms-filter: alpha(opacity=' + (this.opacity*100) + '); filter: alpha(opacity=' + (this.opacity*100) + '); -moz-opacity: ' + this.opacity + '; opacity: ' + this.opacity + ';');
    this.container.setStyle('position: absolute; left: 0px; top: 0px; width: 100%; height: ' + pHeight + 'px; z-index: 100;');

  },

	_Destroy: function()
  {
    //this.window.hide();
    this.container.hide();
    this.overlay.hide();
  }

});

function TcWindowInit()
{ 
  objTcWindow = new TcWindow();
}

Event.observe(window, 'load', TcWindowInit, false);


