// JavaScript Document
window.addEvent('load', function(){ 
	
	if($chk($('banner'))) var banner = new ecceSlider({'sliderID': 'banner',
		'effect' : 'fade',
		'wait':7000,
		'stopAnimationOnRollover':false,
		'listButtons':false
	});
	
	if ($chk($('gallery'))) var gallery = new ecceGallery();
	
	if ($chk($('top-navigation'))) var topNav = new topNavDropdown();
	
	if ($chk($('faq') ) ){ var collapseWinelist = new collapsibleContent({ 'holderID' : 'faq' }); }
	
	var toolTips = new ecceToolTip({ 'offset' : 0, 'mouseoverupdate' : false, 'posOnBindTo' : true , 'debug' : true });
	
	//$$(".chzn-select").chosen();
	
});


var ecceGallery = new Class({
	Implements: [Options],
	options: {
		'galleryID'  	: 'gallery',
		'lightboxID' 	: 'lightbox',
		'lightboxTarget': 'product-main-lightbox-js',
		'captionTarget' : 'main-lightbox-caption',
		'images'		: '',
		'folder'		: '/gfx/',
		'spacer'		: 'spacer.gif',
		'loadSpacer'	: false,
		'initialDelay'	: 900,
		'currentFrame'	: 0	//start point
	},
	initialize: function(options) {
		
		this.setOptions(options);
		this.options.images = $$('#'+this.options.galleryID + ' img');
		var o = this.options;
		var c = this;
		c.lightbox =  $( o.lightboxID );
		c.buildLightbox();
		
		//o.images.addStyle('cursor','pointer');
		o.images.each(function(img, i){
			
			img.addEvent('click', function(){
				c.updateLightbox( i );
			});
			var caption = c.cleanCaption( img.get('title') );
			
			//hack to correct any default titles
			
			img.set({ 'title' : 'Click the thumbnail to preview the image above', 'data-caption' : caption }).setStyle('cursor','pointer');
		});
		
	},
	cleanCaption : function( captionStr ){
		if ( typeOf( captionStr) != 'null' ){
				if ( captionStr.substr(-4) == ".jpg" ){
					
					var chopAt = captionStr.length - 4;
					
					return captionStr.substr( 0, chopAt ).split('-').join(" ");//replace(/_-/g," ");
				}
			}
			return captionStr;
	},
	updateLightbox : function( frameMarker ){
		var src 	= this.options.images[ frameMarker ].get('src');
		var caption = this.options.images[ frameMarker ].get('data-caption');
		
		/* - captioning not needed
		var caption = this.options.images[ frameMarker ].get('title');
		if ( typeOf( caption) != 'null' ){
			if ( caption != src.substr() ){
				var captionBox = new Element('div', {'class' : 'lightbox-caption', 'html' : caption }).inject( $(this.options.lightboxTarget) );
			}
		}
		*/
		$(this.options.captionTarget).set( 'html', caption );
		$(this.options.lightboxTarget).set( 'src', src );
		
	},
	buildLightbox : function(){
		var c = this;
		var o = c.options;
		if ( o.loadSpacer == true ){
			 var src =  o.folder + o.spacer;
		}else{
			var src = o.images[ 0 ].get('src');
		}
		var caption = c.cleanCaption( o.images[ 0 ].get('title') );
		
		c.lightbox.setStyle('opacity',0);
		new Element('span', {'id' : o.captionTarget, 'class' : 'caption', 'html' : caption }).inject( c.lightbox );
		var img = new Element('img', { 'src' : src, 'alt' : '','id': o.lightboxTarget }).inject( c.lightbox );
		(function(){ c.lightbox.fade(1); }).delay( o.initialDelay );
		
	}
});

var collapsibleContent = new Class({
	Implements: [Options],
		options: {
			'holderID' 		: 'collapsible',
			'headerEl' 		: 'dt',
			'bodyEl' 		: 'dd',
			'openAtStart' 	: false,
			'delay'			: 500,
			'heightPadding' : 200
		},
		initialize: function(options){
			this.setOptions(options);
			this.headers = $$('#'+ this.options.holderID + ' '+ this.options.headerEl );
			this.bodies	 = $$('#'+ this.options.holderID + ' '+ this.options.bodyEl);
			c = this;
			
			c.bodies.each( function(el){
				el.set('data-height', el.getStyle('height') );
				if (c.options.openAtStart == false){
					el.set('data-state','closed').set('opacity', 0).addClass('collapsedContent');
				}else{
					el.set('data-state','open');
				}
				
			});
			
			
			c.headers.each(function(el, i){
				el.set('title','Click to toggle the content below').setStyle('cursor', 'pointer');
				el.addEvent('click', function(e){
					e.stop();
					var elState= c.bodies[i].get('data-state');
					var h = c.bodies[i].get('data-height');
					//h += c.options.heightPadding;
					
					switch( elState ){
						case 'open' :
						c.bodies[i].fade(0).set('data-state', 'closed');
						(function(){ c.bodies[i].tween('height', 0).addClass('collapsedContent'); }).delay( c.options.delay );
						
						break;
						
						case 'closed':
						default:
						c.bodies[i].tween('height',[0,h]).set('data-state', 'open');
						(function(){ c.bodies[i].fade(1).removeClass('collapsedContent'); }).delay( c.options.delay );
						break;	
					}
				});
			});
		}
});

