 // accordion.js v2.0
//
// Copyright (c) 2007 stickmanlabs
// Author: Kevin P Miller | http://www.stickmanlabs.com
// 
// Accordion is freely distributable under the terms of an MIT-style license.
//
// I don't care what you think about the file size...
//   Be a pro: 
//	    http://www.thinkvitamin.com/features/webapps/serving-javascript-fast
//      http://rakaz.nl/item/make_your_pages_load_faster_by_combining_and_compressing_javascript_and_css_files
//

/*-----------------------------------------------------------------------------------------------*/
var totalacc = 0;
if (typeof Effect == 'undefined') 
	throw("accordion.js requires including script.aculo.us' effects.js library!");

var accordion = Class.create();
accordion.prototype = {

    //
    //  Setup the Variables
    //
    showAccordion: null,
    currentAccordion: null,
    duration: null,
    effects: [],
    animating: false,

    //  
    //  Initialize the accordions
    //
    initialize: function (container, options) {
        if (!$(container)) {
            throw (container + " doesn't exist!");
            return false;
        }

        this.options = Object.extend({
            resizeSpeed: 8,
            classNames: {
                toggle: 'accordion_toggle',
                toggleActive: 'accordion_toggle_active',
                toggleAfter: 'according_toggle_after',
                content: 'accordion_content'
            },
            defaultSize: {
                height: null,
                width: null
            },
            direction: 'vertical',
            onEvent: 'click',
            container: container
        }, options || {});

        this.duration = ((11 - this.options.resizeSpeed) * 0.15);

        this.tOut = 0;
        this.container = container;
        var accordions = $$('#' + container + ' .' + this.options.classNames.toggle);
        accordions.each(function (accordion) {
            Event.observe(accordion, this.options.onEvent, this.activate.bind(this, accordion), false);
            if (this.options.onEvent == 'click') {
                accordion.onclick = function () { return false; };
            }

            if (this.options.direction == 'horizontal') {
                var options = $H({ width: '0px', height: this.options.defaultSize.height ? this.options.defaultSize.height + 'px' : 'auto' });
            } else {
                var options = $H({ height: '0px' });
            }
            options.merge({ display: 'none' });
            totalacc += 1;
            this.currentAccordion = $(accordion.next(0)).setStyle(options);
        } .bind(this));
        this.accordions = accordions
        //        alert(totalacc);

    },


    //
    //  Activate an accordion
    //
    activate: function (accordion) {
        if (this.animating) {
            return false;
        }

        this.effects = [];

        this.currentAccordion = $(accordion.next(0));


        this.currentAccordion.setStyle({
            display: 'block'
        });

        this.currentAccordion.previous(0).addClassName(this.options.classNames.toggleActive);

        if (this.options.direction == 'horizontal') {
            this.scaling = $H({
                scaleX: true,
                scaleY: false
            });
        } else {
            this.scaling = $H({
                scaleX: false,
                scaleY: true
            });
        }

        if (this.currentAccordion == this.showAccordion) {
            //this.deactivate();
        } else {

            this._handleAccordion();


        }

    },


    // 
    // Deactivate an active accordion
    //
    deactivate: function () {
        var options = $H({
            duration: this.duration,
            scaleContent: false,
            transition: Effect.Transitions.sinoidal,
            queue: {
                position: 'end',
                scope: 'accordionAnimation'
            },
            scaleMode: {
                originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.scrollHeight,
                originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
            },
            afterFinish: function () {
                this.showAccordion.setStyle({
                    height: 'auto',
                    display: 'none'
                });
                this.showAccordion = null;
                this.animating = false;
            } .bind(this)
        });
        options.merge(this.scaling);
        alert("a");
        this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);

        new Effect.Scale(this.showAccordion, 0, options);
    },

    //
    // Handle the open/close actions of the accordion
    //
    _handleAccordion: function () {
        var options = $H({
            sync: true,
            scaleFrom: 0,
            scaleContent: false,
            transition: Effect.Transitions.sinoidal,
            scaleMode: {
                originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.scrollHeight,
                originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
            }
        });
        options.merge(this.scaling);
        makeAllInvisible();
        this.effects.push(
			new Effect.Scale(this.currentAccordion, 100, options)
		);

        if (this.showAccordion) {
            this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
            //  alert(this.currentAccordion);
            var _actId = $(this.currentAccordion.previous(0)).readAttribute('id');

            // makeInvisible(this.showAccordion, _actId);
            options = $H({
                sync: true,
                scaleContent: false,
                transition: Effect.Transitions.sinoidal
            });
            options.merge(this.scaling);

            this.effects.push(
				new Effect.Scale(this.showAccordion, 0, options)
			);

        }

        new Effect.Parallel(this.effects, {
            duration: this.duration,
            queue: {
                position: 'end',
                scope: 'accordionAnimation'
            },
            beforeStart: function () {
                this.animating = true;
            } .bind(this),
            afterFinish: function () {
                if (this.showAccordion) {
                    this.showAccordion.setStyle({
                        display: 'none'
                    });
                }
                $(this.currentAccordion).setStyle({
                    height: this.options.defaultSize.height ? this.options.defaultSize.height + 'px' : 'auto'
                });
                var _actId = $(this.currentAccordion.previous(0)).readAttribute('id');
                // alert(this.currentAccordion);
                //alert(_actId.substring(1));
                var current = this.currentAccordion;
                makeVisible(_actId.substring(1));
                makeInvisible(_actId.substring(1));
                var _className = 'toggle_after';
                var accordions = $$('#' + this.options.container + ' .' + this.options.classNames.toggle);
                var prev = false;


                accordions.each(function (a, index) {

                    if (prev) {
                        $(a).addClassName(_className);


                    } else {
                        $(a).removeClassName(_className);
                        if ($(a).readAttribute('id') == _actId) {
                            //  alert(index + 1);
                            //alert(current);

                            prev = true;
                        }
                    }
                });
                this.showAccordion = this.currentAccordion;
                this.animating = false;
            } .bind(this)
        });

        // timer
        if (this.tOut != 0) {
            clearTimeout(this.tOut);
        }
        var t = this
        this.tOut = setTimeout(function () {
            var newIndex = false;
            t.accordions.each(function (accordion, index) {
                var curAccordion = $(accordion.next(0));
                if (curAccordion == t.currentAccordion) {
                    newIndex = index - 1;
                    if (newIndex < 0) {
                        newIndex = t.accordions.length - 1;
                    }
                }
            });
            t.activate($$('#' + t.container + ' .' + t.options.classNames.toggle)[newIndex]);
        }, 10000);

    }
}

