jQ(document).ready(function() {

    // SelectBox create
    aagForms.createSelectBox();

    // Funkcionality formularu
    redirectForms();

    // Funkcionaliza seznamu zastoupeni ve paticce stranky
    listOfOffices();

    // PNG Fix pro IE6
    if (isIE6()) {
        DD_belatedPNG.fix('.pngfix');
    }

    // Jsou nastaveny uzivatelske COOKIE
    if (isCookie('userLocalOffice') && isCookie('userLocalLanguage')) {
        var localOffice = getCookie('userLocalOffice');
        var localLng = getCookie('userLocalLanguage');       
        //window.location = zast_href[localOffice] + '?userLanguage=' + localLng;       
    }
});

/**
 * Funkcionality formularu
 */
function redirectForms() {

    // Presmerovani na zastoupeni
    jQ('#submitRedirect').click(function() {
        languageChange();
    });
    
    // Zmena zastoupeni v combu
    jQ('#countryS').change(function() {
        countryChange();
    });
    
    // Odeslani pole ostatni aktivity
    jQ('#otherActivities').submit(function() {
        followOAlink();
        return false;
    });
    
    // Odeslani formulare vyberu zastoupeni
    jQ('#chooseCountry').submit(function() {
        return false;
    });
    
}

/**
 * Vraci, jestli je COOKIE nastavena
 * 
 * @return boolean
 */
function isCookie(nameCookie) {
    var result = false;
    var profilCookie = getCookie(nameCookie);
    if (profilCookie != null && profilCookie != "") {
        result = true;
    } else {
        result = false;
    }
    
    return result; 
}

function languageChange() {
    var objL, objC;
    objL = document.getElementById('languageS');
    objC = document.getElementById('countryS');
    if (objL && objC && objL.options[objL.selectedIndex].value) {
        setCookieLocalOffice('worldwide_' + objC.options[objC.selectedIndex].value, objL.options[objL.selectedIndex].value);
        //showInterestitial(objL.options[objL.selectedIndex].value);
        //setTimeout('redirect(\''+zast_href[objC.options[objC.selectedIndex].value] + '?userLanguage=' + objL.options[objL.selectedIndex].value+'\');', 2000);
        window.location = zast_href[objC.options[objC.selectedIndex].value] + '?userLanguage=' + objL.options[objL.selectedIndex].value;
    }
}

function redirect(url) {
    window.location = url;
}

function countryChange() {
    var objL, objC, objO;
    var i, kod, jazyky;
    objL = document.getElementById('languageS');
    objC = document.getElementById('countryS');
    if (objL && objC) {
        kod = zastoupeni[objC.options[objC.selectedIndex].value];
        jazyky = zast_lng[kod];
        if (objL.options.length > 0) {
            objL.selectedIndex = 0;
            for (i=objL.options.length-1; i>=0; i--) objL.options[i] = null;
        }    
        if (jazyky.length) {
            for (i=0; i<jazyky.length; i++) {
                objO = document.createElement('option');
                objL.appendChild(objO);
                objO.text = lng_name[jazyky[i]];
                objO.setAttribute('value', jazyky[i]);
            }
            
            var elSelect = jQ('#languageS');
            aagForms.refreshSelect(elSelect);
        }
    }
}

function setCookieLocalOffice(uSection, uLanguage) {
    var expires = new Date();
    expires = expires.valueOf() + 31*24*60*60*1000;
    expires = new Date(expires);
    setCookie('userLocalOffice', uSection, expires, '/');
    setCookie('userLocalLanguage', uLanguage, expires, '/');
}

function followOAlink() {
    var objO;
    objC = document.getElementById('otherS');
    if (objC && objC.options[objC.selectedIndex].value) {
      redirect(objC.options[objC.selectedIndex].value);        
    }
}

function showInterestitial(lng) {
    var pic;
    pic = document.getElementById('interestitial_div');
    if (pic) pic.style.display = 'block';
    pic = document.getElementById('interestitial_'+lng);
    if (pic) pic.style.display = 'block';
}

function isIE6() {
    if(String(navigator.appVersion).match("MSIE 6")) {
        return true;
    } else {
        return false;
    }
}

/**
 * Slouzi k rozbalovani seznamu zemi v patice dokumentu
 */
function listOfOffices() {

    var elFields = jQ('#fieldListOffices');

    // Funkcionalita roztahovani seznamu zastoupeni 
    
    var fieldsHeight = jQ(elFields).height() + 'px';
    
    jQ('#openListOffices').click(function() {
        if (jQ(this).hasClass('open') == true) {
            jQ(elFields).animate( { height: '0px'}, 600, function(){});        
            jQ(this).removeClass('open');
        } else {
            jQ(elFields).animate( { height: '230px'}, 600, function(){});
            jQ(this).addClass('open');
        }
    });
    
    // Funkcionalita nastavovani cookie pro jednotliva zast.
    jQ(elFields).find('a').click(function() {

        var uSection = jQ(this).attr('rel');

        // Vyhledava vychozi jazyk
        var langs = zast_lng[zastoupeni[uSection]];
        var uLanguage = '';
        if (langs.length) {
            uLanguage = langs[0];
        }
         
        uSection = 'worldwide_' + uSection; 
       
        setCookieLocalOffice(uSection, uLanguage);
    });

}