var lazyLoader = new Class({
	Implements: [Options],
	options: {
		'selector'  		: '#gallery',
		'gfxFolder' 		: '/gfx/',
		'spacer'			: 'spacer.gif'
	},
	initialize: function(options){
		this.setOptions(options);
		var c = this;
		this.imgs = $$( this.options.selector +' img');
		
		this.imgs.each( function( img ){
			c.prepImg( img );
		});
	},
	prepImg : function( img ){
		img.set('data-original-src', img.get('src') );
		img.set('src', this.options.gfxFolder + this.options.spacer);
	},
	lazyLoad: function( img ){
		var src = img.get('data-original-src');
		//fade it in ??
		img.set('src', src);
	}
});

var topNavDropdown = new Class({
		Implements: [Options],
		options: {
			'topNavID' 			: 'top-navigation',
			'dropdownEl' 		: 'ul',
			'buttonClass' 		: 'dropdown-button',
			'linkClass' 		: 'dropdown-link',
			'ddVisibleClass' 	: 'visible'
		},
		initialize: function(options){
			this.setOptions(options);
			
			this.dropdowns 		= $$( '#' + this.options.topNavID + ' '+ this.options.dropdownEl );
			this.triggerButtons = $$( '.' + this.options.buttonClass );
			this.triggerLinks 	= $$( '.' + this.options.linkClass );
			var c = this;
			c.dropdowns.set('opacity',0);
			
			
			c.triggerLinks.each( function( a ){
				a.addEvent('mouseover', function(e){
					li = a.getParent('li');
					dd = li.getChildren( c.options.dropdownEl );
					
					state = li.get('data-dd-state');
					
					if (state != 'open'){
						c.addLinkState( a );
						c.openDropdown( dd[0] );
						c.openListState( li );
					}
					
					li.addEvent('mouseleave', function(){
						
						c.removeLinkState( a );
						c.closeDropdown( dd[0] );
						c.closeListState( li );
						
					});
					
					
					
				});
			});
			
			c.triggerButtons.each( function( button, i ){
				
				button.addEvent('click', function(e){
					e.stop();
					
					li 		= button.getParent('li');
					state 	= li.get('data-dd-state');
					//dd 		= c.dropdowns[i];
					dd = li.getChildren( c.options.dropdownEl );
					
					if (state == 'open'){
						c.closeDropdown( dd[0] );
						c.removeLinkState( button );
						c.closeListState( li );
					}else{
						c.openDropdown( dd[0] );
						c.addLinkState( button );
						c.openListState( li );
					}
				});
			});
		
		},
		addLinkState : function ( el){
			var span = el.getParent('span');
			span.getChildren('a').addClass('selected');
		},
		removeLinkState : function ( el){
			var span = el.getParent('span');
			span.getChildren('a').removeClass('selected');
		},
		openDropdown	: function( el ){
			el.addClass( this.options.ddVisibleClass ).tween('opacity',1);	
		},
		closeDropdown 	: function( el ){
			el.tween('opacity',0).removeClass( this.options.ddVisibleClass ); // - cauisng typeOf error
		},
		openListState : function(el){
			el.set('data-dd-state', 'open');
		},
		closeListState : function(el){
			el.set('data-dd-state', 'closed');
		}
	}); // end topNav class

