//Toggle for IE
anchorToggle();
function anchorToggle() {
    if (location.href.indexOf('?to_anchor=') != -1) {
        var newLocation = location.href.replace('?to_anchor=', '#');
        location.href = newLocation;
    }
}

function changePageLevel1(lnk) {
    //var re = new RegExp('\/page(\\d+)(_(\\d+))?$', 'i');
    var re = new RegExp('\\/#page(\\d+)$', 'i');
    var arr = re.exec(lnk.href);

    if (re.test(lnk.href)) {
        animator.hideOpenedPopUp();

        if (!animator.inProgress) {
            if ((arr[1] >= 1) && (arr[1] <= 6)) {
                $('footer').className = 'bg';
            } else {
                $('footer').className = '';
            }
            animator.changePage(arr[1]);
            location.hash = '#page' + arr[1];
            return true;
        }
    }

    return false;
}

/* ========================================================================== */

/* Menu only */
function changePage(lnk, toIndex, noCache) {
    //var re = new RegExp('\/page(\\d+)(_(\\d+))?$', 'i');
    var re = new RegExp('\\/#page(\\d+)(_(.+))?$', 'i');
    var arr = re.exec(lnk.href);

    if (toIndex && (typeof(toIndex) != 'undefined')) {
        animator.hideOpenedPopUp();
        //Main page
        if (!animator.inProgress) {
            $('footer').className = '';
            $('menu').className = '';
            animator.changePage(0);
            location.hash = '#';
            return true;
        }
    } else if (re.test(lnk.href)) {
        //Inner pages
        if (arr[3] && (typeof(arr[3]) != 'undefined')) {
            //Level 2
            if ((arr[1] == animator.activeIndex) || ((arr[1] == animator.newIndex) && animator.inProgress)) {
                var popup = $('popup_' + arr[1] + '_' + arr[3]);

                if (popup && (typeof(popup) != 'undefined') && (!noCache || (typeof(noCache) == 'undefined'))) {
                    if (popup.style.display == 'none') {
                        animator.hideOpenedPopUp(true);
                        animator.showPopUp(arr[1] + '_' + arr[3]);
                    }
                } else {
                    if (noCache && (typeof(noCache) != 'undefined')) {
                        var curTime = new Date();
                        arr[3] += '.' + curTime.getTime();
                    }
                    animator.hideOpenedPopUp(true);
                    animator.showPopUp(arr[1] + '_' + arr[3], false, noCache);
                }

                return true;
            }
        } else {
            animator.hideOpenedPopUp();
            //Level 1
            if (!animator.inProgress) {
                if ((arr[1] >= 1) && (arr[1] <= 6)) {
                    $('footer').className = 'bg';
                } else {
                    $('footer').className = '';
                }
                animator.changePage(arr[1]);
                location.hash = '#page' + arr[1];
                return true;
            }
        }
    }

    return false;
}

function contentLink(lnk, noCache) {
    var span = $E('span', lnk);
    var pos = lnk.href.indexOf('#page');
    var fromHash = location.hash;

    if ((pos > -1) && (typeof(pos) != 'undefined')) {
        var id = lnk.href.substring(pos + 5);

        var arr = /^(\d+)(_(.+))?$/.exec(id);

        if (noCache && (typeof(noCache) != 'undefined')) {
            var curTime = new Date();
            id += '.' + curTime.getTime();
        }

        if (arr && (typeof(arr) != 'undefined') && (toInt(arr[1]) > 0)) {
            if ($('center_c' + toInt(arr[1])) && $('right_c' + toInt(arr[1]))) {
                var iteration = toInt(arr[1]);

                if (span && (typeof(span) != 'undefined')) {
                    if (arr[3] && (typeof(arr[3]) != 'undefined')) {
                        //Level 2
                        if ((iteration == animator.activeIndex) || ((iteration == animator.newIndex) && animator.inProgress)) {
                            //do nothing at now
                        } else if (!animator.inProgress) {
                            if ((iteration >= 1) && (iteration <= 6)) {
                                $('footer').className = 'bg';
                            } else {
                                $('footer').className = '';
                            }
                            animator.changePage(iteration);
                            location.hash = '#page' + iteration;
                        } else {
                            return false;
                        }

                        animator.fadeOut();
                        animator.hideOpenedPopUp(true);
                        animator.currentPopupId = id;

                        var link = span.innerHTML;
                        while (link.indexOf("&amp;") != -1) {
                            link = link.replace("&amp;", '&');
                        }
                        link += ((link.indexOf('?') != -1) ? '&' : '?') + 'mode=bot_inner&popup_id=' + id + '&iteration=' + iteration;

                        if (noCache && (typeof(noCache) != 'undefined')) {
                            var curTime = new Date();
                            link += '&nocache=' + curTime.getTime();
                        }


                        var ajaxRequest = new Ajax(link, {method: 'get', encoding: 'CP1251', postBody: '', onComplete:
                            function(txt, xml) {
                                if (trim(txt) != 404) {
                                    var div = document.createElement('div');
                                    div.innerHTML = txt;
                                    $('fake').appendChild(div);
                                    redrawMenu('', id);
                                    animator.showPopUpAction(id);
                                } else {
                                    animator.fadeIn();
                                    animator.changePage(404);
                                }
                            }
                        });
                        ajaxRequest.request();

                        return true;
                    } else {
                        //Level 1
                        animator.hideOpenedPopUp();

                        if ((iteration != animator.activeIndex) && (iteration != animator.newIndex)) {
                            if (!animator.inProgress) {
                                if ((iteration >= 1) && (iteration <= 6)) {
                                    $('footer').className = 'bg';
                                } else {
                                    $('footer').className = '';
                                }
                                animator.changePage(iteration);
                                location.hash = '#page' + iteration;
                                return true;
                            }
                        }
                    }
                }
            }
        }
    }

    animator.hideOpenedPopUp();
    animator.changePage(404);
    return false;
}

