
BIA.namespace("inline");

//
//
// EditField
//
//

BIA.inline.EditField = function( name, ignoreIgnore )
{
    this.ignoreIgnore = false;
    if(ignoreIgnore)
        this.ignoreIgnore = true;

    if(name) {
        this.init(name);
    }
}

BIA.inline.EditField.prototype.init = function( name, insertEditBtn, insertDelBtn )
{
    this.name = name;
    this.div = document.getElementById( this.name );

    this.hideBtns = ((typeof(this.hideButtons) != 'undefined') && this.hideButtons)?true:false;

    this.view = YAHOO.util.Dom.getElementsByClassName("inlineEditView", "div", this.div )[0];

    this.buttonDiv = document.createElement("div");
    this.buttonDiv.className = "inlineButtons";

    if( insertDelBtn == true && !this.hideBtns) {
        this.deleteButton = document.createElement("div");
        this.deleteButton.className = "inlineDeleteButton";
        this.deleteButton.title = "Delete";
        this.buttonDiv.appendChild(this.deleteButton);
    }

    if( insertEditBtn != false && !this.hideBtns) {
        this.editButton = document.createElement("div");
        this.editButton.className = "inlineEditButton";
        this.editButton.title = "Edit";
        this.buttonDiv.appendChild(this.editButton);
    }

    this.view.appendChild(this.buttonDiv);

    this.form = false;
    for( var n=0; n < this.div.childNodes.length; n++ )
        if( this.div.childNodes[n].tagName == "FORM" )
            this.form = this.div.childNodes[n];


    if( !this.form ) {
        var tmpHTML = '<form method="post" action="'+this.formActionURL+'"><input type="hidden" name="goto" value="" />'+(this.previewURL?'<input type="button" class="button previewButton" value="Preview" /><input type="button" class="button editButton" value="Edit" />&nbsp;':'')+'<input type="submit" class="button submitButton" value="Save" />&nbsp;<input type="reset" class="button cancelButton" value="Cancel"/>';
        if( this.maxLength ) {
            tmpHTML += '<div class="counter"></div>';
        }
        tmpHTML += '</form>';
        this.div.innerHTML += tmpHTML;
    }

    for( var n=0; n < this.div.childNodes.length; n++ )
        if( this.div.childNodes[n].tagName == "FORM" )
            this.form = this.div.childNodes[n];


    this.previewButton = YAHOO.util.Dom.getElementsByClassName("previewButton", "input", this.form)[0];
    this.undoPreviewButton = YAHOO.util.Dom.getElementsByClassName("editButton", "input", this.form)[0];
    this.counter = YAHOO.util.Dom.getElementsByClassName( "counter", "div", this.form)[0];

    if( this.form && this.view ) {
        if (!this.hideBtns) {
            YAHOO.util.Event.addListener( this.div, "mouseover", this.displayEditButton, this, true );
            YAHOO.util.Event.addListener( this.div, "mouseout", this.hideEditButton, this, true );
        }
        YAHOO.util.Event.addListener( this.form, "reset", this.cancelEdit, this, true );
        YAHOO.util.Event.addListener( this.div, "click", this.click, this, true );
        if( this.editButton && !this.hideBtns) {
            YAHOO.util.Event.addListener( this.editButton, "click", this.startEdit, this, true );
            YAHOO.util.Event.addListener( this.div, "dblclick", this.startEdit, this, true );
        }
        if( this.deleteButton && !this.hideBtns)
            YAHOO.util.Event.addListener( this.deleteButton, "click", this.deleteHandler, this, true );
        if( this.previewButton )
            YAHOO.util.Event.addListener( this.previewButton, "click", this.preview, this, true );
        if( this.undoPreviewButton )
            YAHOO.util.Event.addListener( this.undoPreviewButton, "click", this.undoPreview, this, true );
            
    }

}

BIA.inline.EditField.prototype.displayEditButton = function( e ) {
    if( YAHOO.util.Dom.hasClass( this.div, "edit" ) )
        return;

    var target = YAHOO.util.Event.getTarget( e );
    while( !this.ignoreIgnore && target && target.parentNode && (target = target.parentNode) )
        if( YAHOO.util.Dom.hasClass( target, "ignore" ) )
            return;

    if( this.view.style.display == "" ) {
        YAHOO.util.Dom.addClass( this.div, "hover" );
    }
}

