// =========================================================================================================
// UNIVERSAL WINDOW FUNCTIONS
// =========================================================================================================


function rezGetWindowHeight () {
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myHeight = document.body.clientHeight;
	  }
	  return myHeight;
}

function rezGetWindowWidth() {
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth; 
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientWidth ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientWidth ) ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	  }
	  return myWidth;
}

// =========================================================================================================
// MODAL SMALL WINDOW
// =========================================================================================================

RezWindowSmall = function(instance, buttons) {

	this.initialize = function(instance, buttons) {

		// params init
		this.buttons = null;
		this.instance = instance;
		
		// params update
		this.updateButtons(buttons);
		
		// modal init
 		this.modal = new Control.Modal($(this.instance+'_layout'),{  
     		className: this.instance,
     		closeOnClick: false,
			positon: 'center',
			width: 400,
			fadeDuration: 0.75,
			iframeshim: false,
   			height: rezGetWindowHeight()/2,
     		fade: true
		});
	}
	// open window (with params)
	this.open = function(contentParams) {
//			this.initialize(this.instance, this,buttons);

			this.updateContentParams(contentParams);
			this.modal.open();
			return false;
	}

	// open window with message
	this.openExt = function(message) {
//			this.initialize(this.instance, this,buttons);

			this.updateContentMessage(message);
			this.modal.open();
			return false;
	}

	this.close = function() {
			this.modal.close();
			return false;
	}
	
	this.action = null;
	
	this.actionAndClose = function() {
		if (this.action != 'undefined') {
			this.action();
		}
		this.close();
		return false;
	}

	this.updateContentParams = function(contentParams) {
	   content = $(this.instance+'_default_content').innerHTML;
	   if (typeof(contentParams) != 'undefined') {
			for(cp in contentParams) {
				content = content.replace(cp, contentParams[cp]);
			}
	   $(this.instance+'_content').update(content);
	   }	
	}

	this.updateContentMessage = function(message) {
	   $(this.instance+'_content').update(message);
	}
		
	this.updateButtons = function(buttons) {
		   content = '';
		   if (typeof(buttons) != 'undefined') {
		   		this.buttons = buttons;
				closeOnButtonClick = this.closeOnButtonClick == true ? this.instace+'.close(); ' : '';
				for(button in this.buttons)
				{
					this.buttons[button] = buttons[button];
					titlexxx = typeof(buttons[button]['title']) != 'undefined' ? buttons[button]['title'] : '';
					url = typeof(buttons[button]['url']) != 'undefined' ? 'href="'+buttons[button]['url']+'"' : '"#"';
					retvalue = typeof(buttons[button]['url']) != 'undefined' ? 'return true; ' : 'return false; ';
					eventxxx = typeof(buttons[button]['event']) != 'undefined' ? buttons[button]['event']+'; ' : ' ';
					script = 'onClick="return '+closeOnButtonClick+eventxxx+retvalue+'"';
					type = typeof(buttons[button]['type']) != 'undefined' ? 'class="button_'+buttons[button]['type']+'"' : 'class="button_yes"';
					content = content+'<a '+' '+url+' '+type+' '+script+'>'+titlexxx+'</a>';
		   		}
		   }
		  $(this.instance+'_buttons').update(content);
		}
	
//	this.instance = instance;
//	this.buttons = buttons;
	this.initialize(instance, buttons);
}

// =========================================================================================================
// MODAL LARGE WINDOW
// =========================================================================================================

RezWindowLarge = function(instance) {

	this.initialize = function(instance) {
		this.instance = typeof(instance) != 'undefined' ? instance : 'rez_window_large';

 		this.modal = new Control.Modal($(this.instance+'_layout'),{  
     		className: this.instance,
			positon: 'center',
//			offsetLeft: 300,
     		closeOnClick: false,
     		afterOpen: this.afterOpen,
     		width: 760,
			height: rezGetWindowHeight()-50,
			fadeDuration: 0.75,
			iframeshim: false,
     		fade: true
		});
		this.modal.rwl = this; 
	}
	
	this.close = function() {
		this.modal.close();
		return false;
	}
	
	
	// opens window and add ajax content from url
	this.open =  function(url, useLoading) {
		this.useLoading = typeof(useLoading) != 'undefined' ? useLoading : true;
		this.url = url; 
		
		if (this.useLoading) {
			this.showLoading();
		}
		this.modal.open();
		return false;
	}
	
	this.showLoading = function() {
		src = $(this.instance+'_loading').innerHTML;
		dst = $(this.instance+'_content');
		dst.update(src);
	}
	
	// starts ajax updater
	this.afterOpen = function(event) {
		rwl = this.rwl;
		rwl.ajax = new Ajax.Updater(rwl.instance+'_content', rwl.url, {method: 'get', evalScripts: true});
		rwl.ajax.rwl = this;
		//TODO: add onFailure event
	}

	this.toggleHelp = function(helpElement, contentElement) {
		helpEl = $(helpElement);
		conEl = $(contentElement);

		if (( helpEl != null) && ( conEl !== null )) {
		
			if (helpEl.style.display == 'none') 
			{
				helpEl.style.display = 'block';
				conEl.style.display = 'none';
			}
			else {
				helpEl.style.display = 'none';
				conEl.style.display = 'block';
			}
		}
  		return false;
	}
	
	this.initialize(instance);
	
}

