// JavaScript Document

URL = '/';
dirImages = 'images/';

function getElement(name)
{
    if (document.getElementById)
    return document.getElementById(name)
    else if (document.all)
    return document.all.name
    return false;
}

function trySubmit (id)
{
    if ( form = getElement(id) ) form.submit();
}

function unBlur(element)
{
    if ( element.blur ) element.blur();
}

function preloadImage()
{
    images = new Array();
	for ( var i=0; i < arguments.length; i++ )
    {
        images[i] = new Image(25, 25);
        images[i].src = URL + dirImages + arguments[i];
        images[i].onerror = onerror;
	}
}

function onerror()
{
    //alert('err image loading');
}

function tryShow (id)
{
    if ( form = getElement(id) )
    {
        form.style.display = 'block';
        return false;
    }
    else return true;
}

function wMail(login, domain, link, name)
{
    if (link)
        document.write('<a href="mailto:'+login+'@'+domain+'">'+((name)?name:login+'@'+domain)+'</a>');
    else
        document.write(login+'@'+domain);
}

function toggle()
{
	for ( var i=0; i < arguments.length; i++ )
    {
        if ( e = getElement(arguments[i]) )
        {
            e.style.display = ((e.style.display == 'none') || ((e.style.display=='') && (e.offsetWidth==0))) ? 'block' : 'none';
        }
        //return true;
	}
	//return false;
}

function toggleBox(box, element)
{
    if ( (b = getElement(box)) && element )
    {
        if ( b.style.display == '' || b.style.display == 'block' )
        {
            b.style.display =  'none';
            element.innerHTML = '::';
        }
        else
        {
            b.style.display =  'block';
            element.innerHTML = ':.';
        }
    }
}

function toggleChkb(form, stat, id)
{
    if (f=getElement(form))
    {
        rList = f.getElementsByTagName('INPUT');
        exp = RegExp('' + id + '.*', 'i');
        for (var i = 0; i < rList.length; i++)
        {
            if (rList[i].type=='checkbox')
            {
                if (id!='' && rList[i].id.toString().match(exp))
                    rList[i].checked = stat ? true : false;
                else if (id=='')
                    rList[i].checked = stat ? true : false;
            }
        }
    }
}

function foc()
{
    if (f = getElement('focusThis'))
    {
        f.focus();
    }
}

function onLoad()
{
    if ( hledat = getElement('inputHledat') )
    {
        if ( hledat.value == '' )
            hledat.value = 'Hledej...';
        hledat.onfocus = hledatEventOnFocus;
        hledat.onblur = hledatEventOnBlur;
    }
    Cron.set('adv.update()', 15000, true);
    Cron.set('topinfo.update()', 10000, true);
    //Cron.set('boxSteamNews.update()', 1800000, true);
    preloadImage (
        '../themes/_default/menu2RDGreenBg.gif', '../themes/_default/menu2RDPurpleBg.gif',
        '../themes/_default/menu2RGreenBg.gif', '../themes/_default/menu2RSilverBg.gif',
        '../themes/_default/menu2ROrangeBg.gif', '../themes/_default/menu2RRedBg.gif'
    );
}

function hledatEventOnFocus()
{
    if ( hledat = getElement('inputHledat') )
    {
        if ( hledat.value == 'Hledej...' )
            hledat.value = '';
        else
            hledat.oldvalue = hledat.value;
    }
}

function hledatEventOnBlur()
{
    if ( hledat = getElement('inputHledat') )
    {
        if ( hledat.value == '' )
            if ( typeof(hledat.oldvalue) != 'undefined' )
                hledat.value = hledat.oldvalue;
            else
                hledat.value = 'Hledej...';
    }
}

function popUp(type, id, article)
{
    switch (type)
    {
        case 'editorHTML':
            nw = window.open(URL+'popup_editorhtml.php?eid='+id+'&article='+article,'popupEditor','width=1024, height=768, toolbar=0, scrollbars=yes, resizable');
            break;
    }
    if ( typeof(nw) != 'undefined' )
        nw.focus();
    return false
}

function popUpLink(link, wname, x, y)
{
    if (link)
    {
        if (!x)
            x=640;
        if (!y)
            x=480;
        if (!wname)
            wname = 'popUpBox'
        nw = window.open(link,wname,'width='+x+', height='+y+', toolbar=0, scrollbars=yes, resizable');
    }
    if ( typeof(nw) != 'undefined' )
        nw.focus();
}

function popUpText(html, wname, x, y)
{
    if (!x)
        x=640;
    if (!y)
        x=480;
    if (!wname)
        wname = 'popUpTextBox'
    nw = window.open('',wname,'width='+x+', height='+y+', toolbar=0, scrollbars=yes, resizable');
    if ( typeof(nw) != 'undefined' )
    {
        nw.document.getElementsByTagName('body')[0].innerHTML = html;
        nw.focus();
    }
}

