/**
 * implements ErrorHandlingInterface
 * 
 * this.handleError(msg)
 */


if (!Atira) var Atira = {}

Atira.FloatingWindow = function(id, properties) {
	
	this._id = id;
	this._win = document.getElementById(id);
	
	if(!this._win){alert('Internal error: Create window failed, ' +id+ ' does not exist'); return false;}
	
	var width;
	var height;
	
	if(properties){
		width = properties['width']; 
		height = properties['height'];
		this._properties = properties;
	}else{
		this._properties = {};
	}
	
	if(width && !(isNaN(width))){
		this._width = width;
	}else{
		//default width
		this._width = 300;
	}
	
	if(height && !(isNaN(height))){
		this._height = height;
	}else{
		//default height
		this._height = 200;
	}
	
	this._win.style.display = 'none';
	this._win.style.left = '-' + this._width/2 + 'px';
	this._win.style.width = this._width + 'px';
	this._win.style.height = this._height + 'px';
	
	this._win.innerHTML = Atira.FloatingWindow.renderWindowInstance(this);
}

Atira.FloatingWindow.prototype.setTop = function(top) {
	//set offset
	if(top){
		var newTop = top - (this._height/2);
		this._win.style.top = newTop + 'px';
	}
}

Atira.FloatingWindow.prototype.show = function() {
	//HACK do this from somwhere else
	CountSystem.getInstance().setRegisteredErrorHandlerObject(this);
	this.setDynamicContent
	this._win.style.display='';
}

Atira.FloatingWindow.prototype.hide = function() {
	CountSystem.getInstance().releaseRegisteredErrorHandlerObject();
	this.resetError();
	this._win.style.display='none';
}

Atira.FloatingWindow.prototype.getId = function() {
	return this._id;
}

Atira.FloatingWindow.prototype.getWidth = function() {
	return this._width;
}

Atira.FloatingWindow.prototype.getHeight = function() {
	return this._height;
}

Atira.FloatingWindow.prototype.disableDrag = function() {
	this._drag.destroy();
}

Atira.FloatingWindow.prototype.enableDrag = function() {
	this._drag = new Draggable(this._id, {starteffect:false, endeffect:false});
}

Atira.FloatingWindow.prototype.getProperty = function(prop) {
	if(prop){
		return this._properties[prop];
	}
}

Atira.FloatingWindow.prototype.setDynamicTitle = function(html) {
	if(html){
		var title_id = this.getId() + '_dynamic_title';
		var element = document.getElementById(title_id);
		element.innerHTML = html;
	}
}

Atira.FloatingWindow.prototype.setDynamicContent = function(html) {
	if(html){
		var content_id = this.getId() + '_dynamic_content';
		var element = document.getElementById(content_id);
		element.innerHTML = html;
	}
}

Atira.FloatingWindow.prototype.disableDynamicContentPaneScrolling = function() {
	var content_id = this.getId() + '_dynamic_content';
	var element = document.getElementById(content_id);
	element.style.overflow = 'hidden';
}


Atira.FloatingWindow.prototype.setFirstButtonOnclickFunction = function(func){
	if(func){
		var button_id = this.getId() + '_button1';
		var element = document.getElementById(button_id);
		element.onclick = func;
	}
}

Atira.FloatingWindow.prototype.setSecondButtonOnclickFunction = function(func){
	if(func){
		var button_id = this.getId() + '_button2';
		var element = document.getElementById(button_id);
		element.onclick = func;
	}
}

Atira.FloatingWindow.prototype.handleError = function(msg){
	if(msg){
		var content_id = this.getId() + '_error';
		var element = document.getElementById(content_id);
		element.innerHTML = msg;
	}
}

