/* * *
	developer: bitl@mail.ru
 * * */

function pause( msec ){
        d = new Date();
        while(1){
                mill = new Date();
                diff = mill - d;
                if( diff > msec ){ break; }
        }
}

/* * *
	Point class
 * * */

function Point( x, y ){
	this.x = x;
	this.y = y;

	this.distanceTo = Point_distanceTo;
	this.distanceToPoint = Point_distanceToPoint;
}

function Point_distanceTo( x, y ){
	return Math.sqrt( Math.pow( ( this.x - x ), 2 ) + Math.pow( ( this.y - y ), 2 ) );
}

function Point_distanceToPoint( p ){
	return this.distanceTo( p.x, p.y );
}

/* * *
	OrderedRect class
 * * */

function OrderedRect( x1, y1, x2, y2 ){
	this.squarizeByLessSide = OrderedRect_squarizeByLessSide;

	this.assign = OrderedRect_assign;
	this.assignPoints = OrderedRect_assignPoints;
	this.minX = OrderedRect_minX;
	this.minY = OrderedRect_minY;
	this.maxX = OrderedRect_maxX;
	this.maxY = OrderedRect_maxY;
	this.diagonal = OrderedRect_diagonal;
	this.area = OrderedRect_area;
	this.greaterSide = OrderedRect_greaterSide;
	this.lessSide = OrderedRect_lessSide;
	this.halfWidth = OrderedRect_halfWidth;
	this.halfHeight = OrderedRect_halfHeight;
	this.width = OrderedRect_width;
	this.height = OrderedRect_height;
	this.applyConstraint = OrderedRect_applyConstraint;

	this.assign( x1, y1, x2, y2 );
}

function OrderedRect_squarizeByLessSide(){
	ls = this.lessSide();
	this.max_point.x = this.min_point.x + ls;
	this.max_point.y = this.min_point.y + ls;
}

function OrderedRect_assign( x1, y1, x2, y2 ){
	this.min_point = new Point( Math.min( x1, x2 ), Math.min( y1, y2 ) );
	this.max_point = new Point( Math.max( x1, x2 ), Math.max( y1, y2 ) );
}

function OrderedRect_assignPoints( min_point, max_point ){
	this.min_point = min_point;
	this.max_point = max_point;
}

function OrderedRect_minX(){ return this.min_point.x; }

function OrderedRect_minY(){ return this.min_point.y; }

function OrderedRect_maxX(){ return this.max_point.x; }

function OrderedRect_maxY(){ return this.max_point.y; }

function OrderedRect_diagonal(){
	return this.min_point.distanceToPoint( this.max_point );
}

function OrderedRect_area(){
	return ( this.maxX() - this.minX() ) * ( this.maxY() - this.minY() );
}

function OrderedRect_greaterSide(){
	return Math.max( this.maxX() - this.minX(), this.maxY() - this.minY() );
}

function OrderedRect_lessSide(){
	return Math.min( this.maxX() - this.minX(), this.maxY() - this.minY() );
}

function OrderedRect_halfWidth(){ return ( this.max_point.x - this.min_point.x )/2; }

function OrderedRect_halfHeight(){ return ( this.max_point.y - this.min_point.y )/2; }

function OrderedRect_width(){ return this.max_point.x - this.min_point.x; }

function OrderedRect_height(){ return this.max_point.y - this.min_point.y; }

function OrderedRect_applyConstraint( cns_rect, fixedSize ){
	if( this.minX() < cns_rect.minX() ){ // fix to right
		if( fixedSize ){ this.max_point.x += cns_rect.minX() - this.minX(); }
		this.min_point.x = cns_rect.minX();
		//window.alert( "this.min_point.x: "+ this.min_point.x );
	}
	if( this.maxX() > cns_rect.maxX() ){ // fix to left
		if( fixedSize ){ this.min_point.x -= this.maxX() - cns_rect.maxX(); }
		this.max_point.x = cns_rect.maxX();
		//window.alert( "this.max_point.x: "+ this.max_point.x );
	}
	if( this.minY() < cns_rect.minY() ){ // fix to bottom
		if( fixedSize ){ this.max_point.y += cns_rect.minY() - this.minY(); }
		this.min_point.y = cns_rect.minY();
		//window.alert( "this.min_point.y: "+ this.min_point.y );
	}
	if( this.maxY() > cns_rect.maxY() ){ // fix to top
		if( fixedSize ){ this.min_point.y -= this.maxY() - cns_rect.maxY(); }
		this.max_point.y = cns_rect.maxY();
		//window.alert( "this.max_point.y: "+ this.max_point.y );
	}
}

/* * *
 * * */


function parseInt2( sInt ){
	var i = parseInt( sInt );
	if( isNaN(i) ) return 0;
	else return i;
}


function GetPositionPoint( oElement ){
        var oOrgElm = oElement;
        var iX=0;
        var iY=0;
        var iCount;
        var oElement2;
        var oElementOffset = oElement.offsetParent;

        while( oElement ){
                oElement2 = oElement.parentNode;
                if( oElementOffset == oElement ){
                        iX += oElement.offsetLeft;
                        iY += oElement.offsetTop;
                        // Add for borders
                        iY += parseInt2(oElement.style.borderTopWidth);
                        iX += parseInt2(oElement.style.borderLeftWidth);
                        // Add for scrolling
                        iX -= parseInt2(oElement.scrollLeft);
                        iY -= parseInt2(oElement.scrollTop);
                        // Get the next offsetParent
                        oElementOffset = oElementOffset.offsetParent;
                }else{
                        iX -= parseInt2(oElement.scrollLeft);
                        iY -= parseInt2(oElement.scrollTop);
                }
                oElement = oElement2;
        }

        iX += parseInt2( document.body.scrollLeft );
        iY += parseInt2( document.body.scrollTop );

	return new Point( iX, iY );
}

function DumpObject( obj ){
	s = "";
	//for( var i in document.getElementById( oid ) ){ s += i + " : "; }
	//for( var i in obj ){ s += i +"= "+ obj[i] +";   "; }
	for( var i in obj ){ s += i +";  "; }
	window.alert( s );
	//window.open("./help.phtml","help","width=750,height=580,resizab
	//le=yes,location=yes,status=no,menubar=yes,scrollbars=yes,toolbar=yes");
}

