// VARS
var searchValue = ''; 
var photos = new Array();
var currentPhoto = -1;
var periodical = null;
var itemWidth = 130;
var itemsPerSlide = 7;
var currentSlide = 0;
var slideViewerWidth = 800;
var totalSlides;
var currentActie = 0;
var totalActies = 0;
var acties = [];
var slidePeriodical; 
var bDomain = 'default';
var clickEvent;
var is3dPlattegrondLoaded = false;

/* EVENTS */
window.addEvent('domready', function(){
	 Shadowbox.init({overlayOpacity: 0.8}, initShadowbox);
	 if (!$('wmToolbar')) initOverviewClick();
	 initTabs();
	 initPhotoRotator();
	 initPhotoNavigator();	
	 initSlider();
	 initYoutubeBlockLinks();
	 initFlitsBlockLinks();
	 initSearch();
	 if($('overzicht') != null) {
		 initCompanyOverview();
	 }
});

window.addEvent('domready', function() {
  var formMerkProductTips = new Tips($$('.tickettooltip','.questiontooltip','.seasontickettip','.previewtickettip'),{
                          offset:{
                              x:-24,
                              y:32
                           },
                           fixed: true
                           });
});

/* INIT FUNCTIES */
// shadowbox slider
function initShadowbox() {
	Shadowbox.setup("a.shadowbox-album", {
    gallery:        "shadowbox",
    continuous:     true,
    counterType:    "skip"
  });
    
  Shadowbox.setup('a.spnl-popup', {
    height:     640,
    width:      800
  });
  
  Shadowbox.setup('a.shadowbox-video', {
    height:     640,
    width:      800
  });
  
  Shadowbox.setup('a.medewerker-popup', {
    height:     600,
    width:      380
  });
  
  Shadowbox.setup('a.picturewall-popup', {
    height:     632,
    width:      828,
    handleOversize: "none"
  });
  
  Shadowbox.setup('a.form-popup', {
    height:     722,
    width:      585
  });
  
  
}

