/*
   SiteComponents version:
   Id: fontsize.js,v 1.4 2009/03/27 07:54:02 kurt Exp 
   Name: SC_6_6_0 
   
   Disclaimer
   
   While we make every effort to ensure that this code is fit for its intended
   purpose, we make no guarantees as to its functionality. CoreTrek AS will
   accept no responsibility for the loss of data or any other damage or
   financial loss caused by use of this code.
   
   Copyright
   
   This programming code is copyright of CoreTrek AS. Permission to run this
   code is given to approved users of CoreTrek's publishing system CorePublish.
   
   This source code may not be copied, modified or otherwise repurposed for use
   by a third party without the written permission of CoreTrek AS.
   
   Contact webmaster@coretrek.com for information.
  
*/

/*

    ============================================================================
    IMPORTANT! This javascript is dependent on Prototype.
    ============================================================================
    
    Class that make it possible to changes fontsize.
    We have two links, larger and smaller, for changing fontsize.
    Fonsize can endless increase/decrease with 1px for each click on the links.
    
 */  
var NorskdesignFontsize = Class.create({

    initialize: function() {
    	// Set the interval for how muzh fontsize should increase/decrease in each click. Value given as px.
    	this.fontsizeChangeInterval = 1; //px
    	
    	this.cookie = new CtCookie();
    	
    	// Set fontsize from cookie
    	this.initializeFromCookie();
        
    	// Add click listener for larger font size
        $$('a.fontsize-increase').each(function(element) {
            element.observe('click', this.changeFontsizeListener.bindAsEventListener(this,'increase'));
        }.bind(this));
        
        // Add click listener for smaller font size
        $$('a.fontsize-decrease').each(function(element) {
            element.observe('click', this.changeFontsizeListener.bindAsEventListener(this,'decrease'));
        }.bind(this));
    },
    
    /**
    * ClickListener for both increase and decrease of fontsize.
    * 
    * @param event - The event that is trigged
    * @changeType - Specifies if fontsize should increase or decrease
    */
    changeFontsizeListener: function(event,changeType) {
    	event.stop();
    	
    	// Get the current fontsize
    	var currentFontsize = this.getCurrentFontsize();
    	
    	// Set new fontsize to be current font size, in case nothing happens in the switch
    	var newFontsize = currentFontsize;
    	
    	// Calculate new fontsize based on changeType
    	switch(changeType) {
    		case "increase":
    			newFontsize = currentFontsize*1 + this.fontsizeChangeInterval;
    			break;
    		case "decrease":
    			newFontsize = currentFontsize - this.fontsizeChangeInterval;
    			break;
    	}
    	
    	this.setFontsize(newFontsize);
    	
    },
    
    /**
     * Set a font size in cookie and as body style. The provided style must
     * be used directly with a prototype setStyle argument. E.g. "12px".
     *
     * @param size string The font size without ending, e.g. "12"
     */
    setFontsize: function(size) {
    	// Put fontsize in cookie so it's kept while browsing the site
        this.cookie.set("fontsize", size);
        
        // Set new fontsize to body element
        $(document.body).setStyle({
            fontSize: size+"px"
        });
        
        // Adjust height of li-elements again (also done on body onload) since the height must be changed
        // when fontsize changes
        splitArticlelistHeightCorrection();
        
    },
   
    /**
    * Get the current fontsize so that we know what fontsize to calculate new fontsize to
    * 
    * The function tries first to get the fontsize from cookie. If this is not set, the fontsize set
    * as style on body tag is returned.
    * 
    * @return currentFontsize
    */
    getCurrentFontsize: function() {
    	
    	var currentFontsizeCookie = this.cookie.get("fontsize");
    	
    	var currentFontsize = 0;
    	
    	// Check if we can use fontsize set in cookie
    	if(currentFontsizeCookie > 0) {
    		currentFontsize =  currentFontsizeCookie;
    	}
    	else {
    		// Get the current font size. It's returned with the ending set in stylesheet, e.g. px, pt
	    	currentFontsize = $(document.body).getStyle('fontSize');
	    	
	    	// Remove the ending - removes both px and pt so that this script won't break if ending is changed in stylesheet
	    	currentFontsize = currentFontsize.gsub("px","");
	    	currentFontsize = currentFontsize.gsub("pt","");
    	}

    	return currentFontsize;
    },
    
    /**
     * Function used to initialize the font size from the one
     * stored in cookie. Must be called on or after dom:loaded event.
     */
    initializeFromCookie: function() {
    	
    	var currentFontsize = this.getCurrentFontsize();

    	this.setFontsize(currentFontsize);
    }
});