/**
 * ERROR - OVERLAY
 */ 
var mErrorDivHidHandler = null;
function hideErrorDiv()
{
  if (mErrorDivHidHandler!=null) window.clearTimeout(mErrorDivHidHandler);
  mErrorDivHidHandler = null;
  $("error_div").fade({duration:0.5});
}
function showErrorDiv(errorText)
{
  var newLeft = mouseXPos-$("error_div").getWidth()/2;
  if (newLeft<0) newLeft = 0;
  var newTop = mouseYPos-$("error_div").getHeight()/2;
  if (newTop<0) newTop = 0;
  $("error_div").style.left = newLeft+"px";
  $("error_div").style.top = newTop+"px";
  _showErrorDiv(errorText);
}
function showErrorDiv4Element(errorText, elementID)
{
  var pos = $(elementID).cumulativeOffset();
  $("error_div").style.left = pos.left+"px";
  $("error_div").style.top = (pos.top+10)+"px";
  _showErrorDiv(errorText);
}
function _showErrorDiv(errorText)
{
  // TODO stop fading out if in progress...
  // set error message and let the error message appear:
  $("error_div_text").update(errorText);
  $("error_div").appear( {duration: 0.5, to:0.9 } );
  if (mErrorDivHidHandler!=null) window.clearTimeout(mErrorDivHidHandler);
  mErrorDivHidHandler = window.setTimeout("hideErrorDiv()", 5000);
}

/**
 * INFO - OVERLAY
 */ 
var mInfoDivHidHandler = null;
function hideInfoDiv()
{
  if (mInfoDivHidHandler!=null) window.clearTimeout(mInfoDivHidHandler);
  mInfoDivHidHandler = null;
  $("info_div").fade({duration:0.5});
}
function showInfoDiv(infoText)
{
  var newLeft = mouseXPos-$("info_div").getWidth()/2;
  if (newLeft<0) newLeft = 0;
  var newTop = mouseYPos-$("info_div").getHeight()/2;
  if (newTop<0) newTop = 0;
  $("info_div").style.left = newLeft+"px";
  $("info_div").style.top = newTop+"px";
  _showInfoDiv(infoText);
}
function showInfoDiv4Element(infoText, elementID)
{
  var pos = $(elementID).cumulativeOffset();
  $("info_div").style.left = pos.left+"px";
  $("info_div").style.top = (pos.top+10)+"px";
  _showInfoDiv(infoText);
}
function _showInfoDiv(infoText)
{
  // TODO stop fading out if in progress...
  // set error message and let the error message appear:
  $("info_div_text").update(infoText);
  $("info_div").appear( {duration: 0.5, to:0.9 } );
}


/**
 * REG-HINT - OVERLAY
 */ 
function hideRegHintDiv()
{
  $("reg_hint_01").fade({duration:1.0});
  $("reg_hint_02").fade({duration:0.5});
}



/**
 * PHONE-CODE - OVERLAY
 */ 
var mPhoneCodeDivHidHandler = null;
function hidePhoneCodeDiv()
{
  if (mPhoneCodeDivHidHandler!=null) window.clearTimeout(mPhoneCodeDivHidHandler);
  mPhoneCodeDivHidHandler = null;
  $("phone_code_div").fade({duration:0.5});
}
function showPhoneCodeDiv(infoText)
{
  // set error message and let the error message appear:
  $("phone_code_text").update(infoText);
  $("phone_code_div").appear( {duration: 0.5, to:1 } );
}



/**
 * DETAIL - OVERLAY
 * Displays additional information for sale saleID in an overlay
 * that is positioned such that $('elementID') is still visible.  
 */