// zoeken
function initSearch() {
	if ($('search')) {
		searchValue = $('search').value;
		$('search').addEvents(
			{'click': 
				function(e) {
					if (this.value == searchValue) {
						this.value = '';
					}
				}
			,
			'blur': 
				function(e) {
					if (this.value == '') {
						this.value = searchValue;
					}
				}
			}
		);
	} 	
}
// Blokken: klik op hele blok
function initOverviewClick() {
	$$('#ctaBlokken div', '.overview div', '#detailRightBottom div', '#detailLeftBekijken div', '#content.home #ctaTicketSmall', 'div.article').each(function(object, value) {
		// klik + mouseover niet toevoegen bij medewerkers pagina & agenda pagina
		object.removeEvents('click');
		object.removeEvents('mouseover');
		object.removeEvents('mouseout');
		if (!object.getParent('div').hasClass('smaller') && !object.hasClass('agenda')) {
			object.addEvents(
				{'click': 
					function(e) {
						if(e != null) {
							e.stopPropagation();
						};
						if(this.getLast('a') != null && this.getLast('a').hasClass('form-popup')) {
							this.getLast('a').fireEvent('click');
							return false;
						}
						if (this.getLast('a')) {
							href = this.getLast('a').href;
							
							// fix voor homepagina routebeschrijving
							var c;
							c = this.getElements('a');
							if (c.length == 2) {
								href = this.getFirst('a').href;								
							}							
							
						  if (this.getLast('a').get('target') != '_blank') {
						 		window.location = href;
						  }
						}
						else if (this.getElement('a')) {
							href = this.getElement('a').get('href');
						  if (this.getElement('a').get('target') != '_blank') {
						  	window.location = href;
						  }
						}
					}
				,
				'mouseover': 
					function(e) {
						var c;
						c = this.getElements('a');
						if (!this.hasClass('norollover') && c.length < 2) {
							this.addClass('active');
						}						
					}
				,
				'mouseout': 
					function(e) {
						if (!this.hasClass('norollover')) {
							this.removeClass('active');
						}
					}
				}
			);
		}
		// agenda uitklap
		else if (object.hasClass('agenda')) {
			object.addEvents(
				{'click': 
					function(e) {
						if (this.getLast('div').hasClass('active')) {
							this.getLast('div').removeClass('active');
							this.getLast('h3').removeClass('omhoog');
						}
						else {
							this.getLast('div').addClass('active');
							this.getLast('h3').addClass('omhoog');
						}
					}
					,'mouseover': 
					function(e) {
							if (!this.hasClass('norollover')) {
								this.addClass('active');
							}						
						}
					,
					'mouseout': 
					function(e) {
						if (!this.hasClass('norollover')) {
							this.removeClass('active');
						}
					}
			});
		}
	});
	
	$$('a.medewerker-popup').each(function(elm){
		parentDiv = elm.getParent('div');
		
		parentDiv.addEvents({
		    mouseenter: function(){
		        $(this).addClass('active');
		    },
		    mouseleave: function(){
		        $(this).removeClass('active');
		    },
		    click: function(e){
		    	if(e != null) {
						e.preventDefault();
					}
					link = $(this).getElement('a.medewerker-popup');
					link.fireEvent('click');
		    }
		});
	});
	$$('a.friends-overview').each(function(elm){
		parentDiv = elm.getParent('div');
		
		parentDiv.addEvents({
		    mouseenter: function(){
		        $(this).addClass('active');
		    },
		    mouseleave: function(){
		        $(this).removeClass('active');
		    },
		    click: function(e){
		    	if(e != null) {
						e.preventDefault();
					}
					link = $(this).getElement('a.friends-overview');
					window.location = link;
		    }
		});
	});
}
// Tabs: active zetten van tab
function initTabs() {
	if ($('tabs') && !$('tabs').hasClass('noscript') && !$('quiz')) {
		$$('#tabs ul li a').each(function(object, value) {
			object.addEvents(
				{'click': 
					function(e) {
						if(e != null) {
							e.preventDefault();
						}
						if($$('#tabs ul li').length > 1){
							$$('#tabs ul li').each(function(object, value) {
								object.removeClass('active');						
								$(object.getLast('a').getProperty('rel')).removeClass('active');
								$(object.getLast('a').getProperty('rel')).addClass('inactive');
								//$(object.getLast('a').getProperty('rel')).fireEvent('hide');
							});
							// huidige actief zetten
							this.getParent('li').addClass('active');
							$(this.getProperty('rel')).addClass('active');
							$(this.getProperty('rel')).removeClass('inactive');
							if($('floriadeloader')) {
								if(this.getProperty('rel') == 'plattegrond') {
									$('floriadeloader').addClass('inactive');
									$('filtersLokatie').setStyle('display', 'none');
									$('filtersLokatieHr').setStyle('display', 'none');
								} else {
									$('floriadeloader').removeClass('inactive');
									$('filtersLokatie').setStyle('display', 'block');
									$('filtersLokatieHr').setStyle('display', 'block');
								}
							}
							
							if (!$('plattegrond').hasClass('inactive') && !$('open3d') && $('openmaps')) {
								$('openmaps').fireEvent('click');							
							}
							if ( $('openmaps') && this.getProperty('rel') != 'plattegrond' ) {
								 $('openmaps').fireEvent('hide');
							}
						}
						
						

					}
				}
			);
		});
	}
	if ($('open3d')) {
		$('open3d').addEvent('click', function(e) {
			e.preventDefault();
			togglePlattegrond(this.get('rel'));
		});
		if ($('open3d').hasClass('active')){
			init3DPlattegrond('map_3d');
		}
	}
	if ($('openmaps')) {
		$('openmaps').addEvent('click', function(e) {
			if (e) e.preventDefault();
			togglePlattegrond(this.get('rel'));
		});
	}
	
	$$('a.plattegrondlink').addEvent('click', function(e){
		if(e != null) {
			e.preventDefault();
		}
		plattegrondLink = $('tabs').getElement('a[rel=plattegrond]');
		if(plattegrondLink != null) {
			plattegrondLink.fireEvent('click');
		}
	});
}
// Foto rotator next / prev
function initPhotoNavigator() {
	if ($('content').hasClass('home')) return;
	
	if (photos.length > 1 && $('next')) {
		$('next').addEvent('click', function(e){
			e.preventDefault();									   
			fadePhoto(true);
			cleartimer();
		});
		$('previous').addEvent('click', function(e){
			e.preventDefault();									   
			fadePhoto(false);
			cleartimer();
		});	
	}
	else {
		if ($('line')) $('line').addClass('inactive');
		if ($('next')) $('next').addClass('inactive');
		if ($('previous')) $('previous').addClass('inactive');
	}
}
// Foto rotator afbeeldingen
function initPhotoRotator() {	
	// fotos
	var photo = $('rotatorImages');
	// foto's op detailpagina
	if (!photo) {
		var photo = $('detailImage');
	}
	if (photo) {
		photos = photo.getElements('img');	
		
		currentPhoto = photos.length - 1;
		
		for(i=1;i<photos.length;i++) photos[i].xOpacity = 0;
		photos[0].style.display = "block";
		photos[0].xOpacity = .99;	
		
		fadePhoto(true);
			
		if (photos.length > 1) {
			periodical = fadePhoto.periodical(4000);
		}
		
	}		
}
// Foto fade overgang
function fadePhoto(next) {
	if (next) {
		if (photos[currentPhoto+1]==null)
			nextPhoto = 0;
		else
			nextPhoto = currentPhoto + 1;
	}
	else {
		if (currentPhoto-1<0 )
			nextPhoto = photos.length - 1;
		else
			nextPhoto = currentPhoto - 1;
	}
	
	if (!photos[currentPhoto]) return;
	$(photos[nextPhoto]).setStyles({'display': 'block', 'opacity': 0});
	$(photos[currentPhoto]).fade('out');
	$(photos[nextPhoto]).fade('in');
				
	ie6 = false;
	if (Browser.Engine.trident && Browser.Engine.version < 5) {
		ie6 = true;	
	}	
	if (!ie6 && $$('#rotatorImages li')[currentPhoto]) { // IE6 hack
		//$$('#rotatorImages li')[currentPhoto].removeClass('active');
		$$('#rotatorImages li')[nextPhoto].addClass('active');	

		
		// titel zetten ahv alt
		$$('#controls div.title').set('html', $$('#rotatorImages li')[nextPhoto].getFirst('img').getProperty('alt'));
		
		// url ophalen ahv de rel
		if ($$('#rotatorImages li')[nextPhoto].getFirst('img').getProperty('rel') != null){
			clickEvent = $$('#controls div.title').addEvent('click', function(e){
				if ($$('#rotatorImages li')[currentPhoto].getFirst('img').getProperty('rel') != null) {
					window.location = $$('#rotatorImages li')[currentPhoto].getFirst('img').getProperty('rel');
				}
			});
			$$('#controls div.title').setStyle('cursor', 'pointer');
		}
		else {
			$$('#controls div.title').removeEvent('click', clickEvent);
			$$('#controls div.title').setStyle('cursor', '');
		}
	}
	currentPhoto = nextPhoto*1;
}
// Photo timer
function cleartimer() {
	if (periodical != null) $clear(periodical);	
}
// Slider
function initSlider() {
	if (!$$('.slider')[0]) return;
	
	if ($$('.slider')[0].hasClass('smallSlider')) {
		itemsPerSlide = 5;
		itemWidth = 122;
	}
	
	
	var items = $$('.slider ul li').length;
	if (length == 0) {
		var items = $$('.slider div').length-1;
		itemsPerSlide = 4;
		itemWidth = 250;
		slideViewerWidth = 964;
	}
	
  if ($$('.slider')[0].hasClass('three')) {
    initOverviewClick();
    itemsPerSlide = 3;
    itemWidth = 310;
  }
  
  if ($$('.slider')[0].hasClass('overviewTips')) {
    initOverviewClick();
  }
	
	var width = (Math.ceil(items/itemsPerSlide)*itemsPerSlide)*itemWidth;
	totalSlides = Math.ceil(items/itemsPerSlide);
	//alert(items+' - '+totalSlides);
	/*var lazyloader = new LazyLoad({
		container: $$('.slider')[0],
		mode: "horizontal",
		range: 0
	});	*/					  
	
	$$('.slider ul').setStyle('width', width);
	$$('.sliderLeft').addEvent('click', function(e){
		e.preventDefault();											 
		slide(false);
	});	
	$$('.sliderRight').addEvent('click', function(e){
		e.preventDefault();	
		slide(true);
	});
	
	if (!removeButtons() ) {
		buttons();
	}
}
// Slider: huidige slide
function getCurrentSlide() {
	var slideWidth = itemWidth * itemsPerSlide;
	var currentSlide = ( Math.round(( $$('.slider')[0].getScroll().x) / slideWidth) ); 
}
// Slider: Knoppen
function buttons () {
	if (currentSlide == 0){
		$$('.sliderLeft').fade(0.3);
	} else {
		$$('.sliderLeft').fade(1);
	}
	if (currentSlide == totalSlides-1){
		$$('.sliderRight').fade(0.3);
	} else {
		$$('.sliderRight').fade(1);
	}
}
// Slider: Knoppen inactief
function removeButtons () {
	if (currentSlide == 0 && currentSlide == totalSlides-1){
		$$('.sliderLeft').setStyle('display','none');
		$$('.sliderRight').setStyle('display','none');
		return true;
	} 
	return false;
}
// Slider: vorige / volgende
function slide(next) {
	if (next) {
		currentSlide++;
		if ( currentSlide >= (totalSlides-1) ) {
			currentSlide = (totalSlides-1);
		}
	}
	else {
		currentSlide--;
		if ( currentSlide <= 0 ) {
			currentSlide = 0;
		}
	}
	buttons();
	var myFx = new Fx.Scroll($$('.slider')[0], {
		onComplete: function(){
			getCurrentSlide();
		},							   
		duration: 500,
		wait: false
	}).start(slideViewerWidth*(currentSlide), 200);	
}

