
function isIESix(){
  var iep = /MSIE 6\.0/gi;
  var ies = navigator.appVersion;
  var iet = iep.test(ies);
  if(iet){
    return true;
  }else{
    return false;
  }
}

function mooSlide(el){

	/* settings */
	var showDuration = 4000;
	var container = $(el);
	var images = container.getElements('img');
	var currentIndex = 0;
	var interval;
	var toc = [];
	var tocActive = 'toc-active';
	var thumbOpacity = 0.7;
	
	/* new: create caption area */
	var captionDIV = new Element('div',{
		id: el+'-caption',
		styles: {
			//display:none,
			opacity: thumbOpacity
		}
	}).inject(container);
	var captionHeight = captionDIV.getSize().y;
	captionDIV.setStyle('height',0);
	
	/* new: starts the show */
	var start = function() { interval = show.periodical(showDuration); };
	var stop = function() { $clear(interval); };
	/* worker */
	var show = function(to) {
		images[currentIndex].fade('out');
		toc[currentIndex].removeClass(tocActive).fade(thumbOpacity);
		images[currentIndex = ($defined(to) ? to : (currentIndex < images.length - 1 ? currentIndex+1 : 0))].fade('in');
		toc[currentIndex].addClass(tocActive).fade(1);
		captionDIV.set('tween',{
			onComplete: function() {
				captionDIV.set('tween',{
					onComplete: $empty
				}).tween('height',captionHeight);
				/* parse caption */
				var title = '';
				var captionText = '';
				if(images[currentIndex].get('alt')) {
					cap = images[currentIndex].get('alt').split('::');
					title = cap[0].replace(/ \[(.*)\]/, '');
					link = cap[0].match(/\[(.*)\]/)[0].replace('[', '').replace(']', '');
					captionText = cap[1];
					captionDIV.set('html','<h3><a href="' + link + '">' + title + '</a></h3>' + (captionText ? '<p>' + captionText + '</p>' : ''));
					//images[currentIndex].setProperty('alt', '');
				}
			}
		}).tween('height',0);
	};
	
	/* new: create preview area */
	var preview = new Element('div',{
		id: el+'-controls'
	}).inject(container,'after');
	
	/* new: control: table of contents */
	images.each(function(img,i){
		/* add to table of contents */
		toc.push(new Element('img',{
			src: img.get('src'),
			title: img.get('alt'),
			styles: {
				opacity: thumbOpacity
			},
			events: {
				click: function(e) {
					if(e) e.stop();
					stop();
					show(i);
					start();
				},
				mouseenter: function() {
					this.fade(1);
				},
				mouseleave: function() {
					if(!this.hasClass(tocActive)) this.fade(thumbOpacity);
				}
			}	
		}).inject(preview));
		if(i > 0) { img.set('opacity',0); }
		
	});
	
	/* control: start/stop on mouseover/mouseout */
	container.addEvents({
		mouseenter: function() { stop(); },
		mouseleave: function() { start(); }
	});

  show(0);
  start();

}

window.addEvent('domready', function(){

  // Remove text from first nav links
  $$('#access ul.menu > li > a:first-child').each(function(el){
    el.set('html', '&nbsp;');
  });
  
  // Hack to remove # from hrefs
  $$('#access a').each(function(el){
    if(el.getProperty('href') == '#'){
      el.setProperty('href', 'javascript:;');
    }
  });
  
  // Set opacity for menus
  if(navigator.appName !== 'Microsoft Internet Explorer'){
    $$('#access ul ul').setStyle('opacity', 0.9);
  }
  
  // Menu hover effect
  $$('#access ul li').addEvent('mouseenter', function(){
    var ulel = this.getElement('ul');
    if(ulel){
      ulel.setStyle('visibility', 'hidden');
      ulel.setStyle('display', 'block');
      var guls = (ulel.getSize().y)-23;
      var ulfx = new Fx.Morph(ulel, {duration: 150, transition: Fx.Transitions.Linear, wait: 0});
      if(guls > 0){
        ulel.setStyle('height', '0px');
        ulel.setStyle('visibility', 'visible');
        ulfx.start({
          'height': guls+'px'
        });
      }
    }
  });
  $$('#access ul li').addEvent('mouseleave', function(){
    var ulel = this.getElement('ul');
    if(ulel){
      ulel.setStyle('display', 'none');
    }
  });

  // Homepage slideshow
  if($('slideshow-container')){
    mooSlide('slideshow-container');
  }

  // Font resize buttons
  var fc = Cookie.read('fontSize');
  if(fc == 'big'){
    $('content').setStyle('font-size', 'large');
  }else if(fc == 'medium'){
    $('content').setStyle('font-size', 'medium');
  }
  $('amp-tc-small').addEvent('click', function(){
    $('content').setStyle('font-size', 'small');
    Cookie.write('fontSize', 'small', {duration: 14});
  });
  $('amp-tc-medium').addEvent('click', function(){
    $('content').setStyle('font-size', 'medium');
    Cookie.write('fontSize', 'medium', {duration: 14});
  });
  $('amp-tc-big').addEvent('click', function(){
    $('content').setStyle('font-size', 'large');
    Cookie.write('fontSize', 'big', {duration: 14});
  });

  // Slimbox Lightbox
  Slimbox.scanPage = function(){
    $$("a").filter(function(el){
      return el.rel && el.rel.test(/^lightbox/i);
    }).slimbox({/* Put custom options here */}, null, function(el){
      return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
    });
  };
  if(!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)){
    Slimbox.scanPage();
  }
  
  // Videos hack
  $$('embed').each(function(embedel){
    embedel.set('wmode', 'opaque');
  });
  $$('object').each(function(objectel){
    new Element('param', {name: 'wmode', value: 'opaque'}).inject(objectel, 'bottom');
    objectel.setStyle('display', 'none');
    objectel.setStyle('display', 'block');
  });

});