BIA.inline.EditField.prototype.hideEditButton = function() {
    YAHOO.util.Dom.removeClass( this.div, "hover" );
}

BIA.inline.EditField.prototype.click = function( e ) {

    var target = YAHOO.util.Event.getTarget( e );
    while( !this.ignoreIgnore && target && target.parentNode && (target = target.parentNode) )
        if( YAHOO.util.Dom.hasClass( target, "ignore" ) )
            return;

    var target = YAHOO.util.Event.getTarget( e );
    if(target.className == "inlineEditButton")
        return this.startEdit();
    else if(target.className == "inlineDeleteButton")
        return this.deleteHandler();
}

BIA.inline.EditField.prototype.startEdit = function( e ) {

    if( YAHOO.util.Dom.hasClass( this.div, "edit" ) )
        return true;

    if( e ) {
        var target = YAHOO.util.Event.getTarget( e );
        while( !this.ignoreIgnore && target && target.parentNode && (target = target.parentNode) )
            if( YAHOO.util.Dom.hasClass( target, "ignore" ) )
                return;
    }

    YAHOO.util.Dom.removeClass( this.div, "hover" );
    YAHOO.util.Dom.addClass( this.div, "edit" );

    YAHOO.util.Event.addListener( document, "click", this.clickOutside, this, true );

    // Clear selection if possible
    try {
        if ( window.getSelection && window.getSelection().collapse )
            window.getSelection().collapse();
        else if( window.getSelection && window.getSelection().removeAllRanges )
            window.getSelection().removeAllRanges();
        else if( document.selection && document.selection.empty )
            document.selection.empty();
        else if( document.selection && document.selection.clear )
            document.selection.clear();
    } catch(e) {}

    return;
}

BIA.inline.EditField.prototype.cancelEdit = function() {

    YAHOO.util.Event.removeListener( document, "click", this.clickOutside );
    YAHOO.util.Dom.removeClass( this.div, "edit" );
    YAHOO.util.Dom.removeClass( this.div, "preview" );
    
    // Remove RTE
    if (typeof(this.areaID) != 'undefined') {
       tinyMCE.execCommand('mceRemoveControl', false, this.areaID);
    }

    // Unset id
    //this.textarea.id = "";
}

BIA.inline.EditField.prototype.clickOutside = function( e ) {

    // Find out what was clicked on
    var target = YAHOO.util.Event.getTarget( e );

    // Was it a rightclick?
    rightclick = false;
    if (e.which) rightclick = (e.which == 3);
    else if (e.button) rightclick = (e.button == 2);

    // If the user clicked inside the div or rightclicked
    if( YAHOO.util.Dom.isAncestor( this.div, target ) || rightclick ) {
        return true;
    }

    if (typeof(tinyMCE) != 'undefined') {
/*

        if (tinyMCE.activeEditor) {
            if (tinyMCE.activeEditor.isDirty()) {
                if( confirm( "Do you want to discard your changes and stop editing?" ) ) {
                    this.form.reset();
                    return true;
                }
            }
        } else {
            this.form.reset();
        }
*/
        return true;
    } else if( this.changed ) {
        if( confirm( "Do you want to discard your changes and stop editing?" ) ) {
            this.form.reset();
            return true;
        }
    } else {
        this.form.reset();
    }
}

BIA.inline.EditField.prototype.updateCounter = function( e ) {

    var charsLeft = ( this.maxLength - this.textarea.value.length );

    if( charsLeft < 0 ) {
        this.textarea.value = this.textarea.value.substring( 0, this.maxLength );
        this.textarea.focus();
    } else if( charsLeft < 15 ) {
        YAHOO.util.Dom.removeClass( this.counter, "less50" );
        YAHOO.util.Dom.addClass( this.counter, "less15" );
    } else if( charsLeft < 50 ) {
        YAHOO.util.Dom.removeClass( this.counter, "less15" );
        YAHOO.util.Dom.addClass( this.counter, "less50" );
    } else {
        YAHOO.util.Dom.removeClass( this.counter, "less50" );
        YAHOO.util.Dom.removeClass( this.counter, "less15" );
    }

    if( charsLeft < 0 )
        charsLeft = 0;
    this.counter.innerHTML = charsLeft;
}

//
//
// Text
//
//