// Home actie toevoegen
function addActie(title, text, page, photo, counter, button){
	var item = [];
	item[0] = title;
	item[1] = text;
	item[2] = page;
	item[3] = photo;
	if(button != null){
		item[4] = button;
	}
	
	acties[counter] = item; 
	totalActies++;
}

// Home actie starten
function initActieSlider(domain) {
	
	currentActie = 0;	
	if(domain == null){		
		slideActie();
		if (totalActies > 1) slidePeriodical = slideActie.periodical(6000);
		initNavigation();
	}
	else if(domain == 'business'){
		bDomain = 'business';
		slideActie(domain);
		if (totalActies > 1) slidePeriodical = slideActie.periodical(6000);
		initNavigation(domain);
	}
	else if(domain == 'kids'){
    bDomain = 'kids';
    slideActie(domain);
    if (totalActies > 1) slidePeriodical = slideActie.periodical(6000);
    initNavigation(domain);
  }
}

// Home actie volgende Slide
function slideActie(domain) {
	var item = [];
	item = acties[currentActie];
	//alert(currentActie);
	$$('#rotatorImages li.active img').set('src', item[3]);
	$$('#imageRotator h1').set('html', item[0]);
	$$('#imageRotator h2').set('html', item[1]);
	
	if(item[4] && bDomain == 'business'){
		$$('#imageRotator a.knopOranje').set('href', item[2]);
		$$('#imageRotator a.knopOranje').set('html', '<span>'+item[4]+'</span>');
		if(currentActie == '0'){
			$('overlay').setStyle('background-position', '0 0');
		}
		else if(currentActie == '1'){
			
			$('overlay').setStyle('background-position', '0 -316px');
		}
		else if(currentActie == '2'){
			$('overlay').setStyle('background-position', '0 -633px');
		}
	}
	else if(item[4] && bDomain == 'kids'){
	  $('dynamicSliderButton').set('href', item[2]);
    if(currentActie == '0'){
      
      $('overlay').setStyle('background-position', '0 0');
    }
    else if(currentActie == '1'){
 
     $('overlay').setStyle('background-position', '0 -350px');
    }
    else if(currentActie == '2'){
      
      $('overlay').setStyle('background-position', '0 -700px');
    }
	}
	else{
		$$('#imageRotator a.knopOranjeMeerInfo').set('href', item[2]);
	}
		
	// navigator zetten
	$$('#imageRotator ul.navigation li').removeClass('active');
	$$('#imageRotator ul.navigation li').each(function(object, value){
		if (object.getElement('a').get('rel') == currentActie){
			object.addClass('active');
			if(bDomain == 'kids'){
       var activeSliderItem = $('homeSliderNavigation').getElement('li.active');
       var sInfoButtonName = activeSliderItem.get('rel');
       $('dynamicSliderButton').set('html', '<span>'+sInfoButtonName+'</span>');
      }
		}
		
		
	});
	
	$('imageRotator').addEvent('click', function(e) {
		if(e != null) {
			e.stopPropagation();
			e.preventDefault();
		}
		if(domain == null){
			link = $(this).getElement('a.knopOranjeMeerInfo').get('href');
		}
		else if(domain == 'kids'){
      link = $('dynamicSliderButton').get('href');
    } 
    else {
			link = $(this).getElement('a.knopOranje').get('href');
		}
		window.location = link;
	});
	if($('ctaTicketSmall')){
		$('ctaTicketSmall').addEvent('click', function(e) {
			if(e != null) {
				e.stopPropagation();
				e.preventDefault();
			}
			link = $(this).getElement('a.knopOranje').get('href');
			window.location = link;
		});
	}
		
	if (currentActie < (totalActies - 1)){
		currentActie++;
	}
	else {
		currentActie = 0;
	}
	//alert(currentActie+'-'+totalActies);
}

