/**
 * $RCSfile: Utils.js,v $ $Revision: 1.7 $ $Date: 2009/03/30 14:13:09 $
 *
 * Copyright 2007 Olaf Havnes. All Rights Reserved.
 * http://www.havnes.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */


/**
 * Small collection of web utilities.
 *
 * @author      Olaf Havnes
 * @version     $Revision: 1.7 $ $Date: 2009/03/30 14:13:09 $
 */


// Measure the browser window, open new windows.
function WindowUtil()
{
    // Only static methods
}


WindowUtil.fullScreen = function
(
    url,
    chrome_width,
    chrome_height,
    name
)
{
    if (isNaN (chrome_width) || chrome_width < 0)
    {
        if 
        (
            navigator &&
            navigator.appName &&
            navigator.appName.indexOf("Microsoft") >= 0
        )
        {
            chrome_width = 10;
        }
        else
        {
            chrome_width = 8;
        }
    }

    if (isNaN (chrome_height) || chrome_height < 0)
    {
        if 
        (
            navigator &&
            navigator.appName &&
            navigator.appName.indexOf("Microsoft") >= 0
        )
        {
            chrome_height = 50;
        }
        else
        {
            chrome_height = 46;
        }
    }

    if (name == null)
    {
        name = "FullScreenWindow";
    }

    return window.open
    (
        url,
        name,
        "width=" + (screen.width - chrome_width) + "," +
        "height=" + (screen.height - chrome_height) + "," +
        "left=0," +
        "top=0," +
        "menubar=no," +
        "toolbar=no," +
        "location=no," +
        "directories=no," +
        "status=no," +
        "scrollbars=no," +
        "titlebar=no," +
        "resizable=yes"
    );
}


WindowUtil.width = function()
{
    if
    (
        window && 
        window.innerWidth &&
        window.innerWidth > 0
    )
    {
        // Most browsers...
        return window.innerWidth;
    }
    else if
    (
        document && 
        document.documentElement && 
        document.documentElement.clientWidth
    )
    {
        // Internet Explorer
        return document.documentElement.clientWidth;
    }
    else
    {
        // Panic!
        return -1;
    }
}


WindowUtil.height = function()
{
    if
    (
        window && 
        window.innerHeight &&
        window.innerHeight > 0
    )
    {
        // Most browsers...
        return window.innerHeight;
    }
    else if
    (
        document && 
        document.documentElement && 
        document.documentElement.clientHeight
    )
    {
        // Internet Explorer 6+
        return document.documentElement.clientHeight;
    }
    else
    {
        // Panic!
        return -1;
    }
}




// XOR scramble a mail address.
function MailUtil
(
    mail,
    text
)
{
    this.mail = mail.indexOf ("@") > -1 ?

        mail :
        MailUtil.unscramble (mail);

    this.text = text == null ? this.mail : text;

    // The actual embedding
    this.doEmbed = function ()
    {
        document.write
        (
            '<a href="mailto:' +
            this.mail + '">' +
            this.text + '</a>'
        );
    }
}


// Some static methods
MailUtil.scramble = function
(
    clean
)
{
    var scrambled = '';
    for (var i = 0; i < clean.length; i++)
    {
        scrambled += (255^clean.charCodeAt(i)).toString (16);
    }
    return scrambled;
}


MailUtil.unscramble = function
(
    scrambled
)
{
    var clean = '';
    for (var i = 0; i < scrambled.length; i+=2)
    {
        clean += String.fromCharCode
        (
            255^parseInt (scrambled.substring(i,i+2), 16)
        );
    }
    return clean;
}


// Call this in FireFox by entering 'javascript:MailUtil.scrambleMail("you@example.com")' in the address field
MailUtil.scrambleMail = function
(
    clean
)
{
    var scrambled = MailUtil.scramble (clean);
    prompt
    (
        'Paste this code where you want the mailto-link:',
        '<script type="text/javascript">MailUtil.writeMail("' + scrambled + '");</script>'
    );
}


MailUtil.writeMail = function
(
    scrambled,
    text
)
{
    new MailUtil(scrambled, text).doEmbed();
}
