// phpjs isset function taken from: http://phpjs.org/functions/isset:454
function isset () {
    // !No description available for isset. @php.js developers: Please update the function summary text file.
    //
    // version: 909.322
    // discuss at: http://phpjs.org/functions/isset    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: FremyCompany
    // +   improved by: Onno Marsman
    // *     example 1: isset( undefined, true);
    // *     returns 1: false    // *     example 2: isset( 'Kevin van Zonneveld' );
    // *     returns 2: true

    var a=arguments, l=a.length, i=0;
        if (l===0) {
        throw new Error('Empty isset');
    }

    while (i!==l) {        if (typeof(a[i])=='undefined' || a[i]===null) {
            return false;
        } else {
            i++;
        }    }

}
function phpBBGoogleWave() {}

phpBBGoogleWave.prototype.setup = function(bgcolour,colour,font,fontsize) {
    this.waves = [];
    this.wavePanels = [];
    this.bgcolour = bgcolour;
    this.colour = colour;
    this.font = font;
    this.fontsize = fontsize;
    this.debug = false;
    // Some named colors to work with
    // From Interface by Stefan Petre
    // http://interface.eyecon.ro/
    this.colors = {
        aqua:[0,255,255],
        azure:[240,255,255],
        beige:[245,245,220],
        black:[0,0,0],
        blue:[0,0,255],
        brown:[165,42,42],
        cyan:[0,255,255],
        darkblue:[0,0,139],
        darkcyan:[0,139,139],
        darkgrey:[169,169,169],
        darkgreen:[0,100,0],
        darkkhaki:[189,183,107],
        darkmagenta:[139,0,139],
        darkolivegreen:[85,107,47],
        darkorange:[255,140,0],
        darkorchid:[153,50,204],
        darkred:[139,0,0],
        darksalmon:[233,150,122],
        darkviolet:[148,0,211],
        fuchsia:[255,0,255],
        gold:[255,215,0],
        green:[0,128,0],
        indigo:[75,0,130],
        khaki:[240,230,140],
        lightblue:[173,216,230],
        lightcyan:[224,255,255],
        lightgreen:[144,238,144],
        lightgrey:[211,211,211],
        lightpink:[255,182,193],
        lightyellow:[255,255,224],
        lime:[0,255,0],
        magenta:[255,0,255],
        maroon:[128,0,0],
        navy:[0,0,128],
        olive:[128,128,0],
        orange:[255,165,0],
        pink:[255,192,203],
        purple:[128,0,128],
        violet:[128,0,128],
        red:[255,0,0],
        silver:[192,192,192],
        white:[255,255,255],
        yellow:[255,255,0]
    };
};

phpBBGoogleWave.prototype.turnDebugOn = function() {
    this.debug = true;
};

phpBBGoogleWave.prototype.turnDebugOff = function() {
    this.debug = false;
};

phpBBGoogleWave.prototype.randomDivId = function() {
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 16;
    var divId = 'google_wave_bbcode_embed_';
    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        divId += chars.substring(rnum,rnum+1);
    }
    return divId;
};

phpBBGoogleWave.prototype.addWave = function(waveId) {
    if(this.debug){
        var debugMessage = '';
    }
    var div = this.randomDivId();
    tempWaveId = waveId.split("#restored:wave:");
    if(tempWaveId[1]) {
        if(this.debug){
            debugMessage += 'WaveId seems to be an URL, trying to remove all extras from the string and extract the waveId: ' + waveId + '\r\n';
        }
        prePreRealWaveId = tempWaveId[1].replace("%252B","+");
        if(this.debug){
            debugMessage += '%252B replace with +\r\n';
        }
        preRealWaveId = prePreRealWaveId.split(",");
        preRealWaveId = preRealWaveId[0];
        if(this.debug){
            debugMessage += 'splitted on "," result: ' + preRealWaveId + '\r\n';
        }
        tmp = preRealWaveId.split("+");
        tmp[1] = tmp[1].split(".");
        tmp[1] = tmp[1][0];
        preRealWaveId = tmp[0] + "+" + tmp[1];
        if(this.debug){
            debugMessage += 'splitted on "." result: ' + preRealWaveId + '\r\n';
        }
    } else {
        if(this.debug){
            debugMessage += 'WaveId seems just a waveId\r\n';
        }
        preRealWaveId = waveId;
    }
    realWaveId = preRealWaveId.replace(" ","");
    phpBBGoogleWave.waves.push({
        div: div,
        waveId: realWaveId
    });
    if(this.debug){
        debugMessage += 'WaveId: ' + realWaveId;
        alert(debugMessage);
    }
    return div;
};