// Home actie navigator
function initNavigation(domain) {
	$$('#imageRotator ul.navigation li a').each(function(object, value){
		object.addEvents(
				{'click': 
					function(e) {
						
						e.preventDefault();
						e.stopPropagation();
						$clear(slidePeriodical);
						currentActie = this.get('rel');
						slideActie(domain);
					}
			});
	});
}

function initYoutubeBlockLinks() {
	$$('div.youtubeItem').each(function(elm){
		elm.addEvent('click', function(e) {
			if(e != null) {
				e.preventDefault();
			}
			link = $(this).getElement('a').fireEvent('click');			

		});
		
	});
}

function initFlitsBlockLinks() {
  $$('div.flitsItem').each(function(elm){
    elm.addEvent('click', function(e) {
      if(e != null) {
        e.preventDefault();
      }
      //link = $(this).getElement('a').fireEvent('click');    
      window.location.href = $(this).getElement('a').get('href');
      //link.fireEvent('click');
    });
    
  });
}

function initSearch() {
	if($('searchForm')){
		 $('searchForm').addEvent('submit', function(e) {
		if(e != null) {
			e.preventDefault();
		}
		input = $(this).getElement('input[name=search]');
		if(input.value != input.get('rel')) {
				$(this).submit();
			}
			
		});
			$$('input.autofillinput').each(function(elm) {
		elm.addEvent('focus', function() {
			if ($(this).get('value').toString() == $(this).get('rel').toString() ) {$(this).set('value',''); }
		});
		elm.addEvent('blur', function() {
			if ($(this).getProperty('value').toString() == '' ) {$(this).setProperty('value',$(this).getProperty('rel')); }
			});
		});
		
		$$('div.searchfilter ul label').addEvent( 'click', function() { 
		if ( $(this).getElement('input').getProperty('checked') == true ) {
			$(this).getElement('input').setProperty('checked', false);  
		} else {
			$(this).getElement('input').setProperty('checked', true);
		}
		
		var search = $$('#overview #searchBarPage').getElement('input[name=search]');
		inputValue = search.get('value');
		inputRel = search.get('rel');
		if ( inputValue == inputRel) {
	
			search.setProperty('value', '');
		}
		
		$(this).getParent('form').submit();  
		} );
		
		$$('div.searchfilter ul input[type=checkbox]').addEvent( 'click', function() { 
		var search = $$('#overview #searchBarPage').getElement('input[name=search]');
		if ( search.get('value')== search.get('rel') ) {
			search.setProperty('value', '');
		}
		
		$(this).getParent('form').submit();  
		} );
		
		
		if($('ticketForm') != null) {
	
		}
	}
}

