/* Copyright 1997-2006 Syndicat Interactie. All rights reserved. */
// global variables
var HIT_AREAS 			= new Array();
var MOUSE_IS_DOWN		= false;
var TARGET				= null;
var TIMER				= setTimeout('',100);
var SCROLL_SPEED		= 24;
var SCROLL_MIN_HEIGHT	= 20;

var SCROLL_WIDTH		= 20;
var SCROLL_ARROW		= 20;
var SCROLL_ARROW_UP		= '<img src="/img/scroll_up.gif" width=20" height="20" />';
var SCROLL_ARROW_DOWN	= '<img src="/img/scroll_down.gif" width=20" height="20" />';
var SCROLL_DRAG			= '<img src="/img/scroll_handle.gif" width=20" height="20" />';


/* CONTENT GENERATOR FUNCTIONS */
function sDIV(id,vis,z,top,left,width,height,content,color){
	var str = '<div id="'+id+'" style="';
	str+= 'position: absolute; ';
	str+= 'visibility: '+vis+'; ';
 	str+= 'z-index: '+z+'; ';
	str+= 'top: '+top+'px;';
	str+= 'left: '+left+'px; ';
	str+= 'width: '+width+'px; '
	str+= 'height: '+height+'px;';
	if(color!=''){
		str+= 'background-color: '+color+';';
	}
	str+= '; overflow: hidden">'+content+'<\/div>'
	return str;
}

function scrollMake(id,top,left,width,height,bg100,bg80){

	var str = sDIV(id+'_scroll', 'visible',1,top + SCROLL_ARROW, left + width, SCROLL_WIDTH, height-SCROLL_ARROW*2,'',bg80);
	str+= sDIV(id+'_protect', 'visible',3, top, left+width, SCROLL_WIDTH,height,'','');
	str+= sDIV(id+'_up', 'visible',1,top, left+width,SCROLL_WIDTH,SCROLL_ARROW,SCROLL_ARROW_UP,bg100);
	str+= sDIV(id+'_down', 'visible',1,top-SCROLL_ARROW+height,left+width,SCROLL_WIDTH,SCROLL_ARROW,SCROLL_ARROW_DOWN,bg100);
	str+= sDIV(id+'_drag', 'visible',2,top+SCROLL_ARROW,left+width,SCROLL_WIDTH,SCROLL_ARROW,'&nbsp;',bg100);
	D.write(str);
}

function divBehaviour( div ) {
	this.content		= ref(div);
	this.clip			= ref(div+'_clip');
	this.drag			= ref(div+'_drag');
	this.scroll			= ref(div+'_scroll');

	this.height			= NN?this.content.clip.bottom:this.content.offsetHeight;
	this.clip_height	= NN?this.clip.clip.bottom:this.clip.offsetHeight;

	this.cs				= sref(div);
	this.ds				= sref(div+'_drag');
	this.ss				= sref(div+'_scroll');

	this.cs.visibility	= S;

	this.scroll_height	= NN?this.scroll.clip.bottom:this.scroll.offsetHeight;

	// do we need a scroll?
	if ( this.height > this.clip_height ) {
		var h = pxToInt(this.scroll_height) / (this.height / this.clip_height);
		this.ds.height		= intToPx(h > SCROLL_MIN_HEIGHT ? h : SCROLL_MIN_HEIGHT);

		divSet(div+'_drag','<table height="'+h+'" cellspacing="0" cellpadding="0" border="0"><tr><td height="'+h+'" valign="middle">'+SCROLL_DRAG+'</td></tr></table>')

		this.scrollheight	= pxToInt(this.scroll_height) - pxToInt(this.ds.height);

		this.dragstep		= Math.ceil( this.scrollheight / pxToInt(this.ds.height) );
		this.step			= Math.ceil( (this.height - this.clip_height) / (this.scrollheight/this.dragstep) );

		// finalize object config
		this.ds.visibility	= S;

		// register up / down / scroll hit area's
		this.hit			= new Array();
		this.hit[0]			= new Area( sref(div+'_up'), 0, this);
		this.hit[1]			= new Area( sref(div+'_down'), 1, this);
		this.hit[2]			= new Area( this.ss, 2, this);

		for (var i in this.hit){HIT_AREAS=HIT_AREAS.concat(this.hit[i])}

		eventListen();
	} else {
 		//this.ds.height	= intToPx(pxToInt(this.scroll_height));
		this.ds.visibility	= H;
		this.ss.visibility	= H;
		divHide('content_protect');
		divHide('content_up');
		divHide('content_down');
		//divSet(div+'_protect','<div style="width:100%; height:100%; background-color:#211F60"></div>');
		//divSet(div+'_protect','<div style="width:100%; height:100%; background-color:#FFFFFF"></div>');
	}

}