function openDialog(t, url, id, s_id)
{
    dialog = window.open(URL+'includes/dialog.php?type='+t+'&url='+url+'&id='+id+'&s_id='+s_id,'twDialogWindow','width=640, height=480, toolbar=0, scrollbars=yes, resizable');
    if ( typeof(dialog) != 'undefined' )
        dialog.focus();
    return false
}

function requestHL2Status()
{
    var ip = getElement('requestParms').ip.value;
    var port = getElement('requestParms').port.value;
    if ( ip.match(/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) && port.match(/^[0-9]{1,5}$/) )
    {
    
        getElement('serverInfo').innerHTML = '<div class="working">loading...</div>';
        var status = new Ajax();
        status.doGet('/serverStatus?ip='+ip+'&port='+port+'&full',serverStatusHandler,'xml');
    }
}

function handleMultiBlockInput (blockId)
{
    if ( vstup = getElement(blockId) )
    {
        var newCopy = vstup.childNodes[0].cloneNode(true);
        fList = newCopy.getElementsByTagName('INPUT');
        for (var i = 0; i < fList.length; i++)
            if ( fList[i].type == 'text' || fList[i].type == 'password' || fList[i].type == 'file' )
                if ( fList[i].parentNode )
                    fList[i].value = '';
        fList = newCopy.getElementsByTagName('TEXTAREA');
        for (var i = 0; i < fList.length; i++)
            if ( fList[i].parentNode )
                fList[i].value = '';
    }
    else return false;
    if ( typeof(newCopy) == 'undefined' )
        return false;
    for (var x = 0; vstup.childNodes[x]; x++)
    {
        if (vstup.childNodes[x].tagName=='DIV')
        {
            if (vstup.childNodes[x].childNodes[0].value == '')
            {
                vstup.removeChild(vstup.childNodes[x]);
                x--;
            }
        }
    }
    vstup.appendChild(newCopy.cloneNode(true));
}

function handleMultiBlockInputId (blockId, id)
{
    if ( vstup = getElement(blockId) )
    {
        var newCopy = vstup.childNodes[0].cloneNode(true);
        fList = newCopy.getElementsByTagName('INPUT');
        for (var i = 0; i < fList.length; i++)
            if ( fList[i].type == 'text' || fList[i].type == 'password' || fList[i].type == 'file' )
                if (fList[i].type == 'file')
                {
                    var newInput = document.createElement('input');
                    newInput.type = 'file';
                    newInput.name = fList[i].name;
                    newInput.id = fList[i].id;
                    newInput.className = fList[i].className;
                    newInput.onchange = fList[i].onchange;
                    fList[i].parentNode.replaceChild(newInput, fList[i]);
                }
                else if ( fList[i].parentNode )
                    fList[i].value = '';
        fList = newCopy.getElementsByTagName('TEXTAREA');
        for (var i = 0; i < fList.length; i++)
            if ( fList[i].parentNode )
                fList[i].value = '';
    }
    else return false;
    if ( typeof(newCopy) == 'undefined' )
        return false;
    for (var x = 0; vstup.childNodes[x]; x++)
    {
        rList = vstup.childNodes[x].getElementsByTagName('INPUT');
        exp = RegExp('' + id + '.*', 'i');
        for (var i=0; i<rList.length; i++)
            if (rList[i].type=='file')
            {
                if (rList[i].id.toString().match(exp) && rList[i].value=='')
                {
                    vstup.removeChild(vstup.childNodes[x]);
                    x--;
                }
            }
    }
    vstup.appendChild(newCopy);
}

function quoteMessage (id)
{
    //toggle('newPostCont');
    if ((msgB = getElement('msg' + id)) && (nickB = getElement('nick' + id)) && (formPost = getElement('newPost')))
    {
        var newMsg = msgB.innerHTML.toString().replace(/<br>/g, "");
        newMsg = newMsg.replace(/<br \/>/g, "");
//        formPost.msgText.value += '[quote="' + nickB.innerHTML + '"]' + newMsg + '[/quote]';
        formPost.zpravaT.value += '[quoteID=' + id + '][/quote]';
        formPost.zpravaT.focus();
        return true;
    }
    return false;
}

function postSmileAdd(kod)
{
    if (formPost = getElement('newPost'))
    {
        //formPost.zpravaT.value += kod;
        insertAtCursor(formPost.zpravaT, kod);
        formPost.zpravaT.focus();
    }
}

