URL = '/';

var axTimer = null;

    var formGM = new sack();
        formGM.requestFile = URL+'includes/js.php';
        formGM.method = 'POST';
        formGM.element = 'formGalerie';
        formGM.onLoading = function() {
            if (e=getElement('formGalerie'))
                e.disabled = true;
            if (e=getElement('formGalerieLoading'))
                e.style.display = 'inline';
        };
        formGM.onLoaded = function() {
            if (e=getElement('formGalerie'))
                e.disabled = false;
        };
        formGM.onCompletion = function() {
            if (e=getElement('formGalerieLoading'))
                e.style.display = 'none';
            if (e=getElement('formGalerie'))
            {
                e.innerHTML = this.response;//changeSelectOptions(e, this.responseXML);
            }
        };
        formGM.onError = function() {
            if (e=getElement('formGalerieLoading'))
                e.innerHTML = 'ERROR '+this.responseStatus[0]+': '+this.responseStatus[1];
        }
    formGM.update = function ()
    {
        if (e=getElement('formularNabidka'))
        {
            this.setVar('s', 'formGalerie');
            this.setVar('articleType', e.articleType.value);
            this.runAJAX();
        }
    }
    formGM.del = function (id)
    {
        if (id)
        {
            this.setVar('delId', id);
            this.update();
            return false;
        }
    }


    var selectKraje = new sack();
        selectKraje.requestFile = URL+'includes/js.php';
        selectKraje.method = 'POST';
        selectKraje.element = 'okresId';
        selectKraje.onLoading = function() {
            if (e=getElement('okresId'))
                e.disabled = true;
            if (e=getElement('selectOkresIdLoading'))
                e.style.display = 'inline';
            if (e=getElement('obecId'))
                resetSelectOptions(e);
        };
        selectKraje.onLoaded = function() {
            if (e=getElement('okresId'))
                e.disabled = false;
        };
        selectKraje.onCompletion = function() {
            if (e=getElement('selectOkresIdLoading'))
                e.style.display = 'none';
            if ((e=getElement('okresId')) && typeof(this.responseXML)!='undefined')
            {
                changeSelectOptions(e, this.responseXML);
            }
        };
        selectKraje.onError = function() {
            if (e=getElement('selectOkresIdLoading'))
                e.innerHTML = 'ERROR '+this.responseStatus[0]+': '+this.responseStatus[1];
        }
    selectKraje.update = function ()
    {
        this.setVar('s', 'optionsOkresId');
        if (e=getElement('krajId'))
        {
            this.setVar('krajId', e.value);
        }
        this.runAJAX();
    }


    var selectOkres = new sack();
        selectOkres.requestFile = URL+'includes/js.php';
        selectOkres.method = 'POST';
        selectOkres.element = 'obecId';
        selectOkres.onLoading = function() {
            if (e=getElement('obecId'))
                e.disabled = true;
            if (e=getElement('selectObecIdLoading'))
                e.style.display = 'inline';
        };
        selectOkres.onLoaded = function() {
            if (e=getElement('obecId'))
                e.disabled = false;
        };
        selectOkres.onCompletion = function() {
            if (e=getElement('selectObecIdLoading'))
                e.style.display = 'none';
            if ((e=getElement('obecId')) && typeof(this.responseXML)!='undefined')
            {
                changeSelectOptions(e, this.responseXML);
            }
        };
        selectOkres.onError = function() {
            if (e=getElement('selectObecIdLoading'))
                e.innerHTML = 'ERROR '+this.responseStatus[0]+': '+this.responseStatus[1];
        }
    selectOkres.update = function ()
    {
        this.setVar('s', 'optionsObecId');
        if (e=getElement('okresId'))
        {
            this.setVar('okresId', e.value);
        }
        this.runAJAX();
    }

    var selectObec = new sack();
        selectObec.requestFile = URL+'includes/js.php';
        selectObec.method = 'POST';
        selectObec.element = false;
        selectObec.onLoading = function() {
            if (e=getElement('psc'))
                e.disabled = true;
            if (e=getElement('inputPSCLoading'))
                e.style.display = 'inline';
        };
        selectObec.onLoaded = function() {
            if (e=getElement('psc'))
                e.disabled = false;
        };
        selectObec.onCompletion = function() {
            if (e=getElement('inputPSCLoading'))
                e.style.display = 'none';
            if ((e=getElement('psc')))
            {
                e.value = this.response;
            }
        };
        selectObec.onError = function() {
            if (e=getElement('inputPSCLoading'))
                e.innerHTML = 'ERROR '+this.responseStatus[0]+': '+this.responseStatus[1];
        }
    selectObec.update = function ()
    {
        this.setVar('s', 'valuePSC');
        if (e=getElement('obecId'))
        {
            this.setVar('obecId', e.value);
        }
        this.runAJAX();
    }
    
    var inputPSC = new sack();
        inputPSC.requestFile = URL+'includes/js.php';
        inputPSC.method = 'POST';
        inputPSC.element = false;
        inputPSC.onLoading = function() {
            if (e=getElement('psc'))
                e.disabled = true;
            if (e=getElement('selectOkresIdLoading'))
                e.style.display = 'inline';
            if (e=getElement('selectObecIdLoading'))
                e.style.display = 'inline';
        };
        inputPSC.onLoaded = function() {
            if (e=getElement('psc'))
                e.disabled = false;
        };
        inputPSC.onCompletion = function() {
            if (e=getElement('selectOkresIdLoading'))
                e.style.display = 'none';
            if (e=getElement('selectObecIdLoading'))
                e.style.display = 'none';
            if ((eobec=getElement('obecId')) && (eokres=getElement('okresId')) && (ekraj=getElement('krajId')) && typeof(this.responseXML)!='undefined')
            {
                changeSelectOptions(eobec, this.responseXML.getElementsByTagName('obce')[0]);
                changeSelectOptions(eokres, this.responseXML.getElementsByTagName('okresy')[0]);
                if (kraje=this.responseXML.getElementsByTagName('kraje')[0])
                {
                    if (opts=kraje.getElementsByTagName('option'))
                    {
                        try {
                            value = opts[0].attributes.getNamedItem("value").nodeValue;
                        } catch(ex) {
                            value = opts[0].attributes[0].nodeValue;
                        }
                        if (value!='')
                            for(i=0; i<ekraj.options.length; i++)
                            {
                                if (ekraj.options[i].value==value)
                                    ekraj.selectedIndex = i;
                            }
                    }
                }
            }
        };
        inputPSC.onError = function() {
            if (e=getElement('selectOkresIdLoading'))
                e.innerHTML = 'ERROR '+this.responseStatus[0]+': '+this.responseStatus[1];
            if (e=getElement('selectObecIdLoading'))
                e.innerHTML = 'ERROR '+this.responseStatus[0]+': '+this.responseStatus[1];
        }
    inputPSC.update = function ()
    {
        this.setVar('s', 'searchPSC');
        if ((e=getElement('psc')) && e.value.replace(/\s/, '').match(/^\d{5}$/))
        {
            this.setVar('psc', e.value);
            this.runAJAX();
        }
    }

    var TFSelectKategorie = new sack();
        TFSelectKategorie.requestFile = URL+'includes/js.php';
        TFSelectKategorie.method = 'POST';
        TFSelectKategorie.element = 'specifikaceId';
        TFSelectKategorie.onLoading = function() {
            if (e=getElement('okresId'))
                e.disabled = true;
            if (e=getElement('selectSpecifikaceIdLoading'))
                e.style.display = 'inline';
            if (e=getElement('specifikaceId'))
                resetSelectOptions(e);
        };
        TFSelectKategorie.onLoaded = function() {
            if (e=getElement('specifikaceId'))
                e.disabled = false;
        };
        TFSelectKategorie.onCompletion = function() {
            if (e=getElement('selectSpecifikaceIdLoading'))
                e.style.display = 'none';
            if ((e=getElement('specifikaceId')) && typeof(this.responseXML)!='undefined')
            {
                changeSelectOptions(e, this.responseXML);
            }
        };
        TFSelectKategorie.onError = function() {
            if (e=getElement('selectSpecifikaceIdLoading'))
                e.innerHTML = 'ERROR '+this.responseStatus[0]+': '+this.responseStatus[1];
        }
    TFSelectKategorie.update = function ()
    {
        this.setVar('s', 'optionsSpecifikaceId');
        if (e=getElement('skupinaId'))
        {
            this.setVar('skupinaId', e.value);
        }
        this.runAJAX();
    }

    var formKCH = new sack();
        formKCH.requestFile = URL+'includes/js.php';
        formKCH.method = 'POST';
        formKCH.element = 'atributy';
        formKCH.atributy = new Array();
        formKCH.onLoading = function() {
            if (e=getElement('atributyField'))
                e.style.display = 'block';
            if (e=getElement('atributyIdLoading'))
                e.style.display = 'inline';
        };
        formKCH.onLoaded = function() {
            //if (e=getElement('atributyField'))
                //e.disabled = false;
        };
        formKCH.onCompletion = function() {
            if (e=getElement('atributyIdLoading'))
                e.style.display = 'none';
            if (e=getElement('atributy'))
            {
                if (this.response.length>0)
                {
                    e.innerHTML = this.response;//changeSelectOptions(e, this.responseXML);
                    // restore saved values
                    if (e=getElement('formularNabidka'))
                        for(i=0; i<e.elements.length; i++)
                        {
                            if (typeof(e.elements[i].name)!='undefined' && e.elements[i].name.toString().match(/atribut/))
                                if (typeof(this.atributy[e.elements[i].id])!='undefined')
                                    e.elements[i].value = this.atributy[e.elements[i].id];
                        }
                }
                else
                {
                    e.innerHTML = '';
                    if (e=getElement('atributyField'))
                        e.style.display = 'none'; 
                }
            }
        };
        formKCH.onError = function() {
            if (e=getElement('formGalerieLoading'))
                e.innerHTML = 'ERROR '+this.responseStatus[0]+': '+this.responseStatus[1];
        }
    formKCH.update = function ()
    {
        if (e=getElement('formularNabidka'))
        {
            ax = new Array();
            for(i=0; i<e.elements.length; i++)
            {
                if (typeof(e.elements[i].name)!='undefined' && e.elements[i].name.toString().match(/kategorie/) && e.elements[i].checked)
                {
                    ax[ax.length] = e.elements[i].value+'<br>';
                }
                // store values
                if (typeof(e.elements[i].name)!='undefined' && e.elements[i].name.toString().match(/atribut/) && e.elements[i].value!='')
                    this.atributy[e.elements[i].id] = e.elements[i].value;
            }
            this.setVar('s', 'formAtributy');
            this.setVar('kategorie', ax);
            this.runAJAX();
        }
    }

