// ZOOMFT v1.1
// (c) Pawel Gruszecki, grusza@gmail.com
// The following script can be used to allow visitors to increase or decrease the size of text on your page. This can be useful for visitors who have trouble reading smaller text and allows them to increase it to something they can view more easily.
// License: http://creativecommons.org/licenses/LGPL/2.1/
// NOTE: script needs jQuery cookie plugin for working, http://plugins.jquery.com/project/Cookie

// useful for debuging
//function trace(s) {  try { console.log(s) } catch (e) { alert(s) }};

var fontsize = {
    maxZoomOut: -3,
    maxZoomIn: 3,
    minFontSize: 9, // px
    maxFontSize: 20, // px
    reg: /([\d.]+)([^\d\r\n.]+)/,
    tags: new Array("span", "strong", "li", "a", "td", "p", "div"), // html tags sensitive for font resizing
    actualZoom: !isNaN( parseInt( $.cookie('FONT-ZOOM') ) ) ? parseInt( $.cookie('FONT-ZOOM') ) : 0, // get font zoom from cookie
    
    getSizeInPX: function(o) { // parse old size and returns font size in px
    	
    	switch (o) {
    	    case 'xx-small': o = '9px'; break;
    	    case 'x-small': o = '10px'; break;
    	    case 'small': o = '13px'; break;
    	    case 'medium': o = '16px'; break;
    	    case 'large': o = '18px'; break;
    	    case 'x-large': o = '24px'; break;
    	    case 'xx-large': o = '32px'; break;
    	}
    	return o;
    },
    increment: function (n) { // increment font size
            
        if (this.actualZoom < this.maxZoomIn) {
            $.cookie('FONT-ZOOM', ++this.actualZoom); // set font zoom in cookie
            
        	for (var i in this.tags) {
        		
        		o = $(n+' '+this.tags[i]);
        		
	            o.each(function (index){
	                var oldSize = fontsize.getSizeInPX($(this).css('font-size')); // font-size of current element (jQuery returns this value in pixels)
	                
	                var match = fontsize.reg.exec(oldSize);
	                if (match != null) {
	                    var numb = parseFloat(match[1]); // actual font-size, eg: '12', '13.2', '100'
	                    var type = match[2];             // type, eg: 'px', '%', 'pt', 'em' (should be in px)
	            
	                    switch (type) {
	                        case 'px': 
	                        	var size = numb + 1 >= this.maxFontSize ? this.maxFontSize : ++numb; 
	                            $(this).css({'font-size': size + 'px'}); // set new size for elements in main content
	                            break;
	                    }   
	                }
	            });
            
        	}
        //window.location.reload(); 
        } 
        else {
        	alert('Osiągnięto mksymalne powiększenie. Dalsza zmiana rozmiaru czcionki ograniczy czytelność strony.');
        	return false;
        }	
        //console.log('zoom='+this.actualZoom+' min='+this.maxZoomOut+' max='+this.maxZoomIn);
    },
    decrement: function(n) { // decrement font size
    	
        if (this.actualZoom > this.maxZoomOut) {
            $.cookie('FONT-ZOOM', --this.actualZoom);
            
        	for (var i in this.tags) {
        		o = $(n+' '+this.tags[i]);
        	
	            o.each(function (index){
	                var oldSize = fontsize.getSizeInPX($(this).css('font-size')); // font-size of current element (jQuery returns this value in pixels)
	                
	                var match = fontsize.reg.exec(oldSize);
	                if (match != null) {
	                    var numb = parseFloat(match[1]); // actual font-size, eg: '12', '13.2', '100'
	                    var type = match[2];             // type, eg: 'px', '%', 'pt', 'em' (should be in px)
	            
	                    switch (type) {
	                        case 'px': 
	                        	
	                        	if (numb - 1 <= this.minFontSize) size = this.minFontSize; 
	                        	else size = --numb; 

	                            $(this).css({'font-size': size + 'px'}); // set new size for elements in main content
	                            break;
	                    }   
	                }
	            });
            
        	}
        } 
        else {
        	alert('Osiągnięto mksymalne pomniejszenie. Dalsza zmiana rozmiaru czcionki ograniczy czytelność strony.');
        	return false;
        }
    },
    reset: function(n) { // reset font size
    	if (this.actualZoom < 0)
    		while (this.actualZoom < 0) this.increment(n);
    	else 
    		while (this.actualZoom > 0) this.decrement(n);
    },
    zoom: function(n) { // set font size from cookie
       
    	var i = this.actualZoom;
    	this.actualZoom = 0;
    	
    	if (i < 0)
    		while (i < 0) {
    			this.decrement(n);
    			i++;
    		}
    	else 
    		while (i > 0) {
    			this.increment(n);
    			i--;
    		}
    }
}


// when page is loaded zoom font size for elements in object
$(document).ready(function(){
    fontsize.zoom( '.main_content' );  
});


