// == common scripts

function hlitesrch()
{
    document.getElementById('s').focus();
    return true;
}

function zkontroluj_email(adresa)
{
    if(!adresa) return false;
    re = /^[^.]+(\.[^.]+)*@([^.]+[.])+[a-z]{2,6}$/;
    return adresa.search(re) == 0;
}


// šířka viewportu
function getWportWidth()
{
    if(typeof window.innerWidth!='undefined') {
        return (window.innerWidth - 20);
    }
    else {
        return (document.documentElement.clientWidth - 4);
    }
}

function correct_position()
{
    var dropdowns = jQuery('ul.dropdown');
    for(var i=0; i<dropdowns.length; i++) {
        if(dropdowns[i].offsetParent) {
            var rborderPos = dropdowns[i].offsetWidth + dropdowns[i].offsetParent.offsetLeft + 25;
            var wportWidth = getWportWidth();
            if((rborderPos+2)>wportWidth) {
                var newLpos = wportWidth - rborderPos - 3;
                dropdowns[i].style.left = newLpos + 'px';
            }
            else if(dropdowns[i].offsetLeft!='0') {
                dropdowns[i].style.left = '0px';
            }
        }
    }
    return true;
}

// oblak štítků
var tagCloudItemColors = new Array();
function make_white()
{
    tagCloudItemColors[this.id] = this.style.color
    this.style.color = "#fff";
    this.style.background = "#09f";
    this.style.borderBottom = "1px solid #06c";
    return true;
}
function unmake_white()
{
    this.style.color = tagCloudItemColors[this.id];
    this.style.background = "transparent";
    this.style.border = "none";
    return true;
}
function clear_cloud()
{
    var tagClItems = jQuery('a.st-tags');
    for(var i=0; i<tagClItems.length; i++) {
        tagClItems[i].style.background = "transparent";
        tagClItems[i].style.border = "none";
    }
    return true;
}

// drop-down menu
var showdelay = new Array();
var hidedelay = new Array();
function show_dd()
{
    var element;
    if(typeof this.nextSibling.tagName!='undefined' && this.nextSibling.tagName=='UL') {
        element = this.nextSibling;
    }
    else {
        element = this.nextSibling.nextSibling;
    }
    if(typeof(hidedelay[element.id])!='undefined') clearTimeout(hidedelay[element.id]);
    showdelay[element.id] = setTimeout("jQuery('#" + element.id + "').show();correct_position();", 110);
    correct_position();
    return true;
}
function hide_dd()
{
    var element;
    if(this.nextSibling && this.nextSibling.tagName=='UL') {
        element = this.nextSibling;
    }
    else if(this.nextSibling && this.nextSibling.nextSibling.tagName=='UL') {
        element = this.nextSibling.nextSibling;
    }
    else {
        element = this;
    }
    hidedelay[element.id] = setTimeout("jQuery('#" + element.id + "').hide()", 220);
    if(typeof(showdelay[element.id])!='undefined') clearTimeout(showdelay[element.id]);
    correct_position();
    return true;
}
function clear_dd_delay()
{
    if(typeof(hidedelay[this.id])!='undefined') clearTimeout(hidedelay[this.id]);
    correct_position();
    return true;
}

