

function findPosX(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;
}

function findPosY(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;
}

var skypeMenu;
var fader = 0;
var hideTimer = null;
var showTimer = null;

function displayMenu (skypename, skypeme, doYouTxt, yesTxt, noTxt, whatIsSkype, noTxtLink, whatIsLink) {
    if(!skypeme) skypeme = window.event;

    var img_x = findPosX(skypeme)  + 50;
    var img_y = findPosY(skypeme);
    var parentmenuLayer = document.createElement("div");
    parentmenuLayer.style.position = "absolute";
    parentmenuLayer.style.filter = "-moz-opacity:0.2";
    parentmenuLayer.style.MozOpacity = "0";
    parentmenuLayer.style.width = "195px"; 
    parentmenuLayer.style.height = "200px"; 
        
    var menuLayer = document.createElement("div");
    menuLayer.id = "skypemenu";
    menuLayer.style.font = "11px Arial";
    menuLayer.style.backgroundColor = "white";
    menuLayer.style.position = "absolute";
    menuLayer.style.border = "1px solid #9999cc";
    menuLayer.style.textAlign = "center";
    menuLayer.style.padding = "0px 20px";
    menuLayer.style.top = "20px";

    parentmenuLayer.onmouseout = function () {
        hideTimer = setTimeout("menuFadeOut();", 500);
    }

    parentmenuLayer.onmouseover = function () {
        if (hideTimer) {
            clearTimeout(hideTimer);
            hideTimer = null;
        }
    }

    var divLogo = document.createElement("div");
    divLogo.style.position = "relative";
    divLogo.style.top = "-20px";

    var imgLogo = document.createElement("img");
    imgLogo.src = picsPath + "skype.jpg";
    imgLogo.style.height = "59px";
    divLogo.appendChild(imgLogo);
    menuLayer.appendChild(divLogo);

    divOptions = document.createElement("div");
    divOptions.style.position = "relative";
    divOptions.style.top = "-16px";
    divOptions.style.fontFamily = "Arial, sans-serif";
    divOptions.style.fontSize = "12px";
    divOptions.style.lineHeight = "150%";
            
    var linkText = document.createElement("span");
    linkText.innerHTML = doYouTxt +"<br>";
    
    var linkCall = document.createElement("a");
    linkCall.href = "callto://"+skypename;
	linkCall.target = "_blank";
    linkCall.innerHTML = yesTxt;
    linkCall.style.fontSize = "14px";
    linkCall.style.fontWeight = "bold";
    linkCall.style.textDecoration = "underline";
    linkCall.style.color = "red";
    
    var linkCallNo = document.createElement("a");
    linkCallNo.href = noTxtLink;
	linkCallNo.target = "_blank";
    linkCallNo.innerHTML = noTxt;
    linkCallNo.style.fontSize = "14px";
    linkCallNo.style.fontWeight = "bold";
    linkCallNo.style.textDecoration = "underline";
    linkCallNo.style.color = "red";
    
    var spacer = document.createElement("span");
    spacer.innerHTML = "&nbsp;&nbsp;";
    
    divOptions.appendChild(linkText);
    divOptions.appendChild(linkCall);
    divOptions.appendChild(spacer);
    divOptions.appendChild(linkCallNo);

    divOptions.appendChild(document.createElement("br"));

    var linkAbout = document.createElement("a");
    linkAbout.href = whatIsLink;
	linkAbout.target = "_blank";
    linkAbout.innerHTML = whatIsSkype;
    divOptions.appendChild(linkAbout);

    divOptions.appendChild(document.createElement("br"));
    menuLayer.appendChild(divOptions);
    parentmenuLayer.appendChild(menuLayer);
    document.body.appendChild(parentmenuLayer);

    var call_x = findPosX(linkCall);
    var call_y = findPosY(linkCall);

    var menu_x = findPosX(menuLayer);
    var menu_y = findPosY(menuLayer);

    if (document.all) {
        call_y = call_y - 42;
    }
    
    x = img_x + (menu_x - call_x) + 20;
    if ( (x + 180)  > document.body.clientWidth ) {
        x = x - ( (x+180) - document.body.clientWidth );
    }

    parentmenuLayer.style.left = x;
    parentmenuLayer.style.top = img_y + (menu_y - call_y - 20);
                               
    skypeMenu = parentmenuLayer;
    fader = 0;
    menuFadeIn();

}

function menuFadeIn() {
    if (skypeMenu) {
        if (fader < .8) {
            fader += 0.1;
             if (document.all) {
                 faderie = 100*fader;
                 skypeMenu.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+faderie+")";
             } else 
                 skypeMenu.style.MozOpacity = fader;
            setTimeout("menuFadeIn();", 70);
        }
    }
}

function menuFadeOut() {
    if (skypeMenu) {
        if (fader > 0) {
            fader -= 0.1;
            if (document.all) {
                faderie = 100*fader;
                skypeMenu.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+faderie+")";
            } else 
                skypeMenu.style.MozOpacity = fader;
            setTimeout("menuFadeOut();", 70);
        }
        else {
            document.body.removeChild(skypeMenu);
            skypeMenu = null;
        }
    }
}

var activex = ((navigator.userAgent.indexOf('Win')  != -1) && (navigator.userAgent.indexOf('MSIE') != -1) && (parseInt(navigator.appVersion) >= 4 ));
var CantDetect = ((navigator.userAgent.indexOf('Safari')  != -1) || (navigator.userAgent.indexOf('Opera')  != -1));


if(typeof(detected) == "undefined" && activex) {
    document.write(
        ['<script language="VBscript">',
        'Function isSkypeInstalled()',
        'on error resume next',
        'Set oSkype = CreateObject("Skype.Detection")',
        'isSkypeInstalled = IsObject(oSkype)',
        'Set oSkype = nothing',
        'End Function',
        '</script>'].join("\n")
    );
}

function skypeCheck(skypename, skypeme, doYouTxt, yesTxt, noTxt, whatIsSkype, noTxtLink, whatIsLink, statUrl) {
    
    updateSkypeMeStatistic(statUrl);

    if(CantDetect) {
	    displayMenu(skypename, skypeme, doYouTxt, yesTxt, noTxt, whatIsSkype, noTxtLink, whatIsLink);
            return false;
    } else if(!activex) {
        var skypeMime = navigator.mimeTypes["application/x-skype"];
        detected = true;
        if(typeof(skypeMime) == "object") {
            return true;
        } else {
	    displayMenu(skypename, skypeme, doYouTxt, yesTxt, noTxt, whatIsSkype, noTxtLink, whatIsLink);
            return false;
        }
    } else {
        if(isSkypeInstalled()) {
            detected = true;
            return true;
        }
    }
    
    detected = true;
    displayMenu(skypename, skypeme, doYouTxt, yesTxt, noTxt, whatIsSkype, noTxtLink, whatIsLink);
    return false;
}


function updateSkypeMeStatistic(statUrl) {
    //This should be generic Ajax call
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.open("GET", statUrl, true);
    req.send(null);
	var debug = document.getElementById("ajax_debug");
	if (debug!=null) {
		debug.innerHTML = debug.innerHTML + "<br/>updateSkypeMeStatistic: "+statUrl;
	}
     return true;
}

