
/**
 * creates a new RatingInputField.
 *
 * Parameters:
 *   - stars : id of the span, containing the star images.
 **/
function StarRatingInputField(stars) {

  alert("StarRatingInputField");

    var ratingInputField = {
        // attributes:
        _value: 0,
        _starsElement: null,
        _stars: null,
        //functions:
        init: function(starsElement) {
            this._starsElement = starsElement;
            this._stars = new Array();
            var index = 0;
            var self = this;
            this._starsElement.onmouseout = function() {
                self._mouseLeave();
            }
            for (i = 0; i < this._starsElement.childNodes.length; i++) {
                if (this._starsElement.childNodes[i].tagName == "IMG") {
                    this._stars[index] = this._starsElement.childNodes[i];
                    this._stars[index].className = "star_inactive";
                    this._stars[index].starsIndex = index;
                    this._stars[index].onmouseover = function() {
                        self._mouseOverStar(this.starsIndex);
                    }
                    this._stars[index].onclick = function() {
                        self._mouseClickedStar(this.starsIndex);
                    }
                    index++;
                }
            }
        },
        onValueChanged: function() {
        },
        getValue: function() {
            return this._value;
        },
        _mouseLeave: function() {
            this._paintStars(this._value - 1);
        },
        _mouseOverStar: function(index) {
            this._paintStars(index);
        },
        _mouseClickedStar: function(index) {
            var oldValue = this._value;
            this._value = index + 1;
            if (oldValue != this._value) this.onValueChanged();
        },
        _paintStars: function(index) {
            for (i = 0; i < this._stars.length; i++) {
                this._stars[i].className = (i > index) ? "star_inactive" : "star_active";
            }
        }
    };
    ratingInputField.init(document.getElementById(stars));
    return ratingInputField;
}
function _StarRatingInputField(stars) {
    var ratingInputField = {
        // attributes:
        _value: 0,
        _starsElement: null,
        _stars: null,
        //functions:
        init: function(starsElement) {
            this._starsElement = starsElement;
            this._stars = new Array();
            var index = 0;
            var self = this;
            this._starsElement.onmouseout = function() {
                self._mouseLeave();
            }
            for (i = 0; i < this._starsElement.childNodes.length; i++) {
                if (this._starsElement.childNodes[i].tagName == "IMG") {
                    this._stars[index] = this._starsElement.childNodes[i];
                    this._stars[index].className = "star_inactive";
                    this._stars[index].starsIndex = index;
                    this._stars[index].onmouseover = function() {
                        self._mouseOverStar(this.starsIndex);
                    }
                    this._stars[index].onclick = function() {
                        self._mouseClickedStar(this.starsIndex);
                    }
                    index++;
                }
            }
        },
        onValueChanged: function() {
        },
        getValue: function() {
            return this._value;
        },
        _mouseLeave: function() {
            this._paintStars(this._value - 1);
        },
        _mouseOverStar: function(index) {
            this._paintStars(index);
        },
        _mouseClickedStar: function(index) {
            var oldValue = this._value;
            this._value = index + 1;
            if (oldValue != this._value) this.onValueChanged();
        },
        _paintStars: function(index) {
            for (i = 0; i < this._stars.length; i++) {
                this._stars[i].className = (i > index) ? "star_inactive" : "star_active";
            }
        }
    };
    ratingInputField.init(document.getElementById(stars));
    return ratingInputField;
}
/**
 * TODO: deprecate the 'specialised' versions of this function
 */
function updateElement(link, update) {
    new Ajax(link, { method: 'get',
        update: $(update),
        evalScripts: true }).request();
    return false;
}
function updateElementWithPost(formId, updateId) {
    var args = { method: 'post', update: $(updateId), evalScripts: true };
    $(formId).send(args);
    return false;
}

  // 
function submitButton(button, update) {
    var args = { method: 'post', update: update, evalScripts: true };
    button.disabled = true;
    button.form.send(args);
    return false;
}
function submitFormWithoutButton(form, update) {
    var args = { method: 'post', update: update, evalScripts: true };
    form.send(args);
    return false;
}
function modifyAndSubmitForm(form, element, value, update) {
    var args = {method: 'post', update: update, evalScripts: true};
    
    form.elements[element].value = value;
    form.send(args);
    return false;
}
function onPostCommentComplete() {
  window.fireEvent("comment-submitted");
}
function disposeCommentForm() {
  if ($('commentEditor') != null) {
    var editor = tinyMCE.selectedInstance;
    if (editor) {  // fixes reload bug
    alert("mceRemoveControl");
      tinyMCE.execCommand('mceRemoveControl', false, 'commentEditor');
    }
  }
}
function postComment(id,targetId) {
//  var editor = tinyMCE.selectedInstance;
tinyMCE.execCommand('mceRemoveControl', true, 'textEditor_' + targetId);
// tinyMCE.execCommand('mceTriggerSave', true, 'textEditor');
//  editor.triggerSave();
//  tinyMCE.execInstanceCommand(editor,'mceCleanup');
  
  var args = {
    method: 'post',
    update: $("commentsArea_" + targetId),
    onComplete: function() { onPostCommentComplete(); },
    onFailure: function() { alert("Failed submitting comment!"); },
    evalScripts: true
  };
$("commentForm_" + id).id.value = targetId;
$("commentForm_" + id).send(args);
  return false;
}