var DEBUG = false;

/*
    Player setUp
*/
var so = new SWFObject('player/flashmp3player.swf', 'player', '220', '200', '9'); // Location of swf file. You can change player width and height here (using pixels or percents).
so.addParam('quality', 'high');
so.addVariable('content_path','mp3'); // Location of a folder with mp3 files (relative to php script).
so.addVariable('color_path','player/default.xml'); // Location of xml file with color settings.
so.addVariable('script_path','player/flashmp3player.php'); // Location of php script.

document.observe('dom:loaded', function() {
    so.write('player');

    CD.init();

    window.msgBoard = new Messages();
    window.newsBoard = new News();
})

/*
    CD
*/

var CD = {
    init : function() {
        this.cd = $('cd');
        this.offset = 0;
        this.width = 649;
        this.view = 220;
        this.step = 2;
        this.ms = 10;
        this.moving = false;
        this.cd.style.backgroundPosition = this.getPositionPx(this.offset);
    },
    getPositionPx : function() {
        return '-' + (this.width - this.offset - this.view) + 'px 0px';
    },
    moveLeftStart : function() {
        this.moving = true;
        this.moveLeft();
    },
    moveRightStart: function() {
        this.moving = true;
        this.moveRight();
    },
    moveLeft : function() {
        if (!this.moving) return;
        this.offset -= this.step;
        if (this.offset < 0) {
            this.offset = 0;
            this.moveEnd();
            return;
        }
        this.cd.style.backgroundPosition = this.getPositionPx(this.offset);
        setTimeout(this.moveLeft.bind(this), this.ms);
    },
    moveRight : function() {
        if (!this.moving) return;
        this.offset += this.step;
        if (this.offset > this.width - this.view) {
            this.offset = this.width - this.view;
            this.moveEnd();
            return;
        }
        this.cd.style.backgroundPosition = this.getPositionPx(this.offset);
        setTimeout(this.moveRight.bind(this), this.ms);
    },
    moveEnd : function() {
        this.moving = false;
    }
}

/*
    Generic Board
*/
var Board = Class.create({
    initialize : function() {
        this.setup();
    },
    setup : function() {
        this.config = {
            urlAction : 'action.php',
            getAction : 'get_messages',
            sendAction : 'send_message',
            deleteAction : 'delete_message',
            getJSONAction : 'get_messages_json',
            prefix : 'MSG'
        };

        this.form = $('message_form');
        this.fields = [$('name_field'), $('email_field'), $('message_field')];
        this.holder = $('messages_content');
        this.errorContainer = $('message_errors');
    },
    formSetUp : function() {
        this.fields.each(function(field) {
            field.observe('focus', function() {
                if (field.hasClassName('edited')) return;
                field.addClassName('edited');
                field.value = '';
            });
        }, this);
    },
    working : function() {
        $$('body').first().addClassName('working');
    },

    idle : function() {
        $$('body').first().removeClassName('working');
    },
    get : function() {
        var parameters = {action : this.config.getAction};
        new Ajax.Request(this.config.urlAction,{
            parameters : parameters,
            method : 'get',
            onSuccess: this.generate.bind(this)
        });
    },
    getJSON : function(id, handler) {
        if (!handler || typeof handler != 'function') return;
        var parameters = {action : this.config.getJSONAction};
        if (id) parameters.id = id;
        new Ajax.Request(this.config.urlAction,{
            parameters : parameters,
            method : 'get',
            onSuccess: function(transport) {handler(transport.responseText.evalJSON(true))}
        });
    },
    send : function() {
        if (this.sending) return;

        this.resetErrors();

        parameters = this.form.serialize(true);
        parameters.action = this.config.sendAction;

        new Ajax.Request(this.config.urlAction,{
            parameters : parameters,
            method : 'post',
            onSuccess: this.sendConfirm.bind(this)
        });

        this.sending = true;
        this.working();

    },
    remove : function(id) {
        parameters = {action : this.config.deleteAction, id : id};

        new Ajax.Request(this.config.urlAction,{
            parameters : parameters,
            method : 'get',
            onSuccess: this.deleteConfirm.bind(this)
        });
    },
    generate : function(transport) {
        var json = transport.responseText.evalJSON(true);
        if (json.success) {
            this.holder.update(json.html);
        }
    },
    sendConfirm : function(transport) {
        this.sending = false;
        this.idle();

        var json = transport.responseText.evalJSON(true);

        if (json.success) {
            if (!DEBUG) this.form.switchOff({queue : 'end'});
            this.holder.insert({top : json.html});
            this.holder.down('div').slideDown({queue : 'end'});
        } else {
            if (json.errors.length) this.report(json.errors);
        }
    },
    deleteConfirm : function(transport) {
        var json = transport.responseText.evalJSON(true);
        if (json.success) {
            node = $(this.config.prefix + json.id);
            if (node) node.puff();
        } else {
            alert(json.error);
        }
    },
    report : function(errors) {
        var node = this.errorContainer;
        if (!node) return;

        var html = '';

        errors.each(function(error) {
            html += '<li>' + error + '</li>';
        });
        html = new Template('<ul>#{content}</ul>').evaluate({content : html});

        node.update(html);
        node.slideDown({queue : 'end'});
    },
    resetErrors : function() {
        var node = this.errorContainer;

        if (!node) return;

        node.hide();
        node.update('');
    }
});

/*
    Messages
*/
var Messages = Class.create(Board, {
    initialize : function($super) {
        $super();
        this.formSetUp();
        this.get();
    },
    send : function($super) {
        if (!this.fields[2].hasClassName('edited') && !DEBUG) return;

        $super();
    },
    deleteMessage : function(id) {
        if (!id) return;
        if (!confirm('Borrar mensaje?')) return;

        this.remove(id);
    }
});

/*
    News
*/
var News = Class.create(Board, {
    setup : function($super) {
        $super();

        this.config.getAction = 'get_news';
        this.config.sendAction = 'send_story';
        this.config.deleteAction = 'delete_story';
        this.config.editAction = 'edit_story';
        this.config.getJSONAction = 'get_news_json';
        this.config.prefix = 'STORY';

        this.form = $('news_form');
        this.fields = [$('title_field'), $('content_field')];
        this.holder = $('news_content');
        this.errorContainer = $('news_errors');
    },
    send : function($super, id) {
        if (id) {
            this.resetErrors();
            var form = $('story_form_' + id)

            parameters = form.serialize(true);
            parameters.action = this.config.editAction;

            new Ajax.Request(this.config.urlAction,{
                parameters : parameters,
                method : 'post',
                onSuccess: this.editConfirm.bind(this)
            });
        } else {
            $super();
        };

    },
    editConfirm : function(transport) {
        var json = transport.responseText.evalJSON(true);
        if (json.success) {
            var story = $(this.config.prefix + json.id);
            var form = $('story_form_' + json.id);

            form.remove();
            story.replace(json.html);
        } else {
            if (json.errors.length) this.report(json.errors);
        }
    },
    deleteStory : function(id) {
        if (!id) return;
        if (!confirm('Borrar noticia?')) return;

        this.remove(id);
    },
    editStory : function(id) {
        if (!id) return;
        var story = $(this.config.prefix + id);
        var form = $('story_form_' + id);
        if (!story || !form) return;

        story.switchOff({queue : 'end'});
        form.slideDown({queue : 'end'});
    },
    cancelEdit : function(id) {
        if (!id) return;
        var story = $(this.config.prefix + id);
        var form = $('story_form_' + id);
        if (!story || !form) return;

        form.switchOff({queue : 'end'});
        story.slideDown({queue : 'end'});
    }
});
