/*
 *
 * Requires Prototype and Script.aculo.us Libraries
 *
 * This code is created by Henry YP @ monkiki.st
 * http://monkiki.st
 * This work is licensed under the Creative Commons Attribution-Share Alike 3.0
 * http://creativecommons.org/licenses/by-sa/3.0/us/
 *
 */
 
if (typeof Prototype == 'undefined' || typeof CookieJar == 'undefined')
  throw("You must have the Prototype & CookieJar library to use txtSizer");

var TxtSizer = Class.create({
		
	//init
	initialize: function(id, target, options) {
		
        if(!$(id)) throw("Attempted to initalize txtSizer with id: "+ id + " which was not found.");
		
		this.target = new Array;
		//first check whether the target is an array
		if (this.isArray(target)) {
			this.isTargetArr = true;
			for (var i=0; i<target.length; i++) {
				if(!$(target[i])) {} else {this.target.push($(target[i]));}
			}
		}
		else {
			this.isTargetArr = false;
			if(!$(target)) throw("Attempted to initalize txtSizer with target: "+ target + " which was not found.");
			this.target = $(target);
		} 
        this.container = $(id);
		this.txtInc = this.container.select('a.textincrease');
		this.txtDec = this.container.select('a.textdecrease');
		this.txtRes = this.container.select('a.textreset');
		//see whether the buttons exist!
		if (!this.txtInc || !this.txtDec || !this.txtRes) throw("txtInc, txtDec or txtRes not exist!");
		
		
		//set the options
		this.setOptions(options);
		//set the default increment
		this.setSizeIncrement();
		
		//use cookiejar to get cookie variables
		this.jar = new CookieJar({expires:3600, path: '/'});

		//get size from query if exist
		size = this.getSizeFromQueryString (this.options.queryName);
		//check which value is available
        if ( isNaN( parseFloat(size) ) || size > this.options.maxSize || size < this.options.minSize ) {
			
            size = this.jar.get(this.options.queryName );
            if ( isNaN( parseFloat(size) ) || size > this.options.maxSize || size < this.options.minSize ) {
				
                size = this.options.defaultSize;
            }
        } 

		//set this current size
		if (this.curSize != size) this.curSize = size;
		//set the default size
		
		if (!this.isTargetArr) {
			this.target.setStyle ({fontSize: this.curSize+this.options.sizeUnit});
		}
		else {
			for (var i=0; i<this.target.length; i++) {
				this.target[i].setStyle ({fontSize: this.curSize+this.options.sizeUnit});
			}
		}
		
		//initiate click handler
        var clickHandler =  this.clickHandler.bindAsEventListener(this);
        this.container.observe('click', clickHandler);
			
	},
	
	//function to increase size
	Increase:function(n) {
		if ( !this.curSize ) return;
		//make sure the max size is achieve(
		if ( this.curSize*1 + n*1 > this.options.maxSize ) n = this.options.maxSize*1 - this.curSize*1;
		if ( n == 0 ) return;
		this.curSize = n*1+ 1*this.curSize;
	
		if (!this.isTargetArr) {
			this.target.setStyle ({fontSize: this.curSize+this.options.sizeUnit});
		}
		else {
			
			for (var i=0; i<this.target.length; i++) {
				this.target[i].setStyle ({fontSize: this.curSize+this.options.sizeUnit});
			}
		}
		this.jar.put(this.options.queryName, this.curSize);
	},
	
	Decrease:function(n) {
		if ( !this.curSize ) return;
		//make sure the max size is achieve(
		if ( this.curSize - n < this.options.minSize ) n = this.curSize - this.options.minSize;
		if ( n == 0 ) return;
		this.curSize -= n;
	
		if (!this.isTargetArr) {
			this.target.setStyle ({fontSize: this.curSize+this.options.sizeUnit});
		}
		else {
			for (var i=0; i<this.target.length; i++) {
				this.target[i].setStyle ({fontSize: this.curSize+this.options.sizeUnit});
			}
		}		this.jar.put(this.options.queryName, this.curSize);
	},
	
	Reset:function() {
		if ( !this.curSize ) return;
		this.curSize = this.options.defaultSize;
		if (!this.isTargetArr) {
			this.target.setStyle ({fontSize: this.curSize+this.options.sizeUnit});
		}
		else {
			for (var i=0; i<this.target.length; i++) {
				this.target[i].setStyle ({fontSize: this.curSize+this.options.sizeUnit});
			}
		}		this.jar.put(this.options.queryName, this.options.defaultSize);
	},
	
	//clickhandler to determine action
	clickHandler:function(e) {
        var el = e.element();
        if(el.hasClassName('textincrease')) {
            this.Increase(this.sizeIncrement);
        }
		
        if(el.hasClassName('textdecrease')) {
            this.Decrease(this.sizeIncrement);
        }
		
        if(el.hasClassName('textreset')) {
            this.Reset();
        }
		
	},
							
	//set options
	setOptions: function(options) {
		
		//set the default values
		this.options = {
		sizeUnit: "px",
		defaultSize: 14,
		maxSize: 26,
		minSize: 8,
		queryName: "txtSz", // name to check query string for when passing size in URL
		cookieLifetime: 180 // how long to keep cookie
		};
		Object.extend(this.options, options || {});
	},
	
	//set the size increment
 	setSizeIncrement: function () {
        var val = 2;
        switch ( this.options.sizeUnit ) {
            case 'px' : val = 2; break;
            case 'em' : val = .2; break;
            case '%' : val = 10; break;
        }
        this.sizeIncrement = val*1;
    },
	
	//  get the query string --->obj: link or window.location
	getSizeFromQueryString: function(name, obj) {
		obj = obj? obj: window.location; 
		if (obj.search && obj.search.indexOf(name != -1) ) {
			var values = obj.search.toQueryParams(); // name/value pairs
			var setSz = values.txtSz;
			//return if exist
			if (setSz) return setSz;

		}
		return '';
	},
	
	isArray: function(obj) {
		return obj.constructor == Array;
	}
							
});


document.observe("dom:loaded", function(){
										
	var targetArr = ['content','full-content'];
    container = new TxtSizer('menu', targetArr);
})

 