
var ShowDiv = Class.create();

ShowDiv.prototype = {
    initialize: function() {
        this.loadingProc = 'none';        
        $$('a.show_updater').each((function(el) {
            var id = parseInt(el.getAttribute('rev'), 10);
	    if (!isNaN(id) && id > 0) {
		var trailerCount = parseInt(el.getAttribute('rel'), 10);
		if (!isNaN(trailerCount)){
		    this.observeFunction(el, id, trailerCount);
		}else{
		    this.observeFunction(el, id, 0);
		}
            }
        }).bind(this));
	},
    
    observeFunction : function (el, id, trailerCount) {
        el.observe('click', (function(event) {
            event.stop();
            this.startAjax(id, trailerCount);
        }).bind(this));
    },
    
    startAjax: function(id, trailerCount) {
        if (this.loadingProc == 'none'){            
            this.changeLoadingStatus('loading');		    
	    var notice = $("frame_more");
	    if (notice.innerHTML == ""){
		    jQuery("#frame_loading").css("display", "block");
		    new Ajax.Request('/goods/ajax/updater.php?id=' + id + "&show_trailers=" + trailerCount, {
			method: 'get',
			onSuccess: function(transport){
			    notice.update(transport.responseText);				    
			    this.toggleBlind(notice, function(){
				    jQuery("#frame_loading").css("display", "none");
				});
			}.bind(this)
		});
	    }else{
		this.toggleBlind(notice);
	    }
        }
    },
    
    changeLoadingStatus: function(status) {
        this.loadingProc = status;
    },
    
    toggleBlind: function(link, callback) {
        if (link.style.display == "none") {
            new Effect.BlindDown(link, {
                duration: 0.5,
                afterFinish: (function() {
                    this.changeLoadingStatus('none');
		    if (jQuery.isFunction(callback)){
			callback();
		    }
                }).bind(this)
            });
        }
        else {
            new Effect.BlindUp(link, {
                duration: 0.5,
                afterFinish: (function() {
                    this.changeLoadingStatus('none');
                }).bind(this)
            });
        }
    }
};

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