// přepínání bloků článků
function prepare_blocks()
{
    jQuery('#tableofcontents li').show();
    jQuery('#tableofcontents2 li').show();
    jQuery('.jquery-wrapper').hide();
    var anchor = self.document.location.hash;
    if(!anchor || !anchor.match('#goto-')) {
        jQuery('#jquery-main-article').show();
        anchor = '#goto-main-article';

    }
    else {
        var shortanchor = anchor.replace('#goto-', '');
        block_id = 'jquery-' + shortanchor;
        jQuery('#'+block_id).show();
        window.scroll(0,120);
    }
    var h2s = jQuery('h2');
    for(var i=0; i<h2s.length; i++) {
        h2s[i].id = 'changed' + h2s[i].id;
    }
    var toc_hrefs = jQuery('#tableofcontents li');
    for(var i=0; i<toc_hrefs.length; i++) {
        if(toc_hrefs[i].firstChild.hash==anchor) {
            toc_hrefs[i].className = 'current';
        }
    }
    var toc_hrefs = jQuery('#tableofcontents2 li');
    for(var i=0; i<toc_hrefs.length; i++) {
        if(toc_hrefs[i].firstChild.hash==anchor) {
            toc_hrefs[i].className = 'current';
        }
    }
    return true;
}
function switch_blocks()
{
    var block_id = this.hash;
    block_id = block_id.replace('#goto-', 'jquery-');
    jQuery('.jquery-wrapper').hide();
    jQuery('#'+block_id).show();
    reset_toc_currents();
    set_toc_currents(this);
    document.location.replace(this.hash);
    return false;
}
function switch_blocks2()
{
    var block_id = this.hash;
    block_id = block_id.replace('#goto-', 'jquery-');
    jQuery('.jquery-wrapper').hide();
    jQuery('#'+block_id).show();
    window.scroll(0,120);
    reset_toc_currents();
    set_toc_currents(this);
    document.location.replace(this.hash);
    return false;
}
function reset_toc_currents()
{
    var toc_hrefs = jQuery('#tableofcontents li');
    for(var i=0; i<toc_hrefs.length; i++) {
        toc_hrefs[i].className = '';
    }
    var toc_hrefs = jQuery('#tableofcontents2 li');
    for(var i=0; i<toc_hrefs.length; i++) {
        toc_hrefs[i].className = '';
    }
    return true;
}
function set_toc_currents(obj)
{
    var toc_hrefs = jQuery('#tableofcontents li');
    for(var i=0; i<toc_hrefs.length; i++) {
        if(toc_hrefs[i].firstChild.hash==obj.hash) {
            toc_hrefs[i].className = 'current';
        }
    }
    var toc_hrefs = jQuery('#tableofcontents2 li');
    for(var i=0; i<toc_hrefs.length; i++) {
        if(toc_hrefs[i].firstChild.hash==obj.hash) {
            toc_hrefs[i].className = 'current';
        }
    }
    return true;
}

// == pozadí inputů

function set_inputbg()
{
    if(!this.className.match('iehlitebg')) {
        this.className = this.className + ' iehlitebg';
    }
    else {
        this.className = this.className.replace(' iehlitebg','');
    }
    if(this.id=='ajaxNewsletterInput') {
        if(this.value=='Zadejte Váš e-mail') {
            this.style.color = '#111';
            this.value = '';
        }
        else if(this.value=='') {
            this.style.color = '#999';
            this.value = 'Zadejte Váš e-mail';
        }
    }
}

// HighSlide
function call_hslide()
{
    return hs.expand(this);
}

// reference v komentářích
function send_comm_ref()
{
    var txtarea_value = document.getElementById('comment').value;
    txtarea_value = txtarea_value.replace(/^\s*|\s*$/g,'');
    if(txtarea_value!='') txtarea_value = txtarea_value + ' ';
    var referencestr = this.id;
    referencestr = '[' + referencestr.replace('ref_', '') + '] ';
    document.getElementById('comment').value = txtarea_value + referencestr;
    return true;
}

// kontrola formuláře
function check_form()
{
    var formular = document.getElementById('commentform');
    var author = formular.author.value;
    var email = formular.email.value;
    var comment = formular.comment.value;
    jQuery('#author').removeClass('error');
    jQuery('#email').removeClass('error');
    jQuery('#comment').removeClass('error');
    if(!author) {
        jQuery('#author').addClass('error');
        formular.author.focus();
        alert('Chyba: Zadejte, prosím, Vaše jméno.');
        return false;
    }
    else if(!email || !zkontroluj_email(email)) {
        jQuery('#email').addClass('error');
        formular.email.focus();
        alert('Chyba: Zadejte platnou e-mailovou adresu.');
        return false;
    }
    else if(!comment) {
        jQuery('#comment').addClass('error');
        formular.comment.focus();
        alert('Chyba: Napište komentář.');
        return false;
    }
    else {
        jQuery('#comment').removeClass('error');
        return true;
    }
}

// nastavení hodnot cForm

function set_cform_values()
{
    if(document.getElementById('formBuilderFieldletosni_rok')) {
        datum = new Date();
        document.getElementById('fieldformBuilderFieldletosni_rok').value = datum.getFullYear();
        jQuery('#formBuilderFieldletosni_rok').addClass('schovane-pole');
    }
    if(document.getElementById('formBuilderFieldnevyplnene_pole')) {
        jQuery('#formBuilderFieldnevyplnene_pole').addClass('schovane-pole');
    }
}

