/**
 * @author Andrew Romashkan
 */

Window = function( objArgs ){
	this.title = objArgs.title || null;
	this.isPrintbtn = objArgs.isPrintbtn || false;
	this.width = objArgs.width || null;
	this.height = objArgs.height || null;
	this.sourceURL = objArgs.sourceURL || null;
	this.isSRCDefined = false;
	this.sourceHTML = objArgs.sourceHTML || null; 
	
	this.domMask = null;
	this.domWindow = null;
}

Window.prototype.render = function(){
	var viewportDimensions = $(document.body).getDimensions();
	
	var m_height = viewportDimensions.height + "px";
	this.domMask = new Element('div', { 'style' : 'height : ' + m_height });
    this.domMask.className = 'window_mask mask_hidden';  //
	
	var width = this.width == null ? 'auto' : this.width + 'px';
	var top = this.getTopCoordinate() + "px";
	var left = this.width == null ? '0px' : (viewportDimensions.width - this.width)/2 + "px";
	
	this.domWindow = new Element('div', { 'style' : 'width : ' + width + '; top : ' + top + '; left : ' + left });
    this.domWindow.className = 'window_container window_hidden';
	
	// Header
	var domWindowHeader = new Element('div');
    domWindowHeader.className = 'window_header';
	
	if( this.isPrintbtn ){
		Element.insert( domWindowHeader, new Element('a', { 'href' : '#', 'class': 'window_btn_print' }).update("<span>Print</span>"));
	}
	
	if( this.title ){
		Element.insert( domWindowHeader, new Element('div', { 'class': 'window_title' }).update(this.title));
	}
	
	
	// Body
	var domWindowBody = new Element('div', { 'class': 'window_body' });
	
	if( this.sourceURL ){
		var i_width = this.width == null ? '100%' : (this.width - 25) + 'px';
		var i_height = this.height == null ? '100%' : (this.height - 60) + 'px';
		Element.insert( domWindowBody, new Element('iframe', { 'class': 'window_external_source', 'style' : 'width : ' + i_width + '; height : ' + i_height, 'scrolling':'no', 'frameborder':'0', 'border':'0' }));
	}else if( this.sourceHTML ){
		domWindowBody.update(this.sourceHTML);
	}
	
	// Footer
	var domWindowFooter = new Element('div', { 'class': 'window_footer' });
	
	Element.insert( domWindowFooter, new Element('a', { 'href': '#', 'class' : 'window_btn_close' }).update("Close"));
	
	// Build window
	Element.insert( this.domWindow, domWindowHeader );
	Element.insert( this.domWindow, domWindowBody );
	Element.insert( this.domWindow, domWindowFooter );
	
	// Render to DOM
	Element.insert( $(document.body), this.domMask );
	Element.insert( $(document.body), this.domWindow );
	
	this.initControls();
}

Window.prototype.initControls = function(){
	var btnPrint = this.domWindow.select('a[class="window_btn_print"]')[0];
	var btnClose = this.domWindow.select('a[class="window_btn_close"]')[0];
	var printArea = this.domWindow.select('iframe[class="window_external_source"]')[0];
	var mask = this.domMask;

	var scope = this;
	btnClose.onclick = function(){return false;};
	btnClose.observe('click',function(){scope.hide();});
	mask.observe('click',function(){scope.hide();});
	document.observe('keypress',function(e){if(e.keyCode == 27){ scope.hide() } });
	btnPrint.onclick = function(){return false;};
	btnPrint.observe('click',function(){printArea.contentWindow.focus(); printArea.contentWindow.print();});
}

Window.prototype.show = function(){
	this.domMask.removeClassName('mask_hidden');
	this.domWindow.removeClassName('window_hidden');
	this.domWindow.style.top = this.getTopCoordinate() + "px";
	
	if( this.sourceURL && !this.isSRCDefined ){
		this.domWindow.select('iframe[class="window_external_source"]')[0].src = this.sourceURL;
		//this.isSRCDefined = true;
	}
}

Window.prototype.hide = function(){
	this.domMask.addClassName('mask_hidden');
	this.domWindow.addClassName('window_hidden');
	this.domWindow.select('iframe[class="window_external_source"]')[0].src = "about:blank";
}

Window.prototype.getTopCoordinate = function(){
	var yCoordinate = (this.getWindowSize()[1] - this.height)/ 2;
	if( yCoordinate <= 0 ) yCoordinate = 5;
	
	return yCoordinate + this.getScrollXY()[1];
}


Window.prototype.getWindowSize = function() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
}

Window.prototype.getScrollXY = function() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}