var Site = {

  
  // initialize page and all its many wonderful functions
  start: function(){
    Site.setSiteWidth();
    if ($$('.calendar')[0]) {
      Site.calendarDays();
    }
    if ($('switch')) {
      Site.switchSwitchers();
      Site.showSwitchers();
      Site.setSwitchers();
      Site.resetCookie();
      Site.displaySwitch();
    }
    if ($('featured-text')) {
      Site.setFeatured();
    }
  },
  
  setSiteWidth: function(){
    if (screen.width < 1050) {
      new Asset.css('/css/col_small.css');
    }
  },
  
  // persistant hover for calendar
  calendarDays: function() {
    days = $$('.calendar li ol');
    days.each(function(day) {
      day.getParent().addEvent('click', function(){
        days.removeClass('current');        
        day.addClass('current');
      });
    });
  },
  
  // slideshow on homepage
  setFeatured: function() {
    var mySlideShow = new swapContent($$('#other-featured li'));
    mySlideShow.displayFeatured.periodical(10000,mySlideShow);
    
    // preload images
    images = $$('#other-featured .image');
    variables = ['a', 'b', 'c', 'd', 'e'];
    for (var i=0; i < images.length; i++) {
      variables[i] = new Image();
      imageNumber = images[i].getProperty('text');
      variables[i].src = "/image?id="+imageNumber+"&ext=l";
    }
  },
  
  
  // show and hide switch
  switchSwitchers: function() {
    swichdiv = $$('#switch div')[0];
    switchers = $$('#change a');
    var fade = new Fx.Morph(swichdiv, { 'duration': '500' });
    $('wheel').addEvent('mouseover', function(){
      Site.resetSwitchers();
      fade.start({'opacity': 1, 'height': 'auto'});
    });
    $('switch').addEvent('mouseleave', function(){
      fade.start({'opacity': 0, 'height': '0'});
    });
  },
  
  // return switch to default state
  resetSwitchers: function() {
    $$('#switch ul').addClass('hidden');
  },
  
  // show and hide switchers
  showSwitchers: function() {
    $$('#change a').each(function(e) {
      e.title = e.href.split('#')[1];
      e.removeAttribute('href');
    });
    $$('#change a').addEvent('click', function(){
      target = $(this.title);
      $$('#switch ul').addClass('hidden');
      if (target) {
        target.removeClass('hidden');        
      }
    });
  },
  
  // add events for actual switching
  setSwitchers: function() {
  controls = $$('#switch ul li a');
  controls.addEvent('click', function(){
    style = this.getParent().className;
    end = this.title;
    Site.applySwitch(style, end)
    });
  },
  
  // apply switches
  applySwitch: function(style, end) {
    if(Site.testLegibility(style, end)) {
      body = $$('body')[0];
      Site.switchMain(style, end);
      if (style !== 'font-size') {
        Site.removeNotch();
        Site.switchWeak(style, end);
      }
    }
  },
  
  switchMain: function(style, end) {
    var mainTween = new Fx.Tween(body, style, {
      'duration': '500',
      onComplete: function() {
      		Site.writeCookie();
      		Site.displaySwitch();
      	}
    	});
  	mainTween.start(end);
  },
  
  // switch faded colour
  switchWeak: function(style, end) {
    weak = $$('.weak').
           merge($$('.weak a')).
           merge($$('dl.tab')).
           merge($$('dl.tab dt')).
           merge($$('dl.tab dd')).
           merge($$('dl.tab dd')).
           merge($$('h2')).
           merge($$('.head'));
    if (weak[0]) {
      currentWeak = weak[0].getStyle('color');
      currentBorder = $$('h2')[0].getStyle('border-color');
      if (style == 'color') {
        if (body.getStyle('background-color') == '#ffffff') {
          alpha = 70;
        }
        else {
          alpha = 55;
        }
        endWeak = new Color(end).mix(body.getStyle('background-color'), alpha);        
        endBorder = new Color(end).mix(body.getStyle('background-color'), alpha+10);        
      }
      else {
        if (end == '#ffffff') {
          alpha = 70;
        }
        else {
          alpha = 55;
        }
        endWeak = new Color(body.getStyle('color')).mix(end, alpha);
        endBorder = new Color(body.getStyle('color')).mix(end, alpha+10);
      }
      tds = $$('td');
      
      var weakMorph = new Fx.Elements(weak, {
          'duration': '500'
      });
      var tdMorph = new Fx.Elements(tds, {
           'duration': '500'
       });
      var o = {};
      for(i = 0; i < weak.length; i++) {
        o[i] = {'color': [currentWeak, endWeak], 'border-color': [currentBorder, endBorder]};
      }
      var p = {};
      for(i = 0; i < tds.length; i++) {
        p[i] = {'border-color': [currentBorder, endBorder]};
      }
      weakMorph.start(o);
      tdMorph.start(p);
    }
  },
  
  removeNotch: function() {
    notched = $$('td').
              merge($$('dl.tab dd')).
              merge($$('h2')).
              merge($$('.head'));
    notched.setStyle('background-image', 'none');
  },
  
  testLegibility: function(style, end) {
    if (style == 'color') {
      fore = end;
      back = $$('body')[0].getStyle('background-color');
    }
    else if (style == 'background-color') {
      back = end;
      fore = $$('body')[0].getStyle('color');
    }
    else return 1;
    
    if (fore == back) return 0;
    else return 1;
  },
  
  displaySwitch: function() {
    var colorNames = {};
    colorNames['#000000'] = 'black'; 
    colorNames['#ffffff'] = 'white'; 
    colorNames['#fffe00'] = 'yellow'; 
    colorNames['#ff4b00'] = 'red'; 
    colorNames['#684520'] = 'brown'; 
    
    background = body.getStyle('background-color');
    color = body.getStyle('color');
    type = body.getStyle('font-size');
    $('current-type-color').innerHTML = colorNames[color];
    $('current-backgound-color').innerHTML = colorNames[background];
    $('current-type-size').innerHTML = type;
  },
  
  writeCookie: function() {
    body = $$('body')[0];
    background = body.getStyle('background-color');
    color = body.getStyle('color');
    type = body.getStyle('font-size');
    weak = $$('h2')[0].getStyle('color');
    weakBorder = $$('h2')[0].getStyle('border-color').split(' ')[0];
    Cookie.set('styles', type + ',' + background + ',' + color + ',' + weak + ',' + weakBorder, {path: '/', duration: 30});
  },
  
  resetCookie: function() {
    body = $$('body')[0];
    $$('a[title=reset]').addEvent('click', function(){
      Site.switchMain('color', '#000000');
      Site.switchMain('background-color', '#ffffff');
      if (body.getStyle('background-color') == '#ffffff') {
        Site.switchWeak('color', '#000000');        
      }
      else {
        setTimeout("Site.switchWeak('color', '#000000')", 500);
      }      
      Site.switchMain('font-size', '11px');
      Cookie.remove('styles');
    });
  }
}

var swapContent = new Class({
  initialize: function(altContent) {
    this.max = $$(altContent).length;
    this.active = 0;
  },

  displayFeatured: function() { 
    // Set next image or the first
    if (this.active == this.max - 1)  {
      this.active = 0;
    }
    else {
      this.active++;
    }
    
    // Get Content
    content = $$('#other-featured li');
    content = content[this.active];
    contentText = content.getChildren('.text').getProperty('html');
    contentTitle = content.getChildren('.title').getProperty('text');
    contentImage = content.getChildren('.image').getProperty('text');
    contentLink = content.getChildren('.link').getProperty('text');
    
    // Swap Content
    $('featured-title').set('text', contentTitle);
    $('featured-text').set('html', contentText);
    $$('#featured-link a')[0].set('href', contentLink);
    $('featured-image').set('href', contentLink);
    $$('#featured-image img')[0].set('src', 'image?id='+contentImage+'&ext=l');

  }
});

window.addEvent('domready', Site.start);