phpBBGoogleWave.prototype.applyWaves = function() {
    if(this.debug){
        var debugMessage = '';
    }
    for( var i in phpBBGoogleWave.waves ) {
        if(phpBBGoogleWave.waves[i].div){
            var div = phpBBGoogleWave.waves[i].div;
            var waveId = phpBBGoogleWave.waves[i].waveId;
            phpBBGoogleWave.wavePanels[div] = new WavePanel('http://wave.google.com/wave/');
            phpBBGoogleWave.wavePanels[div].setUIConfig(
                phpBBGoogleWave.getParentCSSAttributeColor(document.getElementById(div),'background-color',this.bgcolour),
                phpBBGoogleWave.getParentCSSAttributeColor(document.getElementById(div),'color',this.colour),
                phpBBGoogleWave.getParentCSSAttribute(document.getElementById(div),'font-family',this.font),
                phpBBGoogleWave.getParentCSSAttribute(document.getElementById(div),'font-size',this.fontsize)
                );
            phpBBGoogleWave.wavePanels[div].loadWave(waveId);
            phpBBGoogleWave.wavePanels[div].init(document.getElementById(div));
        }
    }
};

phpBBGoogleWave.prototype.getParentCSSAttribute = function(element,attribute,defaultValue) {
    /*var parents = '';
    for(var i = 0;i<25;i++){
        parents += 'parent().';
        eval('var attr = $(element).' + parents + 'css(\'' + attribute + '\');');
        return attr;
    }*/
    return defaultValue;
};

phpBBGoogleWave.prototype.getParentCSSAttributeColor = function(element,attribute,defaultValue) {
    var parents = '';
    for(var i = 0;i<25;i++){
        parents += 'parent().';
        eval('var bg = jQuery(element).' + parents + 'css(\'' + attribute + '\');');
        if(isset(bg) && bg && bg!='transparent') {
            bg = this.getRGB(bg);
            return '#' + this.dechex(bg[0]) + this.dechex(bg[1]) + this.dechex(bg[2]);
        }
    }
    return defaultValue;
};

// Color Conversion functions from highlightFade
// By Blair Mitchelmore
// http://jquery.offput.ca/highlightFade/

// Parse strings looking for color tuples [255,255,255]
phpBBGoogleWave.prototype.getRGB = function(color) {
    var result;

    // Check if we're already dealing with an array of colors
    if ( color && color.constructor == Array && color.length == 3 )
        return color;

    // Look for rgb(num,num,num)
    if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
        return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];

    // Look for rgb(num%,num%,num%)
    if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
        return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];

    // Look for #a0b1c2
    if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
        return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];

    // Look for #fff
    if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
        return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];

    // Otherwise, we're most likely dealing with a named color
    return this.colors[jQuery.trim(color).toLowerCase()];
};

// HEXA <-> DECA conversion from: http://www.pocketnes.org/hexa.html
phpBBGoogleWave.prototype.hexdec = function(f1) {
    f1.hexval.value = f1.hexval.value.toUpperCase();
    rval = parseInt(f1.hexval.value,16);
    f1.result.value=rval;
};
phpBBGoogleWave.prototype.dechex = function(f1) {
    rval= f1.redval.value;
    rhex = this.tohex(rval);
    f1.hexnot.value = rhex;
};
phpBBGoogleWave.prototype.tohex = function(i) {
    a2 = ''
    ihex = this.hexQuot(i);
    idiff = eval(i + '-(' + ihex + '*16)')
    a2 = this.itohex(idiff) + a2;
    while( ihex >= 16) {
        itmp = this.hexQuot(ihex);
        idiff = eval(ihex + '-(' + itmp + '*16)');
        a2 = this.itohex(idiff) + a2;
        ihex = itmp;
    }
    a1 = this.itohex(ihex);
    return a1 + a2 ;
};
phpBBGoogleWave.prototype.hexQuot = function(i) {
    return Math.floor(eval(i +'/16'));
};
phpBBGoogleWave.prototype.itohex = function(i) {
    if( i == 0) {
        aa = '0'
    }
    else {
        if( i== 1) {
            aa = '1'
            }
        else {
            if( i== 2) {
                aa = '2'
                }
            else {
                if( i == 3) {
                    aa = '3'
                }
                else {
                    if( i== 4) {
                        aa = '4'
                        }
                    else {
                        if( i == 5) {
                            aa = '5'
                        }
                        else {
                            if( i== 6) {
                                aa = '6'
                                }
                            else {
                                if( i == 7) {
                                    aa = '7'
                                }
                                else {
                                    if( i== 8) {
                                        aa = '8'
                                        }
                                    else {
                                        if( i == 9) {
                                            aa = '9'
                                            }
                                        else {
                                            if( i==10) {
                                                aa = 'A'
                                                }
                                            else {
                                                if( i==11) {
                                                    aa = 'B'
                                                    }
                                                else {
                                                    if( i==12) {
                                                        aa = 'C'
                                                        }
                                                    else {
                                                        if( i==13) {
                                                            aa = 'D'
                                                            }
                                                        else {
                                                            if( i==14) {
                                                                aa = 'E'
                                                                }
                                                            else {
                                                                if( i==15) {
                                                                    aa = 'F'
                                                                    }

                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return aa
};