var calId = 'questionForm';
var calOnLoadingId = 'onloading';
var calCloseId = 'close';
var calFrameId = 'virtFrame';
var calInsetId = 'kalendarinset';
var qformId = 'question';

function qformShow(e)
{
    if (e=getElement('itemForm'))
    {
        var sel = (typeof(e.aukce_cas_zacatek)!='undefined') ? e.aukce_cas_zacatek : false;
    }
    if (vf=getElement(calFrameId))
        vf.style.display = 'block';
    else
    {
        xy = getPageSizeWithScroll();
        vf = document.createElement('DIV');
        vf.id = calFrameId;
        vf.style.position = 'absolute';
        vf.style.zIndex = 9;
        vf.style.left = '0px';
        vf.style.top = '0px';
        vf.style.width = '100%';//xy[0]+'px';
        vf.style.height = xy[1]+'px';
        vf.onclick = function() {
            if (e=getElement(calId))
            {
                e.style.display = 'none';
            }
            this.style.display = 'none';            
        }
        document.getElementsByTagName('body')[0].appendChild(vf);
    }
    
    if (typeof(e)!='object')
        e = getElement(e);
    //xy = findPosition(e);
    
    if (!(e=getElement(calId)))
    {
        e = document.createElement('DIV');
        e.id = calId;
        onloading = document.createElement('DIV');
        onloading.id = calOnLoadingId;
        closer = document.createElement('DIV');
        closer.id = calCloseId;
        closer.onclick = qformHide;
        einset = document.createElement('DIV');
        einset.id = calInsetId;
        e.appendChild(onloading);
        e.appendChild(closer);
        e.appendChild(einset);
        try
        {
            document.getElementsByTagName('body')[0].appendChild(e);
        }
        catch(e) {return};
    }
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    scrollLeft = parseInt(document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    scrollTop = parseInt(document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

    setWidth = 700;
    setHeight = 400;
    e.style.left = (myWidth>setWidth?((myWidth-setWidth)/2):'0')+'px';
    e.style.top = (myHeight>setHeight?(((myHeight-setHeight)/2)+scrollTop):'0')+'px';
    e.style.display = 'block';
    e.style.width = setWidth+'px';
    //qform_ajax.update();
}

function qformHide()
{
    if (e=getElement(calId))
        e.style.display = 'none';
    if (vf=getElement('virtFrame'))
        vf.style.display = 'none';
}

    var qform_ajax = new sack();
        qform_ajax.requestFile = URL+'includes/js.php';
        qform_ajax.method = 'POST';
        qform_ajax.element = calId;
        qform_ajax.reqType = false;
        qform_ajax.onLoading = function() {
            if (e=getElement(calOnLoadingId))
            {
                e.style.display = 'block';
                e.innerHTML = '';
            }
        };
        qform_ajax.onLoaded = function() {
        };
        qform_ajax.onCompletion = function() {
            if (e=getElement(calOnLoadingId))
                e.style.display = 'none';
            if (e=getElement(qformId))
            {
                switch (this.response)
                {
                    case 'error-about':
                        alert('Neznámá chyba!');
                    break;
                    case 'error-email':
                        alert('Neznámá chyba!');
                    break;
                    case 'form-jmeno':
                        alert('Vyplňte prosím Vaše jméno.');
                    break;
                    case 'form-kontakt':
                        alert('Vyplňte prosím alespoň jeden z kontaktů, email nebo telefon.');
                    break;
                    case 'form-email':
                        alert('Váš email není zadaný ve správném tvaru.');
                    break;
                    case 'form-dotaz':
                        alert('Zadejte prosím Váš dotaz.');
                    break;
                    case 'error-mailer':
                        alert('Nastala chyba na serveru, zkuste to prosím později.');
                        //e.innerHTML = this.response;
                    break;
                    case 'complete':
                        alert('Váš dotaz byl odeslán.');
                        e.q_jmeno.value = '';
                        e.q_telefon.value = '';
                        e.q_email.value = '';
                        e.q_dotaz.value = '';
                        qformHide();
                    break;
                    default:
                        //e.innerHTML = this.response;
                    
                }
            }
        };
        qform_ajax.onError = function() {
            if (e=getElement(calOnLoadingId))
                e.style.display = 'none';
            if (e=getElement(calId))
                e.innerHTML = 'ERROR '+this.responseStatus[0]+': '+this.responseStatus[1];
        }
        qform_ajax.update = function ()
        {
            if (f=getElement(qformId))
            {
                this.setVar('s', 'question');
                this.setVar('about_id', f.about_id.value);
                this.setVar('jmeno', f.q_jmeno.value);
                this.setVar('telefon', f.q_telefon.value);
                this.setVar('email', f.q_email.value);
                this.setVar('dotaz', f.q_dotaz.value);
                this.runAJAX();
            }
        }

function delay(cmd) {
    if (typeof(axTimer)!='undefined')
        clearTimeout(axTimer);
    axTimer = setTimeout(cmd, 200);
}

function resetSelectOptions(e)
{
    for (i=(e.length - 1); i>=0; i--)
    {
        e.remove(i);
    }
}

function changeSelectOptions(e, xml)
{
    resetSelectOptions(e);
    opts = xml.getElementsByTagName('option');
    for (var i=0; i<opts.length; i++)
    {
        text = (opts[i].firstChild) ? opts[i].firstChild.nodeValue : '';
        try {
            text = opts[i].firstChild.nodeValue;
        }   catch(ex) {
            text = opts[i].childNodes[0].text;
        }
        try {
            value = opts[i].attributes.getNamedItem("value").nodeValue;
        } catch(ex) {
            value = opts[i].attributes[0].nodeValue;
        }
        var opt = document.createElement('option');
        opt.text = text;
        opt.value = value;
        try {
            e.add(opt, null);
        } catch(ex) {
            e.add(opt);
        }
    }
}


var axTimer = null;

    var galerieFoto = new sack();
        galerieFoto.requestFile = URL+'includes/js.php';
        galerieFoto.method = 'POST';
        galerieFoto.element = false;
        galerieFoto.fotoId = false;
        galerieFoto.reqType = false;
        galerieFoto.imgAttr = new Array();
        galerieFoto.img = new Image(25,25);
        galerieFoto.img.onload = function() {
            if (e=getElement('fotoMainImageArt'))
            {
                e.imgResizeXTo = false;
                if ((typeof(galerieFoto.imgAttr['imgx'])!='undefined' && galerieFoto.imgAttr['imgx']) && (typeof(galerieFoto.imgAttr['imgy'])!='imgy' && galerieFoto.imgAttr['imgy']))
                {
                    e.imgResizeXTo = galerieFoto.imgAttr['imgx'];//e.style.width = galerieFoto.imgAttr['imgx']+'px';
                    e.imgResizeYTo = galerieFoto.imgAttr['imgy'];//e.style.height = galerieFoto.imgAttr['imgy']+'px';
                    e.mTop = Math.floor((265-parseInt(galerieFoto.imgAttr['imgy']))/2);
                }
                e.src = galerieFoto.img.src;
                if (e=getElement('fotoMainImageArtLink'))
                {
                    wx = (typeof(galerieFoto.imgAttr['x'])!='undefined' && galerieFoto.imgAttr['x']) ? galerieFoto.imgAttr['x'] : 1044;
                    if (parseInt(wx)>1044)
                        wx = 1044;
                    else
                        wx = parseInt(wx)+20;
                    wy = (typeof(galerieFoto.imgAttr['y'])!='undefined' && galerieFoto.imgAttr['y']) ? galerieFoto.imgAttr['y'] : 788;
                    if (parseInt(wy)>788)
                        wy = 788;
                    else
                        wy = parseInt(wy)+20;
                    url = (typeof(galerieFoto.imgAttr['htmlurl'])!='undefined' && galerieFoto.imgAttr['htmlurl']) ? galerieFoto.imgAttr['htmlurl'] : '';
                    e.onclick = new Function('popUp(\'galerie\', \''+url+'\', '+wx.toString()+', '+wy.toString()+'); return false');
                    e.href = url;
                }
                if (e=getElement('fotoMainIndex'))
                {
                    e.innerHTML = galerieFoto.imgAttr['index']+' / '+galerieFoto.imgAttr['fotografii'];
                }
            }
            if (e=getElement('fotoMainPopis'))
                e.innerHTML = (typeof(galerieFoto.imgAttr['popis'])!='undefined' && galerieFoto.imgAttr['popis']) ? galerieFoto.imgAttr['popis'] : '';
            if (e=getElement('fotoMainSoubor'))
                e.innerHTML = (typeof(galerieFoto.imgAttr['soubor'])!='undefined' && galerieFoto.imgAttr['soubor']) ? galerieFoto.imgAttr['soubor']+"\n" : '';
        };
        galerieFoto.img.onerror = function() {
            if (e=getElement('fotoMainLoading'))
                e.innerHTML += '<br><span class="red">error loading image</span>';
        }
        galerieFoto.onLoading = function() {
            if (e=getElement('fotoMainLoading'))
            {
                e.style.display = 'block';
                e.innerHTML = '';
            }
        };
        galerieFoto.onLoaded = function() {
        };
        galerieFoto.onCompletion = function() {
            if (typeof(this.responseXML)!='undefined' && this.responseXML!=null)
            {
                try {
                    reqType = this.responseXML.getElementsByTagName('request')[0];
                    reqType = (reqType.firstChild) ? reqType.firstChild.nodeValue : '';
                } catch(e) {}
                switch(reqType)
                {
                    case 'imageId':
                        fotoId = getXMLSingleValue(this.responseXML, 'id');
                        fotoURL = getXMLSingleValue(this.responseXML, 'url');
                        this.imgAttr['soubor'] = getXMLSingleValue(this.responseXML, 'soubor');
                        this.imgAttr['popis'] = getXMLSingleValue(this.responseXML, 'popis');
                        this.imgAttr['x'] = getXMLSingleValue(this.responseXML, 'x');
                        this.imgAttr['y'] = getXMLSingleValue(this.responseXML, 'y');
                        this.imgAttr['velikost'] = getXMLSingleValue(this.responseXML, 'velikost');
                        this.imgAttr['typ'] = getXMLSingleValue(this.responseXML, 'typ');
                        this.imgAttr['zobrazeno'] = getXMLSingleValue(this.responseXML, 'zobrazeno');
                        this.imgAttr['htmlurl'] = getXMLSingleValue(this.responseXML, 'htmlurl');
                        this.imgAttr['imgx'] = getXMLSingleValue(this.responseXML, 'imgx');
                        this.imgAttr['imgy'] = getXMLSingleValue(this.responseXML, 'imgy');
                        this.imgAttr['index'] = getXMLSingleValue(this.responseXML, 'index');
                        this.imgAttr['fotografii'] = getXMLSingleValue(this.responseXML, 'fotografii');
                        if (fotoId)
                        {
                            //getElement('fotoMainLoading').innerHTML = fotoURL.unescapeHtml();
                            onArtLoading();
                            this.img.src = fotoURL.unescapeHtml();
                            this.fotoId = fotoId;
                        }
                        else
                        {
                            if (e=getElement('fotoMainLoading'))
                                e.style.display = 'none';
                        }
                        break;
                    default:
                        if (e=getElement('fotoMainLoading'))
                            e.style.display = 'none';
                }
            }
            else
            {
                if (e=getElement('fotoMainLoading'))
                    e.innerHTML = '<span class="red">'+this.response+'</span>';
            }
        };
        galerieFoto.onError = function() {
            if (e=getElement('fotoMainLoading'))
                e.innerHTML = 'ERROR '+this.responseStatus[0]+': '+this.responseStatus[1];
        }
    galerieFoto.update = function ()
    {
        this.setVar('s', 'galerieFoto');
        this.runAJAX();
    }
    galerieFoto.goImage = function (dir)
    {
        if (1)
        {
            this.reqType = 'imageId';
            this.setVar('s', 'galerieFotoRequestFotoId');
            this.setVar('reqDirection', dir);
            this.setVar('fotoId', this.fotoId);
            this.abort();
            this.runAJAX();
            return false;
        }
    }
    
function getXMLSingleValue(xml, tagName)
{
    values = xml.getElementsByTagName(tagName);
    if (values.length>0)
        try {
            value = (typeof(values[0].firstChild.nodeValue)!='undefined') ? values[0].firstChild.nodeValue : false;
            if (!value)
                value = (typeof(values[0].childNodes[0].text)!='undefined') ? values[0].childNodes[0].text : false;
        } catch(ex) {
            value = false;
        }
    else
        return false;
    return (value!='') ? value.unescapeHtml() : false
}

String.prototype.unescapeHtml = function () {
    var temp = document.createElement("div");
    temp.innerHTML = this;
    var result = temp.childNodes[0].nodeValue;
    temp.removeChild(temp.firstChild)
    return result;
}

function delay(cmd) {
    if (typeof(axTimer)!='undefined')
        clearTimeout(axTimer);
    axTimer = setTimeout(cmd, 200);
}


    
function gup( name )
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}

