/**
 * @projectDescription Styleswitch-Class
 * @author Simon Duss
 * @version 1.001 (14.02.2009)
 *
 * @copyright Flydesign.ch
 */

var fd_styleswitch = (function () {
  /* private vars */
  var thisObject = this;
	  //Variables
  /** defaultvalue for the screen stylesheet (title)
    * @type {string} */  
  var title_default    = "Standardansicht";   
  /** defaultvalue for the print stylesheet (title)
   * @type {string} */  
  var title_print      = "Druckvorschau";
	/** activeStyleSheet (title)
	 * @type {string} active_style */
	var active_style     = title_default;
	
  /**
  * setActiveStyleSheet
  * @version 1.000 (09.02.2009)
  * @private
  * @param {string} i_title Title-Value of the Link-Tag
  */
  function setActiveStyleSheet(i_title){
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
	  if(a.getAttribute("rel").toLowerCase().indexOf("style") != -1 && a.getAttribute("title")) {
	    a.disabled = true;
	    if(a.getAttribute("title") == i_title) {
				a.disabled = false;
				active_style = i_title;
			}
	  }
	}
  }
  
 /**
  * addprintbar
  * @version 1.000 (14.02.2009)
  * @private
  */
  function addprintbar(){
	var printbar = document.createElement("p");
	printbar.id = "printbar";
	printbar.className = "printbar";

	var printbutton = document.createElement("a");
	printbutton.appendChild(document.createTextNode("Dokument ausdrucken"));
	printbutton.title="Dokument ausdrucken";
	printbutton.href="javascript:window.print()";
	
	printbar.appendChild(printbutton);
	
	var closebutton = document.createElement("a");
	closebutton.className = "closebutton";
	closebutton.appendChild(document.createTextNode("Druckvorschau schliessen"));
	closebutton.title="Druckvorschau schliessen";
	closebutton.href="javascript:fd_styleswitch.toggleStyle()";
	
	printbar.appendChild(closebutton);
	
	document.body.appendChild(printbar);
  }
  
 /**
  * addprintbar
  * @version 1.000 (14.02.2009)
  * @private
  */
  function del_printbar(){
  	if(document.getElementById("printbar")){
		var body = document.getElementsByTagName("body")[0];
		body.removeChild(document.getElementById("printbar"));			
	}
  }
	  
  return {
   
    /**
    * Initialize the Class
    * @version 1.000 (09.02.2009)
    * @alias fd_styleswitch.init
    * @param {string} i_title_default
		* @param {string} i_title_print
    */
    init:function(i_title_default, i_title_print){
      if(typeof(i_title_default)== "string"){
				thisObject.title_default = i_title_default;
			}
			
			if(typeof(i_title_print)== "string"){
				thisObject.title_print = i_title_print;
			}
    },
		
    /**
    * Show Print Stylesheet
    * @version 1.000 (09.02.2009)
    * @alias fd_styleswitch.showPrintStyle
    */
		showPrintStyle:function(){
			addprintbar();
			setActiveStyleSheet(title_print);
		},
		
    /**
    * Show Default Stylesheet
    * @version 1.000 (09.02.2009)
    * @alias fd_styleswitch.showDefaultStyle
    */
		showDefaultStyle:function(){
			del_printbar();
			setActiveStyleSheet(title_default);
		},
		
	 /**
    * Show Default Stylesheet
    * @version 1.000 (09.02.2009)
    * @alias fd_styleswitch.toggleStyle
    */
		toggleStyle:function(){
			if(active_style == title_default){
				fd_styleswitch.showPrintStyle();
			}
			else if(active_style == title_print){
				fd_styleswitch.showDefaultStyle();
			}
		}		

  };
})();