function Mouse(e){return new Point(e?e.pageX:(event.clientX+D.body.scrollLeft),e?e.pageY:(event.clientY+D.body.scrollTop))}
function Point(x,y) {
	this.x=x;
	this.y=y;
	this.isWithin = function(area){
		return ( ( this.x >= area.left ) && ( this.x <= area.right ) && ( this.y <= area.bottom ) && ( this.y >= area.top ) )
	}
}

function Area( s, t, parent ) {
	this.parent		= parent;
	this.type		= t;
	this.left		= pxToInt(s.left);
	this.top		= pxToInt(s.top);
	this.right		= this.left + pxToInt(NN?s.clip.width:s.width);
	this.bottom		= this.top + pxToInt(NN?s.clip.height:s.height);
	this.action		= function(e) {
		clearTimeout(TIMER);
		switch(this.type){
			case 0:scrollUp();break;
			case 1:scrollDown();break;
			case 2:moveMouse(new Mouse(e));break;
		}
	}
}

// helper functions
function pxToInt(s){return parseInt((new String(s)).replace(/px/,''))}
function intToPx(i){return NN?i:i+'px'}

function getArea( point ) {
	for ( var i in HIT_AREAS ) {
		if ( point.isWithin( HIT_AREAS[i] ) ) {
			return HIT_AREAS[i]
		}
	}
	return false
}

function boundry(o){
	if ( pxToInt(o.cs.top) < ( o.clip_height - o.height ) || pxToInt(o.ds.top) > (pxToInt(o.ss.top) + pxToInt(o.ss.height)) - pxToInt(o.ds.height) ) {
		o.cs.top = intToPx(o.clip_height - o.height);
		o.ds.top = intToPx(pxToInt(o.ss.top) + pxToInt(o.scroll_height) - pxToInt(o.ds.height));
	} else 	if ( pxToInt(o.cs.top) > 0 || pxToInt(o.ds.top) < pxToInt(o.ss.top)) {
		o.cs.top = intToPx(0);
		o.ds.top = intToPx(pxToInt(o.ss.top));
	}
}

function scrollUp(){
	if ( MOUSE_IS_DOWN && TARGET ) {
		var o		= TARGET.parent;
		var t;
		o.cs.top	= intToPx((t=(o.cs.top?pxToInt(o.cs.top):0)) + SCROLL_SPEED);
		o.ds.top	= intToPx(pxToInt(o.ss.top) + ( o.scrollheight / ( ( o.height - o.clip_height) / SCROLL_SPEED ) ) * ( Math.abs(t)/SCROLL_SPEED ));
		boundry(o);
		TIMER	 	= setTimeout('scrollUp()',25);
	}
}

function scrollDown(){
	if ( MOUSE_IS_DOWN && TARGET ) {
		var o		= TARGET.parent;
		var t;
		o.cs.top	= intToPx((t=(o.cs.top?pxToInt(o.cs.top):0)) - SCROLL_SPEED);
		o.ds.top	= intToPx(pxToInt(o.ss.top) + ( o.scrollheight / ( ( o.height - o.clip_height ) / SCROLL_SPEED ) ) * ( Math.abs(t)/SCROLL_SPEED ))
		boundry(o);
		TIMER	 	= setTimeout('scrollDown()',25);
	}
}

function moveMouse(mouse){
	if ( MOUSE_IS_DOWN && TARGET ) {
		var o		= TARGET.parent;
		var dtop	= mouse.y - (pxToInt(o.ds.height)/2);
		o.ds.top	= intToPx(dtop);
		o.cs.top	= intToPx(0 - ((dtop - pxToInt(o.ss.top))/o.dragstep ) * o.step);
		boundry(o);
	}
}


// event handlers
function mouseDown(e){
	MOUSE_IS_DOWN=true;
	TARGET=getArea(new Mouse(e))
	if(TARGET){
		TARGET.action(e)
	}
	return NN6?false:true
}
function mouseMove(e){if(MOUSE_IS_DOWN&&TARGET){TARGET.action(e)}}
function mouseUp(){MOUSE_IS_DOWN=TARGET=false;clearTimeout(TIMER)}

function keyPress(e) {
	MOUSE_IS_DOWN	= true;
	var up			= HIT_AREAS[0];
	var down		= HIT_AREAS[1];
	if (NN) {
		if ( e.which == 30 ) {
			TARGET	= up;
			scrollUp();
		}
		if ( e.which == 31 ) {
			TARGET	= down;
			scrollDown();
		}
	} else {
		e=e?e:event;
		if ( e.keyCode == 38 ) {
			TARGET	= up;
			scrollUp();
		}
		if ( e.keyCode == 40 ) {
			TARGET	= down;
			scrollDown();
		}
	}
	mouseUp();
}

// initialize event capturing
function eventListen() {
	if ( !D.getElementById ) {
		D.captureEvents( Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP | Event.KEYPRESS );
		window.onresize = function(){ L.reload() }
	}
	D.onmousedown	= mouseDown;
	D.onmousemove	= mouseMove;
	D.onmouseup		= mouseUp;
	D.onkeypress	= keyPress;
	D.onkeydown		= keyPress;
}