function togglePlattegrond(type){
	if (type == 'map_3d') {
		$('map_3d').addClass('active');
		$('map_3d').removeClass('inactive');
		$('map_container').addClass('inactive');
		$('map_container').removeClass('active');
		if ($('openmaps')) { 
			$('openmaps').removeClass('active');
		 }
		if ($('open3d')) {
			$('open3d').addClass('active');
			$('open3d').fireEvent('show');
		}
		init3DPlattegrond();
	}
	if (type == 'map_container') {
		if ($('map_3d')) {
			$('map_3d').addClass('inactive');
			$('map_3d').removeClass('active');
		}
		$('map_container').addClass('active');
		$('map_container').removeClass('inactive');		
		if ($('open3d')) {
			$('open3d').removeClass('active');
		}
		if ($('openmaps')) {
			$('openmaps').addClass('active');
			$('openmaps').fireEvent('show');
		}
	}
}

function init3DPlattegrond() {
	if (!$('map_3d') || is3dPlattegrondLoaded == true) return false;
	var world;
	world = $('map_3d').get('rel');
	
	switch(world) {
		case 'relaxHeal':
			world = 'rh';
			break;
		case 'greenEngine':
			world = 'ge';
			break;
		case 'educationInnovation':
			world = 'ei';
			break;
		case 'worldShowStage':
			world = 'ws';
			break;
		case 'environment':
			world = 'en';
			break;
		case 'basisPark':
			world = 'bp';
		default:
			break;
	}
	
	var lang = 'nl';
	if ($(document).getElement('html').getProperty('lang')) {
		lang = $(document).getElement('html').getProperty('lang');
	}
	
	if($('kidsMap')){
	  var obj = new Swiff('/application/assets/default/swf/3dkaart-'+lang+'-kids.swf', {
      id: 'map_3dplattegrond',
      width: 916,
      height: 500,
      params: {
          wMode: 'transparent',
          bgcolor: 'transparent'
      },
      vars: {
          startpunt: world
      }
    });
	} else {
	  var obj = new Swiff('/application/assets/default/swf/3dkaart-'+lang+'.swf', {
      id: 'map_3dplattegrond',
      width: 916,
      height: 500,
      params: {
          wMode: 'transparent',
          bgcolor: 'transparent'
      },
      vars: {
          startpunt: world
      }
    });
	}
	
	
	
	obj.inject($('map_3d'));
	is3dPlattegrondLoaded = true;
}