var ecceSlider = new Class({
	Implements: [Options],
	options: {
		'sliderID': 'slider',
		'effect':'hslide',
		'effectDuration': 500,
		'wait': 5000,
		'transitionEffect':Fx.Transitions.linear,
		'autorun': true,
		'waitbeforereturning': 3000,
		'nextbuttonID':'next',
		'nextbuttonEvent':'click',
		'previousbuttonID':'previous',
		'previousbuttonEvent':'click',
		'stopAnimationOnRollover':false,
		'listButtons':true,
		'listButtonsID':'slider_buttons',
		'listButtonsActiveFunction':function(currentid){
			$(currentid).setStyle('font-weight','bold');
		}
	},
	
	
	initialize: function(options) {
		this.setOptions(options);
		if(this.options.listButtons==true){
			this.assignIDtoSliders();
			this.createButtons();
		}
		this.applySetupStyles();
		this.options.allowButtons = true;
		if(this.options.autorun==true) this.runEffect();
		this.nextbutton();
		this.previousbutton();
		if(this.options.stopAnimationOnRollover==true) this.mouseOver();
		
	},
	assignIDtoSliders: function(){
		var c = this;
		$$('#'+this.options.sliderID+' ul li').each(function(elm,i){
			elm.setProperty('id',c.options.sliderID+'_slider_'+i);
		});
	},
	updateActiveButton :function(sliders,currentid){
		sliders.each(function(elm,i){
			$(elm.getProperty('id')+'_button').removeProperty('style');
		});
		this.options.listButtonsActiveFunction(currentid);
	},
	createButtons: function(){
		var c = this;
		var buttoncontainer = new Element('div', {'id': c.options.listButtonsID}).inject($(c.options.sliderID),'after');
		var buttonlist = new Element('ul').inject(buttoncontainer);
		var updatebutton =0;
		$$('#'+this.options.sliderID+' ul li').each(function(elm,i){
			var list = new Element('li').inject(buttonlist);
			if (i==0) updatebutton = elm.getProperty('id')+'_button';
			var anc = new Element('a',{'href':'#','id':elm.getProperty('id')+'_button','text':(i+1),'events':{
				'click':function(e){
					c.clearTimers();
					e.stop();
					if(c.options.allowButtons){
						switch(c.options.effect){
							case 'fade':
								c.options.runpID = c.fadeUsingID(elm.getProperty('id'));
								if(c.options.autorun){
									var slidefunction = function(){
										c.options.runpID = c.fade.periodical(c.options.effectDuration+c.options.wait,c);
									}
									c.options.runbID = slidefunction.delay(c.options.waitbeforereturning,c);
								}
								break;
							case 'hslide':
								c.options.runpID = c.hslideUsingID(elm.getProperty('id'));
								if(c.options.autorun){
									var slidefunction = function(){
										c.options.runpID = c.hslide.periodical(c.options.effectDuration+c.options.wait,c);
									}
									c.options.runbID = slidefunction.delay(c.options.waitbeforereturning,c);
								}
								break;
							case 'hsliderev':
								c.options.runpID = c.hslideUsingID(elm.getProperty('id'));
								if(c.options.autorun){
									var slidefunction = function(){
										c.options.runpID = c.hslide.periodical(c.options.effectDuration+c.options.wait,c);
									}
									c.options.runbID = slidefunction.delay(c.options.waitbeforereturning,c);
								}
								break;
							case 'vslide':
								c.options.runpID = c.vslideUsingID(elm.getProperty('id'));
								if(c.options.autorun){
									var slidefunction = function(){
										c.options.runpID = c.vslide.periodical(c.options.effectDuration+c.options.wait,c);
									}
									c.options.runbID = slidefunction.delay(c.options.waitbeforereturning,c);
								}
								break;
							case 'vsliderev':
								c.options.runpID = c.vslideUsingID(elm.getProperty('id'));
								if(c.options.autorun){
									var slidefunction = function(){
										c.options.runpID = c.vslide.periodical(c.options.effectDuration+c.options.wait,c);
									}
									c.options.runbID = slidefunction.delay(c.options.waitbeforereturning,c);
								}
								break;
						}
					}
				}
			}}).inject(list);
		});
		c.updateActiveButton($$('#'+this.options.sliderID+' ul li'),updatebutton);	
	},

	
	orderSlides: function(slides,reversed){
		var c = this;
		if(reversed==true){
			var CurrentParts = slides[slides.length-1].getProperty('id').split('_');
			var CurrentID = CurrentParts[CurrentParts.length-1].toInt();
			for(i=CurrentID+1;i<slides.length;i++) $(c.options.sliderID+'_slider_'+i).inject(slides[0].getParent('ul'),'top');
			for(i=0;i<CurrentID;i++) $(c.options.sliderID+'_slider_'+i).inject(slides[0].getParent('ul'),'top');
		}else{
			var CurrentParts = slides[0].getProperty('id').split('_');
			var CurrentID = CurrentParts[CurrentParts.length-1].toInt();
			for(i=CurrentID+1;i<slides.length;i++) $(c.options.sliderID+'_slider_'+i).inject(slides[0].getParent('ul'));
			for(i=0;i<CurrentID;i++) $(c.options.sliderID+'_slider_'+i).inject(slides[0].getParent('ul'));
		}
	},
	
	mouseOver:function(){
		var c = this;
		$(this.options.sliderID).addEvent('mouseover',function(){
			c.clearTimers();
		});
		$(this.options.sliderID).addEvent('mouseout',function(){
			if(c.options.autorun==true) c.runEffect();
		});
	},
	
	clearTimers:function(){
		$clear(this.options.runpID);
		$clear(this.options.runbID);
	},
	previousbutton:function(){
		var c = this;
		if($chk($(c.options.previousbuttonID))){
			$(c.options.previousbuttonID).addEvent(c.options.previousbuttonEvent,function(e){
				c.clearTimers();
				e.stop();
				if(c.options.allowButtons){
					switch(c.options.effect){
						case 'fade':
							c.options.runpID = c.fadeReverse();
							if(c.options.autorun){
								var slidefunction = function(){
									c.options.runpID = c.fade.periodical(c.options.effectDuration+c.options.wait,c);
								}
								c.options.runbID = slidefunction.delay(c.options.waitbeforereturning,c);
							}
							break;
						case 'hslide':
							c.options.runpID = c.hslideReverse();
							if(c.options.autorun){
								var slidefunction = function(){
									c.options.runpID = c.hslide.periodical(c.options.effectDuration+c.options.wait,c);
								}
								c.options.runbID = slidefunction.delay(c.options.waitbeforereturning,c);
							}
							break;
						case 'hsliderev':
							c.options.runpID = c.hslide();
							if(c.options.autorun){
								var slidefunction = function(){
									c.options.runpID = c.hslideReverse.periodical(c.options.effectDuration+c.options.wait,c);
								}
								c.options.runbID = slidefunction.delay(c.options.waitbeforereturning,c);
							}
							break;
						case 'vslide':
							c.options.runpID = c.vslideReverse();
							if(c.options.autorun){
								var slidefunction = function(){
									c.options.runpID = c.vslide.periodical(c.options.effectDuration+c.options.wait,c);
								}
								c.options.runbID = slidefunction.delay(c.options.waitbeforereturning,c);
							}
							break;
						case 'vsliderev':
							c.options.runpID = c.vslide();
							if(c.options.autorun){
								var slidefunction = function(){
									c.options.runpID = c.vslideReverse.periodical(c.options.effectDuration+c.options.wait,c);
								}
								c.options.runbID = slidefunction.delay(c.options.waitbeforereturning,c);
							}
							break;
					}
				}
			});
		}
	},
	nextbutton:function(){
		var c = this;
		if($chk($(c.options.nextbuttonID))){
			$(c.options.nextbuttonID).addEvent(c.options.nextbuttonEvent,function(e){
				c.clearTimers();
				e.stop();
				if(c.options.allowButtons){
					switch(c.options.effect){
						case 'fade':
							c.options.runpID = c.fade();
							if(c.options.autorun){
								var slidefunction = function(){
									c.options.runpID = c.fade.periodical(c.options.effectDuration+c.options.wait,c);
								}
								c.options.runbID = slidefunction.delay(c.options.waitbeforereturning,c);
							}
							break;
						case 'hslide':
							c.options.runpID = c.hslide();
							if(c.options.autorun){
								var slidefunction = function(){
									c.options.runpID = c.hslide.periodical(c.options.effectDuration+c.options.wait,c);
								}
								c.options.runbID = slidefunction.delay(c.options.waitbeforereturning,c);
							}
							break;
						case 'hsliderev':
							c.options.runpID = c.hslideReverse();
							if(c.options.autorun){
								var slidefunction = function(){
									c.options.runpID = c.hslideReverse.periodical(c.options.effectDuration+c.options.wait,c);
								}
								c.options.runbID = slidefunction.delay(c.options.waitbeforereturning,c);
							}
							break;
						case 'vslide':
							c.options.runpID = c.vslide();
							if(c.options.autorun){
								var slidefunction = function(){
									c.options.runpID = c.vslide.periodical(c.options.effectDuration+c.options.wait,c);
								}
								c.options.runbID = slidefunction.delay(c.options.waitbeforereturning,c);
							}
							break;
						case 'vsliderev':
							c.options.runpID = c.vslideReverse();
							if(c.options.autorun){
								var slidefunction = function(){
									c.options.runpID = c.vslideReverse.periodical(c.options.effectDuration+c.options.wait,c);
								}
								c.options.runbID = slidefunction.delay(c.options.waitbeforereturning,c);
							}
							break;
					}
				}
			});
		}
	},
	
	
	runEffect: function(){
		switch(this.options.effect){
			case 'fade':
				this.options.runpID = this.fade.periodical(this.options.effectDuration+this.options.wait,this);
				break;
			case 'hslide':
				this.options.runpID = this.hslide.periodical(this.options.effectDuration+this.options.wait,this);
				break;
			case 'hsliderev':
				this.options.runpID = this.hslideReverse.periodical(this.options.effectDuration+this.options.wait,this);
				break;
			case 'vslide':
				this.options.runpID = this.vslide.periodical(this.options.effectDuration+this.options.wait,this);
				break;
			case 'vsliderev':
				this.options.runpID = this.vslideReverse.periodical(this.options.effectDuration+this.options.wait,this);
				break;
			default:
				alert('ecceSlider cannot run this effect');
		}
	},
	fade:function(){
		var c=this;
		c.options.allowButtons = false;
		var slides = $$('#'+c.options.sliderID+' ul li');
		this.options.slideFX = new Fx.Morph(slides[slides.length-1],{'duration':c.options.effectDuration,'transition':c.options.transitionEffect,'onComplete':function(){
			slides[slides.length-1].inject(slides[slides.length-1].getParent('ul'),'top');
			slides[slides.length-1].setStyle('opacity',1);
			c.options.allowButtons = true;
			var slides2 = $$('#'+c.options.sliderID+' ul li');
			if(c.options.listButtons) c.updateActiveButton(slides2,slides2[slides2.length-1].getProperty('id')+'_button');
		}});
		c.options.slideFX.start({'opacity': 0});
	},
	fadeReverse:function(){
		var c=this;
		c.options.allowButtons = false;
		var slides = $$('#'+c.options.sliderID+' ul li');
		slides[0].setStyle('opacity',0);
		slides[0].inject(slides[0].getParent('ul'));
		this.options.slideFX = new Fx.Morph(slides[0],{'duration':c.options.effectDuration,'transition':c.options.transitionEffect,'onComplete':function(){
			c.options.allowButtons = true;
			var slides2 = $$('#'+c.options.sliderID+' ul li');
			if(c.options.listButtons) c.updateActiveButton(slides2,slides2[slides2.length-1].getProperty('id')+'_button');
		}});
		c.options.slideFX.start({'opacity': 1});
	},
	fadeUsingID:function(ID){
		var c=this;
		c.options.allowButtons = false;
		var slides = $$('#'+c.options.sliderID+' ul li');
		if($(ID).getProperty('id') != slides[slides.length-1].getProperty('id')){
			$(ID).setStyle('opacity',0);
			$(ID).inject(slides[0].getParent('ul'));
			this.options.slideFX = new Fx.Morph($(ID),{'duration':c.options.effectDuration,'transition':c.options.transitionEffect,'onComplete':function(){
				c.options.allowButtons = true;
				var slides2 = $$('#'+c.options.sliderID+' ul li');
				c.orderSlides(slides2,true);
				if(c.options.listButtons) c.updateActiveButton(slides2,slides2[slides2.length-1].getProperty('id')+'_button');
			}});
			c.options.slideFX.start({'opacity': 1});
		}
	},
	
	hslide:function(){
		var c=this;
		c.options.allowButtons = false;
		var slides = $$('#'+c.options.sliderID+' ul li');
		this.options.slideFX = new Fx.Morph(slides[0],{'duration':c.options.effectDuration,'transition':c.options.transitionEffect,'onComplete':function(){
			slides[0].inject(slides[0].getParent('ul'));
			slides[0].setStyle('margin-left',0);
			c.options.allowButtons = true;
			var slides2 = $$('#'+c.options.sliderID+' ul li');
			if(c.options.listButtons) c.updateActiveButton(slides2,slides2[0].getProperty('id')+'_button');
		}});
		c.options.slideFX.start({'margin-left': -slides[0].getSize().x});
	},
	hslideReverse:function(){
		var c=this;
		c.options.allowButtons = false;
		var slides = $$('#'+c.options.sliderID+' ul li');
		slides[slides.length-1].setStyle('margin-left',-slides[slides.length-1].getSize().x);
		slides[slides.length-1].inject(slides[0].getParent('ul'),'top');
		this.options.slideFX = new Fx.Morph(slides[slides.length-1],{'duration':c.options.effectDuration,'transition':c.options.transitionEffect,'onComplete':function(){
			c.options.allowButtons = true;
			var slides2 = $$('#'+c.options.sliderID+' ul li');
			if(c.options.listButtons) c.updateActiveButton(slides2,slides2[0].getProperty('id')+'_button');
		}});
		c.options.slideFX.start({'margin-left':0});
	},
	hslideUsingID:function(ID){
		var c=this;
		var slides = $$('#'+c.options.sliderID+' ul li');
		if($(ID).getProperty('id') != slides[0].getProperty('id')){
			c.options.allowButtons = false;
			$(ID).setStyle('margin-left',-slides[slides.length-1].getSize().x);
			$(ID).inject(slides[0].getParent('ul'),'top');
			this.options.slideFX = new Fx.Morph($(ID),{'duration':c.options.effectDuration,'transition':c.options.transitionEffect,'onComplete':function(){
				c.options.allowButtons = true;
				var slides2 = $$('#'+c.options.sliderID+' ul li');
				c.orderSlides(slides2,false);
				if(c.options.listButtons) c.updateActiveButton(slides2,slides2[0].getProperty('id')+'_button');
			}});
			c.options.slideFX.start({'margin-left':0});
		}
	},
	
	
	
	vslide:function(){
		var c=this;
		c.options.allowButtons = false;
		var slides = $$('#'+c.options.sliderID+' ul li');
		c.options.slideFX = new Fx.Morph(slides[0],{'duration':c.options.effectDuration,'transition':c.options.transitionEffect,'onComplete':function(){
			slides[0].inject(slides[0].getParent('ul'));
			slides[0].setStyle('margin-top',0);
			c.options.allowButtons = true;
			var slides2 = $$('#'+c.options.sliderID+' ul li');
			if(c.options.listButtons) c.updateActiveButton(slides2,slides2[0].getProperty('id')+'_button');
		}});
		c.options.slideFX.start({'margin-top': -slides[0].getSize().y});
	},
	vslideReverse:function(){
		var c=this;
		c.options.allowButtons = false;
		var slides = $$('#'+c.options.sliderID+' ul li');
		slides[slides.length-1].setStyle('margin-top',-slides[slides.length-1].getSize().y);
		slides[slides.length-1].inject(slides[slides.length-1].getParent('ul'),'top');
		c.options.slideFX = new Fx.Morph(slides[slides.length-1],{'duration':c.options.effectDuration,'transition':c.options.transitionEffect,'onComplete':function(){
			c.options.allowButtons = true;
			var slides2 = $$('#'+c.options.sliderID+' ul li');
			if(c.options.listButtons) c.updateActiveButton(slides2,slides2[0].getProperty('id')+'_button');
		}});
		c.options.slideFX.start({'margin-top':0});
	},
	vslideUsingID:function(ID){
		var c=this;
		var slides = $$('#'+c.options.sliderID+' ul li');
		if($(ID).getProperty('id') != slides[0].getProperty('id')){
			c.options.allowButtons = false;
			$(ID).setStyle('margin-top',-slides[slides.length-1].getSize().y);
			$(ID).inject(slides[0].getParent('ul'),'top');
			c.options.slideFX = new Fx.Morph($(ID),{'duration':c.options.effectDuration,'transition':c.options.transitionEffect,'onComplete':function(){
				c.options.allowButtons = true;
				var slides2 = $$('#'+c.options.sliderID+' ul li');
				c.orderSlides(slides2,false);
			if(c.options.listButtons) c.updateActiveButton(slides2,slides2[0].getProperty('id')+'_button');
			}});
			c.options.slideFX.start({'margin-top':0});
		}
	},
	
	
	applySetupStyles: function(){
		if($chk($(this.options.sliderID))){
			var op = this.options;
			var sizes = new Object();
			sizes.x = $(this.options.sliderID).getStyle('width').toInt();
			sizes.y = $(this.options.sliderID).getStyle('height').toInt();
			$(this.options.sliderID).setStyles({'overflow':'hidden','padding':0});
			$$('#'+this.options.sliderID+' ul').each(function(elm){
				elm.setStyles({'padding':0,'margin':0,'height':sizes.y,'width':(sizes.x*3)});
			});
			$$('#'+this.options.sliderID+' ul li').each(function(elm){
				switch(op.effect){
					case 'fade':
						elm.setStyles({'padding':0,'margin':0,'height':sizes.y,'width':sizes.x,'list-style':'none','position':'absolute'});
						break;
					case 'hslide':
						elm.setStyles({'padding':0,'margin':0,'height':sizes.y,'width':sizes.x,'list-style':'none','float':'left'});
						break;
					case 'hsliderev':
						elm.setStyles({'padding':0,'margin':0,'height':sizes.y,'width':sizes.x,'list-style':'none','float':'left'});
						break;
					case 'vslide':
						elm.setStyles({'padding':0,'margin':0,'height':sizes.y,'width':sizes.x,'list-style':'none'});
						break;
					case 'vsliderev':
						elm.setStyles({'padding':0,'margin':0,'height':sizes.y,'width':sizes.x,'list-style':'none'});
						break;
					default:
						alert('ecceSlider cannot run this effect');
				}
			});
			if(op.effect=='fade'){
				var	slides = $$('#'+this.options.sliderID+' ul li');
				for(i=slides.length-2;i>=0;i--){
					slides[i].inject(slides[i].getParent('ul'));	
				}
			}
			if(op.effect=='hsliderev' || op.effect=='vsliderev'){
				var	slides = $$('#'+this.options.sliderID+' ul li');
				for(i=slides.length-2;i>=1;i--){
					slides[i].inject(slides[i].getParent('ul'));	
				}
			}
		}
	}
});

