﻿//if (document.attachEvent && !document.addEventListener) {
//    window.attachEvent('onresize', resize_function);
//    window.attachEvent('onload', onload_function);
//}

//if (document.addEventListener) {
//    window.addEventListener('resize', resize_function, true);
//    window.addEventListener('load', onload_function, true);
//}

function onload_function() {
    top_menu_load();
    //tooltip();
    img_fon_fix();
}

function resize_function() {
    img_fon_fix();
    //top_menu_resize();
}

//// промежуточные глобальные массивы для передачи результатов, полученных в top_menu_load(), top_menu_resize(), tooltip()
//var listArray_1_2 = new Array();
//var listArray_2_4 = new Array();
var listArray_2_5 = new Array();


// получение элементов в меню при загрузке страницы
function top_menu_load() {
//    var listArray_1_1 = document.getElementById("top_menu").getElementsByTagName("LI");// массив всех li в "top_menu"
//    listArray_1_2 = listArray_1_1;
    
    if (document.getElementById("sub_header")) {
        var listArray_2_1 = document.getElementById("sub_header").getElementsByTagName("LI"); // массив всех li в "sub_header"
//        var listArray_2_2 = new Array();                                                      // массив всех li уровня категории
        var listArray_2_3 = new Array();                                                      // массив всех li внутри категории
        var j = 0;
        var k = 0;
        
        for (i = 0; i < listArray_2_1.length; i++) {
           if (listArray_2_1[i].parentNode.parentNode.tagName != "DIV") { 
//               listArray_2_2[j++] = listArray_2_1[i];          
//           } else {
               listArray_2_3[k++] = listArray_2_1[i];
           }
        }
        
        // эмуляция :first-child в ie6
        if (navigator.userAgent.indexOf('MSIE 6') != -1) {
            for (i = 0; i < listArray_2_1.length; i++) {
               if ((listArray_2_1[i].parentNode.parentNode.tagName == "DIV") && (listArray_2_1[i+1]) && (listArray_2_1[i+1].className == "active")){ 
                   listArray_2_1[i+1].style.marginTop = "-3px";         
               }  
            }   
        }   

//        listArray_2_4 = listArray_2_2;
        listArray_2_5 = listArray_2_3;
    }
    
    if (navigator.userAgent.indexOf('MSIE 6') != -1) {
        if ((document.body.offsetHeight % 2) != 0) {
            document.getElementById("footer_block").style.bottom = "-1px";   
        } else {
            document.getElementById("footer_block").style.bottom = "0px"; 
        }
    }

    
//    top_menu_resize();  
}

//// расчет смещений элементов в меню при изменении ширины окна браузера
//function top_menu_resize() {

//    for (i = 0; i < listArray_1_2.length; i++) {
//       if (i == (listArray_1_2.length - 1)) {
//           listArray_1_2[i].style.marginRight = "0px";
//       } else {
//           listArray_1_2[i].style.marginRight = (document.body.offsetWidth - 888.89) / 5.56 + "px";
//       }
//    }

//    if (document.getElementById("sub_header")) {
//        for (i = 0; i < listArray_2_4.length; i++) {
//           if (i == (listArray_2_4.length - 1)) {
//               listArray_2_4[i].style.marginRight = "0px";
//           } else {
//               listArray_2_4[i].style.marginRight = Math.floor((document.body.offsetWidth - 864.96) / 5.88) + "px";
//           }
//        }
//    }
//}

// всплывающие подсказки
function tooltip() {
    for (i = 0; i < listArray_2_5.length; i++) {

        if (listArray_2_5[i].firstChild.tagName == "A"){
            
            listArray_2_5[i].firstChild.onmouseover = function() {
                if (this.firstChild.style) {
                    this.parentNode.style.zIndex = "100";
                    this.firstChild.style.cursor = "pointer";
                    this.firstChild.style.display = "block";
                    this.firstChild.setAttribute("id","current_tooltip");
                    
                    var x = document.getElementById("current_tooltip").getElementsByTagName("IMG");
                    
                    x[4].style.width = this.firstChild.offsetWidth - 10 + "px";
                    x[5].style.height = this.firstChild.offsetHeight - 10 + "px";
                    
                    if (navigator.userAgent.indexOf('MSIE 6') != -1) {
                        if ((this.firstChild.offsetHeight % 2) != 0) {
                            x[1].style.bottom = "-9px";
                            x[3].style.bottom = "-9px";
                            x[4].style.bottom = "-9px";
                        }
                        if ((this.firstChild.offsetWidth % 2) != 0) {
                            x[2].style.right = "-9px";
                            x[3].style.right = "-9px";
                            x[5].style.right = "-9px";
                        }
                    }
                    
                    if (this.firstChild.offsetHeight > this.parentNode.offsetHeight) {
                        this.firstChild.style.top = -((this.firstChild.offsetHeight - this.parentNode.offsetHeight) / 2) - 2 + "px";
                    } else {
                       this.firstChild.style.top = "0px"; 
                    }    
                }
            }
                
            listArray_2_5[i].firstChild.onmouseout = function() {
                if (this.firstChild.style) {
                    this.parentNode.style.zIndex = "1";
                    this.firstChild.removeAttribute("id","current_tooltip");
                    this.firstChild.style.display = "none";
                }
            }
        }
    }
}

// активное фото в каталоге
function active_foto(foto) {
    var listFoto = document.getElementById("catalog_pager").getElementsByTagName("DIV");
    
    for (i = 0; i < listFoto.length; i++) {
        listFoto[i].className = "";
    }
    
    foto.className = "active";
}

//форма обратной связи на странице контактов
function form_contacts() {
    var element = document.getElementById("form_contacts");
    
    element.style.display = (element.style.display == "block") ? "none" : "block";

    element.style.left = ((document.body.offsetWidth - element.offsetWidth) / 2) + "px";

    if (document.documentElement.clientHeight > element.offsetHeight) {
        element.style.top = (((document.documentElement.clientHeight - element.offsetHeight) / 2) + GetScrollY()) + "px";
    } else {
        element.style.top = GetScrollY() + "px";
    }
}

function GetScrollY() {
    if (document.documentElement && document.documentElement.scrollTop) {
        return document.documentElement.scrollTop;
    }
    else if (document.body) {
        return document.body.scrollTop;
    }
}

function img_fon_fix() {
    if ((navigator.userAgent.indexOf('MSIE 6') != -1) || (navigator.userAgent.indexOf('MSIE 7') != -1)) {
        var body = document.getElementById("site"); 
        body.style.backgroundPosition = ((body.offsetWidth % 2) == 0) ? "50% 0px" : (body.offsetWidth - 1000) / 2 + "px 0px";
    }
}
