// JavaScript Document

var gg_releases = new Class(
    {
        toggles:[], elements:[], container:null, accordion:null, lyricAccordion:null, lyricContent:null, currentLyric:null, myScroll:null,

        request_lyric: function (uid) {
//debugger;

            var parent = this;

            var myRequest = new Request({method:'get', url:'/?eID=releases&action=lyrics&id=' + uid,
                onSuccess: function(response) {
                    parent.injectLyric(response);
                },
                onFailure: function(xhr) {
                    html = 'The request to ' + targetURL + ' failed. ';
                    parent.injectLyric(html);
                }
            }).send();

        },

        injectLyric: function(response) {

            if (this.lyricContent) {
                //just replace the html content
                this.lyricContent.set('html', response);
            } else {
                //make new toggle and content element, inject into DOM, add to accordion

                this.lyricContent = new Element('div', {'class':'release_content','html':response})

                // create toggler
                toggler = new Element('li').adopt(
                    new Element('a', {
                        'class': 'act',
                        'html': 'Lyrics'
                    })
                );

                // create content
                var content = new Element('div', {
                    'id': 'release_text_lyrics',
                    'class': 'conteina'
                }).adopt(
                    new Element('h3', {'class': 'release_head toggla'}).adopt(
                        new Element('span', {'class':'hidden','html':'Lyrics'})
                    ));

                content.adopt(new Element('div', {
                    'class': 'release_content_container'
                }).adopt(this.lyricContent)
                );


                toggler.inject($('release_view').getElement('ul'));
                content.inject($('accordeonContainer'));

                // add the section to our myAccordion using the addSection method
                this.accordion.addSection(toggler, content);
            }

            this.accordion.display(this.accordion.elements.length - 1);
        },

        showLyric: function(pos, useFx) {
            this.lyricAccordion.display(pos, useFx);
        },

        loadLyric: function(uid) {
            this.accordion.display(this.accordion.elements.length - 1);
            return this.showLyric(mediaMyTempArray.indexOf(uid), true);
            //fix it quick snap
            if (this.currentLyric != uid && uid > 0) {
                this.currentLyric = uid;
                this.request_lyric(uid);
            } else {
                this.accordion.display(this.accordion.elements.length - 1);
                //return false;
            }
        },

        initialize: function() {
            try {
                if (mediaMyTempArray && (linkToPlaylist = $('mediaShowPlaylist'))) {
                    lyricContainer = $('release_text_lyrics').getElement('.release_content');

                    linkToLyricMenu = new Element('h3', {'id':'mediaShowLyricMenu'}).inject(linkToPlaylist, 'after');
                    linkToLyricMenuNext = linkToLyricMenu.getNext();

                    linkToScrollToTop = new Element('h3', {'id':'mediaScrollToTop'}).inject(linkToLyricMenu, 'after');
                    linkToScrollToTop.addEvent('click', function(event) {
                        scrollBody.scrollIntoView($('navi'));
                    });

                    linkToLyricMenu.addEvent('click', function(event) {
                        this.fade('in');
                        scrollBody.scrollIntoView(this);
                    }.bindWithEvent(linkToLyricMenuNext));
                    linkToPlaylist.addEvent('click', function(event) {
                        this.accordion.display(0);
                        scrollBody.toElement(this.container);
                    }.bindWithEvent(this));
                }
                ;
            }
            catch(err) {
                //@TODO handle errors here
            }


            this.lyricAccordion = new Accordion($$('.lToggler'), $$('.lContent'), {
                options: {
                    initialDisplayFx: false,
                    returnHeightToAuto: true,
                    alwaysHide: true,
                    height: false,
                    show: true
                },

                onActive: function(toggler, element) {
                    toggler.addClass('act');
                    toggler.getParent().fade('hide');

                    //@TODO make this work for lowRes monitors!
                    //scrollBody.scrollIntoView(element.getParent());
                    //@Possible Fix:
                    scrollBody.scrollIntoView($('scrollToMe'));
                },
                onBackground: function(toggler, element) {
                    toggler.removeClass('act');
                }
            });

            this.toggles = $$('.releaseTop ul li.toggler');
            this.links = $$('.releaseTop ul li a');

            this.links.each(function(el, index) {
                el.erase('href');
            });

            this.elements = $$('.conteina');
            this.elements.setStyle('position', 'absolute');

            this.container = $('accordeonContainer');

            this.container.setStyle('height', 709);

            $$('.release_content_container').setStyle('height', 669);

            this.accordion = new Accordion(this.toggles, this.elements, {
                initialDisplayFx : false,
                returnHeightToAuto: false,
                fixedHeight: '666px',
                opacity: 1,
                height: false,

                onActive: function(toggler, element) {

                    toggler.getElement('a').addClass('act');
                    element.getElement('.release_content').setStyle('height', '100%');

                },
                onBackground: function(toggler, element) {

                    toggler.getElement('a').removeClass('act');

                }
            });
        }
    })


window.addEvent('domready', function() {

    scrollBody = new Fx.Scroll($(document.body)).toTop();

    releases = new gg_releases;

//releases.loadLyric(16);
});