// == nastavení handlerů

function set_handlers()
{
    // drop-down menu
    var ddTriggers = jQuery('li.trigger');
    for(var i=0; i<ddTriggers.length; i++) {
        var trgChildren = ddTriggers[i].childNodes;
        for(var n=0; n<trgChildren.length; n++) {
            if(trgChildren[n].className=='dropdown') {
                ddTriggers[i].firstChild.onmouseover = show_dd;
                ddTriggers[i].firstChild.onmouseout = hide_dd;
            }
        }
    }
    var ddowns = jQuery('ul.dropdown');
    for(var i=0; i<ddowns.length; i++) {
        ddowns[i].onmouseover = clear_dd_delay;
        ddowns[i].onmouseout = hide_dd;
    }
    
    // oblak štítků
    var tagCloudItems = jQuery('a.st-tags');
    for(var i=0; i<tagCloudItems.length; i++) {
        tagCloudItems[i].onmouseover = make_white;
        tagCloudItems[i].onmouseout = unmake_white;
    }
    
    // přepínání bloků
    var all_hrefs = jQuery('a');
    for(var i=0; i<all_hrefs.length; i++) {
        if(all_hrefs[i].hash && all_hrefs[i].hash.match('#goto-')
                && all_hrefs[i].href.replace(location.hash,'').match(location.href.replace(location.hash,''))
                && !location.href.match('http://www.kultura.slansko.cz/odbor-kultury/publikace')
                && location.href!='http://www.kultura.slansko.cz/'
                && location.href!='http://kultura.slansko.cz/'  ) {
            all_hrefs[i].onclick = switch_blocks2;
        }
    }
    var toc_hrefs = jQuery('#tableofcontents a');
    for(var i=0; i<toc_hrefs.length; i++) {
        toc_hrefs[i].onclick = switch_blocks;
    }
    var toc_hrefs = jQuery('#tableofcontents2 a');
    for(var i=0; i<toc_hrefs.length; i++) {
        toc_hrefs[i].onclick = switch_blocks2;
    }
    
    // pozadí inputů (input[type='text']:focus, input[type='password']:focus, textarea:focus, #iehlitebg)
    if(document.compatMode && document.all) {
        var inputstohlite = jQuery('input');
        for(var i=0; i<inputstohlite.length; i++) {
            if(inputstohlite[i].type=='text' || inputstohlite[i].type=='password') {
                inputstohlite[i].onfocus = set_inputbg;
                inputstohlite[i].onblur = set_inputbg;
            }
        }
        var areastohlite = jQuery('textarea');
        for(var i=0; i<areastohlite.length; i++) {
            areastohlite[i].onfocus = set_inputbg;
            areastohlite[i].onblur = set_inputbg;
        }
    }
    
    // HighSlide
    var gall_thumbnails = jQuery('.gallery-icon a');
    for(var i=0; i<gall_thumbnails.length; i++) {
        gall_thumbnails[i].className = 'highslide';
        gall_thumbnails[i].onclick = call_hslide;
    }
    var nobg_thumbnails = jQuery('#entry a.nobg');
    for(var i=0; i<nobg_thumbnails.length; i++) {
        nobg_thumbnails[i].className = 'nobg highslide';
        nobg_thumbnails[i].onclick = call_hslide;
    }
    var nobg_thumbnails = jQuery('#entry a.nobg');
    for(var i=0; i<nobg_thumbnails.length; i++) {
        nobg_thumbnails[i].className = 'nobg highslide';
        nobg_thumbnails[i].onclick = call_hslide;
    }

    // reference v komentářích
    var comm_refs = jQuery('.do_rererence');
    for(var i=0; i<comm_refs.length; i++) {
        comm_refs[i].onclick = send_comm_ref;
    }
    
    // kontrola hodnot komentářů
    if(document.getElementById('commentform'))
        document.getElementById('commentform').onsubmit = check_form;

    // --
    return true;
}

// == inicializace funkcí

// onResize
function init_onresize()
{
    //correct_position();
}
window.onresize = init_onresize;

// onLoad
function init_all()
{
    //hlitesrch();
    //correct_position();
    set_handlers();
    clear_cloud();
    prepare_blocks();
    set_cform_values();
}
window.onload = init_all;