function parseUrl() {
    var re = new RegExp('\\/#page(\\d+)(_(.+))?$', 'i');
    var directLink = toggleDirectLinks();

    if (re.test(location.href) && (location.href.indexOf('page777') == -1)) {
        var arr = re.exec(location.href);
        arr[1] = toInt(arr[1]);

        //Inner pages
        if (arr[3] && (typeof(arr[3]) != 'undefined')) {
            //Level 2
            if ($('center_c' + arr[1]) && (typeof($('center_c' + arr[1])) != 'undefined') && $('right_c' + arr[1]) && (typeof($('right_c' + arr[1])) != 'undefined')) {
                animator.activeIndex = toInt(arr[1]);
                resizer.activeIndex = toInt(arr[1]);
                $('center_c' + arr[1]).style.top = '92px';
                $('right_c' + arr[1]).style.top = '0';

                if ($('subscribe_left_c') && (typeof($('subscribe_left_c')) != 'undefined')) {
                    if (arr[1] == 5) {
                        $('subscribe_left_c').style.display = 'block';
                    } else {
                        $('subscribe_left_c').style.display = 'none';
                    }
                }

                if ($('secondmenu_' + arr[1]) && (typeof($('secondmenu_' + arr[1])) != 'undefined')) {
                    $('footer').className = 'bg';
                    $('secondmenu_' + arr[1]).style.display = 'block';
                    $('mainmenu_lnk' + arr[1]).className = 'selected';
                    if (arr[1] == 1) {
                        $('left_corner_toggle').style.display = 'block';
                    }
                    if (arr[1] > 0) {
                        if ((arr[1] > 1) && (arr[1] <= 6)) {
                            $('mainmenu_lnk' + (arr[1] - 1)).className = 'toggle_l';
                        }
                        if (arr[1] < 6) {
                            $('mainmenu_lnk' + (arr[1] + 1)).className = 'toggle_r';
                        }
                    }

                    if ($('secondmenu_lnk' + arr[1] + '_' + arr[3]) && (typeof($('secondmenu_lnk' + arr[1] + '_' + arr[3])) != 'undefined')) {
                        $E('a', 'secondmenu_lnk' + arr[1] + '_' + arr[3]).className = 'selected';
                    }
                }

                /* Toggle for left fixed menu */
                if ((arr[1] == 7) || (arr[1] == 8)) {
                    if ($('fixed_menu' + arr[1])) {
                        $('fixed_menu' + arr[1]).className = 'selected';
                    }
                }

                animator.popUpToOpen = arr[1] + '_' + arr[3];
                return;
            }
        } else {
            //Level 1
            if ($('center_c' + arr[1]) && (typeof($('center_c' + arr[1])) != 'undefined') && $('right_c' + arr[1]) && (typeof($('right_c' + arr[1])) != 'undefined')) {
                animator.activeIndex = toInt(arr[1]);
                resizer.activeIndex = toInt(arr[1]);
                $('center_c' + arr[1]).style.top = '92px';
                $('right_c' + arr[1]).style.top = '0';

                if ($('subscribe_left_c') && (typeof($('subscribe_left_c')) != 'undefined')) {
                    if (arr[1] == 5) {
                        $('subscribe_left_c').style.display = 'block';
                    } else {
                        $('subscribe_left_c').style.display = 'none';
                    }
                }


                if ($('secondmenu_' + arr[1]) && (typeof($('secondmenu_' + arr[1])) != 'undefined')) {
                    $('footer').className = 'bg';
                    $('secondmenu_' + arr[1]).style.display = 'block';
                    $('mainmenu_lnk' + arr[1]).className = 'selected';
                    if (arr[1] == 1) {
                        $('left_corner_toggle').style.display = 'block';
                    }
                    if (arr[1] > 0) {
                        if ((arr[1] > 1) && (arr[1] <= 6)) {
                            $('mainmenu_lnk' + (arr[1] - 1)).className = 'toggle_l';
                        }
                        if (arr[1] < 6) {
                            $('mainmenu_lnk' + (arr[1] + 1)).className = 'toggle_r';
                        }
                    }
                }

                /* Toggle for left fixed menu */
                if ((arr[1] == 7) || (arr[1] == 8)) {
                    if ($('fixed_menu' + arr[1])) {
                        $('fixed_menu' + arr[1]).className = 'selected';
                    }
                }

                return;
            }
        }
    } else if (directLink.length > 0) {
        //Toggle for direct links with GET params (like subscribe confirmation)
        if ($('center_c777') && (typeof($('center_c777')) != 'undefined') && $('right_c777') && (typeof($('right_c777')) != 'undefined')) {
            animator.activeIndex = 777;
            resizer.activeIndex = 777;
            $('center_c777').style.top = '92px';
            $('right_c777').style.top = '0';

            //get page by ajax
            directLink = directLink.replace('[get]', '?');

            directLink += '&mode=bot_plain';

            var curTime = new Date();
            directLink += '&nocache=' + curTime.getTime();

            var ajaxRequest = new Ajax(directLink, {method: 'get', encoding: 'UTF-8', postBody: '', onComplete:
                function(txt, xml) {
                    if (trim(txt) != 404) {
                        var div = $('center_c777');
                        deleteChildsIn(div);
                        div.innerHTML = txt;

                        animator.fadeIn();
                        if (animator.centerScroller[777] && (typeof(animator.centerScroller[777]) != 'undefined') && animator.centerScrollbar[777] && (typeof(animator.centerScrollbar[777]) != 'undefined')) {
                            animator.centerScroller[777] = null;
                            animator.centerScrollbar[777] = null;

                            animator.initCenterScroll(777);
                        }

                        animator.initCenterScroll(777);
                    } else {
                        animator.fadeIn();
                        animator.changePage(404);
                    }
                }
            });

            animator.fadeOut();
            ajaxRequest.request();

            location.hash = '#page777';
            return;
        }
    }

    if ($('center_c0') && (typeof($('center_c0')) != 'undefined') && $('right_c0') && (typeof($('right_c0')) != 'undefined')) {
        $('center_c0').style.top = '92px';
        $('right_c0').style.top = '0';
        location.hash = '#';
    }
}

