/**
 * ENABLE CUSTOM COMMENTS ON PROJECT ELEMENTS
 */
var lastajaxurl = null;
function ajaxWindow(url, params, title) {
  /**
   * CREATE THE CONTANER divs AND MAKE AJAX CALLS TO SERVER TO BUILD COMMENT FORMS
   */
  var winwidth;
  var winheight;
  
  if( typeof( window.innerWidth ) == 'number' ) {
  	//Non-IE
  	winwidth = window.innerWidth;
		winheight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		winwidth = document.documentElement.clientWidth;
		winheight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		winwidth = document.body.clientWidth;
		winheight = document.body.clientHeight;
	}
  
  var wincenterx = winwidth/2;
  var wincentery = winheight/2;

  var grid = document.getElementById('gridlayer')
  if (!grid) {
    /**
     * CREATE GRID LAYER TO MAKE THE BACKGROUND SEMI INVISIBLE 
     */
    var newdiv = document.createElement('div');
    newdiv.setAttribute('id','gridlayer');
    document.body.appendChild(newdiv);
    grid = document.getElementById('gridlayer');
    grid.style.position = 'absolute';
    grid.style.left = '0px';
    grid.style.top = '0px';
    // grid.style.width = '4000px';
    // grid.style.height = '4000px';
    grid.style.width = winwidth + 'px';
    grid.style.height = winheight + 'px';
    grid.style.background = 'url(/images/grid.gif)';
    grid.style.zIndex = 1000;
  } else {
    /**
     * Enable if already created before 
     */
    grid.style.display = 'block';
  }
  
  /**
   * Add Ajax View Layer
   */
  var newdiv = document.createElement('div');			// the ajax box container
  newdiv.setAttribute('id','ajaxbox');
  grid.appendChild(newdiv);
  var ajaxbox = document.getElementById('ajaxbox');
  var newdiv = document.createElement('div');			// the top bar with closing x
  newdiv.setAttribute('id','ajaxbox2');
  ajaxbox.appendChild(newdiv);
  var ajaxbox2 = document.getElementById('ajaxbox2');
  var newdiv = document.createElement('div');			// the main area of the ajaxbox
  newdiv.setAttribute('id','ajaxboxmain');
  ajaxbox.appendChild(newdiv);
  var ajaxbox3 = document.getElementById('ajaxboxmain');

  
  /**
   * Postion Ajax Layers
   */
  ajaxwidth = 500;
  if (ajaxwidth>winwidth) ajaxwidth = winwidth - 50;
  ajaxheight = 400;
  if (ajaxwidth>winheight) ajaxheight = winheight - 50;
  
  ajaxbox.style.color = 'black';
  ajaxbox.style.position = 'absolute';
  //ajaxbox.style.left = (wincenterx-(ajaxwith/2)) + 'px';
  x = wincenterx - (ajaxwidth/2);
  y = wincentery - (ajaxheight/2);
  ajaxbox.style.left = x + 'px';
  ajaxbox.style.top = y + 'px';
  ajaxbox.style.width = ajaxwidth + 'px';
  ajaxbox.style.height = ajaxheight + 'px';
  ajaxbox.style.border = '1px solid gray'; 
  ajaxbox.style.backgroundColor = 'white';
  
  ajaxbox2.style.color = '#660000';
  ajaxbox2.style.textAlign = 'right';
  ajaxbox2.style.padding = '3px';
  ajaxbox2.style.paddingRight = '6px';
//  ajaxbox2.style.backgroundColor = '#0060FC';
  ajaxbox2.style.borderBottom = '1px solid gray';
  ajaxbox2.innerHTML = '<a href="#" onclick="return closeAjaxWindow();" style="text-decoration:none; font-weight: bold; color: red; font-size: 13px;">X</a>';

  ajaxbox3.style.paddingTop = '10px';
  ajaxbox3.style.paddingLeft = '10px';
  ajaxbox3.style.paddingRight = '10px';
  ajaxbox3.style.paddingBottom = '10px';
  ajaxbox3.style.height = (ajaxheight - 45) + 'px';
  ajaxbox3.style.overflow = 'scroll'; 
  ajaxbox3.style.overflowX = 'hidden';
  ajaxbox3.style.textAlign = 'left';

  /**
   * Make this layer dragable, requires <script src="/js/dom-drag.js" type="text/javascript"></script>
   */  
  if ((typeof(Drag) != "undefined")) {
  	Drag.init(document.getElementById('ajaxbox2'), document.getElementById('ajaxbox'));
	}
  
  /**
   * Fill ajaxboxmain with HTML from server
   */
  lastajaxurl = url;
  var myAjax = new Ajax.Updater( 'ajaxboxmain', url,  { method: 'get',  parameters: params });
  
  return false;
}

