﻿//global extend
MyIEOSLib = "MyIEOS Lib Version 1.0";
if( navigator.userAgent.toLowerCase().indexOf( "msie" ) != -1 && ( document.all && navigator.userAgent.toLowerCase().indexOf( "opera" ) == -1 ) )
{
    __browerCategory = "ie";    
}
else
{
    __browerCategory = "opera";
}    

Request = {
    QueryString : function( key ){
        var svalue = window.location.search.match( new RegExp( "[\?\&]" + key + "=([^\&]*)(\&?)", "i" ) );
        return svalue ? svalue[1] : svalue;
    }
};

//runtime functions
if( __browerCategory == "opera" )
{
    HTMLDocument.prototype.__defineGetter__( "parentWindow", function () { return this.defaultView; } );
    HTMLDivElement.prototype.setCapture = function(){
        if( typeof this.__EventHandler == "function" )
        {
            this.ownerDocument.addEventListener( "mousemove", this.__EventHandler, true );
            this.ownerDocument.addEventListener( "mouseup", this.__EventHandler, true );
        }
    };
    HTMLDivElement.prototype.releaseCapture = function(){
        if( typeof this.__EventHandler == "function" )
        {
            this.ownerDocument.removeEventListener( "mousemove", this.__EventHandler, true );
            this.ownerDocument.removeEventListener( "mouseup", this.__EventHandler, true );
        }
    };    
    HTMLElement.prototype.insertAdjacentHTML = function( where, html ){ 
        var e = this.ownerDocument.createRange(); 
        e.setStartBefore( this ); 
        e = e.createContextualFragment( html ); 
        switch( where ) 
        { 
            case "beforeBegin": this.parentNode.insertBefore(e, this); break; 
            case "afterBegin": this.insertBefore( e, this.firstChild ); break; 
            case "beforeEnd": this.appendChild( e ); break; 
            case "afterEnd": 
            if( !this.nextSibling ) this.parentNode.appendChild( e ); 
            else this.parentNode.insertBefore( e, this.nextSibling ); break; 
        } 
    }; 
}

//Date
Date.prototype.oldToString = Date.prototype.toString;
Date.prototype.toString = function(){
    if( arguments.length == 0 ) return this.oldToString();
    if( arguments.length == 1 )
    {
        var instance = this;
        return arguments[0].replace( /(yyyy|yy|MM|M|dd|d|hh|h|mm|m|ss|s)/g, function(){
            switch( arguments[1] )
            {
                case "yyyy":
                    return instance.getFullYear();
                case "yy":
                    return instance.getFullYear().toString().substr( 2, 2 );
                case "MM":
                    return instance.getMonth() < 9 ? "0" + ( instance.getMonth() + 1 ).toString() : instance.getMonth() + 1;
                case "M":
                    return instance.getMonth() + 1;
                case "dd":
                    return instance.getDate() < 10 ? "0" + instance.getDate().toString() : instance.getDate();
                case "d":
                    return instance.getDate();
                case "hh":
                    return instance.getHours() < 10 ? "0" + instance.getHours().toString() : instance.getHours();
                case "h":
                    return instance.getHours();
                case "mm":
                    return instance.getMinutes() < 10 ? "0" + instance.getMinutes().toString() : instance.getMinutes();
                case "m":
                    return instance.getMinutes();
                case "ss":
                    return instance.getSeconds() < 10 ? "0" + instance.getSeconds().toString() : instance.getSeconds();
                case "s":
                    return instance.getSeconds();
                default:
                    return arguments[1];
            }
        } );
    }
    return this.toLocaleString();
};

//Inherit Support
Object.prototype.__Inherit = function( base, args ){
    switch( typeof base )
    {
        case "undefined":
            return false;
        case "object":
        case "function":
            if( arguments.length > 1 )
            {
                var evalString = "base.call( this";
                for( var i = 1; i < arguments.length; i ++ )
                {
                    evalString += ", arguments[" + i.toString() + "]";
                } 
                evalString += " );";
                eval( evalString );
            }
            else
            {
                base.call( this );
            }
            return true;
        case "string":
            return eval( "this.__Inherit( " + base + ", args );" );
    }
    return false;
};

Object.prototype.GetTypeName = function(){
    if( typeof this.__TypeName == "string" )
    {
        return this.__TypeName;
    }
    return "unknown";
};

Object.prototype.Clone = function(){
    if( this == null ) return null;
    var newObj = {};
    for( var x in this )
    {
        newObj[x] = this[x];
    }
    return newObj;
};

Object.prototype.Release = function(){
    
};