BIA.inline.Text = function( name, fetchSourceURL ) {
    if( name && fetchSourceURL ) {
        this.fetchSourceURL = fetchSourceURL;
        this.init( name );

        this.textarea = document.createElement("textarea");
        this.textarea.name = "text";
        this.textarea.className = "inlineEditText";
        this.form.appendChild(this.textarea);
    }
}

BIA.inline.Text.prototype = new BIA.inline.EditField();

BIA.inline.Text.prototype.startEdit = function( e ) {

    if( YAHOO.util.Dom.hasClass( this.div, "preview" ) || YAHOO.util.Dom.hasClass( this.div, "edit" ) )
        return true;

    if( e ) {
        var target = YAHOO.util.Event.getTarget( e );
        while( target && target.parentNode && (target = target.parentNode) )
            if( YAHOO.util.Dom.hasClass( target, "ignore" ) )
                return;
    }
    this.textarea.style.width = (this.div.offsetWidth-10)+"px";
    this.textarea.style.height = (this.div.offsetHeight < 60) ? "60px" : this.div.offsetHeight+"px";

    YAHOO.util.Dom.addClass( this.div, "edit" );
    YAHOO.util.Event.addListener( document, "click", this.clickOutside, this, true );

    // Clear selection if possible
    try {
        if ( window.getSelection && window.getSelection().collapse )
            window.getSelection().collapse();
        else if( window.getSelection && window.getSelection().removeAllRanges )
            window.getSelection().removeAllRanges();
        else if( document.selection && document.selection.empty )
            document.selection.empty();
        else if( document.selection && document.selection.clear )
            document.selection.clear();
    } catch(e) {}

    callback = {
        success:this.fetchSourceSuccess,
        failure:this.fetchSourceFailure,
        scope: this
    };

    try {
        document.body.style.cursor='progress';
    } catch(e) {}

    var time = new Date().getTime();
    YAHOO.util.Connect.asyncRequest('GET', this.fetchSourceURL+"?ajax=1&nocache="+time, callback );
    YAHOO.util.Event.addListener( this.textarea, "change", this.change, this, true );
}

BIA.inline.Text.prototype.fetchSourceSuccess = function( o ) {

    try {
    document.body.style.cursor = 'auto';
    } catch(e) {}

    this.textarea.value = o.responseText;
    this.oldValue = o.responseText;
    this.textarea.focus();
    if( this.maxLength )
        this.updateCounter();
    
    // Set id for active editor
    // this.textarea.id = "activeEditor";
    
    // Apply RTE
    if (typeof(this.areaID) != 'undefined') {
        tinyMCE.execCommand('mceAddControl', false, this.areaID);
    }
}

BIA.inline.Text.prototype.fetchSourceFailure = function( o ) {

    try {
    document.body.style.cursor = 'auto';
    } catch(e) {}
    this.textarea.value = "Unable to fetch text source";
}

BIA.inline.Text.prototype.change = function( o ) {
    // Bacause safari fires a onchange event a little too often, we have to check if the value was changed
    if( this.oldValue != this.textarea.value )
        this.changed = true;
}

BIA.inline.Text.prototype.preview = function( e ) {

    if(this.previewURL) {
        var callback = {
            success:this.previewSuccess,
            failure:this.previewFailure,
            scope: this
        };

        try {
            document.body.style.cursor='progress';
        } catch(e) {}

        var time = new Date().getTime();
        YAHOO.util.Connect.setForm(this.form);
        YAHOO.util.Connect.asyncRequest('POST', this.previewURL+"?ajax=1&nocache="+time, callback );
    }
}


BIA.inline.Text.prototype.previewSuccess = function( o ) {
    YAHOO.util.Dom.replaceClass( this.div, "edit", "preview")

    if( !this.previewDiv ) {
        this.previewDiv = document.createElement("div");
        this.previewDiv.className = "inlineEditPreview";
        this.div.insertBefore(this.previewDiv, this.form);
    }
    this.previewDiv.innerHTML = "";
    for( var i=0; i<this.view.childNodes.length; i++) {
        if(this.view.childNodes[i].className) {
            if( YAHOO.util.Dom.hasClass( this.view.childNodes[i], "ignore") ) {
                var thumb = this.view.childNodes[i].cloneNode(true);
                this.previewDiv.appendChild(thumb);
            }
        }
    }
    this.previewDiv.innerHTML += o.responseText;
    try {
    document.body.style.cursor = 'auto';
    } catch(e) {}
}