function initCompanyOverview() {
	
	$$('#filters a').each(function(elm) {
		
		elm.addEvent('click', function(e) {
			if(e != null) {
				e.preventDefault();
			}
			loadCompanyOverview($(this));
		});
		
	});
	
	if($('floriadeloader') != null) {
		var containerParentCoords = $('overzicht').getParent().getCoordinates();
		var containerCoords = $('overzicht').getCoordinates();
		$('floriadeloader').setStyle('left',(containerParentCoords.width/2)-20);
		$('floriadeloader').setStyle('top',containerCoords.top-100);
	}
	
}

function loadCompanyOverview(elm) {
	$$('#filters a').removeClass('active');
	elm.addClass('active');
	link = elm.get('href');
	if($('categorytitle')) {
		$('categorytitle').set('html', elm.get('title'));
	}
	questionMarkLocation = link.indexOf("?");
	queryString = link.substring(questionMarkLocation+1);
	equalLocation = queryString.indexOf("=");
	filter = queryString.substring(equalLocation+1);
	if($('map_container') != null) {
		$('map_container').set('filter', filter);
	}
	if ($('map_container').get('formFilterValue') == 'Hotels'){
		$$('.starsSelection').setStyle('display','');
	} else {
		$$('.starsSelection').setStyle('display','none');
		$('starsFilterValue').value ='';
		selectStar();
	}
	if ($('aSelect')){
		$('aSelect').selectedIndex = 0;		
	}
	
	if ($('pSelect')){
		$('pSelect').selectedIndex = 0;		
	}
	 
	
	$('formFilterValue').value = filter;
	if($('maincategory') != null) {
		extraString = '&maincat='+$('maincategory').value;
		queryString = queryString+extraString;
	}
	
	if(floriadeMaps !=  null) {
		floriadeMaps.clearLocations();
		floriadeMaps.getLocations(filter);
	} 
	
	if ($('plattegrond').hasClass('inactive')){
		location.href=link; //+'#Overview';
		return;
	} 
	
	var myHTMLRequest = new Request.HTML({
		url:'/application/assets/default/handlers/spnl.php?'+queryString,
		onRequest: function() {
				$('overzicht').tween('opacity', 0.3);
				if(!$('floriadeloader').hasClass('inactive')) {
					(function(){$('floriadeloader').setStyle('display','block');}).delay(100);
				}
		},
		onSuccess: function(tree,elms,HTML,javascript) {
			(function(){
				
				$('overzicht').set('html', HTML);
				$('floriadeloader').setStyle('display','none');
				$('overzicht').tween('opacity', 1);
				initOverviewClick();
			}).delay(900);
		}
	}).get();
}


