function IsWebMate_Installed () {
    if ($.browser.msie) { 
        return (document.getElementById("WMO").object!=null);
    } else if ($.browser.mozilla) {
        return Detect_WebMate_MozillaPlugin();
    } else if ($.browser.safari) {
        return Detect_WebMate_MozillaPlugin();
    }
    return false;
}

function refreshTimer() {is_refresh=true; setTimeout('refresh()', 30000);}
function refreshTimerStop() {is_refresh=false;}
function refresh() { if(is_refresh) {var f=document.getElementById('join_form');if(f) f.submit();} }

function stopTimer() {time_end = null;is_refresh=false;}
function startTimer() { timer_element = document.getElementById('timer'); if(timer_element) {timer();}}

function timer()
{
    if(time_end==null) return;
    
    var d = new Date();
    var d_now = Math.round(d.getTime()/1000);
    var offset = - d.getTimezoneOffset()/60 - DZi;
    var offset_sec = (offset-dtzone)*3600;
    d_now -= offset_sec;
    
    d.setFullYear(time_end[0])
    d.setMonth(time_end[1]-1);
    d.setDate(time_end[2]);
    d.setHours(time_end[3]);
    d.setMinutes(time_end[4]);
    d.setSeconds(time_end[5]);
    
    var d_end = Math.round(d.getTime()/1000);
    var rests = d_end - d_now;
    if(rests>0) {
        var t = rests/(60*60*24);
        var days = Math.floor(t);
        var r = (t - days) * 24;
        var hours = Math.floor(r);
        r = (r - hours) * 60;
        var minutes = Math.floor(r);
        r = (r - minutes) * 60;
        var seconds = Math.round(r);
        var sd = setNumberWord(days, lang_t['d']);
        var sh = setNumberWord(hours, lang_t['h']);
        var sm = setNumberWord(minutes, lang_t['m']);
        var ss = setNumberWord(seconds, lang_t['s']);
        var html = '';
        if(days>0) html += '<b>'+days+'</b> '+sd+' ';
        if(hours>0) html += '<b>'+hours+'</b> '+sh+' ';
        if(minutes>0) html += '<b>'+minutes+'</b> '+sm+' ';
        html += '<b>'+seconds+'</b> '+ss;
        timer_element.innerHTML = html;
        setTimeout('timer()', 1000);
    }else {
        // run conference
        is_refresh=true;
        setTimeout('refresh()', 5000);
    }
    return false;
}

function setNumberWord(num, words)
{
    num = num.toString();
    return (num==1 || (num>20 && num.substring(num.length-1)==1)) ? words[0] : ((num>1 && num<5) || (num>20 && num.substring(num.length-1)>1 && num.substring(num.length-1)<5)) ? words[1] : words[2];
    
}

var timer_element = false;var time_end = null;

window.onload = function() {
    var update_version = Get_Cookie('update_version');
    byid('join_form_div').style.display = 'none';
    byid('plugin-install').style.display = 'block';
    byid('install-msg').style.display = 'block';
        
    if(user_os=='mac' || $.browser.safari || IsWebMate_Installed()==false || update_version) {
        if(user_os=='win') {
            if($.browser.msie) $("#install-ie").show();
            else if($.browser.mozilla) $("#install-ff").show();
            else $("#install-no").show();
        }else if(user_os=='mac') {
            $("#install-mac").show();
            if(mac_app_url!='') {
                var appv = document.createElement('iframe');
                appv.src=mac_app_url;
                appv.id='ifr-mac';
                appv.width=1;
                appv.height=1;
                appv.style.border=0;
                appv.scrolling="none";
                document.body.appendChild(appv);
                //testProtocolHandler(appv);
                /*
                appv.contentWindow.location.href = mac_app_url;
                appv.contentWindow.location = mac_app_url;
                appv.contentWindow.location.protocol = 'videomost:';
                debug(appv.contentWindow.location);
                */
                
                //appv.contentWindow.location = mac_app_url;
            }
        }else {
            $("#install-no").show();
        }
        byid('div_WMO').style.visibility = 'hidden';
        
        if($.browser.msie && update_version) Delete_Cookie('update_version');
        byid('install-msg').style.display = 'none';
        return;
    }else {
        var jfd=byid('join_form_div');
        var jf=byid('join_form');
        if(jfd) jfd.style.cssText = 'display:block;border:#999999 solid 1px;';
        if(jf) jf.style.display = 'block';
        byid('div_WMO').style.visibility = 'visible';
        
    }
    byid('install-msg').style.display = 'none';
    if($('join_form_notice')) {
        SetTopAndCenter('join_form_div', 'join_form_notice');
    }
    if(typeof init!='undefined') {
        init();
    }
}

function testProtocolHandler(el)
{
   if ("protocolLong" in el)
   {
       return false;
   }
   var ifr = document.getElementById(el.id);
   try {
       ifr.contentWindow.location = el.href;
       window.setTimeout(function () {
           try {
               alert(ifr.contentWindow.location); 
           } catch (e) { alert("Application is not installed"); }
       }, 0);
    } catch(e) {
        if (e.name == "NS_ERROR_UNKNOWN_PROTOCOL") {
            alert("Application is not installed");
        }
    }
    return false;
}


window.onresize = function() {
    if($('join_form_notice')) {
        SetTopAndCenter('join_form_div', 'join_form_notice');
    }
}