function $Equals( l, r )
{
    switch( typeof l )
    {
        case "undefined" : return false;
        case "function" : break;
        case "object" :
            if( l == null || r == null )
            {
                if( l != r ) return false;
                break;
            }
            if( l.toString() != r.toString() ) return false;
            for( var x in r )
            {
               if( !$Equals( l[x], r[x] ) ) return false;
            }
            break;
        default:
            if( l != r ) return false;
            break;
    }
    return true;
}

function $( obj, contextDocument ){
    if( typeof contextDocument == "undefined" || contextDocument == null ) contextDocument = document;
    switch( typeof obj )
    {
        case "string":
            return contextDocument.getElementById( obj );
        case "object":
            return obj;
        case "undefined":
            return null;
    }
    return null;
}


function $FrameDocument( id, contextDocument ){
        var frameNode = $( id, contextDocument );
        if( frameNode == null ) return null;
        return frameNode.contentWindow.document;
}

function $Eval( expression, __this, filterExpression, replaceFunction )
{
    expression = expression.replace( /(\r\n|\r|\n)/g, function(){
        if( arguments[0] == "\r\n" ) return "?#@!rn!@#?";
        if( arguments[1] == "\r" ) return "?#@!n!@#?";
        return "?#@!r!@#?";
    } );
    if( typeof __this == "object" && __this != null )
    {
        expression = expression.replace( /\$this\$/g, function(){
                return "__this";
            } );
    }
    var replaceResultArray = new Array();
    if( typeof filterExpression != "undefined" && typeof replaceFunction == "function" )
    {
        expression = expression.replace( filterExpression, function(){
            var filterArguments = new Array();
            var callString = "replaceFunction( ";
            for( var i = 0; i < arguments.length - 1; i ++ )
            {
                callString +=  "filterArguments[" + i + "], ";
                filterArguments[ i ] = arguments[ i ];
            }
            callString += "filterArguments[ " + ( arguments.length - 1 ) + "] )";
            filterArguments[ arguments.length - 1 ] = arguments[ arguments.length - 1 ];
            try
            {
                replaceResultArray[ replaceResultArray.length ] =  eval( callString );
                return "replaceResultArray[" + ( replaceResultArray.length - 1 ) + "]";
            }
            catch( ex )
            {
                replaceResult = null;
                return "[code]error on invoke replace function with filter expression \"" + filterExpression + "\"[/code]";
            }
        } );
    }
    try
    {
       var r = eval( expression );
       if( typeof r == "undefined" ) return "";
       return r;
    }
    catch( ex )
    {
        return "[code]" + expression + "[/code]\r\nexpression error: " + ex.description;
    }  
    return expression.replace( /\?\#\@\!(.*?)\!\@\#\?/g, function(){
            if( arguments[1] == "rn" ) return "\r\n";
            if( arguments[1] == "r" ) return "\r";
            return "\n";
        });
}

function $EvalEx( expression, __this, filterExpression, replaceFunction )
{
    if( typeof expression == "undefined" || expression == null || expression == "" ) return null;
    expression = expression.replace( /(\r\n|\r|\n)/g, function(){
        if( arguments[0] == "\r\n" ) return "?#@!rn!@#?";
        if( arguments[1] == "\r" ) return "?#@!n!@#?";
        return "?#@!r!@#?";
    } );
    
    var replaceResultArray = new Array();
    
    expression = expression.replace( /(\[\%#?\s*)(.*?)(\s*\%\])/g, function(){
        var codeString = arguments[2].replace( /\?\#\@\!(.*?)\!\@\#\?/g, function(){
            if( arguments[1] == "rn" ) return "\r\n";
            if( arguments[1] == "r" ) return "\r";
            return "\n";
        });
        return $Eval( codeString, __this, filterExpression, replaceFunction );
    } );
       
    return expression.replace( /\?\#\@\!(.*?)\!\@\#\?/g, function(){
            if( arguments[1] == "rn" ) return "\r\n";
            if( arguments[1] == "r" ) return "\r";
            return "\n";
        });        
}
  
if( typeof $ObjectCache == "undefined" )
{
    $ObjectCache = {
        
        __ObjectTypeDic : {},
        
        Allote : function( typeName ){
            var typeCache = this.__ObjectTypeDic[ typeName ];
            if( typeCache == null || typeCache.length == 0 ) return null;
            return typeCache.pop();
        },
        
        Recycle : function( typeName, obj ){
            var typeCache = this.__ObjectTypeDic[ typeName ];
            if( typeCache == null )
            {
                typeCache = new Array();
                this.__ObjectTypeDic[ typeName ] = typeCache;
            }
            typeCache.push( obj );
            return true;
        },
            
        GetCacheCount : function( typeName ){
            var typeCache = this.__ObjectTypeDic[ typeName ];
            if( typeCache == null || typeCache.length == 0 ) return 0;
            return typeCache.length;
        }
    };
}

