/** (C) CopyRight 2009. Andry V Trushin
 ** JavaScript Framework. Utils
 **/
var imgPath = '/media/design/';

var IDX = new function Indexer()
{
	var index = 0;

	this.get = function()
	{
		return index++;
	}
	this.over = function()
	{
		return index;
	}
	this.reset = function()
	{
		index = 0;
	}
}
var EF = new function()
{
    this.last = null;
    this.root = "document.body";

    this.create = function( tagName )
    {
        this.last = document.createElement( tagName );
        return this.last;
    }
    this.container = function( cont, el )
    {
        if( cont == null )
            cont = eval( this.root );
        if( el == null )
            cont.appendChild( this.last );
        else
            cont.appendChild( el );
    }
    this.set = function( el )
    {
        this.container( null, el );
    }
    this.createnew = function( tagName )
    {
        this.create( tagName );
        this.set( null );
        return this.last;
    }
    this.remove = function( cont, el )
    {
        if( cont == null )
            cont = eval( this.root );
        cont.removeChild( el );
        this.last = null;
    }
}
var Control = new function()
{
	this.master = null;

    this.find = function ( type )
    {
		var cont = ( this.master == null )? document.body : this.master;
        return this.findIn( cont, type );
    }
    this.findIn = function ( el, type )
    {
        var els = [];
        var nodes = el.childNodes;
		for( var i = 0; i < nodes.length; i++ )
        {
            var n = nodes[ i ];
			var ntype = n.nodeType;
			if( ntype == 1 && n.parentNode == el )
            {
                var attr = n.getAttribute( "control" );
                if( attr && attr == type )
                    els[ els.length ] = n;
                else
                    if( n.nodeName.toLowerCase() == type )
                        els[ els.length ] = n;
			}
		}
        return els;
    }
    this.findElement = function ( type )
    {
		var cont = ( this.master == null )? document.body : this.master;
        var els = [];
        this.findEl( cont, type, els );
        return els;
    }
    this.findEl = function ( el, name, els )
    {
        var nodes = el.childNodes;
		for( var i = 0; i < nodes.length; i++ )
        {
            var n = nodes[ i ];
            if( n.tagName == name )
            {
                els[ els.length ] = n;

            }
            if( n.nodeType == 1 )
                this.findEl( n, name, els );
        }
    }
}
var blankWindow = function( url, width, height )
{
	return window.open( url, '', 'width=' + width + ',height=' + height + ',toolbar=no,menubar=no,resizable=no,status=no' );
}
var coord = function ( el )
{
    var l = 0;
    var t = 0;

    while( el )
    {
        l += el.offsetLeft;
        t += el.offsetTop;
        el = el.offsetParent;
    }
    return { "left":l, "top":t };
}
var getParentDiv = function( el )
{
	while( el.tagName != "DIV" && el.tagName != "BODY" )
		el = el.parentNode;
    return el;
}
var getParentTable = function( el )
{
	while( el.tagName != "TABLE" && el.tagName != "BODY" )
		el = el.parentNode;
    return el;
}
var tag = function( id )
{
    return document.getElementById( id );
}
var tags = function( name )
{
    return document.getElementsByName( name );
}
var isNull = function( value )
{
    return value == null;
}
var isNullOrSpace = function( value )
{
    return value == null || value == '';
}
var getScrollTop = function()
{
    return self.pageYOffset ||( document.documentElement && document.documentElement.scrollTop )||( document.body && document.body.scrollTop );
}
var getScrollLeft = function()
{
    return self.pageXOffset ||( document.documentElement && document.documentElement.scrollLeft )||( document.body && document.body.scrollLeft );
}
var preloadImages = function()
{
    for( var i = 0; i < arguments.length; i++ )
    {
        this[ i ] = new Image();
        this[ i ].src = imgPath + arguments[ i ];
    }
    return this;
}
var png4ie = function ( obj )
{
    var ver = (uag.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1];
    if( /msie/.test( uag ) && !/opera/.test( uag ) && ver <= 7 )
	{
		var src;
		if( obj.tagName == 'IMG' )
		{
			if( /\.png$/.test( obj.src ))
			{
				src = obj.src;
				obj.src = "/media/design/blank.gif";
			}
		}
		else
		{
			src = obj.currentStyle.backgroundImage.match( /url\("(.+\.png)"\)/i );
			if( src )
			{
				src = src[ 1 ];
				obj.runtimeStyle.backgroundImage = "none";
			}
		}
		if( src )
            obj.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
	}
}
var correctBorderHeight = function( num )
{
    return ( browser.msie ? num : ( num - 2 )) + 'px';
}
var notBubble = function( e )
{
    if( browser.msie )
        e = window.event;
    if( e.stopPropagation )
        e.stopPropagation();
    else
        e.cancelBubble = true;
    if( e.preventDefault )
        e.preventDefault();
    else
        e.returnValue = false;
}