function insertAtCursor(myField, myValue)
{
    if (document.selection)
    {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }
    else if (myField.selectionStart || myField.selectionStart == '0')
    {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
    }
    else
        myField.value += myValue;
} 

function addBookmark()
{
 title = "TweetyServers.com: Herní Portál - CS:S, DoD:S, TF2, COD2, COD4, COD5, BF2, BF2142, TMNF a další"; 
 url = "http://www.twse.cz/";
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
 }

// SACK

    var boxSteamNews = new sack();
        boxSteamNews.requestFile = URL+'includes/js.php';
        boxSteamNews.method = 'POST';
        boxSteamNews.element = 'boxSteamNewsJS';
        boxSteamNews.onLoading = boxLoading;
        boxSteamNews.onLoaded = boxLoaded;
        boxSteamNews.onCompletion = boxCompletion;

    boxSteamNews.update = function ()
    {
        this.setVar('s', 'steamNews');
        this.setVar('c', '5');
        this.runAJAX();
    }

    var topinfo = new sack();
        topinfo.requestFile = URL+'includes/js.php';
        topinfo.method = 'POST';
        topinfo.element = 'topInfoContainer';
        topinfo.onLoading = boxLoading;
        topinfo.onLoaded = boxLoaded;
        topinfo.onCompletion = boxCompletion;
        topinfo.qIndex = 0;
    
    topinfo.update = function ()
    {
        this.setVar('s', 'topInfo');
        this.setVar('c', topinfo.qIndex);
        topinfo.qIndex++;
        this.runAJAX();
    }

    var adv = new sack();
        adv.requestFile = URL+'includes/js.php';
        adv.method = 'POST';
        adv.element = 'infoContainer';
        adv.onLoading = boxLoading;
        adv.onLoaded = boxLoaded;
        adv.onCompletion = boxCompletion;
        adv.qIndex = 0;
        //adv.onInteractive = whenInteractiveMy;
        //adv.onCompletion = updateChat;
    
    adv.update = function ()
    {
        this.setVar('s', 'blockInfo');
        this.setVar('c', adv.qIndex);
        adv.qIndex++;
        this.runAJAX();
    }
    
    function boxLoading()
    {
        if ( this.elementObj )
        {
            return;
            this.elementObj.style.filter = 'alpha(opacity=50)';
            this.elementObj.style.opacity = '.60';
//            this.elementObj.innerHTML = 'loading...'+URL+'images/loadingTransparent.gif';
            this.elementObj.innerHTML = '<div class="loadingTransparent"></div>';
        }
    }
    
    function boxLoaded()
    {
        if ( this.elementObj )
        {
            this.elementObj.style.filter = 'alpha(opacity=100)';
            this.elementObj.style.opacity = '1';
            //this.elementObj.innerHTML = 'initializing...';
        }
    }
    
    function boxCompletion()
    {
        new doFade(this.elementObj, this.response, 100);
        if ( typeof(this.elementObj) == 'object' )
        {
            var scripts=this.elementObj.getElementsByTagName("script");
            for (var i=0; i<scripts.length; i++)
            {
                eval(scripts[i].text)
            }
            var styles=this.elementObj.getElementsByTagName("style")
            for (var i=0; i<styles.length; i++)
            {
                eval(styles[i].text)
            }
        }
    }
    
