function open_search_alf_pane() {
    document.getElementById("search_alf_div").style.display = "block";
    return false;
}
function close_search_alf_pane() {
    document.getElementById("search_alf_div").style.display = "none";
    return false;
}


function ajax_http() {
    this.http = null;
    this.working = false;

    this.method = 'GET';
    this.url = '';
    this.go = '';
    this.func_return = null;

    this.init = function() {
        if (!this.http) {
            this.http = this.get_http();
            this.working = false;
        }
        if (!this.working && this.http) {
            this.http.open(this.method, this.url, true);
            this.http.setRequestHeader("Accept-Language", "ru, en");
            this.http.setRequestHeader("Accept-Charset", "windows-1251");
            if (this.func_return != null)
                this.http.onreadystatechange = this.func_return;
            this.working = true;
            this.http.send(null);
        }
        if (!this.http) {
            alert('Ошибка при создании XMLHTTP объекта!')
        }
    }

    this.get_http = function() {
        var xmlhttp;
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        return xmlhttp;
    }
}

function add_to_cart(id) {
    var obj = document.getElementById("f_prod_" + id);
    var val = obj.checked;

    var aj = new ajax_http();
    aj.url = '/catalog/add_cart/' + id;

    aj.func_return = function() {
        if (aj.http.readyState == 4) {
            if (aj.http.responseText != 'no') {
                if (document.getElementById("cart_info_link")) {
                    if(parseInt(aj.http.responseText)>0)
                        document.getElementById("cart_info_link").innerHTML = 'Корзина (' + aj.http.responseText + ')';
                }
                if (document.getElementById("cart_cont_items_all")) {
                    document.getElementById("cart_cont_items_all").innerHTML = aj.http.responseText;
                }
            }
        }
    }
    aj.init();
}

var sd2 = 0;
function add_to_cart2(id) {
    var obj = document.getElementById("f_prod_" + id);
    //var val = obj.checked;
    if (sd2 == 0)
        sd2 = 1;
    else
        return;
    var aj = new ajax_http();
    aj.url = '/catalog/add_cart/' + id;

    aj.func_return = function() {
        if (aj.http.readyState == 4) {
            if (aj.http.responseText != 'no') {
                if (document.getElementById("cart_info_link")) {
                    if (parseInt(aj.http.responseText) > 0)
                        document.getElementById("cart_info_link").innerHTML = 'Корзина (' + aj.http.responseText + ')';
                }
                if (document.getElementById("cart_cont_items_all")) {
                    document.getElementById("cart_cont_items_all").innerHTML = aj.http.responseText;
                }
            }
        }
    }
    aj.init();
}

function open_galary(url)
{
    var iWidth = 150;
	var iHeight = 50;
	var iLeft = (screen.width  - iWidth) / 2 ;
    var iTop  = (screen.height - iHeight) / 2 ;

    var sOptions = "toolbar=no,status=no,resizable=no,dependent=no";
    sOptions += ",width=" + iWidth;
    sOptions += ",height=" + iHeight;
    sOptions += ",left=" + iLeft;
    sOptions += ",top=" + iTop;
    window.open(url, "GalaryWindow", sOptions);
    return false;
}

function input_popup(id, id_control, url) {
    this.id = id;
    this.id_control = id_control;
    this.obj = $(this.id);
    this.obj_list = $(this.id + "_list");
    this.obj_control = $(this.id_control);
    this.ax = new ajax_http();
    this.name_input = "";
    this.receiv_data = "";
    this.url = url;

    this.arr = Array(0);

    this.init = function() {
        this.obj.setStyle("display", "none");
        //alert(this);
        this.obj_control.addEvent('keyup', function(a, b, c) {
            return function() {
                var temp_str = String(a.value);
                if (temp_str.length >= 2) {
                    c.get_data(temp_str);
                }
                else {
                    b.setStyle("display", "none");
                }
            }

        } (this.obj_control, this.obj, this));

        this.obj_control.addEvent('keydown', function(a) {
            return function() {
                if (event.keyCode == 9)
                    a.setStyle("display", "none");
            }

        } (this.obj));

        $(this.id + "_close").addEvent('click', function() {
            var temp_str = String(this.id);
            temp_str = temp_str.replace("_close", "");
            $(temp_str).setStyle("display", "none");
        });

        this.name_input = this.obj_control.name;
        //this.obj_control.name = this.id + String(Math.random()).substr(2, 5);
    }

    this.submit = function() {
        this.obj_control.name = this.name_input;
    }

    this.get_data = function(str) {
        //this.receiv_data = new String("дизайнер|программист|директор|повар");
        this.ax = new ajax_http();
        this.ax.url = this.url + '?value=' + encodeURIComponent(str);
        //alert(this.ax.url);
        this.ax.func_return = function(a, b) {
            return function() {
                if (a.http.readyState == 4) {
                    //alert(a.http.responseText);
                    b.receiv_data = new String(a.http.responseText);
                    b.write_data();
                }
            }
        } (this.ax, this);
        this.ax.init();
    }

    this.write_data = function() {
        var arr_str = this.receiv_data.split("|");
        this.obj_list.innerHTML = '';
        this.arr = Array(0);
        var i = 0;
        var temp_obj = null;
        for (i = 0; i < arr_str.length; i++) {
            temp_obj = new Element('a', {
                'id': this.id + '_item_' + i,
                'href': ''
            });
            temp_obj.innerHTML = arr_str[i];

            eval('temp_obj.addEvent("click", function() {' +
            '        var temp_str = String(this.id);' +
            '        var temp_arr = temp_str.split("_");' +
            '        $("' + this.obj_control.name + '").value = $(this.id).innerHTML;' +
            '        $(temp_arr[0] + "_" + temp_arr[1]).setStyle("display", "none");' +
            '        return false;' +
            '});');

            //if (i == 0)
            //    temp_obj.className = "input_popup_item_sel";
            this.arr[this.arr.length] = temp_obj;
            this.obj_list.appendChild(temp_obj);
        }
        if (this.receiv_data != "")
            this.obj.setStyle("display", "block")
        else
            this.obj.setStyle("display", "none")
    }
}