jQuery( document ).ready(function()
{

    window.storyScriptInstance = new storyScript();
    window.storyScriptInstance.init();

});


var storyScript = function()
{
    this.storyArea = null;
    this.symbolsLeftBox = null;
    this.symbolsLeftNode = null;
    this.storyMaxLength = 1000;
}

storyScript.prototype.init = function()
{
    this.initStoryArea();
    this.initImageFields();
}

storyScript.prototype.initStoryArea = function()
{
    this.symbolsLeftBox = document.getElementById('symbolsLeftBox');
    this.storyArea = document.getElementById('story');
    if  ((!this.storyArea) || (!this.symbolsLeftBox))
    {
        return;
    }

    this.symbolsLeftNode = this.symbolsLeftBox.getElementsByTagName('strong')[0];
    if (!this.symbolsLeftNode)
    {
        return;
    }

    var self = this;
    var updateCall = function()
    {
        return self.storyChanged();
    }

    this.storyArea.onchange         = updateCall;
    this.storyArea.onkeypress       = updateCall;
    this.storyArea.onkeyup          = updateCall;
    this.storyArea.onclick          = updateCall;
    this.storyArea.onpropertychange = updateCall;

    updateCall();
    this.symbolsLeftBox.style.visibility = 'visible';

}


storyScript.prototype.storyChanged = function()
{
    if (
        (!this.storyArea)
        ||
        (!this.symbolsLeftNode)
    )
    {
        return false;
    }

    var symbolsLeft = this.storyMaxLength - this.storyArea.value.length;
    if (symbolsLeft < 0)
    {
        this.storyArea.value = this.storyArea.value.substring(0, this.storyMaxLength);
    }

    this.updateSymbolsLeft();
    return true;
}

storyScript.prototype.updateSymbolsLeft = function()
{
    var symbolsLeft = this.storyMaxLength - this.storyArea.value.length;
    this.symbolsLeftNode.innerHTML = symbolsLeft;
    return;
}

storyScript.prototype.initImageFields = function()
{
    var script = this;
    jQuery('fieldset.images input[type=file]').change(
        function( )
        {
            if (script.areAllFileFieldsFull())
            {
                script.addNewFileField();
            }
        }
    );
}

storyScript.prototype.areAllFileFieldsFull = function()
{
    var isEmpty = true;
    jQuery('fieldset.images input[type=file]').each
    (
        function ()
        {
            if (this.value.length == 0)
            {
                isEmpty = false;
            }
        }
    );
    return isEmpty;
}

storyScript.prototype.addNewFileField = function()
{
    // find last item, read number in name attribute
    var lastItemNumber = jQuery('fieldset.images div.fieldBox:last input[type=file]').attr('name').replace(/[^0-9]+/g, '');
    if (lastItemNumber.toString().length < 1)
    {
        return;
    }
    var newNumber = parseInt(lastItemNumber) + 1;

    var newItem = jQuery('fieldset.images div.fieldBox:first').clone(true);
    var replaceAttrs = ['name'];

    newItem.find('input').val('').each(
        function()
        {
            for (var i=0; i<replaceAttrs.length; i++)
            {
                var attrName = replaceAttrs[i];
                var val = jQuery(this).attr(attrName);
                var newVal = val.replace(/\d+/, newNumber);
                jQuery(this).attr(attrName, newVal);
            }
        }
    );

    newItem.appendTo('fieldset.images');

}