var lastajaxurl = null;
function ajaxWindowNoStyle(url, params, title) {
  /**
   * CREATE THE CONTANER divs AND MAKE AJAX CALLS TO SERVER TO BUILD COMMENT FORMS
   */
  var winwidth;
  var winheight;
  
  if( typeof( window.innerWidth ) == 'number' ) {
  	//Non-IE
  	winwidth = window.innerWidth;
		winheight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		winwidth = document.documentElement.clientWidth;
		winheight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		winwidth = document.body.clientWidth;
		winheight = document.body.clientHeight;
	}
  
  var wincenterx = winwidth/2;
  var wincentery = winheight/2;

  var grid = document.getElementById('gridlayer')
  if (!grid) {
    /**
     * CREATE GRID LAYER TO MAKE THE BACKGROUND SEMI INVISIBLE 
     */
    var newdiv = document.createElement('div');
    newdiv.setAttribute('id','gridlayer');
    document.body.appendChild(newdiv);
    grid = document.getElementById('gridlayer');
    grid.style.position = 'absolute';
    grid.style.left = '0px';
    grid.style.top = '0px';
    // grid.style.width = '4000px';
    // grid.style.height = '4000px';
    grid.style.width = winwidth + 'px';
    grid.style.height = winheight + 'px';
    grid.className = 'ajaxwindowgrid';

  } else {
    /**
     * Enable if already created before 
     */
    grid.style.display = 'block';
  }
  
  /**
   * Add Ajax View Layer
   */
  var newdiv = document.createElement('div');			// the ajax box container
  newdiv.setAttribute('id','ajaxbox');
  // newdiv.style.border = '1px solid red';
  grid.appendChild(newdiv);
  var ajaxbox = document.getElementById('ajaxbox');
  var newdiv = document.createElement('div');			// the top bar with closing x
  newdiv.setAttribute('id','ajaxbox2');
  ajaxbox.appendChild(newdiv);
  var ajaxbox2 = document.getElementById('ajaxbox2');
  var newdiv = document.createElement('div');			// the main area of the ajaxbox
  newdiv.setAttribute('id','ajaxboxmain');
  ajaxbox.appendChild(newdiv);
  var ajaxbox3 = document.getElementById('ajaxboxmain');

  
  /**
   * Postion Ajax Layers
   */
  ajaxwidth = 500;
  if (ajaxwidth>winwidth) ajaxwidth = winwidth - 50;
  ajaxheight = 400;
  if (ajaxwidth>winheight) ajaxheight = winheight - 50;
  ajaxbox.style.color = 'black';
  ajaxbox.style.position = 'absolute';
  x = wincenterx - (ajaxwidth/2);
  y = wincentery - (ajaxheight/2);
  ajaxbox.style.left = x + 'px';
  ajaxbox.style.top = y + 'px';
  ajaxbox.style.width = ajaxwidth + 'px';
  ajaxbox.style.height = ajaxheight + 'px';
  
 	/*
  ajaxbox.style.border = '1px solid gray'; 
  ajaxbox.style.backgroundColor = 'white';
  ajaxbox2.style.color = '#660000';
  ajaxbox2.style.textAlign = 'right';
  ajaxbox2.style.padding = '3px';
  ajaxbox2.style.paddingRight = '6px';
  ajaxbox2.style.borderBottom = '1px solid gray';
  */


  ajaxbox2.innerHTML = '<a href="#" onclick="return closeAjaxWindow();" class="closeAjaxWindow">X</a>';

  ajaxbox3.style.paddingTop = '10px';
  ajaxbox3.style.paddingLeft = '10px';
  ajaxbox3.style.paddingRight = '10px';
  ajaxbox3.style.paddingBottom = '10px';
  ajaxbox3.style.height = (ajaxheight - 45) + 'px';
  ajaxbox3.style.overflow = 'scroll'; 
  ajaxbox3.style.overflowX = 'hidden';
  ajaxbox3.style.textAlign = 'left';

  /**
   * Make this layer dragable, requires <script src="/js/dom-drag.js" type="text/javascript"></script>
   */  
  Drag.init(document.getElementById('ajaxbox2'), document.getElementById('ajaxbox'));
  
  /**
   * Fill ajaxboxmain with HTML from server
   */
  lastajaxurl = url;
  var myAjax = new Ajax.Updater( 'ajaxboxmain', url,  { method: 'get',  parameters: params });
  
  return false;
}

function ajaxPostForm(formid) {
	/**
	 * Post an ajax form, NB!Must be defined something like this:
	 * <form id="ajaxform" method="post" onsubmit="return ajaxPostForm(this);" method="googlemapspoint.php">
	 * All but onsubmit is ignored.
	 */
	url = lastajaxurl;
	params = Form.serialize(formid);
  var myAjax = new Ajax.Updater( 'ajaxboxmain', url,  { method: 'post',  parameters: params });
	return false;
}

function closeAjaxWindow() {
  /**
   * CLOSE THE AJAX WINDOW AND REMOVE CHILD ELEMENTS
   */
  //var grid = $('gridlayer');
  var grid = document.getElementById('gridlayer');
  grid.style.display = 'none';
	//grid.removeChild($('ajaxbox'));
	grid.removeChild(document.getElementById('ajaxbox'));
  if (window.abexAjaxRefresher) {
	  abexAjaxRefresher();
  }
  return false;
}


