/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 **************************************************/
 window.onmousewheel = document.onmousewheel = function( e ){
	if( wheeling == null ){ return; }
	/*var delta = 0;
	e = fixE( e );
	if (e.wheelDelta) { // IE/Opera. 
		delta = e.wheelDelta/3;
		// In Opera 9, delta differs in sign as compared to IE. 
		if (window.opera)
			delta = -delta;
	} else if (e.detail) { // Mozilla case. 
		// In Mozilla, sign of delta is different than in IE.
		 // Also, delta is multiple of 3.					 
		delta = -e.detail/3;
	};
	var ny = wheeling.scrollTop - ( 5 * delta );
	//if( ny < wheeling.minY ){ ny = wheeling.minY; }
	//else if( ny > wheeling.maxY ){ ny = wheeling.maxY; }
	//wheeling.scrollTop = ny;*/
	wheeling.y = wheeling.scrollTop;
	try{ wheeling.onDrag( 0 , 0 , 0 , 0 );}catch(e){}
}; 
if (window.addEventListener){
	// DOMMouseScroll is for mozilla. 
	window.addEventListener('DOMMouseScroll', window.onmousewheel, false);
}
var dragging = null , wheeling = null, docY;
function makeDraggable( obj , target , minX , maxX , minY , maxY , dir ){
	obj.moving = target;
	obj.target = target;
	obj.moving.minX = minX;
	obj.moving.maxX = maxX;
	obj.moving.minY = minY;
	obj.moving.maxY = maxY;
	obj.minX = minX;
	obj.maxX = maxX;
	obj.minY = minY;
	obj.maxY = maxY;
	obj.moving.y = obj.moving.offsetTop;
	obj.moving.x = obj.moving.offsetLeft;
	obj.moving.d = ( dir != null ) ? dir : 1;
	obj.moving.lastY = null;
	obj.fixE = function(e){
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	};
	obj.recalc = recalc;
	obj.onmousedown = function(e){
		e = this.fixE(e);
		//try{ if( this.originalMousDown != this.onmousedown ){ this.originalMousDown( e ); } } catch( er ){ }
		this.moving.lastX = e.clientX;
		this.moving.lastY = e.clientY;
		//this.lastX = e.clientX;
		//this.lastY = e.clientY;
		dragging = this.moving;
		//document.getElementById( "logo_div" ).innerHTML = "( " + dragging.y + " , " + dragging.lastY + " , " + e.clientY + " ) ";
		document.onmousemove	= dragging.drag;
		document.onmouseup		= dragging.end;

		try{ this.onDragStart( e.clientX , e.clientY ); alert(this.onDragStart);} catch( er ){}
		return false;
	};
	target.drag = function( e ){
		try{e = fixE(e);
		//document.getElementById( "logo_div" ).innerHTML = document.getElementById( "logo_div" ).innerHTML + "-> ( " + dragging.y + " , " + dragging.lastY + " , " + e.clientY + " ) ";
		var x = e.clientX - dragging.lastX;
		var y = e.clientY - dragging.lastY;
		
		var ny = dragging.y + dragging.d * y;
		var nx = dragging.x + x;
		
		dragging.lastX = ( nx > dragging.minX && nx < dragging.maxX ) ? e.clientX : dragging.lastX;
		dragging.lastY = ( ny > dragging.minY && ny < dragging.maxY ) ? e.clientY : dragging.lastY;
		
		
		if( nx < dragging.minX ){ nx = dragging.minX; }
		else if( nx > dragging.maxX ){ nx = dragging.maxX; }
		if( ny < dragging.minY ){ ny = dragging.minY; }
		else if( ny > dragging.maxY ){ ny = dragging.maxY; }
		
		dragging.y = ny;
		dragging.x = nx;
		
		dragging.style.top = ny + "px";
		dragging.style.left = nx + "px";
		
		dragging.onDrag( e.clientX , e.clientY , nx , ny );
		}catch( er ){  }
		return false;
	};
	target.end = function( e ){
		e = fixE(e);
		try{ dragging.onDragEnd(); } catch( ee ){}
		dragging = null;
		document.onmousemove = document.onmouseup = null;
	};

}		
function makeWheelable( obj ){
	obj.onmouseover = function( ){
		wheeling = this;
		docY = document.body.scrollTop;
	};
	obj.onmouseout = function( e ){
		wheeling = null;
	};
}
function makeClickDragable( scr ){
	var scrollArea = scr.parentNode;
	scrollArea.scr = scr;
    scrollArea.onmousedown = function (evt) {
		evt = fixE( evt );
		var sH = this.scr;
	   var scrollY = evt.clientY + document.body.scrollTop - totalOffsetTop( this );
	   //click below the scroller on the bar
	   if( scrollY > sH.offsetTop + sH.offsetHeight ){
	   		scrollY -= Math.max( .835 * sH.offsetHeight , 7 );
		}
		//click above
	   else if( scrollY < sH.offsetTop ){
	   		scrollY -= Math.max( .165 * sH.offsetHeight , 7 );
		}
		//click on
	   else{ return; }
	   scrollY = within( scrollY , sH.minY ,  sH.maxY );
	   sH.style.top = scrollY + "px";
	   sH.y = sH.offsetTop;
	   sH.onmousedown( evt );
	   document.onmousemove( evt );
    }
}
function dragStartForFlip( x , y ){
	this.startX = x;
} 
function placeTick( mx , my , x , y ){
	var div = this, placer = document.getElementById( "content_placer" );
	var y = div.offsetTop * ( placer.offsetHeight - 5 ) / ( div.offsetHeight - placer.offsetHeight );
	document.getElementById( "tick" ).style.top = Math.abs( y ) + "px";
	var dX = div.startX - mx;
	if( Math.abs( dX ) > 80 ){
		var div = document.getElementById( 'about_subs' );
		var divs = [];
		for( var i = 0; i < div.childNodes.length; i++ ){
			if( div.childNodes[ i ].nodeName != null && div.childNodes[ i ].nodeName.toLowerCase() == "div" ){
				div.childNodes[ i ].style.background = "none";
				divs.push( div.childNodes[ i ]  );
			}
		}
		var target = ( div.selected != null ) ? div.selected : 0;
		divs[ target ].style.background = "none";
		target = target - ( Math.abs( dX ) / ( dX ) );
		target = ( target >= 0 ) ? target % divs.length : divs.length + target; 
		div.selected = target;
		divs[ target ].style.backgroundColor = "#ffffff";
		this.startX = mx;
	}
}

