//------------------------------------------------------------------------------
// IPS Global JS functions
//------------------------------------------------------------------------------
// Matt Mecham Woz Here
// (c) 2005 Invision Power Services, Inc.
// http://www.invisionpower.com
//------------------------------------------------------------------------------


function ipsclass()
{
    this.cookies        = new Array();
    this.ignore_cookies = new Array( ipb_var_cookieid + 'ipb_stronghold',
                                     ipb_var_cookieid + 'session_id',
                                     ipb_var_cookieid + 'ipb_admin_session_id',
                                     ipb_var_cookieid + 'member_id',
                                     ipb_var_cookieid + 'pass_hash' );
     
    this.settings       = {
                            'do_linked_resize' : 0,
                            'resize_percent'   : 50
                          };
    
    /*-------------------------------------------------------------------------*/
    // Sort out cookies
    /*-------------------------------------------------------------------------*/
    
    this.init = function()
    {   
        var _tmp = document.cookie.split(';');
    
        if ( _tmp.length )
        {
            for( i = 0 ; i < _tmp.length ; i++ )
            {
                if ( _tmp[i].match( new RegExp( ipb_var_cookieid + ".*$" ) ) ) 
                {
                    var _data  = _tmp[i].split('=');
                    var _key   = this.trim( _data[0] );
                    var _value = unescape( this.trim( _data[1] ) );
     
                    if ( _key && ( ! this.in_array( _key, this.ignore_cookies ) ) ) 
                    {
                        this.cookies[ _key.replace( ipb_var_cookieid, '' ) ] = _value;
                    }
                }
            }
        }
        /*-------------------------------------------------------------------------*/
        // Firefox HTTP only cookies
        /*-------------------------------------------------------------------------*/
     
        if ( is_moz && ! is_safari )
        {
            HTMLDocument.prototype.__defineGetter__( "cookie", function () { return null; } );
     
            window.addEventListener( 'load', function() {
                                                            var _a = document.body;
                                                            var _x = _a.innerHTML;
                                                            var _y = new RegExp( "HTMLDocument\\.prototype", 'ig' );

                                                            if ( _x.match( _y ) )
                                                            {
                                                                _x = _x.replace( _y, 'HTMLDocument_prototype' );
                                                                _a.innerHTML = _x;
                                                            }
                                                        }, false );
        }

        _tmp = null;
    };


    //=============================================================================
    // PHP EMULATION
    //=============================================================================

    /**
    * Emulate PHPs trim function
    */
    this.trim = function( text )
    {
        if( typeof(text) == 'undefined' )
        {
            return '';
        }

        text = text.replace(/^\s+/, '');
        return text.replace(/\s+$/, '');
    };

    /**
    * Emulate PHPs html_entity_decode function
    */
    this.html_entity_decode = function( text )
    {
        if( typeof ca == 'undefined' )
        {
            var ca = document.createElement( 'textarea' );
        }

        ca.innerHTML = text.replace( /</g, '&lt;' ).replace( />/g, '&gt;' );

        return ca.value;
    };

    /**
    * Emulate PHPs in_array function
    */
    this.in_array = function( needle, haystack )
    {
        if ( ! haystack.length )
        {
            return false;
        }

        for ( var i = 0 ; i < haystack.length ; i++)
        {
            if ( haystack[i] === needle )
            {
                return true;
            }
        }

        return false;
    };

    /**
    * Emulate PHPs htmlspecial chars function
    */
    this.htmlspecialchars = function( text )
    {
        text = text.replace( /</g, '&lt;' );
        text = text.replace( />/g, '&gt;' );
        text = text.replace( /"/g, '&quot;' );

        return text;
    };

    /**
    * Emulate PHPs un htmlspecial chars function
    */
    this.un_htmlspecialchars = function( text )
    {
        text = text.replace( /&lt;/g  , '<' );
        text = text.replace( /&gt;/g  , '>' );
        text = text.replace( /&quot;/g, '"' );

        return text;
    };
    //=============================================================================
    // EDITOR FUNCTIONS
    //=============================================================================

    /**
    * Get contents from an open editor
    */
    this.get_editor_contents = function( editor_id, editor_array )
    {
        var aIPS_editor = editor_array ? editor_array : IPS_editor;

        if ( ! editor_id )
        {
            //-----------------------------------------
            // Assume we don't care, and get first box
            //-----------------------------------------

            for( var i in aIPS_editor )
            {
                editor_id = i;
                break;
            }
        }

        return aIPS_editor[ editor_id ].editor_get_contents;
    };

    /**
    * Add contents to an open editor
    */
    this.add_editor_contents = function( text, editor_id, editor_array )
    {
        var aIPS_editor = editor_array ? editor_array : IPS_editor;

        if ( ! editor_id )
        {
            //-----------------------------------------
            // Assume we don't care, and get first box
            //-----------------------------------------

            for( var i in aIPS_editor )
            {
                editor_id = i;
                break;
            }
        }

        return aIPS_editor[ editor_id ].insert_text( text );
    };
    /**
    * Convert saved tags to display tags
    */
    this.convert_saved_tags_to_display_tags = function( text )
    {
        text = text.replace( /(<|&lt;|&#60;)!--/, "{" );
        text = text.replace( /--(>|&gt;|&#62;)/ , "}" );

        return text;
    };

    //=============================================================================
    // MISC FUNCTIONS
    //=============================================================================


    /**
    * Fade in colour...
    */
    this.fade_in_element = function( div )
    {
        //-----------------------------------------
        // INIT
        //-----------------------------------------

        var hash        = '#';
        var color_items = "0123456789abcdef";
        var start_color = '#ffff66';
        var orig_color  = document.getElementById( div ).style.backgroundColor;
        var temp_end    = '#ffffff';
        var iter        = 20;
        var time        = 80;

        var rbeg = color_items.indexOf(start_color.substr(1,1))*16 + color_items.indexOf(start_color.substr(2,1));
        var gbeg = color_items.indexOf(start_color.substr(3,1))*16 + color_items.indexOf(start_color.substr(4,1));
        var bbeg = color_items.indexOf(start_color.substr(5,1))*16 + color_items.indexOf(start_color.substr(6,1));
        var rend = color_items.indexOf(temp_end.substr(1,1))*16    + color_items.indexOf(temp_end.substr(2,1));
        var gend = color_items.indexOf(temp_end.substr(3,1))*16    + color_items.indexOf(temp_end.substr(4,1));
        var bend = color_items.indexOf(temp_end.substr(5,1))*16    + color_items.indexOf(temp_end.substr(6,1));

        for ( i = 1, r = rbeg, g = gbeg, b = bbeg;
                i <= iter;
                r = Math.round(rbeg + i * ((rend - rbeg) / (iter-1))),
                g = Math.round(gbeg + i * ((gend - gbeg) / (iter-1))),
                b = Math.round(bbeg + i * ((bend - bbeg) / (iter-1))), i++
            )
        {
            hstr = '#' + color_items.charAt(Math.floor(r/16)) + color_items.charAt(r%16) + color_items.charAt(Math.floor(g/16)) + color_items.charAt(g%16) + color_items.charAt(Math.floor(b/16)) + color_items.charAt(b%16);

            setTimeout('var div = document.getElementById("' + div + '"); div.style.backgroundColor = "' + hstr + '";', i * time);
        }

        setTimeout('var div = document.getElementById("' + div + '"); div.style.backgroundColor = "' + orig_color + '";', (i+1) * time);
    };

    /**
    * Accepts a string followed by a list of replacements
    */

    this.lang_build_string = function()
    {
        if ( ! arguments.length || ! arguments )
        {
            return;
        }

        var string = arguments[0];

        for( var i = 1 ; i < arguments.length ; i++ )
        {
            var match  = new RegExp('<%' + i + '>', 'gi');
            string = string.replace( match, arguments[i] );
        }

        return string;
    };

    /**
    * Return the ID from an ID title string
    *
    * @param    string  ID title
    * @return   integar ID
    */
    this.get_id_from_text = function( id )
    {
        return id.replace( /.*(\-|_)(\S+)/, "$2" );
    };

    /**
    * Return the name from an ID string
    *
    * @param    string  ID title
    * @return   integar ID
    */
    this.get_name_from_text = function( id )
    {
        return id.replace( /(.*)(\-|_)(\S+)/, "$1" );
    };

    /**
    * Location jump
    * @param    string  URL to redirect to
    * @param    boolean Do not prepend normal URL
    */
    this.location_jump = function( url, full )
    {
        url = url.replace( /&amp;/g, '&' );

        if ( full )
        {
            window.location.href = url;
        }
        else
        {
            window.location.href = ipb_var_base_url + url;
        }
    };

    /**
    * Confirm an action
    */
    this.confirm_action = function(url, msg)
    {
        if ( ! msg )
        {
            msg = 'PLEASE CONFIRM:\nOK to proceed?';
        }

        if ( url )
        {
            url = url.replace( '&amp;', '&' );
        }
        else
        {
            url = '';
        }

        if ( confirm( msg ) )
        {
            window.location.href = url;
        }
        else
        {
            alert ( 'OK, action cancelled!' );
            return false;
        }
    };
    /**
    * Create pop-up window
    */
    this.pop_up_window = function(url, width, height, name)
    {
        if ( ! name )
        {
            var mydate = new Date();
            name = mydate.getTime();
        }

        var Win = window.open( url.replace( '&amp;', '&' ), name, 'width='+width+',height='+height + ',resizable=1,scrollbars=1,location=no,directories=no,status=no,menubar=no,toolbar=no');

        Win.focus();

        return false;
    };

    /**
    * Set stuff unselectable
    */
    this.set_unselectable = function(obj)
    {
        if ( ! is_ie4 && typeof( obj.tagName ) != 'undefined' )
        {
            if ( obj.hasChildNodes() )
            {
                for (var i = 0; i < obj.childNodes.length; i++)
                {
                    this.set_unselectable( obj.childNodes[i] );
                }
            }
            obj.unselectable = 'on';
        }
    };

    /**
    * GET LEFT OBJECT POSITION
    * Safari friendly version...
    */
    this.get_obj_leftpos = function(obj)
    {
        var curleft = 0;

        if (obj.offsetParent)
        {
            while (obj.offsetParent)
            {
                curleft += obj.offsetLeft;
                obj      = obj.offsetParent;
            }
        }
        else if (obj.x)
        {
            curleft += obj.x;
        }

        return curleft;
    };

    /**
    * GET TOP OBJECT POSITION
    * Safari friendly version...
    */
    this.get_obj_toppos = function(obj)
    {
        var curtop = 0;

        if (obj.offsetParent)
        {
            while (obj.offsetParent)
            {
                curtop += obj.offsetTop;
                obj     = obj.offsetParent;
            }
        }
        else if (obj.y)
        {
            curtop += obj.y;
        }

        return curtop;
    };

    //=============================================================================
    // CANCEL BUBBLE FUNCTIONS
    //=============================================================================

    /**
    * Stop the browser bubbling events
    */
    this.cancel_bubble = function(obj, extra)
    {
        if ( extra !== false )
        {
            extra = true;
        }

        if ( ! obj || is_ie)
        {
            if ( extra )
            {
                window.event.returnValue = false;
            }

            window.event.cancelBubble = true;

            return window.event;
        }
        else
        {
            obj.stopPropagation();

            if ( extra )
            {
                obj.preventDefault();
            }

            return obj;
        }
    };

    this.cancel_bubble_all = function( obj )
    {
        return ipsclass.cancel_bubble( obj, true );
    };

    this.cancel_bubble_low = function( obj )
    {
        return ipsclass.cancel_bubble( obj, false );
    };

    //=============================================================================
    // COOKIE FUNCTIONS
    //=============================================================================

    /**
    * Cookie: Get Cookie
    */
    this.my_getcookie = function( name )
    {
        return this.cookies[ name ];
        /*cname = ipb_var_cookieid + name + '=';
        cpos  = document.cookie.indexOf( cname );

        if ( cpos != -1 )
        {
            cstart = cpos + cname.length;
            cend   = document.cookie.indexOf(";", cstart);

            if (cend == -1)
            {
                cend = document.cookie.length;
            }

            return unescape( document.cookie.substring(cstart, cend) );
        }

        return null;*/
    };

    /**
    * Cookie: Set Cookie
    */
    this.my_setcookie = function( name, value, sticky )
    {
        expire = "";
        domain = "";
        path   = "/";

        if ( sticky )
        {
            expire = "; expires=Wed, 1 Jan 2020 00:00:00 GMT";
        }

        if ( ipb_var_cookie_domain != "" )
        {
            domain = '; domain=' + ipb_var_cookie_domain;
        }

        if ( ipb_var_cookie_path != "" )
        {
            path = ipb_var_cookie_path;
        }

        document.cookie = ipb_var_cookieid + name + "=" + value + "; path=" + path + expire + domain + ';';

        //-----------------------------------------
        // Update internal array
        //-----------------------------------------

        this.cookies[ name ] = value;
    };

    //=============================================================================
    // ARRAY FUNCTIONS
    //=============================================================================

    /**
    * Array: Get stacksize
    * @param    array   Array
    * @return   int     Array length
    */
    this.array_stacksize = function( thearray )
    {
        for (i = 0 ; i < thearray.length; i++ )
        {
            if ( (thearray[i] == "") || (thearray[i] == null) || (thearray == 'undefined') )
            {
                return i;
            }
        }

        return thearray.length;
    };

    /**
    * Array: Add element to the stack
    * @param    array   Array
    * @param    mixed   New value
    */
    this.array_pushstack = function ( thearray, newval )
    {
        var arraysize = this.array_stacksize(thearray);
        thearray[arraysize] = newval;
    };

    /**
    * Array: Remove last array element
    * @param    array   Array
    * @return   int     Array length
    */
    this.array_popstack = function( thearray )
    {
        var arraysize = this.array_stacksize( thearray );
        var theval    = thearray[arraysize - 1];

        delete thearray[arraysize - 1];
        return theval;
    };

}

var ipsclass = new ipsclass();
