Emv.Rating = {};

/**
 * Rating Handler for one or more bars
 * 
 * @param {string} id
 * @param {string} ajaxUrl
 */
Emv.Rating.ratingHandler = function(id, ajaxUrl) {
    
    this.id      = id;
    this.ajaxUrl = ajaxUrl;
    this.bars    = new Array();
    this.toEdit  = false;
    this.lastBut = null;
    
    this.yuiConnectionCallback = {

          success: this.ratingStored,
          failure: this.ratingStored,
          argument: [ this ]
        };
}

/**
 * getid function
 * 
 * @return string
 */
Emv.Rating.ratingHandler.prototype.getId = function(){

    return this.id;
}

/**
 * Registers an new bar with the handler
 * 
 * @param {string} id
 * @param {string} name
 */
Emv.Rating.ratingHandler.prototype.addBar = function(id, name){

    this.bars[this.bars.length] = new Array(id, name); 
}


/**
 * goto EditMode
 * 
 * Put all registered bars to editmode.
 * 
 * @param {element} submitButton
 */
Emv.Rating.ratingHandler.prototype.edit = function(submitButton){

    if (this.toEdit) {
    
         var params = '';
    
        for (i = 0; i < this.bars.length; i++) {
        
            bar = Emv.Registry.getObject(this.bars[i][0]);
            if (bar) {
            
                params += this.bars[i][1] + '=' + bar.getEditValue() + '&';
            }
        }
       
        var request = YAHOO.util.Connect.asyncRequest(
                'POST',
                this.ajaxUrl,
                this.yuiConnectionCallback,
                params
        );
    }
    else {

        this.toEdit = true;
    
        for (i = 0; i < this.bars.length; i++) {
        
            bar = Emv.Registry.getObject(this.bars[i][0]);
            if (bar) {
            
                bar.edit();
            }
        }
        
        imgs = YAHOO.util.Dom.getChildren(submitButton);
        if (imgs && imgs[0]) {
            
            imgs[0].src = '/_assets/pics/buttons/button-save.gif';
        }
        
        this.lastBut = submitButton;
    }
}

/**
 * Ajax callbacl
 * 
 * @param {Object} o
 */
Emv.Rating.ratingHandler.prototype.ratingStored = function(o){

    var _this = o.argument[0];
    
    var json = o.responseText.substring(o.responseText.indexOf('{'), o.responseText.lastIndexOf('}') + 1);
    var data = eval('(' + json + ')');

    count = document.getElementById(_this.id + 'RatingCount');
    if (count) {
        
        count.innerHTML = data.ratingCount;
    }

    Emv.VideodeJsoverlay.get(_this.id + 'RatingButton').show('Best&auml;tigung', 'Danke f&uuml;r Ihre Bewertung!');
    
    ratingButton = document.getElementById(_this.id + 'RatingButton');
    if (ratingButton) {
        
        if (data.allowedToRate) {
            
            ratingButton.style.display = 'block';
        } else {
            
            ratingButton.style.display = 'none';
        }
    }

    imgs = YAHOO.util.Dom.getChildren(_this.lastBut);
    if (imgs && imgs[0]) {
        
        imgs[0].src = '/_assets/pics/buttons/button-vote-now.gif';
    }
    
    _this.toEdit = false;

    for (i = 0; i < _this.bars.length; i++) {
    
        bar = Emv.Registry.getObject(_this.bars[i][0]);
        if (bar) {
        
            //Setting new value
            if (data.bars[_this.bars[i][1]]) {
                
                bar.setValue(data.bars[_this.bars[i][1]]);
            }
            bar.display();
        }
    }    
}


/**
 * Creates ONE bar
 * 
 * @param {string} id
 */
Emv.Rating.ratingBar = function(id, width, height) {

    this.id       = id;
    this.toEdit   = false;
    this.oldValue = 0;
    this.width    = width;
    this.height   = height;
    
    //Create Progressbar for Display
    this.ratingDisplayBar = new YAHOO.widget.ProgressBar({anim:true, width:width, height: height, maxValue:100, minValue:0, value:0}).render(this.id + 'Display');
    var anim = this.ratingDisplayBar.get('anim');
    anim.duration = 2;
    anim.method = YAHOO.util.Easing.easeOut;
    this.ratingDisplayBar.set('value', 0);
    
    var _id = this.id;
    this.ratingDisplayBar.on('complete', function (value) {
        
        pb = Emv.Registry.getObject(_id);
        
        if (pb.getEditState() && value == 0) {
            
            pb.postEditSwitch();
        }        
    });

    //Create Slider for Editing
    this.ratingEditBar = YAHOO.widget.Slider.getHorizSlider( this.id + 'Edit', this.id + 'Thumb', 0, width, (width / 100) * 10);
    this.ratingEditBar.animate = true;

    YAHOO.lang.augmentObject(this.ratingEditBar, {

        // The highlight element
        _highlight : YAHOO.util.Dom.get(_id + 'Highlight'),

        // A method to update the status and update the highlight
        updateHighlight : function () {
            // Adjust the width of the highlight to match inner boundary
            YAHOO.util.Dom.setStyle(this._highlight, 'width', this.getValue() + 'px');
        }
    },true);

    this.ratingEditBar.subscribe('change',this.ratingEditBar.updateHighlight, this.ratingEditBar, true);
    this.ratingEditBar.setValue(0, true);
    
    YAHOO.util.Dom.addClass(this.id + 'Display', 'ratingShow');
    YAHOO.util.Dom.addClass(this.id + 'Edit', 'ratingHide');
}

/**
 * Set the value of the displaybar
 * 
 * @param {element} setValue
 */
Emv.Rating.ratingBar.prototype.setValue = function(value){

    if (!this.toEdit) {

        this.ratingDisplayBar.set('value', value);    
    }
    this.oldValue = value;
}

/**
 * Get the value of the editbar
 * 
 * @return integer
 */
Emv.Rating.ratingBar.prototype.getEditValue = function(){

    //divided with factor
    return this.ratingEditBar.getValue() / (this.width / 100);
}

/**
 * getid function
 * 
 * @return string
 */
Emv.Rating.ratingBar.prototype.getId = function(){

    return this.id;
}

/**
 * getEditState function
 * 
 * @return string
 */
Emv.Rating.ratingBar.prototype.getEditState = function(){

    return this.toEdit;
}

/**
 * set bar to editmode
 */
Emv.Rating.ratingBar.prototype.edit = function() {
    
    if (this.toEdit) {
        
        return;
    }
    this.toEdit = true;
    var anim = this.ratingDisplayBar.get('anim');
    anim.duration = 1;
    this.ratingDisplayBar.set('value', 0);
}

/**
 * set bar to display
 */
Emv.Rating.ratingBar.prototype.display = function() {
    
    if (!this.toEdit) {
        
        return;
    }
    this.toEdit = false;
    YAHOO.util.Dom.replaceClass(this.id + 'Edit', 'ratingShow', 'ratingHide');
    YAHOO.util.Dom.replaceClass(this.id + 'Display', 'ratingHide', 'ratingShow');
    
    var anim = this.ratingDisplayBar.get('anim');
    anim.duration = 2;
    this.ratingDisplayBar.set('value', this.oldValue);
}

/**
 * switch displaybar to editbar
 */
Emv.Rating.ratingBar.prototype.postEditSwitch = function(){
    
    YAHOO.util.Dom.replaceClass(this.id + 'Display', 'ratingShow', 'ratingHide');
    YAHOO.util.Dom.replaceClass(this.id + 'Edit', 'ratingHide', 'ratingShow');
    this.ratingEditBar.setValue(0, true);
}