//remade version based on TipTip for jQuery
var tipTip = new Class({
	Implements: [Options],
	options: {
		'bindTo'			: '.tooltip',
		'activation' 		: 'hover',
		'keepAlive'			: false,
		'maxWidth'			: '200px',
		'edgeOffset'		: 3,
		'defaultPosition'	: 'bottom',
		'delay'				: 400,
		'fadeIn'			: 200,
		'fadeOut'			: 200,
		'opacity'			: 1,
		'attr'				: 'title',
		'content'			: false,	//html or string to use as content
		'enter'				: false,	//mouseover callback function
		'exit'				: false 	//mouseout callback function
	},
	initialize: function(options) {
		this.setOptions(options);
		var c 		= this;
		var op 		= c.options;
		c.toolTips 	= $$(bindTo);
		c.createHolder();
		c.toolTips.each(function( el ){
			if ( op.activation == 'hover'){
				el.addEvents({ 
					'mouseover' : function(e){
					c.populateTip( el );
					},
					'mouseleave': function(){
						c.depopulateTip();
					}
				});
			}else if ( op.activation == 'focus'){
				el.addEvents({
					'focus' : function(e){
					c.populateTip( el );	
					},
					'blur' : function(){
					c.depopulateTip();	
					}
				});
			}else if ( op.activation == 'click'){
				el.addEvent({ 
					'click' : function(e){
					e.stop();
					c.populateTip( el );
					}
					,
					'mouseleave': function(){
					c.depopulateTip();
					}
				});
			} //end activation validation
			
		}); //end toolTips each
	},
	'populateTip' : function( el ){
		var c 		= this;
		var op 		= c.options;
		c.tipHolder.fade(0); //hide the holder if present
		var content = ( typeOf(op.content) == 'string' && op.content.length > 0 )? op.content : el.get(op.attr); //if we have content explicity set
		if ( content.length > 0 ){
			el.set({
				'data-tipContent' 	: content, //do we need this?
				'title'				: '' //empty the title out!
			});	
			var timeout = false; //explicitly set to false for timings
			
			c.tipContent.set('html', content);
			c.tipPointer.set('style',''); //clear any styles for the pointer!
			
			//Position calculations! - taken from tiptip jQuery
			var elPosition 	= el.getPosition();
			var elSize		= el.getSize();
			var tipSize		= this.tipHolder.getSize();
			//var top = parseInt(org_elem.offset()['top']);
			//var left = parseInt(org_elem.offset()['left']);
						//var elSize.x = parseInt(org_elem.outerWidth());
						//var elSize.y = parseInt(org_elem.outerHeight());
						//var tip_w = tiptipSize.yolder.outerWidth();
						//var tipSize.y = tiptipSize.yolder.outerHeight();
			var w_compare 	= Math.round(( elSize.x - tipSize.x) / 2);
			var h_compare 	= Math.round(( elSize.y - tipSize.y) / 2);
			var marg_left 	= Math.round( elPosition.x + w_compare );
			var marg_top  	= Math.round(	elPosition.y + elSize.y + op.edgeOffset);
			var t_class 	= "";
			var arrow_top 	= "";
			var arrow_left 	= Math.round(tipSize.x - 12) / 2;
	
			if(op.defaultPosition == "bottom"){
				t_class = "_bottom";
			} else if(op.defaultPosition == "top"){ 
				t_class = "_top";
			} else if(op.defaultPosition == "left"){
				t_class = "_left";
			} else if(op.defaultPosition == "right"){
				t_class = "_right";
			}
						
			var right_compare 	= (w_compare + elPosition.x ) < parseInt($(window).getScrollSize().x );
			var left_compare 	= (tipSize.x + elPosition.x) > parseInt($(window).getSize().x);
						
						if((right_compare && w_compare < 0) || (t_class == "_right" && !left_compare) || (t_class == "_left" && elPosition.x < (tipSize.x + op.edgeOffset + 5))){
							t_class 	= "_right";
							arrow_top 	= Math.round(tipSize.y - 13) / 2;
							arrow_left 	= -12;
							marg_left 	= Math.round(left + elSize.x + op.edgeOffset);
							marg_top 	= Math.round(top + h_compare);
						} else if((left_compare && w_compare < 0) || (t_class == "_left" && !right_compare)){
							t_class 	= "_left";
							arrow_top 	= Math.round(tipSize.y - 13) / 2;
							arrow_left 	=  Math.round(tip_w);
							marg_left 	= Math.round(elPosition.x - (tip_w + op.edgeOffset + 5));
							marg_top 	= Math.round(elPosition.y + h_compare);
						}
	
						var top_compare = (elPosition.x + elSize.y + op.edgeOffset + tipSize.y + 8) > parseInt($(window).height() + $(window).scrollTop());
						var bottom_compare = ((elPosition.y + elSize.y) - (op.edgeOffset + tipSize.y + 8)) < 0;
						
						if(top_compare || (t_class == "_bottom" && top_compare) || (t_class == "_top" && !bottom_compare)){
							if(t_class == "_top" || t_class == "_bottom"){
								t_class = "_top";
							} else {
								t_class = t_class+"_top";
							}
							arrow_top = tipSize.y;
							marg_top = Math.round(top - (tipSize.y + 5 + op.edgeOffset));
						} else if(bottom_compare | (t_class == "_top" && bottom_compare) || (t_class == "_bottom" && !top_compare)){
							if(t_class == "_top" || t_class == "_bottom"){
								t_class = "_bottom";
							} else {
								t_class = t_class+"_bottom";
							}
							arrow_top = -12;						
							marg_top = Math.round(elPosition.y + elSize.y + op.edgeOffset);
						}
					
						if(t_class == "_right_top" || t_class == "_left_top"){
							marg_top = marg_top + 5;
						} else if(t_class == "_right_bottom" || t_class == "_left_bottom"){		
							marg_top = marg_top - 5;
						}
						if(t_class == "_left_top" || t_class == "_left_bottom"){	
							marg_left = marg_left + 5;
						}
						c.tipPointer.setStyle({"margin-left": arrow_left+"px", "margin-top": arrow_top+"px"});
						c.tipHolder.setStyle({"margin-left": marg_left+"px", "margin-top": marg_top+"px"}).set("class","tip"+t_class);
						
			//fade the element in!
			if (c.timeout){ clearTimeout(c.timeout); }
			c.timeout = setTimeout(function(){ this.tipHolder.fade(1, op.fadeIn); },op.delay );
		} //end content set IF
	},
	'depopulateTip' : function(){
		if (c.timeout){ clearTimeout(c.timeout); }
		this.tipHolder.fade(0, op.fadeOut);
	},
	'createHolder' : function(){
		if ( $('tooltip_holderolder_aQ2rT13').length ){
			this.tipHolder 	= $('tooltipSize.yolder_aQ2rT13');
			this.tipContent	= $('tooltip_content');
			this.tipPointer	= $('tooltip_pointer');
		}else{
			this.tipHolder  = new Element('div', {'id' : 'tooltip_holder_aQ2rT13', 'style' : 'max-width:' + op.maxWidth, 'opacity' : op.opacity }).inject( document.body );
			this.tipContent = new Element('div',{ 'id' : 'tooltip_content'}).inject(this.tipHolder);
			this.tipPointer = new Element('div', { 'id' : 'tooltip_pointer'}).inject(this.tipContent);
		}
	}
});