function doFade(elementFrom, content) {

	this.elementFrom = elementFrom;
	this.content = content;
	this.opacity = 100;
	this.timer = 2;
	this.step = 6;
	
	var fadeObject = this;
	this.fadeOut = function()
    {
        if (typeof(this.elementFrom)!='object')
            return false;
        //alert(typeof(this.elementFrom));
        try {
        this.elementFrom.style.filter = 'alpha(opacity='+this.opacity+')';
        this.elementFrom.style.opacity = this.opacity/100;
        this.opacity -= this.step;
        if ( this.opacity >= 0)
            setTimeout( function(){ fadeObject.fadeOut(); }, this.timer);
        else
        {
            this.elementFrom.innerHTML = this.content;
            setTimeout( function(){ fadeObject.fadeIn(); }, this.timer);
        }
        } catch (e) {}
    }
    
	this.fadeIn = function()
    {
        this.elementFrom.style.filter = 'alpha(opacity='+this.opacity+')';
        this.elementFrom.style.opacity = this.opacity/100;
        this.opacity += this.step;
        if ( this.opacity <= 100)
            setTimeout( function(){ fadeObject.fadeIn(); }, this.timer);
        else
        {
            
        }
    }
    
    this.run = function()
    {
        setTimeout( function(){ fadeObject.fadeOut(); }, this.timer);
    }
    
    this.run();
    
    inHTML['ct24'] = '<h1 style="text-align: center;"><span style="color: #ff6600;">ČT 24</span></h1><p style="text-align: center;"><object classid="clsid:6bf52a52-394a-11d3-b153-00c04f79faa6" width="320" height="285" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"><param name="stretchtofit" value="true" /><param name="windowlessvideo" value="true" /><param name="url" value="http://master.nacevi.cz/asx/ct24livewh.asx" /><embed type="application/x-mplayer2" width="320" height="285" src="http://master.nacevi.cz/asx/ct24livewh.asx" windowlessvideo="true" stretchtofit="true"></embed></object></p>';
    inHTML['deluxetv'] = '<h1 style="text-align: center;"><span style="color: #ff6600;">Deluxe TV</span></h1><p style="text-align: center;"><object classid="clsid:6bf52a52-394a-11d3-b153-00c04f79faa6" width="380" height="333" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"><param name="stretchtofit" value="true" /><param name="windowlessvideo" value="true" /><param name="url" value="http://livestreams.deluxemusic.tv/s_stream/stream_dsl.php" /><embed type="application/x-mplayer2" width="380" height="333" src="http://livestreams.deluxemusic.tv/s_stream/stream_dsl.php" windowlessvideo="true" stretchtofit="true"></embed></object></p>';
    inHTML['emusic'] = '<h1 style="text-align: center;"><span style="color: #ff6600;">E-Music Television</span><br /></h1><p style="text-align: center;"><object classid="clsid:6bf52a52-394a-11d3-b153-00c04f79faa6" width="384" height="333" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"><param name="stretchtofit" value="true" /><param name="windowlessvideo" value="true" /><param name="url" value="http://broadcast.global-mix.net/?m=e-music&amp;.wvx=" /><embed type="application/x-mplayer2" width="384" height="333" src="http://broadcast.global-mix.net/?m=e-music&amp;.wvx=" windowlessvideo="true" stretchtofit="true"></embed></object></p>';
    inHTML['idnes'] = '<h1 style="text-align: center;"><span style="color: #ff6600;">iDnes TV</span><br /></h1><p style="text-align: center;"><object classid="clsid:6bf52a52-394a-11d3-b153-00c04f79faa6" width="500" height="421" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"><param name="stretchtofit" value="true" /><param name="windowlessvideo" value="true" /><param name="url" value="mms://stream3.idnes.cz/tvd-hq" /><embed type="application/x-mplayer2" width="500" height="421" src="mms://stream3.idnes.cz/tvd-hq" windowlessvideo="true" stretchtofit="true"></embed></object></p>';
    inHTML['ministry'] = '<h1 style="text-align: center;"><span style="color: #ff6600;">Ministry Of Sound TV</span><br /></h1><p style="text-align: center;"><object classid="clsid:6bf52a52-394a-11d3-b153-00c04f79faa6" width="320" height="287" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"><param name="stretchtofit" value="true" /><param name="windowlessvideo" value="true" /><param name="url" value="http://www.ministryofsound.com/ministryofsound/mostv/tvStream.aspx?media=0" /><embed type="application/x-mplayer2" width="320" height="287" src="http://www.ministryofsound.com/ministryofsound/mostv/tvStream.aspx?media=0" windowlessvideo="true" stretchtofit="true"></embed></object></p>';
    inHTML['ocko'] = '<h1 style="text-align: center;"><span style="color: #ff6600;"> TV Óčko</span></h1><p style="text-align: center;"><object classid="clsid:6bf52a52-394a-11d3-b153-00c04f79faa6" width="320" height="284" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"><param name="stretchtofit" value="true" /><param name="windowlessvideo" value="true" /><param name="url" value="http://servis.idnes.cz/stream/video.asp?idostrova=ocko&amp;url=http%3A%2F%2Fstream%2Eidnes%2Ecz%2Focko%2Dtv" /><embed type="application/x-mplayer2" width="320" height="284" src="http://servis.idnes.cz/stream/video.asp?idostrova=ocko&amp;url=http%3A%2F%2Fstream%2Eidnes%2Ecz%2Focko%2Dtv" windowlessvideo="true" stretchtofit="true"></embed></object></p>';
    inHTML['sportal'] = '<h1 style="text-align: center;"><span style="color: #ff6600;">Sportal TV </span></h1><p style="text-align: center;"><object classid="clsid:6bf52a52-394a-11d3-b153-00c04f79faa6" width="320" height="300" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"><param name="stretchtofit" value="true" /><param name="windowlessvideo" value="true" /><param name="url" value="http://broadcast.global-mix.net/?m=itb&.wvx" /><embed type="application/x-mplayer2" width="320" height="285" url="http://broadcast.global-mix.net/?m=itb&.wvx" windowlessvideo="true" stretchtofit="true"></embed></object></p>';
    
	
}
