var AjaxBox = Class.create();
AjaxBox.prototype = {

    initialize: function(context) {
	var anchors = null;
	if (context == null || context == undefined){
	    anchors = jQuery("a");
	}else{
	    anchors = jQuery("a", context);
	}
	if (anchors.size() > 0){
	    anchors = anchors.get();
	}else{
	    anchors = new Array();
	}
	var anchorsLength = anchors.length;
	for (var i = 0; i < anchorsLength; i++) {
            var anchor = jQuery(anchors[i]);
            var idAttribute = anchor.attr('id');
	    if (idAttribute.toLowerCase().match('a_')){
		switch (anchor.attr('type')){
		    case 'html_box':
			anchor.unbind('click').bind('click', function () {
			    window.myAjaxBox.HtmlBox(this, 'html');
			    return false;
			});
			break;
		    case 'image_box':
			anchor.unbind('click').bind('click', function () {
			    window.myAjaxBox.HtmlBox(this, 'image');
			    return false;
			});
			break;
		    default:
			break;
		}
	    }
        }
    },

    HtmlBox: function(div, type) {
	divid = div.getAttribute('id')
	div_arr = divid.split("_");
        divlink = $('box_' + div_arr[1]);
		href = div.getAttribute('href');
		if (type == 'html') {
            divlink.addClassName('load-cart');
            Element.update('box_' + div_arr[1], '<span class="ico-cart"><img src="/img/mini2.gif" width="10" height="10" alt="Секундочку, добавляем..." /></span> Секундочку, добавляем...');
        }
        if (type == 'image') {
            Element.update(divid, '<img src="/img/mini2.gif" width="10" height="10" alt="Секундочку, добавляем..." title="Секундочку, добавляем..." class="ico-load" />');
        }
        this.PeriodicalAjax(div_arr, type, href, 0);
    },

    PeriodicalAjax: function (div_arr, type, href, gCall) {
        this.ajaxQuery(div_arr, type, href, null);
        new PeriodicalExecuter((function(pe) {
            tempDivLink = $('box_' + div_arr[1]);
            if ((type == 'image' && tempDivLink.innerHTML.indexOf('ico-full-cart.gif') ==  -1) ||
                (type == 'html' && !tempDivLink.hasClassName('full-cart'))) {
                if (++gCall >= 3) {
                    pe.stop();
                    window.location = href;
                }else{
                    this.ajaxQuery(div_arr, type, href, pe);
                };
            } else pe.stop();
        }).bind(this), 2);
    },

    ajaxQuery: function (div_arr, type, href, pe) {
        new Ajax.Request('/goods/ajax/html_box.php?id_goods=' + parseInt(div_arr[1]) + '&type=' + type, {
	    method: 'get',
	    onSuccess: function(transport) {
		res_arr = transport.responseText.split(';');
		id_goods = parseInt(res_arr[0]);
		if (!isNaN(id_goods) && id_goods > 0){
		    if (pe){
			pe.stop();
		    }
		    goods_num = String(res_arr[1]);
		    updater_text = String(res_arr[2]);
		    // если "расширенный список товаров" и установлен класс load-cart, снимает его и вешает другой класс
		    DivBoxLink = $('box_'+id_goods);
		    if (DivBoxLink.hasClassName('load-cart')) {
			DivBoxLink.removeClassName('load-cart').addClassName('full-cart');
			//DivBoxLink.addClassName('full-cart');
		    }
		    Element.update('box_'+id_goods,updater_text);
		    BoxClassLink = $('box-2');
		    if (goods_num) {
			if (BoxClassLink.hasClassName('m-full-cart') == false) BoxClassLink.addClassName('m-full-cart');
			Element.update('box-1', goods_num);
		    }else{
			if (BoxClassLink.hasClassName('m-full-cart')) BoxClassLink.removeClassName('m-full-cart');
			Element.update('box-1','');
		    }
		}
	    }
        });
    }
};

function initAjaxBox(context){
    window.myAjaxBox = new AjaxBox(context);
};

document.observe('dom:loaded', function () {
    initAjaxBox();
});
