var BodyController = new Class({
    options: {
        'flashHolder': $('flashes'),
        'flashDuration': 5000,

        'grabHtmlFlashes': true
    },
    Implements: Options,
    flashes: {},

    initialize: function(options) {
        this.setOptions(options);
        
        if(this.options.grabHtmlFlashes) {
            this.processHtmlFlashes( );
        }
    },

    processHtmlFlashes: function(element) {
        if(!element) {
            element = $$('body')[0];
        }

        var flashes = element.getElements(".flash");
        flashes.each(function(el) {
            el.removeClass("flash");
            this.addFlash(el.get('class'), el.get('html'), true, false);
            el.dispose();
        }.bind(this));
    },

    addStatusFlash: function(html, show, no_hide) {
        return this.addFlash('status', html, show, !no_hide);
    },
    addWarningFlash: function(html, show, no_hide) {
        return this.addFlash('warning', html, show, !no_hide);
    },
    addErrorFlash: function(html, show, no_hide) {
        return this.addFlash('error', html, show, !no_hide);
    },
    addFlash: function(type, html, show, autohide) {
        var div = new Element('div', {'class': type+" flash", 'html': html});
        this.flashes[div] = type;
        if(show) this.showFlash(div);
        if(autohide) {
            (function( ) { this.hideFlash(div); }.bind(this)).delay(
                this.options.flashDuration);
        }
        return div;
    },
    showFlash: function(div, autohide) {
        var flash = this.flashes[div];
        if(flash) {
            if(!div.retrieve('shown', false)) {
                if(this.options.flashHolder) {
                    div.inject(this.options.flashHolder);
                    div.setOpacity(0);
                    div.tween('opacity', 1);
                    div.store('shown', true);
                }
            }
        }
    },
    hideFlash: function(div) {
        var flash = this.flashes[div];
        if(flash) {
            if(div.retrieve('shown', false)) {
                if(this.options.flashHolder) {
                    div.tween('opacity', 0);
                    var opts = div.get('tween').options;
                    (function( ) { div.dispose(); }).delay(opts.duration);
                    div.store('shown', false);
                }
            }
        }
    }
});

function getUrlVars() { var map = {}; var parts = window.location.search.replace(/[?&]+([^=&]+)(=[^&]*)?/gi, function(m,key,value) { map[key] = (value === undefined) ? true : value.substring(1); }); return map; }

var bodyController = null;

window.addEvent("domready", function(ev) {
    if(Browser.Engine.trident && Browser.Engine.version == 4) {
        $$('.round_blogs').each(function(el) {
            var bg = el.getStyle('background-image');
            if(bg) el.setStyle('background-image', bg.replace(".png", ".jpg"));
        });
        $$('.smallAvatar').each(function(el) {
            var bg = el.getStyle('background-image');
            if(bg) el.setStyle('background-image', bg.replace(".png", ".jpg"));
        });    
        $$('img.weemee').each(function(el) {
            var bg = el.getStyle('background-image');
            if(bg) el.setStyle('background-image', bg.replace(".png", ".jpg"));
        });    
    }
});

window.addEvent("domready", function(ev) {
    bodyController = new BodyController({
        'flashHolder': $$('body')[0].getElement("#flashes")
    });
    $$('body')[0].store('controller', bodyController);    

    $$('.deleteComment').addEvent("click", function(ev) {
        if(!confirm("Are you sure you wish to delete this comment?\nThis action cannot be undone."))
            new Event(ev).stop();
    });

    $$('.editComment').addEvent("click", function(ev) {
        new Event(ev).stop( );
        var body = this.getParent("div").getPrevious("div.cBody");

        var id = "comment"+$time();
        var area = new Element("textarea", {'name': 'body', 'id': id, 'cols': 50});
        area.set('text', body.get('html'));
        area.replaces(body);
        
        this.getParent("ul").setStyle("display", "none");

        var edit = this;

        var br = new Element("p");
        var input = new Element("input", {'class': 'cEdit', 'name': 'approve', 
            'type': 'button', 'value': 'Save and Approve'});
        input.addEvent("click", function(ev, input) {
            new Event(ev).stop( );
            updateComment(area, edit, CKEDITOR.instances[id], this);
        });
        input.inject(br);
        var input = new Element("input", {'class': 'cEdit', 'name': 'unapprove',  
            'type': 'button', 'value': 'Save, and Don\'t Approve'});
       input.addEvent("click", function(ev, input) {
            new Event(ev).stop( );
            updateComment(area, edit, CKEDITOR.instances[id], this);
        });
        input.inject(br);
        var input = new Element("input", {'class': 'cEdit', 'name': 'cancel', 
            'type': 'button', 'value': 'Cancel Changes'});
       input.addEvent("click", function(ev, input) {
            new Event(ev).stop( );
            updateComment(area, edit, CKEDITOR.instances[id], this);
        });
        input.inject(br);
        br.inject(area, 'after');
        CKEDITOR.replace(id, {'toolbar':'Comments'});
    });

    $$('.grow_ul input').addEvent("click", function(ev) {
        window.location = this.getParent("a").get('href');
    });
    $$('.class_lists input').addEvent("click", function(ev) {
        window.location = this.getParent("a").get('href');
    });

    /*if($defined(window.returnLink)) {
        var url_vars = getUrlVars();
        if(url_vars['admin_return']) {
            var el = new Element("div", {'class': 'displayMessage'});
            el.setOpacity(0);
            el.set('html', "Your action has been completed.");

            var a1 = new Element("a", {'href': window.returnLink});
            a1.set('html', 'Return to Admin Area');
            a1.inject(el);

            var a2 = new Element("a", {'href': '#'});
            a2.addEvent("click", function(ev) { 
                new Event(ev).stop( );
                this.getParent("div").tween('opacity', 0);
            });
            a2.set('html', 'Close This Window');
            a2.inject(el);           

            (function( ) {
                var pos = window.getScroll( );
                el.inject(document.body);
                el.setStyle("top", 8);
                el.setStyle("right", 8);
                el.tween('opacity', 1);
            }).delay(50);
        }
    }*/
});

var cRequests = {};

function updateComment(textArea, link, editor, input) {
    var url = link.get('href');

    if(cRequests[url]) cRequests[url].cancel();
    cRequests[url] = new Request.HTML({
        'url': url,
        'onComplete': function(resp, txt, html) {
            var container = textArea.getParent( );
            container.getElement("textarea").destroy();
            container.getElement("span").destroy();
            container.getElement("p").destroy();
            var div = new Element("div", {'class': 'cBody'});
            div.set('html', html);
            div.inject(container, 'top');
            link.getParent("ul").setStyle("display", "");

            if(input.get('name') == 'approve') {
                var parent = link.getParent("div").getParent("div").getParent("div");
                var divs = parent.getElements("div");
                divs[0].removeClass("content_callout_box_left1");
                divs[0].addClass("content_callout_box_left");
                divs[1].removeClass("content_callout_box_right1");
                divs[1].addClass("content_callout_box_right");
            } else if(input.get('name') == 'unapprove') {
                var parent = link.getParent("div").getParent("div").getParent("div");
                var divs = parent.getElements("div");
                divs[0].removeClass("content_callout_box_left");
                divs[0].addClass("content_callout_box_left1");
                divs[1].removeClass("content_callout_box_right");
                divs[1].addClass("content_callout_box_right1");
            }
        }
    });
    cRequests[url].post({'body': editor.document.getBody( ).getHtml( ), 
        'command': input.get('value')});

}

