// some hand made scroller :)
var scrollTo = 2000;
var scrollCur = 0;
var scrollStep = 50;
var scrollTime = 1000;
var scrollInitiator = null;
var scrollObject = null;

function doSlide(timeout)
{
	scrollCur += scrollStep;
	scrollObject.style.left = (scrollCur).toString(10)+'px';
	newtimeout = timeout;
	
	if ( Math.abs(scrollCur) < Math.abs(scrollTo) )
		setTimeout('doSlide('+newtimeout+')',timeout);
	else
		window.location = scrollInitiator.href;
}

function slide(obj)
{
	scrollInitiator = obj;
	
	scrollCur = scrollObject.offsetLeft;
	scrollTo = scrollCur + scrollObject.offsetWidth;
	scrollObject.style.width = scrollObject.offsetWidth;
	scrollObject.style.position = 'absolute';
	scrollObject.style.left = (scrollCur).toString(10)+'px';
	scrollObject.style.top = '0px';
	
	timeout = scrollTime / scrollTo / scrollStep;
	
	doSlide(timeout);
	
	return false;
}


// drag & drop
// thanks to http://dunnbypaul.net/js_mouse/
// with some madmans mods =)

var mousex = 0;
var mousey = 0;
var grabx = 0;
var graby = 0;
var orix = 0;
var oriy = 0;
var elex = 0;
var eley = 0;

var dragobj = null;

function falsefunc() { return false; } // used to block cascading events

function init()
{
  //document.onmousemove = update;
  //update();
	
	scrollObject = document.getElementById('white');
  
  // read saved position
  menux = readCookie('menux');
  menuy = readCookie('menuy');
  
  if ( menux && menuy )
  {
	obj = document.getElementById('menu');
	obj.style.left = (menux).toString(10) + 'px';
	obj.style.top  = (menuy).toString(10) + 'px';
  }
  
  updateLook();
}

function updateLook()
{
	block = document.getElementById('white');
	obj = document.getElementById('menu');
	
	if ( obj.offsetLeft >= block.offsetLeft )
	{
		obj.className = 'menu mright';
	}
	else
	{
		obj.className = 'menu';
	}
	
	// y outside?
	if ( obj.offsetTop > block.offsetHeight )
	{
		obj.style.top = (block.offsetHeight - obj.offsetHeight - 10).toString(10) + 'px' ;
	}
	
	
}

function getMouseXY(e) // works on IE6,FF,Moz,Opera7
{ 
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)
 
  if (e)
  { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mousex = e.pageX;
      mousey = e.pageY;
    }
    else if (e.clientX || e.clientY)
    { // works on IE6,FF,Moz,Opera7
      mousex = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
      mousey = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }
  }
  
  // set grabs (for first drag)
  if ( grabx == 0 && graby == 0 )
  {
	grabx = mousex;
	graby = mousey;
  }
}


function update(e)
{
  getMouseXY(e);
}

function grab(context)
{
  document.onmousedown = falsefunc; // in NS this prevents cascading of events, thus disabling text selection
  dragobj = context;
  dragobj.style.zIndex = 20; // move it to the top
  document.onmousemove = drag;
  document.onmouseup = drop;
  
  grabx = mousex;
  graby = mousey;
  elex = orix = dragobj.offsetLeft;
  eley = oriy = dragobj.offsetTop;
  
  
  update();
}

function drag(e) // parameter passing is important for NS family 
{

  if (dragobj)
  {
    elex = orix + (mousex-grabx);
    eley = oriy + (mousey-graby);
    dragobj.style.position = "absolute";
    dragobj.style.left = (elex).toString(10) + 'px';
    dragobj.style.top  = (eley).toString(10) + 'px';
	
	// save
	createCookie('menux', elex);
	createCookie('menuy', eley);
	
	updateLook();
  }
  
  update(e);
  
  return false; // in IE this prevents cascading of events, thus text selection is disabled
}

function drop()
{
  if (dragobj)
  {
    dragobj.style.zIndex = 10;
    dragobj = null;
  }
  update();
  document.onmousemove = update;
  document.onmouseup = null;
  document.onmousedown = null;
}

// cookie stuff
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


// on load

window.onload = function() {
	init();
}