BIA.inline.Text.prototype.previewFailure = function( o ) {

    try {
    document.body.style.cursor = 'auto';
    } catch(e) {}
    alert("Error fetching preview data");
}

BIA.inline.Text.prototype.undoPreview = function() {
    YAHOO.util.Dom.replaceClass( this.div, "preview", "edit")
}

//
//
// ItemText
//
//
BIA.inline.ItemText = function(id, showEditor) {

    name = "inlineItemText"+id;
    this.fetchSourceURL = "/actions/item.fetchSource/"+id;
    // this.maxLength = 300;
    //this.previewURL = "/actions/item.preview/"+id;
    this.formActionURL = "/actions/media.item.edit/"+id;
    this.showEditor = (typeof(showEditor) != 'undefined')?true:false;
    if (this.showEditor) {
        this.areaID = 'inlineComment-' + id;
    }
    this.init( name, true );

    this.textarea = document.createElement("textarea");
    this.textarea.id = this.areaID;
    this.textarea.name = "text";
    this.textarea.className = "inlineEditText";
    this.form.insertBefore( this.textarea, this.form.elements[0] );
    
    if( this.maxLength ) {
        YAHOO.util.Event.addListener( this.textarea, "keydown", this.updateCounter, this, true );
        YAHOO.util.Event.addListener( this.textarea, "keyup", this.updateCounter, this, true );
    }
}

BIA.inline.ItemText.prototype = new BIA.inline.Text();

//
//
// ItemTitle
//
//
BIA.inline.ItemTitle = function( id, defaultValue ) {

    name = "inlineItemTitle"+id;
    this.formActionURL = "/actions/media.item.edit/"+id;
    this.defaultValue = defaultValue;
    this.init( name, true );

    this.title = document.createElement("input");
    this.title.type = "text";
    this.title.name = "title";
    this.title.className = "inlineInputTitle";
    this.title.defaultValue = defaultValue;
    this.title.setAttribute("value", defaultValue);
    this.title.setAttribute("autocomplete", "off");
    this.form.insertBefore( this.title, this.form.elements[0] );

    this.title.style.width = (this.div.offsetWidth-10)+"px";
//  this.title.style.height = this.div.offsetHeight+"px";
}

BIA.inline.ItemTitle.prototype = new BIA.inline.EditField();


//
//
// ContentText
//
//
BIA.inline.ContentText = function(id, showEditor) {

    name = "inlineContent"+id;
    this.fetchSourceURL = "/actions/content.fetchSource/"+id;
    this.previewURL = "/actions/content.preview/"+id;
    this.formActionURL = "/actions/content.edit/"+id;
    this.showEditor = (typeof(showEditor) != 'undefined')?true:false;
    if (this.showEditor) {
        this.areaID = 'inlineComment-' + id;
    }
    this.init( name, true );

    this.textarea = document.createElement("textarea");
    this.textarea.id = this.areaID;
    this.textarea.name = "text";
    this.textarea.className = "inlineEditText";
    this.form.insertBefore( this.textarea, this.form.elements[0] );
}

BIA.inline.ContentText.prototype = new BIA.inline.Text();

//
//
// ItemTitle
//
//
BIA.inline.ContentHeading = function( id, defaultValue ) {

    name = "inlineContent"+id;
    this.formActionURL = "/actions/content.edit/"+id;
    this.defaultValue = defaultValue;
    this.init( name, true );

    this.title = document.createElement("input");
    this.title.type = "text";
    this.title.name = "heading";
    this.title.className = "inlineInputTitle";
    this.title.defaultValue = defaultValue;
    this.title.setAttribute("value", defaultValue);
    this.title.setAttribute("autocomplete", "off");
    this.form.insertBefore( this.title, this.form.elements[0] );

    this.title.style.width = (this.div.offsetWidth-10)+"px";
//  this.title.style.height = this.div.offsetHeight+"px";
}

BIA.inline.ContentHeading.prototype = new BIA.inline.EditField();


