/**
 * The code in this library is used in conjunction with Livepipe UI modal windows.
 */

/**
 * Create a formatted Modal window, using Livepipe library.  The container is an anchor (a href) id
 * Livepipe knows to render the contents of href inside a modal window.
 */
var modal_window_factory = function(container,options,serverUrl){
	
	var baseUrl=""
	//debugger;

	if( options == null ){
		options = {};
	}
	
	//We want to turn off the iframeshim, because it causes repeated clicking
	//noises in IE7.  This has something to do with the frame loading content
	//and IE responding with a click.
	Object.extend( options, {iframeshim: false});

	if( serverUrl ){
		var baseUrl=serverUrl;
	}
	
  var window_header = new Element('div',{ 'class': 'window_header' });
	
  var window_title = new Element('div');
	if( container != null ){
		window_title.addClassName(container.readAttribute('titleCssClass'));
    
		var window_close = new Element('div',{ 'class': 'window_close' });
    window_close.insert("<img src='" + baseUrl + "/idx/images/btn_close1.png'/>");		
	}
	
	var window_contents = new Element('div',{ 'class': 'window_contents' });
	//window_contents.setStyle({overflow: 'auto'});
	
	////////////////////////////////////////////////////////////////////////////
	//Set to true, if the parent window should reload, after the modal closes
	////////////////////////////////////////////////////////////////////////////
	var reloadOnClose=false;
	if( container != null ){
    var reloadOnCloseAttribute = container.readAttribute('reloadOnClose');
    if( reloadOnCloseAttribute != null ){
			reloadOnCloseAttribute = reloadOnCloseAttribute.toLowerCase();
			if ("true" == reloadOnCloseAttribute ) {
	  	  reloadOnClose = true;
	    }
    }
	}
	

  /**
   * JMF 2010-11-29
   * If afterCloseFunciton is NOT empty, then use it
   * Else, check if reloadOnClose is true
   */
  var afterCloseFunction = function(){};
  if( options != null && options.afterCloseFunction){
    afterCloseFunction =  options.afterCloseFunction;
  }
	
  if (reloadOnClose) {
		afterCloseFunction = afterCloseFunction.wrap( 
		  function( originalFunction ){
				originalFunction();
				location.reload(true);
			}
		);
  } 		
	 

	
	/**
	 * Function that fires after the modal window opens.  A good example of usage
	 * is to load a captcha field for signup pages.
	 */
	var afterOpenFunction = function(){};
	if( options != null ){
    afterOpenFunction = options.afterOpenFunction ;
  }  
	

	
	////////////////////////////////////////////////////////////////////////////
	//Set the height and width of the modal window, if the values are supplied.
	////////////////////////////////////////////////////////////////////////////
	var modalHeight = null;
	var modalWidth = null;
	var position = 'center' ;
	var offsetLeft=null;
	if (container != null ) {
		 if(  container.readAttribute('modalHeight') != null ){
		   modalHeight = container.readAttribute('modalHeight'); 	
		 }
		 if(container.readAttribute('modalWidth') != null ){
		   modalWidth  = container.readAttribute('modalWidth'); 	
		 }
  }
	
  /**
   * Jim Freeby
   * 
   * We have issues in IE, if the page is framed and the modalWidth attribute is NOT set
   * In this case, the modal always extends the entire width of the screen.  In this case
   * we set a sensible value, but this value can be overridden by setting the modalWidth
   * attribute.
   */	
	if (Prototype.Browser.IE){
    if(window.location != window.parent.location){
			if( modalWidth == null || modalWidth == 0 || modalWidth == '' ){
				modalWidth='532';
			}
		}
	}
	
	if(window.location != window.parent.location){
		//this fixes the position of a modal window 
		//when the page is in an iFrame.
		position=new Array(10,10);
		//offsetLeft='50px' ;
		}
  

	
	////////////////////////////////////////////////////////////////////////////
	//Set the modal title text, if values are supplied.
	////////////////////////////////////////////////////////////////////////////
	if (container != null) {
  	var modalTitleText = container.readAttribute('titleText');
		//alert("modalTitleText: " + modalTitleText)
  	if (modalTitleText != null && modalTitleText.length > 0) {
  		window_title.update(modalTitleText);
  	}
  }
	
	////////////////////////////////////////////////////////////////////////////
	//Create the modal window, using LivepipeUI class.
	////////////////////////////////////////////////////////////////////////////
  var modalWindow = new Control.Modal(container,Object.extend({   className: 'modal',
                                                                  closeOnClick: 'overlay',
                                                                  height: modalHeight,
                                                                  width: modalWidth,
																  position: position,
																  offsetLeft: offsetLeft,																			
                                                                  fade: false,
                                                                  overlayOpacity: 0.50,
                                                                  insertRemoteContentAt: window_contents,
                                                                  afterClose: afterCloseFunction,
																  afterOpen: afterOpenFunction  
                                                              },options || {}));

  window_close.observe('click',function(){
  	modalWindow.close();
  });
  ////////////////////////////////////////////////////////////////////////////
	//Insert the header and contents of the modal window
	////////////////////////////////////////////////////////////////////////////
	window_header.insert(window_title);
  window_header.insert(window_close);


  modalWindow.container.insert({
  	top: window_header
  });
  modalWindow.container.insert(window_contents);

  return modalWindow;
};



/**
 * Pass in a form and post that form using AJAX.
 * Update the contents of the modal with the response.
 */
var ajaxCall = function ( form, options ) {
		
  $( form ).request( {
    method: 'post',
    onSuccess: function(response) {
      var activeModal = Control.Modal.current;
      var activeModalContents = activeModal.container.down(4);
      activeModalContents.update( response.responseText ) ;
    },
    onFailure: function(response) {
      var activeModal = Control.Modal.current;
      var activeModalContents = activeModal.container.down(4);
      activeModalContents.update( response.responseText ) ;     
    }
  }); 
};
	
/**
 * Pass in a form and post that form using AJAX.
 * Update the contents of the modal with the response.
 */
var ajaxSubmitAndClose = function ( form ) {
  //debugger;
  $( form ).request( {
    method: 'post',
    onSuccess: function(response) {
      var activeModal = Control.Modal.current;
      activeModal.close();
    },
    onFailure: function(response) {
      var activeModal = Control.Modal.current;
      var activeModalContents = activeModal.container.down(4);
      activeModalContents.update( response.responseText ) ;			
    }
  });	
};