var mProductDetailContent = "";
var mProductDetailDockElementID = "";
function getProductDetailAJAX(elementID)
{
  var saleID = (mStoredSale!=null)?mStoredSale.SaleId:-1;
  getProductDetailAJAXFromSaleId(saleID, elementID);
}
function getProductDetailAJAXFromSaleId(saleID, elementID)
{
  mProductDetailDockElementID = elementID;
  new Ajax.Request('/Index',
  {
    method:'get',
    parameters: { '__ia': 'showProduct', 'sale_id': saleID, 'ajax': '1', 'no_cache':(new Date()).getTime() },
    evalJS: true,
    onSuccess: function(transport)
    {
      _productDetailAJAXResponse(transport);
    }
  });
}
function getProductDetailAJAXFromProductId(productId, elementID)
{
  mProductDetailDockElementID = elementID;
  new Ajax.Request('/Index',
  {
    method:'get',
    parameters: { '__ia': 'showProduct', 'product_id': productId, 'ajax': '1', 'no_cache':(new Date()).getTime() },
    evalJS: true,
    onSuccess: function(transport)
    {
      _productDetailAJAXResponse(transport);
    }
  });
}
function _productDetailAJAXResponse(transport)
{
  var response = transport.responseText;
  if ($('product_detail_container').visible())
  {
    _hideProductDetail();
    mProductDetailContent = response;
    window.setTimeout("_showProductDetail()", 400);
  }
  else
  {
    mProductDetailContent = response;
    _showProductDetail();
  }
}
function _showProductDetail()
{
  // positioning:
  var pos = $(mProductDetailDockElementID).cumulativeOffset();
  var width = $(mProductDetailDockElementID).getWidth();
  var left = pos.left+width+5;
  if (left+$("product_detail_container").getWidth() > 1024) 
    left = pos.left-$("product_detail_container").getWidth()-5;
  if (left<0) left = 0;
  $("product_detail_container").style.left = left+"px";

  // set content and let the overlay appear:
  $('product_detail_content').update(mProductDetailContent);
  $('product_detail_container').appear({duration:0.4});
}
function _hideProductDetail()
{
  $('product_detail_container').fade({duration:0.4});
}



/**
 * Detail-Area in which MyCooni and static pages are displayed
 */ 
var mDetailContent = "";
function showOverlayAJAX(overlayName)
{
  new Ajax.Request('/Index',
  {
    method: 'get',
    parameters: { '__ia': 'getOverlay', 'ovn': overlayName, 'no_cache':(new Date()).getTime() },
    evalJS: true,
    onSuccess: function(transport)
    {
      _detailAJAXResponse(transport);
    }
  });
}
function _detailAJAXResponse(transport)
{
  var response = transport.responseText;
  if (response==null || response.length==0) return; // nothing to display here..
  
  if ($('overlay_container').visible())
  {
    _hideDetail();
    mDetailContent = response;
    window.setTimeout("_showDetail()", 400);
  }
  else
  {
    mDetailContent = response;
    _showDetail();
  }
}
function _showDetail()
{
  $('overlay_content').update(mDetailContent);
  $('overlay_container').appear({duration:0.4});
}
function _hideDetail()
{
  $('overlay_container').fade({duration:0.4});
}



/**
 * Replace provided content in my-pages
 */ 
function updateElementWithAJAX(elementId, parametersJSON)
{
  parametersJSON.no_cache = (new Date()).getTime();
  new Ajax.Updater(elementId, '/Index', {
    method:'get',
    parameters: parametersJSON
  });
}




// global mouse-coordinates:
var mouseXPos = 0;
var mouseYPos = 0;
document.onmousemove = mouseMoved;
function mouseMoved(e)
{
	// get current mouse-coordinates:
	//mouseXPos = Event.pointerX(e);   // doesn't work for IE
	//mouseYPos = Event.pointerY(e);   // doesn't work for IE
  if( window.event && window.event.clientX )
  {
    // Internet-Explorer:
    var isStrictMode = document.compatMode && document.compatMode != 'BackCompat' ? true : false;
    var scrollX = isStrictMode ? document.documentElement.scrollLeft : document.body.scrollLeft;
    var scrollY = isStrictMode ? document.documentElement.scrollTop : document.body.scrollTop;
    mouseXPos = window.event.clientX + scrollX;
    mouseYPos = window.event.clientY + scrollY;
  }
  else
  {
    mouseXPos = e.pageX;
    mouseYPos = e.pageY;
  }
}