function toggleDirectLinks() {
    var pos = location.hash.indexOf('for_ajax_777_');
    if (pos != -1) {
        var lnk = location.hash.slice(pos + 13, location.hash.length);
        return lnk;
    }

    return '';
}

function redrawMenu(from, to) {
    //alert(from + ' | ' + to);
    var i, j, tmpElem, tmpId;
    var pos = from.indexOf('#page');
    if ((pos > -1) && (typeof(pos) != 'undefined')) {
        from = from.substring(pos + 5);
    }

    var fromArr = explode(from, '.');
    if (fromArr && (typeof(fromArr) != 'undefined') && (fromArr.length > 0)) {
        for (i = 0; i < fromArr.length; i++) {
            tmpId = '';
            for (j = 0; j <= i; j++) {
                tmpId += ((j > 0) ? '.' : '') + fromArr[j];
            }
            tmpElem = $('secondmenu_lnk' + tmpId);
            if (tmpElem && (typeof(tmpElem) != 'undefined')) {
                $E('a', 'secondmenu_lnk' + tmpId).className = '';
                break;
            }

            //Toggle with finishing dot
            tmpId += '.';
            tmpElem = $('secondmenu_lnk' + tmpId);
            if (tmpElem && (typeof(tmpElem) != 'undefined')) {
                $E('a', 'secondmenu_lnk' + tmpId).className = '';
                break;
            }
        }
    }

    var toArr = explode(to, '.');
    if (toArr && (typeof(toArr) != 'undefined') && (toArr.length > 0)) {
        for (i = 0; i < toArr.length; i++) {
            tmpId = '';
            for (j = 0; j <= i; j++) {
                tmpId += ((j > 0) ? '.' : '') + toArr[j];
            }

            tmpElem = $('secondmenu_lnk' + tmpId);
            if (tmpElem && (typeof(tmpElem) != 'undefined')) {
                $E('a', 'secondmenu_lnk' + tmpId).className = 'selected';
                break;
            }

            //Toggle with finishing dot
            tmpId += '.';
            tmpElem = $('secondmenu_lnk' + tmpId);
            if (tmpElem && (typeof(tmpElem) != 'undefined')) {
                $E('a', 'secondmenu_lnk' + tmpId).className = 'selected';
                break;
            }
        }
    }
}

