if (!Ascribe) {
	var Ascribe = {};
}

if (typeof(Ascribe.Accordion) == 'undefined') {
	Ascribe.Accordion = function(config) {
		this.currentlyOpen;
		this.handlePosition;
		this.toggleInProgress;
		this.toggleInProgress2;
		
		// set first project visible if option was passed
		if (config.firstProjectVisible) {
	    this.projectElements = $$('.project-preview-container');
		  this.projectElements[0].style.display = 'block';
		  this.firstProjectID = this.projectElements[0].id;
		  this.currentlyOpen = this.firstProjectID;
		}
		
		// set styles
		$('portfolio-scroller-track').style.visibility = 'hidden';
		
		// options
		if (typeof(config) != 'undefined' && config.tables) {
		  document.observe("dom:loaded", function() {
  		  this.slider1 = new Control.Slider('portfolio-scroller-handle', 'portfolio-scroller-track', {
        	axis: 'vertical',
        	onSlide: function(v) { this.scrollVertical(v, $('portfolio-scroller'), this.slider1); }.bind(this),
        	onChange: function(v) { this.scrollVertical(v, $('portfolio-scroller'), this.slider1); this.handlePosition = v; }.bind(this)
        });
  		}.bind(this));
	  }
	  else {
	    this.slider1 = new Control.Slider('portfolio-scroller-handle', 'portfolio-scroller-track', {
      	axis: 'vertical',
      	onSlide: function(v) { this.scrollVertical(v, $('portfolio-scroller'), this.slider1); }.bind(this),
      	onChange: function(v) { this.scrollVertical(v, $('portfolio-scroller'), this.slider1); this.handlePosition = v; }.bind(this)
      });
	  }
	  
	  // set view/hide buttons
	  $$('.project-toggle').each(function(e) {
	    el = e.id;
	    var childEl = e.childNodes[0];
	    new Effect.Opacity(childEl, {
	      from: 1,
	      to: 0,
	      duration: 0
	    });
	    var fadeEffect;
	    Event.observe(el, 'mouseover', function() {
	      fadeEffect = new Effect.Opacity(childEl.id, {
          from: 0,
          to: 1,
          duration: .3
        });
	    });
	    Event.observe(el, 'mouseout', function() {
	      fadeEffect.cancel();
	      fadeEffect = new Effect.Opacity(childEl.id, {
          from: .5,
          to: 0,
          duration: 0
        });
	    });
	  });
    
    // events
    Event.observe(window, 'load', this.sliderEnableDisable.bind(this));
	} // constructor
}

Ascribe.Accordion.prototype = {
  projectSlideDown: function(id) {
    if (!this.toggleInProgress) {
      Effect.SlideDown(id, { 
        duration: .5,
        beforeStart: function() { 
					this.toggleInProgress = true; 
				}.bind(this),
        /*beforeUpdate: function() { 
					console.log('test') 
				},*/
        afterFinish: function() { 
					//scrollVertical(this.handlePosition, $('portfolio-scroller'), this.slider1);
					this.toggleInProgress = false;
					this.currentlyOpen = id;
					this.sliderEnableDisable();
				}.bind(this)
      });
    }
	},
	
  projectSlideUp: function(id, current) { 
    if (!this.toggleInProgress2) {
      Effect.SlideUp(current, {
        duration: .5,
        beforeStart: function() { 
					this.toggleInProgress2 = true; 
				}.bind(this),
        afterFinish: function() { 
					//scrollVertical(this.handlePosition, $('portfolio-scroller'), this.slider1);
					this.toggleInProgress2 = false;
					if (id != current) {
						this.currentlyOpen = id;
					}
					else {
						this.currentlyOpen = null; // unset currentlyOpen
					}
					this.sliderEnableDisable();
				}.bind(this)
      });
    }
  },
  
  //this.firstProjectID = this.projectElements[0].id;
  //this.firstProjectVisible = true;
  
	projectToggle: function(id) {
		if (this.currentlyOpen != id) {
			this.projectSlideDown(id);
			//console.log('slideDown: id = ' + id + ' | currentlyOpen = ' + this.currentlyOpen);
			// NOTE: console.log() causes some wierd timing issues in IE and FF 2 when using it for debugging on these conditions
		}
		if (this.currentlyOpen == id) {
		  this.projectSlideUp(id, this.currentlyOpen);
			//console.log('slideUp: id = ' + id + ' | currentlyOpen = ' + this.currentlyOpen);
		}
		if (this.currentlyOpen && this.currentlyOpen != id) {
			this.projectSlideUp(id, this.currentlyOpen);
			//console.log('accordion: id = ' + id + ' | currentlyOpen = ' + this.currentlyOpen);
		}
	},
	
	scrollVertical: function(value, element, slider) {
  	element.scrollTop = Math.round(value/slider.maximum*(element.scrollHeight-element.offsetHeight));
  },
  
  sliderEnableDisable: function() {
    if ($('portfolio-scroller').scrollHeight <= $('portfolio-scroller').offsetHeight) {
    	this.slider1.setDisabled();
    	$('portfolio-scroller-track').hide();
    }
    else {
      this.slider1.setEnabled();
      $('portfolio-scroller-track').show();
      $('portfolio-scroller-track').style.visibility = 'visible';
    }
  }
} // Ascribe.Accordion.prototype