/* scroll spy plugin / class */
    var ScrollSpy = new Class({
      
      /* implements */
      Implements: [Options,Events],
    
      /* options */
      options: {
        min: 0,
        mode: 'vertical',
        max: 0,
        container: window,
        onEnter: $empty,
        onLeave: $empty,
        onTick: $empty
      },
      
      /* initialization */
      initialize: function(options) {
        /* set options */
        this.setOptions(options);
        this.container = $(this.options.container);
        this.enters = this.leaves = 0;
        this.max = this.options.max;
        
        /* fix max */
        if(this.max == 0) 
        { 
          var ss = this.container.getScrollSize();
          this.max = this.options.mode == 'vertical' ? ss.y : ss.x;
        }
        /* make it happen */
        this.addListener();
      },
      
      /* a method that does whatever you want */
      addListener: function() {
        /* state trackers */
        this.inside = false;
        this.container.addEvent('scroll',function() {
          /* if it has reached the level */
          var position = this.container.getScroll();
          var xy = this.options.mode == 'vertical' ? position.y : position.x;
          /* if we reach the minimum and are still below the max... */
          if(xy >= this.options.min && xy <= this.max) {
              /* trigger Enter event if necessary */
              if(!this.inside) {
                /* record as inside */
                this.inside = true;
                this.enters++;
                /* fire enter event */
                this.fireEvent('enter',[position,this.enters]);
              }
              /* trigger the "tick", always */
              this.fireEvent('tick',[position,this.inside,this.enters,this.leaves]);
          }
          else {
            /* trigger leave */
            if(this.inside) 
            {
              this.inside = false;
              this.leaves++;
              this.fireEvent('leave',[position,this.leaves]);
            }
          }
        }.bind(this));
      }
    });
