// Script used to manage containers
// Copyright Ayashi 2k7
// - Released under FOU [ Free Of Use ] -
//

// Global Variables
var CurrentlyMoving   = 0;
var CurrentDivID      = "";
var YOffset           = 0;
var Boxes             = new Array(0);
var MinTop            = 180;

// Numerical array sorting patch
var compare = function(a, b) { return a - b; }

// Disable Text Selection
function disabletext(e){ return false }
function reEnable(){ return true }
/*
if (window.sidebar) { document.onmousedown=disabletext; document.onclick=reEnable; }
document.onselectstart=new Function ("return false");
*/

// Register Boxes
function RegisterFlyingBox(BoxName)
 {
  Boxes.push(BoxName);
 }

// Retrieve box position from cookies
function RetrieveFromCookie()
 {
  var BoxPositions = ReadCookie("BoxPos");

  // If we have it in a cookie, arrange..
  if ( BoxPositions != null )
   Boxes = BoxPositions.split(",");
 }

// Draw Box floating container
function HighlightBox(DivID,DivTitleID,DaEvent)
 { 
  document.getElementById(DivID).style.cursor          = "move";
  document.getElementById(DivID).style.borderWidth     = 1;
  document.getElementById(DivID).style.backgroundColor = "#F0F0F0";
  document.getElementById(DivID).style.backgroundColor = "#F0F0F0";
  document.getElementById(DivID).style.opacity = ".50";

  CurrentlyMoving   = 1;
  CurrentDivID      = DivID;

  getPositionCurseur(DaEvent);
  YOffset = curY - parseFloat(document.getElementById(CurrentDivID).style.top) + 20;

  document.getElementById(CurrentDivID).style.zIndex = 100;
  MoveBox(DivID,DivTitleID,DaEvent)

  // Disable text selection
  if (window.sidebar) { document.onmousedown=disabletext; document.onclick=reEnable; }
  document.onselectstart=new Function ("return false");
 }

// Clear Box floating container
function ClearBox(DivID)
 {
  if ( CurrentlyMoving == 1 )
   {
    document.getElementById(DivID).style.cursor          = "auto";
    document.getElementById(DivID).style.borderWidth     = 0;
    document.getElementById(DivID).style.backgroundColor = "";
    document.getElementById(DivID).style.opacity = "1.0";

    CurrentlyMoving = 0;

    document.getElementById(CurrentDivID).style.zIndex = 0;
    ArrangeBoxes();
   }

  // Re-enable text selection
  if (window.sidebar) { document.onmousedown=reEnable; document.onclick=reEnable; }
  document.onselectstart=new Function ("return true");
 }
 
// Clear Box Title bg color
function CleanBox(DivID)
 {
  if ( CurrentlyMoving == 0 )
   {
    document.getElementById(DivID).style.backgroundColor = "";
    document.getElementById(DivID).style.borderLeftWidth = "";
    document.getElementById(DivID).style.borderLeftStyle = "";
    document.getElementById(DivID).style.borderLeftColor = "";
    document.getElementById(DivID).style.paddingLeft = "";
   }
 }

// Move a floating container
function MoveBox(DivID,DivTitleID,DaEvent)
 {
  if ( CurrentDivID == DivID && CurrentlyMoving == 1 )
   {
    getPositionCurseur(DaEvent);

    var newPosY = curY - YOffset;
    document.getElementById(CurrentDivID).style.top = newPosY + 'px';
   }
  else
   {
    document.getElementById(DivTitleID).style.backgroundColor = '#E0E0E0';
    document.getElementById(DivID).style.cursor               = "move";

    document.getElementById(DivTitleID).style.borderLeftWidth = "3px";
    document.getElementById(DivTitleID).style.borderLeftStyle = "solid";
    document.getElementById(DivTitleID).style.borderLeftColor = "#A2CC20";
    document.getElementById(DivTitleID).style.paddingLeft = "4px";
   }
 }

// Arrange the boxes
function ArrangeBoxes()
 {
  var OrderedBoxes = new Array(0);
  var BoxesName    = new Array(0);
  for(i=0;i<Boxes.length;i++)
   {
    // Show Divs before retrieving height..
    var Position = document.getElementById(Boxes[i]).offsetTop;
    OrderedBoxes.push(Position);
    BoxesName.push(Boxes[i]);
   }
  OrderedBoxes.sort(compare);

  Boxes = new Array(0);
  for(i=0;i<OrderedBoxes.length;i++)
   {
    var PositionOrdered = OrderedBoxes[i];

    for(j=0;j<BoxesName.length;j++)
     {
      var Position = document.getElementById(BoxesName[j]).offsetTop;
      if ( PositionOrdered == Position )
       Boxes.push(BoxesName[j]);
     }
   }
  Arrange();
}

// Set the position of the arranged the boxes
function Arrange()
 {
  var CurrentPos = MinTop;
  document.getElementById("Preloading").style.display = "none";

  for(i=0;i<Boxes.length;i++)
   {
    // Show Divs before retrieving height..
    document.getElementById(Boxes[i]).style.display = "inline";

    // Place Box
    document.getElementById(Boxes[i]).style.top = CurrentPos;

    // Retrieve Box Height
    var CurrentBoxHeight = document.getElementById(Boxes[i]).offsetHeight;
    CurrentPos = CurrentPos + CurrentBoxHeight + 15;
   }

  SaveCookie("BoxPos",Boxes.toString());
 }

// Save a cookie
function SaveCookie(Name,Value)
 {
  document.cookie = Name+"="+Value+"; expires= Mon, 05-Jan-10 00:00:01 GMT";
 }

// Read a cookie
function ReadCookie(Name)
 {
  var arg  = Name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i=0;
  while (i<clen)
   {
    var j=i+alen;
    if (document.cookie.substring(i, j)==arg)
     return GetCookieArg(j);
    i=document.cookie.indexOf(" ",i)+1;
    if (i==0) break;
   }
  return null; 
 }

// Extract cookie value
function GetCookieArg(offset)
 {
  var endstr=document.cookie.indexOf (";", offset);

  if (endstr==-1)
   endstr=document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr)); 
 }

// Retrieve the mouse pointer position - Cross browser compatible -
function getPositionCurseur(DaEvent)
 {
  // Internet Explorer
  if(document.all)
   {
    curX = event.clientX;
    curY = event.clientY;
   }
	
  // Netscape - 4
  if(document.layers)
   {
    curX = DaEvent.pageX;
    curY = DaEvent.pageY;
   }

  //Mozilla
  if(document.getElementById)
   {
    curX = DaEvent.clientX;
    curY = DaEvent.clientY;
   }
 }

// Open a popup window
function newWindow(mypage,myname,w,h,features)
 {
  if(screen.width)
   {
    var winl = (screen.width-w)/2;
    var wint = (screen.height-h)/2;
   }
  else
   {
    winl = 0;wint =0;
   }
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += features;
  win = window.open(mypage,myname,settings);
  win.window.focus();
  win.document.getElementsByTagName("body")[0].style.margin = 0;
 }