Atira.FloatingWindow.prototype.resetError = function(){
	var content_id = this.getId() + '_error';
	var element = document.getElementById(content_id);
	element.innerHTML = '';
}

	
Atira.FloatingWindow.renderWindowInstance = function(win){
	
	var width = win.getWidth();
	var height = win.getHeight();
	
	var border_width = 6;
	var border_height = 6;
	
	var inside_width = width - (2*border_width);
	var inside_height = height - (2*border_height);
	
	var footer_text = win.getProperty('footertext'); 
	var button1_text = win.getProperty('oktext'); 
	var button2_text = win.getProperty('canceltext'); 
	var button1_width = win.getProperty('button1width') || '80px'; 
	
	
	var title_height = 36;
	var footer_height = 12;
	var buttongroup_height = 36;
	var dynamic_content_height = inside_height - title_height - footer_height - buttongroup_height;
	
	
	var html = '';
	
	
	html +='<div class="windowframe">'+
		
			'<div class="background">'+
			'<div class="middle_right">'+
			'<div class="middle_left">'+
			'<div class="top">'+
			'<div class="top_right">'+
			'<div class="top_left">'+
			'<div class="bottom">'+
			'<div class="bottom_right">'+
			'<div class="bottom_left">'+
				
				'<div class="content_padding">'+	
				'<div class="content">'+	
				'<h4 id="'+win.getId()+'_dynamic_title" class="title">No title</h4>'+
				
				'<div id="'+win.getId()+'_error" class="content_error">'+
					//ERROR
				'</div>' +
				
				'<div id="'+win.getId()+'_dynamic_content" class="content_dynamic">'+
					//CONTENT
				'</div>';
				
				//footer
				if(footer_text){
					html += '<div class="footer">'+footer_text+'</div>';
				}
				
				//buttons
				html+= '<div class="buttongroup">';
					
					if(button1_text){
						html+= '<a class="button" href="#" id="'+win.getId()+'_button1" style="float: left; width: '+button1_width+'; font-size: 8pt;"><span><span>'+button1_text+'</span></span></a>';
					}
					
					if(button2_text) {
						html+= '<a class="cancel_link" href="#" id="'+win.getId()+'_button2" style="float: right; margin-top: 6px;"><span><span>'+button2_text+'</span></span></a>';
					}
				
				html+= '</div>' +
					   
					   '<div style="clear: both;"></div>' +
				
				'</div>'+
				'</div>'+
			 
			'</div>'+
			'</div>'+
			'</div>'+
			'</div>'+
			'</div>'+
			'</div>'+
			'</div>'+
			'</div>'+
			'</div>'+
			
	'</div>';
	
	return html;
}

/*

	test 2009 - 1 - 9

	html +='<div class="windowframe">'+
		
			'<div class="background">'+
			'<div class="middle_right">'+
			'<div class="middle_left">'+
			'<div class="top">'+
			'<div class="top_right">'+
			'<div class="top_left">'+
			'<div class="bottom">'+
			'<div class="bottom_right">'+
			'<div class="bottom_left">'+
				
				'<div class="content_padding">'+	
				'<div class="content">'+	
				'<h4 id="'+win.getId()+'_dynamic_title" class="title">No title</h4>'+
				
				'<div id="'+win.getId()+'_error" class="content_error">'+
					//ERROR
				'</div>' +
				
				'<div id="'+win.getId()+'_dynamic_content" class="content_dynamic">'+
					//CONTENT
				'</div>';
				
				//footer
				if(footer_text){
					html += '<div class="footer">'+footer_text+'</div>';
				}
				
				//buttons
				html+= '<div class="buttongroup">';
					
					if(button1_text){
						html+= '<a class="button" href="#" id="'+win.getId()+'_button1" style="float: left; width: 80px; font-size: 8pt;"><span><span>'+button1_text+'</span></span></a>';
					}
					
					if(button2_text) {
						html+= '<a class="cancel_link" href="#" id="'+win.getId()+'_button2" style="float: right; margin-top: 6px;"><span><span>'+button2_text+'</span></span></a>';
					}
				
				html+= '</div>' +
					   
					   '<div style="clear: both;"></div>' +
				
				'</div>'+
				'</div>'+
			 
			'</div>'+
			'</div>'+
			'</div>'+
			'</div>'+
			'</div>'+
			'</div>'+
			'</div>'+
			'</div>'+
			'</div>'+
			
	'</div>';

*/
