var PlusDinamicConstants = {
    STATUS_PLUS   : "plus",
    STATUS_MINUS  : "minus",
    STATUS_LOADING : "loading",
    STATUS_HIDDEN : "hidden"
}

/**
  loadingProperties{
 	dataLoaded : true/false,
	loadUrl: "http://..."
    requestParameters: "&param1=value1&param2=value2"
	onDataLoaded : function( httpRequestObject, plusObject){}
  }
*/
function PlusDinamic( plusId, plusForId, loadingProperties){
    this.plusId = plusId;
    this.plusForId = plusForId;

    this.obj = $( this.plusId);
    if( this.obj == null) return;



    this.obj.onclick = this.onClick.bindAsEventListener(this);


    this.loadingProperties = loadingProperties;
    this.init();
}

PlusDinamic.prototype = {
    init : function(){
        this.setPlus();
    },

    setMinus : function (){
        this.obj.className = PlusDinamicConstants.STATUS_MINUS;
        if( this.plusForId) Element.show( this.plusForId);
    },

    setPlus : function (){
        this.obj.className = PlusDinamicConstants.STATUS_PLUS;
        if( this.plusForId) Element.hide( this.plusForId);
    },

    setLoading : function(){
//        alert( "set Loading");
        this.obj.className = PlusDinamicConstants.STATUS_LOADING;
        if( this.plusForId) Element.hide( this.plusForId);//daca nu este deja hidden
    },

    setHidden : function(){
        this.obj.style.visibility="hidden";
        if( this.plusForId) Element.hide( this.plusForId);
    },

    togglePlus : function (){
        if( this.getStatus() == PlusDinamicConstants.STATUS_PLUS){
            this.setMinus();
        }else{
            this.setPlus();
        }
    },

    getStatus : function (){
        return this.obj.className;
    },

    onClick : function( onClickEvent){

        if( this.getStatus() == PlusDinamicConstants.STATUS_LOADING){
            return;
        }

        if( !this.loadingProperties.dataLoaded){
            this.setLoading();
            this.loadData();
        } else {
            this.togglePlus();
        }
    },

    loadData : function(){
        new Ajax.Request(
                this.loadingProperties.loadUrl,
                {
                    method : "post",
                    parameters: this.loadingProperties.requestParameters,
                    onComplete : this.onRequestComplete.bind( this)
                });
    },

    onRequestComplete : function( originalRequest){
//        alert( "onRequestComplete");
//        alert( originalRequest.responseText);
        this.loadingProperties.dataLoaded = true;

        var statusToSet = this.loadingProperties.onDataLoaded( originalRequest, this);

        switch (statusToSet){
            case PlusDinamicConstants.STATUS_PLUS: this.setPlus(); break;
            case PlusDinamicConstants.STATUS_MINUS: this.setMinus(); break;

            case PlusDinamicConstants.STATUS_HIDDEN:
            default:
                this.setHidden();
            break;
        }
    }
}




