var slidingNavi = new Class({
	Implements: [Options, Events],
	options: {
	  	sliders: [],
		subnaviClass: '.subnavi',
		showInitialSlider: false,
		closedDistance: -290,
		middledistance: 0,
		openDistance: 0,
//		elementToUpdate : 'iframe'
		elementToUpdate : 'contents'
	},
	initialize: function(options){
		  this.setOptions(options);
		  this.addSliders(this.options.sliders);
		  
		  this.contentFx = new Fx.Morph(this.options.elementToUpdate, { link: 'cancel' });

/*		  if(this.options.showInitialSlider != false)	// test to see if we need to isolate  slider 
		  	this.showSlider.bind(this, this.options.showInitialSlider);
		  else
		  	this.equalizeSliders(); // show all of the sliders
*/
	},
	  



	sliders: [],
	firstTime: true,
 	contentFx: {}, // for controlling the fading of the contents window
	slimboxArray : [],



   	// ******************************** 
	// MAIN NAVI STUFF
	// ******************************** 
	// Add sliders to our master array of sliders and assign them behaviours
	addSliders: function(sliders)
	{
	  	$$(sliders).each(function(slider){
				// Add this slider to our master array of sliders
				this.sliders.include($(slider));
				
				// add behaviours to this slide
				slider.addEvent('click', this.showSlider.bind(this,slider));
				
				this.addSubnavi(slider);
			}, this);
	},
	  addSlider: function(slide){	  // singular version of the above function
		  	this.addSliders($splat(slide));
	},
	  
	  
	showSlider: function(target)	// show this slider, retract the others
	{
	  	// cycle through all of our sliders
		$$(this.sliders).each(function(sliderEl){
				// do this if we are on our target slider
				if (sliderEl == target)
				{
					// extend this slider
					this.rollOut(sliderEl);
					
					// show the slider's subnavi
					this.showSubnavi(sliderEl);
				}
				// do this for not our target slider
				else
				{
					// retract this slider
					this.rollIn(sliderEl);
					
					// hide this slider's subnavi
					this.hideSubnavi(sliderEl);
				}
			}, this);

		return false; // disable the click
	  },
	  equalizeSliders: function()	// show all sliders
	  {
	  	$$(this.sliders).each(function(slider){
			this.rollMiddle(slider);
		}, this);
	},



	// ****************
	// modifications to the way things look
	// **************** 
	rollMiddle: function(slider)
	{
		slider.tween('top', this.options.middleDistance);
	},
	rollOut: function(slider)
	{
		slider.tween('top', this.options.openDistance);
	},
	rollIn: function(slider)
	{
		slider.tween('top',this.options.closedDistance);
	},
	  
	  
	  
	  
   	// ******************************** 
	// SUB NAVI STUFF
	// ********************************
	addSubnavi: function(slider)
	{
		// init look of subnavi
		slider.getElements(this.options.subnaviClass).each(function(element){
//						element.set('opacity', 0);
//						element.style.display = 'none';
					}, this);


		// find all the links & define their behaviour
		// do this for all the links
		slider.getElements('a').each(function(element){
						element.addEvent('click', this.showContents.bind(this, element.href));
						element.addEvent('click', this.makeHot.bind(this, element));
					}, this);

		// do this only for the sublinks
		slider.getElements(this.options.subnaviClass + ' a').each(function(element){
//						element.addEvent('click', this.makeHot.bind(this, element));
					}, this);
	},
	showSubnavi: function(slider)
	{
	  		$(slider).getElements(this.options.subnaviClass).each(function(subnavi){
					subnavi.style.display = 'block';
					subnavi.tween('opacity', 1);
				});
	},
	hideSubnavi: function(slider)
	{
			$(slider).getElements(this.options.subnaviClass).each(function(subnavi){
					var slideMorph = new Fx.Morph(subnavi, {}).start({
																	 opacity: 0,
																	 height: 0
																	 });
//					subnavi.set('opacity', 0);
//					subnavi.style.display = 'none';
				});
	},
	makeHot: function(element)
	{
  		// find my siblings & set them to cold
		var siblings = $$(element.getParent().getParent().getElements('li').getElements('a'));
		siblings.getParent().removeClass('selected');
		
		// set me to hot
		if (element.getParent().getParent().hasClass('subnavi'))
			element.getParent().addClass('selected');
	},


	  
	showContents: function(href)
	{
		if($defined($('fixedimg')))
			$('fixedimg').set('opacity', 0);

		if($defined($$('.ajaxlinks')))
		{
			$$('.ajaxlinks').each(function(element){
					var naviMorph = new Fx.Morph(element, {});
					naviMorph.set({'opacity':0});
			   });
		}

		this.contentFx.start(
								{'opacity': 0}
							).chain(function()
											{
//												$('iframe').src = href;
//												window.document.location = href;
												window.document.location.href = href;
											}.bind(this)
						);
	}
});