//start mooSwitch class
var mooSwitch = new Class({
	
	//implements
	Implements:[Options],

	//options
	options:{
		linksContainer: $('ChangeTextSize'),//container that holds the text size links (error message will be output here)
      maxSize: '125%',
      defaultSize: '100%',//if no cookies are set there must be a default size to set page to
      minSize: '85%',
		smallLink: '.mooSwitch li.small a',//link to trigger small font (could be the li, or an image etc.)
		largeLink: '.mooSwitch li.large a',//link to trigger large font (could be the li, or an image etc.)
		defaultLink: '.mooSwitch li.regular a',//link to trigger default font (could be the li, or an image etc.)
		showAlerts: false,//display alerts to warn users cookies are disabled etc
		alertsPos: 'bottom',//where to inject the alerts relative to the containers specified (allowed: 'top','bottom','after','before' see: http://mootools.net/docs/Element/Element#Element:inject)
		speed: 400//duration the transition will take
	},
	
	//initialization
	initialize:function(options){
		//set options
		this.setOptions(options);
		//start mooSwitch
		if($$(this.options.linksContainer).length > 0 ){this.start();};
	},
	
	//start mooSwitch
	start:function(){
		//test cookie settings
 		var myCookieSizeSet = Cookie.write('mooSwitchTest',1,{duration:365});
		var myCookieSizeRead = Cookie.read('mooSwitchTest');
		var myCookieSaved = Cookie.read('mooSwitchSaved');
		//if cookie above was not successfully set then display error div but only if first visit
		if(this.options.showAlerts == false){
			if(myCookieSizeRead == 1 && myCookieSaved == null){
				//build div as first time user with cookies enabled
				var cookieAlert = new Element('div', {
					'class': 'mooSwitchAlertGreen',
					'html': '<p>Your settings will be saved from now on!</p>'
				});
				//prevent an error if linksContainer isnt specified
				if(this.options.linksContainer != null){
					cookieAlert.inject(this.options.linksContainer,this.options.alertsPos);
				}else{//just inject it at top of container
					cookieAlert.inject(this.options.container,this.options.alertsPos);
				};
				//set cookie so this loop isnt triggered for another year
				Cookie.write('mooSwitchSaved',1,{duration:365});
			}else if(myCookieSizeRead == null){
				//build enable cookie alert div as cookies disabled
				//build div as first time user with cookies enabled
				var cookieAlert = new Element('div', {
					'class': 'mooSwitchAlertRed',
					'html': '<p>You need to enable cookies for settings to be saved!</p>'
				});
				//prevent an error if linksContainer isnt specified
				if(this.options.linksContainer != null){
					cookieAlert.inject(this.options.linksContainer,this.options.alertsPos);
				}else{//just inject it at top of container
					cookieAlert.inject(this.options.container,this.options.alertsPos);
				};
			};
		};
		//call next function
		this.setCookies();
	},
	
	//start building switchers
	setCookies:function(){
		//set up size variables
		var myCookieSize = Cookie.read("mooSwitchSize");
		var smallA = $$(this.options.smallLink);
		var largeA = $$(this.options.largeLink);
      var defaultA = $$(this.options.defaultLink);
		var morphTextSize = new Fx.Morph(this.options.container,{duration:this.options.speed,transition:Fx.Transitions.linear,'unit':'%'});
		
		//set link selected class to selected depending on cookie value
		if(myCookieSize != null){
			//change text size since it must be a percentage value for second template
			this.options.container.setStyle('font-size',myCookieSize);
		}else{
			//set cookie since there isnt one already and set container to default size
			Cookie.write('mooSwitchSize',this.options.defaultSize,{duration:365});
			this.options.container.setStyle('font-size',this.options.defaultSize);
			//if its default template & no cookie-size revert to default option
		};
		
		//set up events for each link to set cookie value
		//small
		smallA.addEvent('click', function(event){
			//prevent default
			event.preventDefault();
			//morph text size and set cookie
         if(this.options.container.getStyle('font-size').toInt() > this.options.minSize.toInt()){
            //set cookie
            Cookie.write('mooSwitchSize',this.options.container.getStyle('font-size').toInt()-5+'%',{duration:365});
            //set font size
            morphTextSize.start({
               'font-size': this.options.container.getStyle('font-size').toInt()-5+'%'
            });
			};
		}.bind(this));
		
		//large
		largeA.addEvent('click', function(event){
			//prevent default
			event.preventDefault();
			//morph text size and set cookie
          if(this.options.container.getStyle('font-size').toInt() < this.options.maxSize.toInt()){
             //set font size
             morphTextSize.start({
                'font-size': this.options.container.getStyle('font-size').toInt()+5+'%'
             });
             //set cookie
             Cookie.write('mooSwitchSize',this.options.container.getStyle('font-size').toInt()+5+'%',{duration:365});
          };
		}.bind(this));


		//default
		defaultA.addEvent('click', function(event){
			//prevent default
         event.preventDefault();
         //morph text size and set cookie
         morphTextSize.start({
            'font-size': this.options.defaultSize
         });
         //set cookie
         Cookie.write('mooSwitchSize',this.options.defaultSize,{duration:365});
		}.bind(this));
	}
});