//
// COMMENTS
//
BIA.inline.Comment = function( id, btnEdit, btnDelete, hideButtons ) {

    name = "inlineComment"+id;
    this.deleteURL = "/actions/comment.delete/"+id;
    this.fetchSourceURL = "/actions/comment.fetchUBB/"+id;
    this.formActionURL = "/actions/comment.edit/"+id;
    // hide inline buttons
    this.hideButtons = (typeof(hideButtons) != 'undefined')?true:false;
    //if (this.hideButtons)
        this.areaID = 'inlineComment-' + id;

    this.init( name, btnEdit, btnDelete );

    this.textarea = document.createElement("textarea");
    this.textarea.id = this.areaID;
    this.textarea.name = "text";
    this.textarea.className = "inlineEditText";
    this.form.insertBefore( this.textarea, this.form.elements[0] );
}

BIA.inline.Comment.prototype = new BIA.inline.Text();

BIA.inline.Comment.prototype.deleteHandler = function() {

    if(confirm("Are you sure you want to delete this comment?")) {

        callback = {
            success:this.deleteSuccess,
            failure:this.deleteFaliure,
            scope: this
        };
        YAHOO.util.Connect.asyncRequest('GET', this.deleteURL+"?ajax=1", callback );
    }
}

BIA.inline.Comment.prototype.deleteSuccess = function( o ) {
    if( o.responseText == "OK")
        this.div.parentNode.parentNode.removeChild(this.div.parentNode);
    else
        alert( o.responseText );
}

BIA.inline.Comment.prototype.deleteFaliure = function() {
    alert("Error deleting comment");
}

//
//
// UserProfileText
//
//
BIA.inline.UserProfileText = function( id ) {

    name                = "inlineUserText"+id;
    this.fetchSourceURL    = "/actions/user.fetchProfileSource/"+id;
    //this.previewURL       = "/actions/user.preview/"+id;
    this.formActionURL  = "/actions/user.edit/"+id;
    this.maxLength      = 255;
    this.init( name );

    this.textarea       = document.createElement("textarea");
    this.textarea.name  = "profileText";
    this.textarea.className = "inlineEditText";
    this.form.insertBefore( this.textarea, this.form.elements[0] );
    YAHOO.util.Event.addListener( this.textarea, "keydown", this.updateCounter, this, true );
    YAHOO.util.Event.addListener( this.textarea, "keyup", this.updateCounter, this, true );
    this.updateCounter();
}

BIA.inline.UserProfileText.prototype = new BIA.inline.Text();

//
//
// MessageDescriptionText
//
//
BIA.inline.MessageDescriptionText = function( id ) {

    name                = "inlineMessageText"+id;
    this.fetchSourceURL = "/actions/admin.message.fetchSource/"+id;
    this.previewURL     = "/actions/admin.message.preview/"+id;
    this.formActionURL  = "/actions/admin.message.edit/"+id;
    this.init( name );

    this.textarea       = document.createElement("textarea");
    this.textarea.name  = "descriptionText";
    this.textarea.className = "inlineEditText";
    this.form.insertBefore( this.textarea, this.form.elements[0] );
}

BIA.inline.MessageDescriptionText.prototype = new BIA.inline.Text();

//
//
// Report
//
//

BIA.inline.Report = function( id ) {

    name = "inlineReport"+id;
    this.deleteURL      = "/actions/item.report.delete/"+id;
    this.fetchSourceURL = "/actions/item.report.fetchSource/"+id;
    this.formActionURL  = "/actions/item.report.edit/"+id;
    this.init( name, true, true );

    this.textarea = document.createElement("textarea");
    this.textarea.name = "text";
    this.textarea.className = "inlineEditText";
    this.form.insertBefore( this.textarea, this.form.elements[0] );
}

BIA.inline.Report.prototype = new BIA.inline.Text();

BIA.inline.Report.prototype.deleteHandler = function() {

    if(confirm("Are you sure you want to delete this report?")) {

        callback = {
            success:this.deleteSuccess,
            failure:this.deleteFaliure,
            scope: this
        };
        YAHOO.util.Connect.asyncRequest('GET', this.deleteURL+"?ajax=1", callback );
    }
}

BIA.inline.Report.prototype.deleteSuccess = function( o ) {
    if( o.responseText == "OK")
        this.div.parentNode.parentNode.parentNode.removeChild(this.div.parentNode.parentNode);
    else
        alert( o.responseText );
}

BIA.inline.Report.prototype.deleteFaliure = function() {
    alert("Error deleting report");
}