function recalc(){
	this.scrollH = ( this.target.offsetHeight * this.parentNode.offsetHeight) / this.target.scrollHeight;
	this.isScrolling = ( this.target.offsetHeight < this.target.scrollHeight );
	if( !this.isScrolling ){ 
		//so as not to fuck up search hiding we don't set visibility
		//alert( this.id + " -> " + this.target.id + ": " + this.target.offsetHeight + " < " + this.target.scrollHeight );
		this.style.opacity = 0;
		this.style.filter = "alpha(opactiy=0)";
		this.style.height = "1px";
		this.innerHTML = "";
		this.style.top = this.minY + "px";
		this.scrollDist = 1;
		
	} else {		
		//so as not to fuck up search hiding we don't set visibility
		this.style.opacity = 1;
		this.innerHTML = '<div class="scroller_top"></div><div id="scroller_middle" class="scroller_middle"></div><div id="scroller_bottom" class="scroller_bottom"></div>';
		this.style.filter = "alpha(opactiy=100)";
		var MH = within( this.scrollH - 12 , 0 , this.parentNode.offsetHeight );
		//NEED DIFERENT FINDING MECHANISM
		for( var i = 0; i < this.childNodes.length; i++ ){
			if( this.childNodes[ i ].className == "scroller_middle" ){
				this.childNodes[ i ].style.height = MH + "px";
			} else if ( this.childNodes[ i ].className == "scroller_bottom" ){
				this.childNodes[ i ].style.top = ( MH + 6 ) + "px";
			}
		}
		this.style.height = this.scrollH + "px";
		this.scrollDist = Math.round(this.parentNode.offsetHeight - this.scrollH);
		
		this.maxY = this.scrollDist - ( this.minY / 2 );
		this.target.onDrag( 0 , 0 , 0 , 0 );
	}
	
}

function lightCat( mx , my , x , y ){
	var div = document.getElementById( 'about_cats' );
	var divs = [];
	for( var i = 0; i < div.childNodes.length; i++ ){
		if( div.childNodes[ i ].nodeName != null && div.childNodes[ i ].nodeName.toLowerCase() == "div" ){
			div.childNodes[ i ].style.background = "none";
			divs.push( div.childNodes[ i ]  );
		}
	}
	var target = Math.round( ( divs.length - 1 ) * y / this.maxY );
	divs[ target ].style.backgroundColor = "#ffffff";
}
function selectCat( x , y ){
	var div = document.getElementById( 'about_cats' );
	var divs = [];
	for( var i = 0; i < div.childNodes.length; i++ ){
		if( div.childNodes[ i ].nodeName != null && div.childNodes[ i ].nodeName.toLowerCase() == "div" ){
			div.childNodes[ i ].style.background = "none";
			divs.push( div.childNodes[ i ]  );
		}
	}
	y = this.offsetTop;
	var target = Math.round( ( divs.length - 1 ) * y / this.maxY );
	divs[ target ].style.backgroundColor = "#ffffff";
	div.selected = target;
	this.y = this.maxY * target / ( divs.length - 1 );
	this.style.top = this.y + "px";
	divs[ target ].getContent();
}
function lightSub( mx , my , x , y ){
	var div = document.getElementById( 'about_subs' );
	var divs = [];
	for( var i = 0; i < div.childNodes.length; i++ ){
		if( div.childNodes[ i ].nodeName != null && div.childNodes[ i ].nodeName.toLowerCase() == "div" ){
			div.childNodes[ i ].style.background = "none";
			divs.push( div.childNodes[ i ]  );
		}
	}
	var target = Math.round( ( divs.length - 1 ) * x / this.maxX );
	divs[ target ].style.backgroundColor = "#ffffff";
}
function selectSub( x , y ){
	var div = document.getElementById( 'about_subs' );
	var divs = [];
	for( var i = 0; i < div.childNodes.length; i++ ){
		if( div.childNodes[ i ].nodeName != null && div.childNodes[ i ].nodeName.toLowerCase() == "div" ){
			div.childNodes[ i ].style.background = "none";
			divs.push( div.childNodes[ i ]  );
		}
	}
	x = this.offsetLeft;
	var target = Math.round( ( divs.length - 1 ) * x / this.maxX );
	divs[ target ].style.backgroundColor = "#ffffff";
	div.selected = target;
	this.x = this.maxX * target / ( divs.length - 1 );
	this.style.left = this.x + "px";
	divs[ target ].getContent();
}
function searchScroll( ex , ey , nx , ny ){
	try{var doc = this.target;
	var scrollY = this.offsetTop - this.minY;
	doc.scrollTop = scrollY * ( doc.scrollHeight - doc.offsetHeight) / ( this.maxY - this.minY );}catch(e){alert(e);}
}
function setScrollBar( x , y , xx , yy ){
	this.target.y = this.target.minY + this.scrollTop * this.target.maxY / ( this.scrollHeight - this.offsetHeight );
	this.target.y = within( this.target.y , this.target.minY , this.target.maxY );
	this.target.style.top = this.target.y + "px";
};