﻿var isMSIE = /*@cc_on!@*/false; // http://dean.edwards.name/weblog/2007/03/sniff/
var isIEmac = false; /*@cc_on@if(@_jscript&&!(@_win32||@_win16)&& 
(@_jscript_version<5.5)) isIEmac=true; @end
@*/
var modal = 0;
var startAlertMsg;
var undefined;

function isEmpty(s) { return ((s == '') || /^\s*$/.test(s)); }

function createBookmarkLink(containerID, text, url, title, alertMsg)
{
    if (!title || isEmpty(title))
        title = document.title;
    startAlertMsg = alertMsg;
    if (!alertMsg || isEmpty(alertMsg))
        startAlertMsg = "Appuyez sur ##KEYS## après avoir fermé cette fenêtre";
    if (!url || isEmpty(url))
        url = location.href;
    makeBookmarkTextLink(containerID, text, url, title);
}

function findKeys()
{
    // user agent sniffing is bad in general, but this is one of the times 
    // when it's really necessary
    var ua = navigator.userAgent.toLowerCase();
    var isMac = (ua.indexOf('mac') != -1);
    var isWebkit = (ua.indexOf('webkit') != -1);
    var str = (isMac ? 'Command/Cmd' : 'CTRL');
    if (window.opera && (!opera.version || (opera.version() < 9)))
    {
        str += ' + T';  // Opera versions before 9
    }
    else if (ua.indexOf('konqueror') != -1)
    {
        str += ' + B'; // Konqueror
    }
    else if (window.opera || window.home || isWebkit || isMSIE || isMac)
    {
        // IE, Firefox, Netscape, Safari, Google Chrome, Opera 9+, iCab, IE5/Mac
        str += ' + D';
    }

    if (isEmpty(startAlertMsg))
        startAlertMsg = "Appuyez sur ##KEYS## après avoir fermé cette fenêtre";
    var msg = str;
    if (startAlertMsg.indexOf("##KEYS##") >= 0)
        msg = startAlertMsg.substr(0, startAlertMsg.indexOf("##KEYS##")) + str + startAlertMsg.substr(startAlertMsg.indexOf("##KEYS##") + 8);

    return msg;
}

function getContainer(containerID)
{
    if (isEmpty(containerID))
        containerID = 'addBookmarkContainer';
    return document.getElementById(containerID) || null;
}

function makeBookmarkTextLink(containerID, text, url, title)
{
    var a = makeBookmarkLink(containerID, text, url, title, true);
    if (a)
    {
        a.appendChild(document.createTextNode(text));
        return;
    }
    var cont = getContainer(containerID);
    if (!cont) return;
//    var elm = document.createElement('span');
//    elm.appendChild(document.createTextNode(findKeys()));
//    cont.appendChild(elm);
}
function makeBookmarkImgLink(containerID, imgPath, text, url, title)
{
    if (!imgPath || isEmpty(imgPath)) return;
    var a = makeBookmarkLink(containerID, text, url, title, false);
    var img = document.createElement('img');
    img.title = img.alt = modal ? text : findKeys();
    img.src = imgPath;
    a.appendChild(img);
}

function makeBookmarkLink(containerID, text, url, title, isText)
{
    var cont = getContainer(containerID);
    if (!cont) return null;
    var a = document.createElement('a');
    a.href = url;
    var s = document.createElement('div').style;
    var isFx35plus = ((navigator.userAgent.toLowerCase().indexOf('firefox') != -1)
                      && (s.wordWrap !== undefined) && (s.MozTransform !== undefined));
    if (window.external && isMSIE && !isIEmac)
    {
        // IE4/Win generates an error when you
        // execute 'typeof(window.external.AddFavorite)'
        // In IE7 the page must be from a web server, not directly from a local 
        // file system, otherwise, you will get a permission denied error.
        // Maxthon shows 'typeof(window.external.AddFavorite)' as 'undefined'
        // even though it is defined.
        a.onclick = function ()
        { // IE/Win
            try
            {
                window.external.AddFavorite(url, title);
            }
            catch (ex)
            {
                alert(findKeys());
            }
            return false;
        }
        modal = 1;
    }
    else if (window.opera || isFx35plus)
    { // Opera 7+, Firefox 3.5+
        a.title = title, a.rel = 'sidebar';
        modal = 1;
    }
//    else if (isText)
//    {
//        return null;
//    }
    else
    {
        a.onclick = function ()
        {
            alert(findKeys());
            return false;
        }
    }
    return cont.appendChild(a);
}