var ecceToolTip = new Class({
	Implements: [Options],
	options: {
		'bindto':'.tooltip',
		'offset':10,
		'opacity':0.9,
		'duration':0.2,
		'mouseoverupdate':true,
		'openDelay' : 120,
		'closeDelay':10,
		'posOnBindTo': false,
		'posOnBindToType' : 'above',
		'debug' : false
	},
	initialize: function(options) {
		this.setOptions(options);
		var op = this.options;
		var c = this;
		op.toolopen=false;
		$$(op.bindto).each(function(elm,i){
			if(elm.getProperty('data-tooltipset')!='yes'){
				elm.addEvent('mouseenter',function(e){
					e.stop();
					c.make(elm,e);
				});
				
				if(op.mouseoverupdate){
					elm.addEvent('mousemove',function(e){
						if (op.debug == true){ console.log('Add Mouse Over Handler'); }
						if(typeof(op.tooltopContainer)=='object'){
							//mrp positioning change
							if (op.posOnBindTo == true){
								//attach it to the bindTo element like FB do!
								var pos = elm.getPosition();
								op.tooltopContainer.setStyles({'top' : pos.x, 'left' : pos.y });
							}else{
							op.tooltopContainer.setStyles({'top':e.page.y+op.offset,'left':e.page.x+op.offset});
							}
							var left = op.boxMiddle.getPosition().x;
							var top = op.boxMiddle.getPosition().y;
							op.boxText.setStyles({'top':top,'left':left});
						}else{
							c.make(elm,e);
						}
					});	
				}
				
				elm.addEvent('mouseleave',function(){
					if($chk(op.tooltopContainer)){
						op.tooltopContainer.dispose();
						op.boxText.dispose();
						op.toolopen = false;
					}
				});
				elm.setProperty('data-tooltipset','yes');
			}
		});
		
		
	},
	runUsing:function(id){
		var op = this.options;
		var c = this;
		if($chk($(id))){
			$(id).removeEvents('mouseleave');
			$(id).addEvent('mouseleave',function(){
				if($chk($('ecceToolTip'))){
					op.tooltopContainer.dispose();
					op.boxText.dispose();
					var closedelay = function(){
						op.toolopen = false;
					}
					closedelay.delay(op.closeDelay);
				}
			});	
		}
		c.make($(id));
	},
	make:function(bindto,e){
		var c = this;
		var op = this.options;
		
		if($chk(bindto.getProperty('title')) && bindto.getProperty('title').length>0){
			bindto.setProperty('tt-title',bindto.getProperty('title'));
			bindto.setProperty('title','');
		}
			
		if(op.toolopen==false && $chk(bindto.getProperty('tt-title'))){
			if(bindto.getProperty('tt-title').length>0){
				op.toolopen=true;
				op.text = bindto.getProperty('tt-title');
				if($chk(op.tooltopContainer)){
					op.tooltopContainer.dispose();
					op.boxText.dispose();
				}
				//mrp adjustments if you want fb-style tooltips
				if ( op.posOnBindTo == true){
					var bindToData = new Object;
					 bindToData.pos 	= bindto.getPosition();
					 bindToData.size 	= bindto.getSize();
					 
					 var left = bindToData.pos.x  + op.offset;
					 var top  = bindToData.pos.y + op.offset;
				}else{
					
					if(typeof(e)=='object'){
						top  = e.page.y+op.offset;
						left = e.page.x+op.offset;
					}else{
						var left = bindto.getPosition().x+bindto.getSize().x+op.offset;
						var top = bindto.getPosition().y+bindto.getSize().y+op.offset;
					}
				
				}
				op.tooltopContainer = new Element('div',{'id':'ecceToolTip','styles':{'z-index':999998,'position':'absolute','opacity':0,'left':left,'top':top}}).inject($(document.body));
				op.boxTop = new Element('div',{'class':'boxTop'}).inject(op.tooltopContainer);
				op.boxMiddle = new Element('div',{'class':'boxMiddle'}).inject(op.tooltopContainer);
				op.boxBottom = new Element('div',{'class':'boxBottom'}).inject(op.tooltopContainer);
						
				//bottom round
				op.boxSW = new Element('div',{'class':'boxSW','styles':{'float':'left'}}).inject(op.boxBottom);
				op.boxS = new Element('div',{'class':'boxS','styles':{'float':'left'}}).inject(op.boxBottom);
				op.boxSE = new Element('div',{'class':'boxSE','styles':{'float':'left'}}).inject(op.boxBottom);
				op.boxS.setStyle('width',op.tooltopContainer.getSize().x-(op.boxSW.getSize().x+op.boxSE.getSize().x));
						
				//top round
				op.boxNW = new Element('div',{'class':'boxNW','styles':{'float':'left'}}).inject(op.boxTop);
				op.boxN = new Element('div',{'class':'boxN','styles':{'float':'left'}}).inject(op.boxTop);
				op.boxNE = new Element('div',{'class':'boxNE','styles':{'float':'left'}}).inject(op.boxTop);
				op.boxN.setStyle('width',op.tooltopContainer.getSize().x-(op.boxNW.getSize().x+op.boxNE.getSize().x));
				
				left = op.boxMiddle.getPosition().x;
				top = op.boxMiddle.getPosition().y;
				op.boxText = new Element('div',{'id':'ecceToolTipText','class':'boxText','styles':{'z-index':999999,'opacity':0,'left':0,'top':0}}).inject( op.boxMiddle );
				op.boxText.set('html',op.text);
				op.boxMiddle.setStyles({'width':op.boxText.getSize().x,'height':op.boxText.getSize().y});
				op.boxS.setStyle('width',op.boxText.getSize().x-op.boxSW.getSize().x-op.boxSE.getSize().x);
				op.boxN.setStyle('width',op.boxText.getSize().x-op.boxNW.getSize().x-op.boxNE.getSize().x);
				
				//positioning corrections based on the tooltip size
				if ( op.posOnBindTo == true){
					var boxSize = new Object;
					boxSize = op.tooltopContainer.getSize();
					
					switch( op.posOnBindToType ){
						case 'above':
							top  -= boxSize.y;
							left = left - Math.floor(boxSize.x / 2) - Math.floor( bindToData.size.x / 2 );
						break;
						
						case 'below':
							left -= Math.floor( boxSize.x / 2 );
						break;
						case 'left':
						
						break;
						
						case 'right':
						
						break; 
					 }
					op.tooltopContainer.setStyles({ 'top' : top, 'left' : left });
				}
				
				
				(function(){ var myFx = new Fx.Tween(op.tooltopContainer,{'duration':op.duration}).start('opacity',op.opacity) }).delay(op.openDelay);
				var myFx = new Fx.Tween(op.boxText,{'duration':op.duration}).start('opacity',op.opacity);
			}
		}
	
	}
});
