// продукт



var Product = Class.create();
Product.prototype = {
    el_block: null,
    el_product: null,
    el_image: null,
    el_title: null,
    el_product_id: null,
    el_text: null,
    el_flag: null,

	el_edit: null, el_del: null,

    data: null,
    loading: false,

    initialize: function(params){

        this.el_block = $(params.block);
        this.el_product = $(params.product);
        this.el_image = $(params.image);
        this.el_title = $(params.title);
        this.el_product_id = $(params.product_id);
        this.el_text = $(params.text);
        this.el_flag = $(params.flag);

        this.el_edit = this.el_product.getElementsBySelector('a[href="#product_edit"]')[0];
        this.el_del = this.el_product.getElementsBySelector('a[href="#product_delete"]')[0];
		if( this.el_del ){
			this.el_del.onclick = Delegate.create(this, this.delete_product);
		}

    },





    load_product: function(id){
        this.send({action: 'get_item', id: id});

        return false;// для ссылки
    },


	delete_product: function(){

		if( !confirm('Удалить "'+this.data['title']+'" навсегда и безвозвратно?') ) return false;

		this.send({action: 'delete_item', id: this.data['id']});
		return false;// для ссылки
	},

    send: function(params){
        if( this.loading ){
            alert('Пожалуйста, подождите. Идёт обработка Вашего запроса.')
            return;
        }

        this.loading = true;
        cur.attach('/media/img/loader.gif', 'загрузка...');

		var url;
		if(params.action == 'set_order') url = '/ajax/order';
		else url = '/ajax/product';

        // делаем запрос
        var obj = this;
        new Ajax.Request(url, {
            method: 'get',
            parameters: params,
            onSuccess: function(transport) {
                obj.on_load(transport.responseText);
            },
            onFailure: function(transport) { obj.on_load(false); },
            onException: function(transport) { obj.on_load(false); }
        });
    },





    on_load: function(str){
        this.loading = false;
        cur.reset();

        if( str == false || !str.isJSON() ){
            alert('ошибка запроса: ' + str);
            return;
        }


        var obj = str.evalJSON();
        if( obj.callback == 'set_data' ){
            this.data = obj.data;
            this.set_data();
            this.el_block.show();
        }else
		if( obj.callback == 'set_order'){
            this.set_order( obj.data );
        }else
		if( obj.callback == 'set_delete'){
			this.set_delete(obj);
		}
    },




    set_data: function(){
        this.el_title.update( this.data.title );
        this.el_product_id.update( this.data.product_id );
        this.el_text.update( this.data.text );
        this.el_image.src =  this.data.image;

        this.el_flag.checked = (this.data.checked == 1)? true: false;
        this.el_flag.value = this.data.id;
        this.el_flag.onclick = Delegate.create(this, this.on_change, this.el_flag);


	   	if( this.el_edit) this.el_edit.href = '/admin/product?action=edit&id=' + this.data.id;
	   	//if( this.el_del) this.el_del.href = '/admin/product?action=delete&id=' + this.data.id;
		if( this.data.flag_order == '1'){ this.el_flag.checked = true; }

    },



    on_change: function(event, checkbox){
        if( !checkbox ) checkbox = event; // IE

        var flag = (checkbox.checked)? 1: 0;
        this.send({ action: 'set_order', id: this.data.id, flag: flag});
    },




    set_order: function(obj){

        if(obj.flag == 1){
            this.data.flag = 1;
            this.el_flag.checked = true;
        }else{
            this.data.flag = 0;
            this.el_flag.checked = false;
        }


        //if( this.data.flag ) alert('Добавленно в список');
        //else alert('Убавленно из спискa');
    },

	set_delete: function(obj){
		if( obj.result == '1'){
			this.el_block.hide();
		}else{
			alert(obj.result);
		}

	}
}// end class
