/* Simple Accordion Script 
 * Requires Prototype and Script.aculo.us Libraries
 * By: Brian Crescimanno <brian.crescimanno@gmail.com>
 * http://briancrescimanno.com
 * This work is licensed under the Creative Commons Attribution-Share Alike 3.0
 * http://creativecommons.org/licenses/by-sa/3.0/us/
 */

if (typeof Effect == 'undefined')
  throw("You must have the script.aculo.us library to use this accordion");

var Accordion = Class.create({

    initialize: function(id, defaultExpandedCount) {
        if(!$(id)) throw("Attempted to initalize accordion with id: "+ id + " which was not found.");
        this.accordion = $(id);
        this.options = {
            toggleClass: "accordion-toggle",
            toggleActive: "accordion-toggle-active",
            contentClass: "accordion-content",
			menuitemClass: "menuitem"
        }
		this.subMenu = $("submenu").select('a.menuitem');
        this.contents = this.accordion.select('div.'+this.options.contentClass);
        this.isAnimating = false;
        this.maxHeight = 465-(this.contents.length*25);
        this.current = defaultExpandedCount ? this.contents[defaultExpandedCount-1] : this.contents[0];
		this.currentMenuItem = defaultExpandedCount ? this.subMenu[defaultExpandedCount-1] : this.subMenu[0];
        this.toExpand = null;
		
		if(this.currentMenuItem != null) {	
			this.currentMenuItem.setStyle({textDecoration: "underline"});
		}
        this.checkMaxHeight();
        this.initialHide();
        this.attachInitialMaxHeight();
        this.maxPhotoPages = 1;
        this.showpage = 0;
		this.playPhotos();
		
        var clickHandler =  this.clickHandler.bindAsEventListener(this);
        this.accordion.observe('click', clickHandler);
		  $("submenu").observe('click', clickHandler);
		  $("left").observe('click', clickHandler);
    },

    expand: function(el) {
        this.toExpand = el.next('div.'+this.options.contentClass);
        if(this.current != this.toExpand){
			this.toExpand.show();
				this.animate();
        }
    },

    checkMaxHeight: function() {
        for(var i=0; i<this.contents.length; i++) {
            if(this.contents[i].getHeight() > this.maxHeight) {
                this.maxHeight = this.contents[i].getHeight();
            }
        }
    },

    attachInitialMaxHeight: function() {
		this.current.previous('div.'+this.options.toggleClass).addClassName(this.options.toggleActive);
        if(this.current.getHeight() != this.maxHeight) this.current.setStyle({height: this.maxHeight+"px"});
    },

    clickHandler: function(e) {
        var el = e.element();
        
        if(!this.isAnimating) {
        	if(el.hasClassName("button_play")){
        		this.playPhotos();
        	}
        	if(el.hasClassName("button_stop")){
        		clearTimeout(this.photoplay);
        	}
        	if(el.hasClassName("button_next")){
        		clearTimeout(this.photoplay);
        		this.nextPhoto();
        	}
        	if(el.hasClassName("button_previous")){
        		clearTimeout(this.photoplay);
        		if (this.showpage > 1)
        		{
        			this.showpage--;
        			this.updatePhoto();
        		}
        	}
        	if(el.hasClassName("button_all")){
        		clearTimeout(this.photoplay);
        		this.showAllPhotos();
        	}
        	if(el.hasClassName("button_one")){
        		clearTimeout(this.photoplay);
        		this.updatePhoto();
        	}
        }
		  
		  if(el.hasClassName("menuitem") && !this.isAnimating) {
				for(var i=0; i<this.subMenu.length; i++){
					if(this.subMenu[i] == el) {
						 el = this.contents[i].previous('div.'+this.options.toggleClass);
					}
				}
				clearTimeout(this.photoplay);
				this.maxPhotoPages = 1;
        		this.showpage = 0;
				this.expand(el);
        } 
		  
        if(el.hasClassName(this.options.toggleClass) && !this.isAnimating) {
        	clearTimeout(this.photoplay);
        	this.maxPhotoPages = 1;
        	this.showpage = 0;
            this.expand(el);
        }
		 
    },

    initialHide: function(){
        for(var i=0; i<this.contents.length; i++){
            if(this.contents[i] != this.current) {
                this.contents[i].hide();
                this.contents[i].setStyle({height: 0});
            }
        }
    },
	 
	 underLine: function(){
		if (undefined != this.subMenu[0]) {  
		  for(var i=0; i<this.contents.length; i++){
            if(this.contents[i] == this.current) {
					 this.subMenu[i].setStyle({textDecoration: "underline"});
					 this.currentMenuItem.setStyle({textDecoration: "none"});
					 this.currentMenuItem = this.subMenu[i];
            }
        }
      }
    },
	
	playPhotos: function(){
		clearTimeout(this.photoplay);
		if (this.showpage == 0)
		{
			this.showpage = 1;
			this.updatePhoto();
		}
		else if (this.showpage == this.maxPhotoPages)
		{
			if (this.maxPhotoPages != 1)
			{
				this.showpage = 1;
				this.updatePhoto();
			}
		}
		else
		{
			this.nextPhoto();
		}
		
		this.photoplay = setTimeout("accordion.playPhotos()", 2500);
    },
    nextPhoto: function(){
		if (this.showpage <  this.maxPhotoPages )
   		{
   			this.showpage++;
   			this.updatePhoto();
   		}
		
    },
	
	updatePhoto: function(){
		new Ajax.Updater('content', '/index.php/content/photo?id='
			+this.current.previous('div.'+this.options.toggleClass).readAttribute('title')
			+'&page='+this.showpage, {
  		asynchronous:true, 
  		evalScripts:true }
		);
		//return false;
    },
    
    showAllPhotos: function(){
		new Ajax.Updater('content', '/index.php/content/allPhotos?id='
			+this.current.previous('div.'+this.options.toggleClass).readAttribute('title'), {
  		asynchronous:true, 
  		evalScripts:true }
		);
		//return false;
    },
    
    linkClick: function(number){
        this.showpage = number-1;
        this.playPhotos();
    },
    
    animate: function() {
        var effects = new Array();
        var options = {
            sync: true,
            scaleFrom: 0,
            scaleContent: false,
            transition: Effect.Transitions.sinoidal,
            scaleMode: {
                originalHeight: this.maxHeight,
                originalWidth: this.accordion.getWidth()
            },
            scaleX: false,
            scaleY: true
        };

        effects.push(new Effect.Scale(this.toExpand, 100, options));

        options = {
            sync: true,
            scaleContent: false,
            transition: Effect.Transitions.sinoidal,
            scaleX: false,
            scaleY: true
        };

        effects.push(new Effect.Scale(this.current, 0, options));

        var myDuration = 0.25;

        new Effect.Parallel(effects, {
            duration: myDuration,
            fps: 35,
            queue: {
                position: 'end',
                scope: 'accordion'
            },
            beforeStart: function() {
                this.isAnimating = true;
                this.current.previous('div.'+this.options.toggleClass).removeClassName(this.options.toggleActive);
                this.toExpand.previous('div.'+this.options.toggleClass).addClassName(this.options.toggleActive);
            }.bind(this),
            afterFinish: function() {
                this.current.hide();
                this.toExpand.setStyle({ height: this.maxHeight+"px" });
					 this.current = this.toExpand;
					 this.underLine();
					 this.playPhotos();
                this.isAnimating = false;
            }.bind(this)
        });
    }

});

document.observe("dom:loaded", function(){
    accordion = new Accordion("test-accordion", 1);
})