var Site = {
	
	start: function(){
		MooTools.lang.setLanguage("en-US");

		// Launch-in-new-window links automagically created
		var extLinks = $$('a.external');
		if ( extLinks.length ) {
			extLinks.each(function(elem, idx) { 
				elem.setProperty('target', '_blank');
			});
		}
		
		
		// Safari Suckerfish 'fix'
		if ( navigator.appVersion.toLowerCase().indexOf('safari') != -1 ) {
			var navElems = $$('#navigation li a');
			navElems.each(function(elem, idx) {
				elem.set('title', '');
			});
		}

		Site.initSlide();
    Site.attachResizeButtons();
    Site.updateScales();
		
	},
	
	updateScales: function() {

	  var currentScale = Cookie.read('fontScale');
    if (currentScale == null) { 
      currentScale = 100; 
      Cookie.write('fontScale', currentScale, {path: '/'}); 
    }
    $('wrapper').style.fontSize = currentScale+"%";
    
	  var currentLineHeight = Cookie.read('lineHeight');
    if (currentLineHeight == null) {
      currentLineHeight = 18; 
      Cookie.write('lineHeight', currentLineHeight, {path: '/'}); 
    }
    $('content_inner').style.lineHeight = currentLineHeight+"px";
    
  },
	
	myFx: null,

	initSlide: function() {
		if ($('login') && $('toggle')) {
			Site.myFx = new Fx.Tween($('login'), {
				onComplete: Site.slideComplete
			});
			$('toggle').addEvent('click', Site.toggleClicked);
			$("login-close").addEvent("click", Site.toggleClicked);
		}
	},

	expanded: false,

	toggleClicked: function(event) {
		event.stop();
		if (Site.expanded) {
			Site.myFx.start('margin-top', '-20px', '-241px');
		} else {
			Site.myFx.start('margin-top','-241px', '-20px');
		}
		Site.expanded = !Site.expanded;
		var search_form = $('search_form');
		search_form.setStyle('z-index', 2000);
	},

	slideComplete: function(event) {
		var search_form = $('search_form');
		if (!Site.expanded) {
			search_form.setStyle('z-index', 2002);
		}
	},
	      
  attachResizeButtons: function() {
  
		if ( $$('.text_small') ) {
			$$('.text_small').addEvent('click', function() {
			  currentScale = 80;
			  Cookie.write('fontScale', currentScale, {path: '/'});
			  currentLineHeight = 16;
			  Cookie.write('lineHeight', currentLineHeight, {path: '/'});
			  Site.updateScales();
			});
		}
		
		if ( $$('.text_normal') ) {
			$$('.text_normal').addEvent('click', function() {
			  currentScale = 100;
			  Cookie.write('fontScale', currentScale, {path: '/'});
			  currentLineHeight = 18;
			  Cookie.write('lineHeight', currentLineHeight, {path: '/'});
			  Site.updateScales();
			});
		}
		
		if ( $$('.text_large') ) {
			$$('.text_large').addEvent('click', function() {
			  currentScale = 130;
			  Cookie.write('fontScale', currentScale, {path: '/'});
			  currentLineHeight = 20;
			  Cookie.write('lineHeight', currentLineHeight, {path: '/'});
			  Site.updateScales();
			});
		}

		if ( $$('.text_largest') ) {
			$$('.text_largest').addEvent('click', function() {
			  currentScale = 160;
			  Cookie.write('fontScale', currentScale, {path: '/'});
			  currentLineHeight = 22;
			  Cookie.write('lineHeight', currentLineHeight, {path: '/'});
			  Site.updateScales();
			});
		}
		
	}
				 
};


// Do stuff on load
window.addEvent('load', Site.start);
