/**
 * @author Nikolai Dimitrov
 * @copyright mochanin.com
 */

function overlibShow(oParent, empty) {
  var over;
  var stand = '';
  
  if (!oParent.innerHTML)
    stand = oParent.getAttribute('alt');
  else
    stand = oParent.innerHTML;
    
  // Get the data from the server
  if (!empty)
    overlibMakeRequest(stand);
  // Clear any open flyouts
  overlibDoHide();
  // Clear any scheduled hidings
  overlibStopHiding();
  
  over = overlibCreateOver(empty);
  overlibPositionOver(oParent, over);
  
  document.body.appendChild(over);
}

function overlibHide() {
	// If there is nothing opened we must not hide
	var over = document.getElementById('overlibElement');
	
	if (over !== null)
		overlibTimeout = window.setTimeout(overlibDoHide, overlibDelay);
}

function overlibCreateOver(empty) {
  var over = document.createElement('DIV');
  
  over.id = 'overlibElement';
  over.className = 'overlibElement';
  
  // Fill in the content
  var html = '<div class="overlibContainer" onMouseOver="overlibStopHiding();" onMouseOut="overlibHide();">';
  
  if (empty)
    html += '<table width="100%"  border="0" cellspacing="0" cellpadding="0"><tr><td width="12"><img src="/images/layout/gcorn1.gif" width="12" height="12" alt=""></td><td class="fpobox_empty"></td><td width="12"><img src="/images/layout/gcorn2.gif" width="12" height="12" alt=""></td></tr><tr><td class="fpobox_empty"></td><td class="fpobox_empty"><div class="fptitle">Available</div></td><td class="fpobox_empty"></td></tr><tr><td><img src="/images/layout/gcorn3.gif" width="12" height="12" alt=""></td><td class="fpobox_empty"></td><td><img src="/images/layout/gcorn4.gif" width="12" height="12" alt=""></td></tr></table>';
  else
    html += '<table width="100%"  border="0" cellspacing="0" cellpadding="0"><tr><td width="12"><img src="/images/layout/fbc1.gif" width="12" height="12" alt=""></td><td class="fpobox"></td><td width="12"><img src="/images/layout/fbc2.gif" width="12" height="12" alt=""></td></tr><tr><td class="fpobox"></td><td class="fpobox" id="overlibElementAjaxCont">&nbsp;</td><td class="fpobox"></td></tr><tr><td><img src="/images/layout/fbc3.gif" width="12" height="12" alt=""></td><td class="fpobox"></td><td><img src="/images/layout/fbc4.gif" width="12" height="12" alt=""></td></tr></table>';
  html += '</div>';
  over.innerHTML = html;
  
  return over;
}

function overlibPositionOver(oParent, over) {
	var left = overlibFindLeft(oParent);
	var top = overlibFindTop(oParent);
	
	try {
		left += overlibOffsetLeft;
		top += overlibOffsetTop;
	} catch (e) {
	}
	
	over.style.left = left +"px";
	over.style.top = top +"px";
}

function overlibFindLeft(oParent) {
	var out = 0;
	
	do {
		out += oParent.offsetLeft;
		oParent = oParent.offsetParent;
	} while(oParent !== null);
	
	return out;
}

function overlibFindTop(oParent) {
	var out = 0;
	
	out += oParent.offsetHeight;
	
	do {
		out += oParent.offsetTop;
		oParent = oParent.offsetParent;
	} while(oParent !== null);
  
	return out;
}

function overlibDoHide() {
	var over = document.getElementById('overlibElement');
	
	while(over !== null) {
		document.body.removeChild(over);
		over = document.getElementById('overlibElement');
	}
}

function overlibStopHiding() {
	if (overlibTimeout !== null)
		window.clearTimeout(overlibTimeout);
}


// AJAX
function overlibMakeRequest(stand) {
  var httpRequest;
  
  // Create the object
  if (window.XMLHttpRequest) {
    httpRequest = new XMLHttpRequest();
    if (httpRequest.overrideMimeType)
      httpRequest.overrideMimeType('text/xml');
  }
  else if (window.ActiveXObject) {
    try {
      httpRequest = new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch (e) {
      try {
        httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
      }
      catch (ee) {}
    }
  }
  
  // Handle errors
  if (!httpRequest) {
    alert('Can not initialize the AJAX connection! Sorry.');
    return;
  }
  
  httpRequest.onreadystatechange = function() { overlibHandleResponse(httpRequest); };
  httpRequest.open('GET', overlibURL +'?stand='+ stand, true);
  httpRequest.send('');
}

function overlibHandleResponse(httpRequest) {
  if (httpRequest.readyState == 4) {
    
    if (httpRequest.status == 200) {
      if (httpRequest.responseText == 'X')
        overlibDoHide();
      else {
        var cont = document.getElementById('overlibElementAjaxCont');
        cont.innerHTML = httpRequest.responseText;
      }
    }
    else
      alert('There was a problem, when retrieving data from the server.');
  }
}