/* ========================================================================== */

function toGET(str) {
    var pos;
    var i = 0;

    pos = str.indexOf('.');
    while (pos != -1) {
        i++;
        if (i == 1) {
            str = str.replace('.', '?');
        } else if ((i % 2) == 0) {
            str = str.replace('.', '=');
        } else {
            str = str.replace('.', '&');
        }

        pos = str.indexOf('.');
    }

    return str;
}

function ajaxAfterRedirectFom(frm, lang) {
        var i;
        var j = 0;
        var lnk = frm.getAttribute('action');

        for (i = 0; i <= frm.elements.length; i++) {
            if (frm.elements[i] && (typeof(frm.elements[i]) != 'undefined') && (frm.elements[i].tagName == 'INPUT')) {
                //2DO: radio, textarea
                //Not a checkbox or checked checkbox
                if ((frm.elements[i].type != 'checkbox') || frm.elements[i].checked) {
                    if (j == 0) {
                        lnk += '[get]';
                    } else {
                        lnk += '&';
                    }
                    lnk += frm.elements[i].name + '=' + frm.elements[i].value;
                    j++;
                }
            }
        }

        lnk += '&mode=bot_plain';

        lnk = lang + '/#for_ajax_777_' + lnk;

        location.href = lnk;

        return false;
}

function formByAjax(frm) {
    if (!animator.inProgress) {
        var i;
        var j = 0;
        var lnk = frm.getAttribute('action');

        for (i = 0; i <= frm.elements.length; i++) {
            if (frm.elements[i] && (typeof(frm.elements[i]) != 'undefined') && (frm.elements[i].tagName == 'INPUT')) {
                //2DO: radio, textarea
                //Not a checkbox or checked checkbox
                if ((frm.elements[i].type != 'checkbox') || frm.elements[i].checked) {
                    if (j == 0) {
                        lnk += '?';
                    } else {
                        lnk += '&';
                    }
                    lnk += frm.elements[i].name + '=' + frm.elements[i].value;
                    j++;
                }
            }
        }

        lnk += '&mode=bot_plain';

        var ajaxRequest = new Ajax(lnk, {method: 'get', encoding: 'UTF-8', postBody: '', onComplete:
            function(txt, xml) {
                if (trim(txt) != 404) {
                    var div = $('center_c777');
                    if (div && (typeof(div) != 'undefined')) {
                        deleteChildsIn(div);
                        div.innerHTML = txt;

                        animator.hideOpenedPopUp();

                        $('footer').className = '';
                        if (animator.activeIndex != 777) {
                            //animator.initCenterScroll(777);
                            animator.changePage(777);
                            location.hash = '#page777';
                        } else {
                            if (resizer && (typeof(resizer) != 'undefined')) {
                                resizer.reset();
                            }

                            setTimeout('animator.fadeIn();', 1000);
                            if (animator.centerScroller[777] && (typeof(animator.centerScroller[777]) != 'undefined') && animator.centerScrollbar[777] && (typeof(animator.centerScrollbar[777]) != 'undefined')) {
                                animator.centerScroller[777] = null;
                                animator.centerScrollbar[777] = null;

                                animator.initCenterScroll(777);
                            }
                        }
                    } else {
                        animator.fadeIn();
                        animator.changePage(404);
                    }
                } else {
                    animator.fadeIn();
                    animator.changePage(404);
                }
            }
        });

        if (animator.activeIndex == 777) {
            animator.fadeOut();
        }

        ajaxRequest.request();
    }

    return false;
}

function deleteChildsIn(div) {
    var i;

    if (div && (typeof(div) != 'undefined')) {
        for (i = 0; i <= div.childNodes.length; i++) {
            while (div.childNodes[i] && (typeof(div.childNodes[i]) != 'undefined')) {
                div.removeChild(div.childNodes[i]);
            }
        }
    }
}

/* ========================================================================== */

function changeLang(path) {
    if ((animator.activeIndex > 0) && (animator.activeIndex <= 8)) {
        path += '#page' + animator.activeIndex;
        location.href = path;
        return false;
    }

    return true;
}