function makeInvisible(index) {
    try {
        var x = 1;
        while (x < totalacc + 1) {
           
            if (x != index) {
                //alert(x);
                var mydiv = $(document.getElementById('t' + x));

                mydiv.style.display = "none";

                var mydiv2 = $(document.getElementById('mo' + x));

                mydiv2.style.display = "none";
            }
            x++;
        }
    }
    catch (err) {
        txt = "There was an error on this page.\n\n";
        txt += "Error description: " + err.description + "\n\n";
        txt += "Click OK to continue.\n\n";
        alert(txt);

    }
    //alert($(showAccordion.previous(0)).readAttribute('id'));
//    var pevItem = (_actId.substring(1) - 1);
//    var txtContent = '.textcontenta' + pevItem;
//    
//    var mydiv = $(showAccordion);
//    var kids = mydiv.childNodes;
//    //alert(_actId);
//    for (var i = 0; i < kids.length; i++) {
//        
//        if (kids[i].className == "textcontenta" + pevItem) {
//            //alert(kids[i].className);
//            var mydiv2 = $(kids[i]);
//            mydiv2.style.display = "none";
//        }
//        if (kids[i].className == "morelink" + pevItem) {
//            // alert(kids[i].className);
//            var mydiv2 = $(kids[i]);
//            mydiv2.style.display = "none";
//        }
//        
    //    }


}



function makeAllInvisible() {
    try {
        var x = 1;
        while (x < totalacc + 1) {


            //alert(x);
            var mydiv = $(document.getElementById('t' + x));

            mydiv.style.display = "none";

            var mydiv2 = $(document.getElementById('mo' + x));

            mydiv2.style.display = "none";

            x++;
        }
    }
    catch (err) {
        txt = "There was an error on this page.\n\n";
        txt += "Error description: " + err.description + "\n\n";
        txt += "Click OK to continue.\n\n";
        alert(txt);

    }
    //alert($(showAccordion.previous(0)).readAttribute('id'));
    //    var pevItem = (_actId.substring(1) - 1);
    //    var txtContent = '.textcontenta' + pevItem;
    //    
    //    var mydiv = $(showAccordion);
    //    var kids = mydiv.childNodes;
    //    //alert(_actId);
    //    for (var i = 0; i < kids.length; i++) {
    //        
    //        if (kids[i].className == "textcontenta" + pevItem) {
    //            //alert(kids[i].className);
    //            var mydiv2 = $(kids[i]);
    //            mydiv2.style.display = "none";
    //        }
    //        if (kids[i].className == "morelink" + pevItem) {
    //            // alert(kids[i].className);
    //            var mydiv2 = $(kids[i]);
    //            mydiv2.style.display = "none";
    //        }
    //        
    //    }


}



function makeVisible(index) {
    
    var mydiv = $(document.getElementById('t' + index));
    
    mydiv.style.display = "block";
    
    var mydiv2 = $(document.getElementById('mo' + index));
    
    mydiv2.style.display = "block";
    

//    var kids = mydiv.childNodes;
    
//        for (var i = 0; i < kids.length; i++) {
//            alert(kids[i]);
//            if (kids[i].className == "textcontenta" + index) {
//                alert(kids[i].className);
//                var mydiv2 = $(kids[i]);
//                mydiv2.style.display = "none";
//            }
//            if (kids[i].className == "morelink" + index) {
//                // alert(kids[i].className);
//                var mydiv2 = $(kids[i]);
//                mydiv2.style.display = "none";
//            }
//        }
//    //alert($(currentAccordion.previous(0)).readAttribute('id'));
//    var div = document.getElementsByClassName('textcontenta' + index);
//    $(div).style.display = "none"; 
//    alert(mydiv);
//    var div2 = document.getElementsByClassName('morelink' + index);
//    var mydiv2 = $(div2);
//    alert(mydiv2);
    
    //mydiv.style.display = "none";
    //alert("a");
   // div2.style.display = "block";

    
}
