#if defined _old_0_included
  #endinput
#endif
#define _old_0_included

#include <strutil.inc>

// ---------------------------------------------------------------------
stock bool:xmlGetAttr( const _tagname_[], const _id_[], const _attr_[], _value_[], _bufsize_ = sizeof _value_ )
{
	//printf( "xmlGetAttr \n" );

	if( _tagname_[0]==0 || _id_[0]==0 ) return false;

	new xmltag: tag = domFindTag( domRoot(), _tagname_, _id_ );
    if( tag )
    {
        new bool:ret = domGetData( tag, _attr_, _value_, _bufsize_ );

        // 属性名が src か href の場合は、fullpath に変換
        if( strcmp(_attr_,"src")==0 || strcmp(_attr_,"href")==0 )
        {
            xmlMakeFullPath( _value_, _bufsize_ );
        }

        return ret;
    }
    return false;
}

stock xmlGroupNum( const tag[], const group[] )
{
	//printf( "xmlGroupNum \n" );

    new count = 0;
    new xmltag: root = domRoot();
    for( new xmltag:child = domFirstChild(root,tag) ; child ; child=domNext(child,tag) )
    {
        new str_group[64];
        domGetData( child, "group", str_group );
        if( strcmp( str_group, group )==0 )
        {
            count ++;
        }
    }
    return count;
}

stock bool:xmlGetGroupAttr( const tag[], const group[], index, const attr[], value[], bufsize = sizeof value )
{
	//printf( "xmlGetGroupAttr \n" );

    new count = 0;
    new xmltag: root = domRoot();
    for( new xmltag:child = domFirstChild(root,tag) ; child ; child=domNext(child,tag) )
    {
        new str_group[64];
        domGetData( child, "group", str_group );

        if( strcmp( str_group, group )==0 )
        {
            if( count == index )
            {
                new bool:ret = domGetData( child, attr, value, bufsize );

                // 属性名が src か href の場合は、fullpath に変換
                if( strcmp(attr,"src")==0 || strcmp(attr,"href")==0 )
                {
                    xmlMakeFullPath( value, bufsize );
                }

                return ret;
            }
            count ++;
        }
    }
    return false;
}

stock bool:xmlGetText( const _tagname_[], const _id_[], _value_[], _bufsize_ = sizeof _value_ )
{
	//printf( "xmlGetText \n" );

    if( _tagname_[0]==0 || _id_[0]==0 ) return false;

	new xmltag: tag = domFindTag( domRoot(), _tagname_, _id_ );
    if( tag )
    {
        return domGetData( tag, "", _value_, _bufsize_ );
    }
    return false;
}

// ---------------------------------------------------------------------
stock SetWndVar( const _key_[], _val_ )
{
	//printf( "SetWndVar \n" );

    new xmltag: wndvar_tag = domFindTag( domRoot(), "__wndvar__" );
	if( ! wndvar_tag )
    {
        wndvar_tag = domInsertTag( domFirstChild(domRoot()), "__wndvar__" );
        //wndvar_tag = domAddTag( domRoot(), "__wndvar__" );
    }

    new xmltag: tag = domFindTag( wndvar_tag, "__wndvar_int__", _key_ );

    if( ! tag )
    {
        tag = domAddTag( wndvar_tag, "__wndvar_int__" );
        domSetData( tag, "id", _key_ );
    }

    new str_value[32 char];
    sprintf( str_value, "%d", _val_ );
    domSetData( tag, "value", str_value );
}

stock bool: GetWndVar( const _key_[], &_val_ )
{
	//printf( "GetWndVar \n" );

    new xmltag: wndvar_tag = domFindTag( domRoot(), "__wndvar__" );
    if( ! wndvar_tag ) return false;

    new xmltag: tag = domFindTag( wndvar_tag, "__wndvar_int__", _key_ );
    if( ! tag ) return false;

    new str_value[32 char];
    domGetData( tag, "value", str_value );
    _val_ = atoi( str_value );

    return true;
}

stock SetWndVarStr( const _key_[], const _val_[] )
{
	//printf( "SetWndVarStr \n" );

    new xmltag: wndvar_tag = domFindTag( domRoot(), "__wndvar__" );
	if( ! wndvar_tag )
    {
        wndvar_tag = domInsertTag( domFirstChild(domRoot()), "__wndvar__" );
        //wndvar_tag = domAddTag( domRoot(), "__wndvar__" );
    }

    new xmltag: tag = domFindTag( wndvar_tag, "__wndvar_str__", _key_ );

    if( ! tag )
    {
        tag = domAddTag( wndvar_tag, "__wndvar_str__" );
        domSetData( tag, "id", _key_ );
    }

    domSetData( tag, "value", _val_ );
}

stock bool: GetWndVarStr( const _key_[], _val_[], _bufsize_ = sizeof _val_ )
{
	//printf( "GetWndVarStr \n" );

    new xmltag: wndvar_tag = domFindTag( domRoot(), "__wndvar__" );
	if( ! wndvar_tag ) return false;

    new xmltag: tag = domFindTag( wndvar_tag, "__wndvar_str__", _key_ );

    if( ! tag ) return false;

    domGetData( tag, "value", _val_, _bufsize_ );
    return true;
}

// ---------------------------------------------------------------------
stock hisPrev()
	hisJump(-1);

stock hisNext()
	hisJump(1);

stock hisReload()
	hisJump(0);

stock hisCheckPrev()
	hisCheck(-1);

stock hisCheckNext()
	hisCheck(1);

stock hisCheckReload()
	hisCheck(0);

// ---------------------------------------------------------------------
enum
{
	LOADTIM2_BUSY = 0,
	LOADTIM2_OK = 1,
	LOADTIM2_ERROR = -1,
};

stock imgLoadTim2Start( const filepath[] )
	return imgDecStart( filepath );

stock imgLoadTim2Sync()
	return imgDecSync();

stock imgLoadTim2Wait()
	return imgDecWait();

// ---------------------------------------------------------------------
stock obj: CreateSprite(obj:parent)
	return CreateNull(parent);

stock obj: CreateStar(obj:parent)
	return CreateNull(parent);
