/*
  divalert.js
  =============================================================================

  Use this as an alert box without the annoying tendancies* of an alertbox :-)
  * Such as modality, pausing, and inability to keep a running log of messages
  This is open-source and free under the following "New BSD style" license:

  =============================================================================
  Copyright (c) 2009, David Gauer
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
      * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
      * Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
      * Neither the name of the person nor the
        names of its contributors may be used to endorse or promote products
        derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY David Gauer ''AS IS'' AND ANY
  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL David Gauer BE LIABLE FOR ANY
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  =============================================================================  
*/


var daMain    = null;
var daContent = null;

function divalert(message, bold){
  // If we don't already have a content tag, create it
  if(daMain == null){
    divalertCreate();
  }
  // Put our text in a bold tag if requested
  var content = daContent;
  if(bold){
    var b = document.createElement("b");
    daContent.appendChild(b);
    content = b;
  }
  content.appendChild(document.createTextNode(message));
  daContent.appendChild(document.createElement("br"));
  daMain.style.display = "block"

  // If divalert has been called before the page loads, it
  // sits in limbo and then calls itself when the page loads
  if(!document.body){
    var oldOnload = window.onload;
    window.onload = function(){
      if(oldOnload){ oldOnload(); }
      divalert("*Page Loaded*", true);
    }
  }
  else{
    document.body.appendChild(daMain);
  }
}

function divalertCreate(){
  var main     = document.createElement("div");
  var titlebar = makeTitleBar();
  var content  = makeContent();

  // Configure main box
  main.id = "divAlertMain";
  main.style.backgroundColor  = "white";
  main.style.color            = "black";
  main.style.padding          = "0px";
  main.style.borderColor      = "silver";
  main.style.borderWidth      = "2px";
  main.style.borderStyle      = "solid";
  main.style.position         = "absolute";
  main.style.top              = "200px";
  main.style.left             = "200px";
  main.style.zIndex           = "100";
  main.style.width            = "300px";
  main.onmousedown            = dragDown;

  // Put it all together
  main.appendChild(titlebar);
  main.appendChild(content);

  daMain    = main;
  daContent = content;
  return content;
}

function makeContent(){
  var content = document.createElement("div");
  content.id = "divAlertContent";
  content.style.margin = "10px";

  return content;
}


function makeTitleBar(){
  var bar      = document.createElement("div");
  var title    = document.createElement("div");
  var xbox     = document.createElement("div");
  var cleardiv = document.createElement("div");

  // Configure bar
  bar.style.backgroundColor = "blue";
  bar.style.color           = "white";
  bar.style.margin          = "0px";
  bar.style.height          = "29px";
  bar.style.cursor          = "move";

  // Configure title
  title.style.styleFloat    = "left";
  title.style.cssFloat      = "left";
  title.style.margin        = "3px";
  title.style.fontWeight    = "bold";
  title.appendChild(document.createTextNode("Alert"));

  // Configure xbox
  xbox.style.styleFloat       = "right";
  xbox.style.cssFloat         = "right";
  xbox.style.color            = "black";
  xbox.style.cursor           = "pointer";
  xbox.style.backgroundColor  = "silver";
  xbox.style.borderColor      = "gray";
  xbox.style.borderWidth      = "2px";
  xbox.style.borderStyle      = "solid";
  xbox.style.width            = "20px";
  xbox.style.height           = "20px";
  xbox.style.margin           = "3px";
  xbox.style.textAlign        = "center";
  xbox.onmouseover = function(){ xbox.style.color = "white"; };
  xbox.onmouseout  = function(){ xbox.style.color = "black"; };
  xbox.onclick     = function(){
    document.getElementById("divAlertMain").style.display = "none";
    return false;
  }
  xbox.appendChild(document.createTextNode("X"));
  
  // Configure cleardiv
  cleardiv.style.clear = "both";

  // put it together
  bar.appendChild(title);
  bar.appendChild(xbox);
  bar.appendChild(cleardiv);

  return bar;
}


// My Awesome Drag And Drop Functions
// =============================================================================

function dragDown(e){
  e = (e ? e : event);
  var top  = (isNaN(parseInt(this.style.top))  ? 0 : this.style.top);
  var left = (isNaN(parseInt(this.style.left)) ? 0 : this.style.left);
	var y = Math.abs(parseInt(top) - e.clientY);
	var x = Math.abs(parseInt(left) - e.clientX);

  var oldCursor = this.style.cursor;
  this.style.cursor = "move";

  var oldMousemove = document.onmousemove;
  var oldMouseup   = document.onmouseup;
  document.onmousemove = dragMakeMoveFunc(this, y, x);
  document.onmouseup   = dragMakeStopFunc(this, oldMousemove, oldMouseup, oldCursor);
}

function dragMakeMoveFunc(elem, y, x){
  return function(e){
    e = (e ? e : event);
		elem.style.top  = (e.clientY - y) + 'px';
		elem.style.left = (e.clientX - x) + 'px';
  }
}

function dragMakeStopFunc(elem, oldMousemove, oldMouseup, oldCursor){
  return function(){
    document.onmousemove  = oldMousemove;
    document.onmouseup    = oldMouseup;
    elem.style.cursor     = oldCursor;
  }
}

