
/**
 * helper class for setget script
 *
 * @author Guillermo Thiemann <guille@negdis.de>
 */
var Setget = new Class({

    /**
     * The variables container
     * @var Element
     */
    variablesContainer: null,

    /**
     * The flash menu
     * @var Element
     */
    resultContainer:null,

    /**
     * A table row clone with its input fields
     * @var Element
     */
    clone:null,

    /**
     * Base Url of the site
     * @var String
     */
    baseUrl:null,

    /**
     * Dynamically createt image element with loading animation
     * @var Element
     */
    loaderImg:null,

    /**
     * The show effect
     * @var Fx.Style
     */
    showFx:null,

	/**
	 * Constructor
	 * adds domready event handler and sets the baseUrl of the site
	 *
	 * @param String baseUrl
	 * @access public
	 */
	initialize: function(baseUrl, loaderImgUrl) {
	    this.baseUrl = baseUrl;
		window.addEvent("domready", this.onDomReady.bind(this));

		this.loaderImg = new Element('img', {
		    'src':loaderImgUrl
		});
	},

	/**
	 * DOM-Ready event handler
	 * Initially writes Element properties and
	 * sets their events and event-handlers
	 *
	 * @access public
	 */
	onDomReady:function() {
        this.variablesContainer = $E('#variables');
        this.clone = $E('#firstVarRow').clone();
        this.clone.removeProperty('id');
        this.resultContainer = $E('#result');

        this.showFx = new Fx.Style('result', 'opacity');

        $E('#addVarBtn').addEvent('click', this.addVarFields.bind(this));
        $E('#removeVarBtn').addEvent('click', this.removeVarFields.bind(this));

        var form = $E('form');
		if (form) {
    	    var href = form.getProperty('action');
    	    href = this.injectAjaxCommand(href);
    	    form.setProperty('action', href);
		    form.setProperty('onsubmit', 'return false;');
		    form.addEvent('submit', function() {
		        //this.lastLink = form;
		        this.sendForm(form);
		    }.bind(this));
		}
	},

	/**
	 * Starts load animation to signalize loading in the site
	 */
	startLoadAnimation:function() {
	    this.resultContainer.empty();
	    this.loaderImg.injectBefore(this.resultContainer);
	    this.resultContainer.setOpacity(0);
	},

	/**
	 * Stops loading-animation and starts show effect for the content
	 */
	onContentLoaded:function() {
	    this.loaderImg.remove();
	    this.showFx.start(0,1);
	},

	/**
	 * adds two input fields to the set get form
	 */
	addVarFields:function() {
	    this.clone.injectInside(this.variablesContainer);
	    this.clone = this.clone.clone();
	},

	/**
	 * removes the last two input fields of set get form
	 */
	removeVarFields:function() {
	    var last = this.variablesContainer.getLast();
	    if (last.getPrevious()) {
	        last.remove();
	    }
	},

	/**
	 * Injects the word 'section' between the baseUrl and the rest of the
	 * given url
	 *
	 * @param String url
	 * @return String
	 * @access public
	 */
	injectAjaxCommand: function (url) {
	    // remove base url:
	    if (url.search(this.baseUrl) != -1) {
	       url = url.substr(this.baseUrl.length);
	    }
	    return this.baseUrl + "/section" + url;
	},

	/**
	 * Validates the set/get form
	 */
	validate:function() {
	    var inputfields = $ES('#variables input');
	    var fails = 0;
	    var even = false;
	    var remove = false;
	    var parent = null;
	    for (var i=0; i<inputfields.length; i++) {
	        even = even ? false : true;
	        if (!inputfields[i].getValue()) {
    	       inputfields[i].addClass('error');
    	       fails++;
	           if (remove && i > 2) {
	               parent = inputfields[i].getParent().getParent();
	               parent.remove();
	               fails = fails > 2 ? fails : 0;
	           }
	           remove = even ? true : false;
	        } else if (inputfields[i].hasClass('error')) {
	            inputfields[i].removeClass('error');
	            remove = false;
	        } else {
	            remove = false;
	        }
	    }
	    return fails > 0 ? false : true;
	},

	/**
	 * Sends a form and loads response into body
	 *
	 * @param Element form
	 * @access public
	 */
	sendForm: function(form) {
	    if (!this.validate()) {
	        return;
	    }
	    this.startLoadAnimation();
	    form.send({
	        update:this.resultContainer,
	        onComplete:this.onContentLoaded.bind(this)
	    });
	}

});
