﻿function E(elID) { return document.getElementById(elID); }
function hideEl(el) { el.style.display = 'none'; }
function hideE(elID) { hideEl(E(elID)) }
function showEl(el) { el.style.display = el.tagName.toUpperCase() == 'SPAN' ? 'inline' : 'block'; }
function showE(elID) { showEl(E(elID)); }

function imageReplace(newIm, oldID)
{
    var o = E(oldID);
    var d = newIm;
    d.className = o.className;
    d.alt = o.alt;
    o.parentNode.replaceChild(d, o);
    newIm.onload = null;
    delete o;
    d.id = o.id;
}

function doAjaxRequestFast(url)
{
    if (typeof XMLHttpRequest != 'undefined')
    {
        var client = new XMLHttpRequest();
        client.open("GET", url);
        client.send("");
    }
    else
    {
        doAjaxRequest(url, function() {})
    }
}

var isIframeLiveRequest = false;

function doAjaxRequest(url, callback)
{
    if (typeof XMLHttpRequest != 'undefined')
    {
        var client = new XMLHttpRequest();
        client.onreadystatechange = function()
        {
            if (this.readyState != 4) return;
            if (this.status == 200) callback(this.responseText);
            else callback('');

        };
        client.open("GET", url);
        client.send("");
    }
    else
    {
        var el = E('ifTemp1');
        if (!el)
        {
            el = document.createElement("iframe");
            el.setAttribute('id', 'ifTemp1');
            E('frameDiv').appendChild(el);
            el.setAttribute('frameborder', '0');
            el.setAttribute('style', 'display:none');
            el.attachEvent('onerror', function () { isIframeLiveRequest = false; })
        }
        if (isIframeLiveRequest)
        {
            setTimeout(function () { doAjaxRequest(url, callback); }, 300);
            return;
        }
        var f = function () { el.detachEvent('onload', f); isIframeLiveRequest = false; callback(E('ifTemp1').contentWindow.document.body.innerHTML) }
        el.attachEvent("onload", f);
        isIframeLiveRequest = true;
        el.setAttribute('src', url);
    }
}

var glowStep = 0.05;
var glowPeriod = 10;
var isIE = (navigator.appName == 'Microsoft Internet Explorer' && parseFloat(navigator.userAgent.split('MSIE ')[1]) < 9);

function glowDown(ctrl, func)
{
    if (ctrl.style.opacity > 0) ctrl.style.opacity = Math.max(parseFloat(ctrl.style.opacity) - glowStep, 0);
    if (ctrl.style.opacity == 0) func();
    else setTimeout(function() { glowDown(ctrl, func); }, glowPeriod);
}

function glowUp(ctrl)
{
    if (ctrl.style.opacity < 1)
        ctrl.style.opacity = Math.min(parseFloat(ctrl.style.opacity) + glowStep, 1);
    if (ctrl.style.opacity < 1)
        setTimeout(function() { glowUp(ctrl); }, glowPeriod);
}

function glowSetValue(ctrl, func)
{
    if (!isGlowEnabled)
        func();
    else
    {
        ctrl.style.opacity = 1;
        setTimeout(function() { glowDown(ctrl, function() { func(); glowUp(ctrl); }); }, glowStep);
    }
}

function glowChangeValue(ctrl, newValue, func)
{
    if (ctrl.innerHTML != newValue)
        glowSetValue(ctrl, function() { ctrl.innerHTML = newValue; func(); });
}

function setTempText(tempString, ctrl1, ctrl2)
{
    var idx = tempString.indexOf('.');
    ctrl1.innerHTML = tempString.substring(0, idx);
    ctrl2.innerHTML = tempString.substring(idx, tempString.length);
}

function checkTempText(tempString, ctrl1, ctrl2)
{
    var idx = tempString.indexOf('.');
    return ctrl1.innerHTML == tempString.substring(0, idx) &&
           ctrl2.innerHTML == tempString.substring(idx, tempString.length);
}

function colorTemp(ctrl, data) { ctrl.style.color = (data > 0 ? "#663333" : "#333366"); }

function glowWorker2(ctrl)
{
	var v = ctrl.getAttribute('glowstate')
	if(v == 'd')
	{
	    if (ctrl.style.opacity > 0) ctrl.style.opacity = Math.max(parseFloat(ctrl.style.opacity) - glowStep, 0);
    	if (ctrl.style.opacity == 0)
		{
			hideEl(ctrl)
			ctrl.removeAttribute('glowstate');
		}
 	    else setTimeout(function() { glowWorker2(ctrl); }, glowPeriod);
	}
	if(v == 'u')
	{
	    if (ctrl.style.opacity < 1) ctrl.style.opacity = Math.min(parseFloat(ctrl.style.opacity) + glowStep, 1);
	    if (ctrl.style.opacity < 1)
	        setTimeout(function() { glowWorker2(ctrl); }, glowPeriod);
		else
			ctrl.removeAttribute('glowstate');
	}
}

function glowWorkStart2(ctrl, t)
{
	var isWork = ctrl.hasAttribute('glowstate')
	ctrl.setAttribute('glowstate', t)
	if(!isWork) glowWorker2(ctrl)	
}

function glowHide2(ctrl)
{
    if (!isGlowEnabled) hideEl(ctrl)
	else glowWorkStart2(ctrl, 'd')
}

function glowShow2(ctrl)
{
	showEl(ctrl);
	if (isGlowEnabled)
		glowWorkStart2(ctrl, 'u')
}

