/**
 * Styleswitch stylesheet switcher built on jQuery
 * Under an Attribution, Share Alike License
 * By Kelvin Luck ( http://www.kelvinluck.com/ )
 **
 * Modified by: Jakub Konefał ( http://www.studio85.pl )
 * 2009-12-24, Jakub Konefał: Switch FontSize and StyleSheet at once.
 * 2010-01-06, Jakub Konefał: Added Key Events
 **/

(function($)
{
  $(document).ready(function()
  {
    // StyleSheed Switcher:
    $('.cssSwitch').click(function()
    {
      switchStylesheet(this.getAttribute("rel"));
      return false;
    });
    if( l = readCookie('layout') ) switchStylesheet('layout-'+l);
    if( f = readCookie('font') ) switchStylesheet('font-'+f);
    // Key Events:
    $(document).keyup(function(e){if(k=_action(e,'u'))_return(e);switchStylesheet(k);});
    $(document).keydown(function(e){if(k=_action(e,'d'))_return(e);switchStylesheet(k);});
    //$(document).keypress(function(e){if(_action(e))_return(e);});
  });

  function switchStylesheet(styleName)
  {
    if( (styleName == 0) || (styleName == 1) ) return;
    // 2009-12-24,JK: Read the type of the style.
    var cooked = styleName.split("-",2);
    if( cooked[0] && cooked[1] )
    {
      // Create pattern:
      switch( cooked[0] )
      {
        case "layout":
          patt = /layout/i;
          break;
        case "font":
          patt = /font/i;
          break;
        default:
          patt = /ERROR/;
          break;
      }
      // Create Cookie:
      createCookie(cooked[0],cooked[1],365);
    }
    // Enable/Disable Stylesheet:
		$('link[rel*=style][title]').each(function(i) 
		{
      if( patt.test( this.getAttribute('title') ) == true)
      {
        this.disabled = true;
        if( this.getAttribute('title') == styleName ) this.disabled = false;
      }
		});
	}
})(jQuery);
// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}
// /cookie functions


/**
 * Key Event for StyleSheet Switcher
 * By Jakub Konefał ( http://www.studio85.pl/ )
 **/

function _preview(e)
{
  alert("key"
    + "\n keyCode = " + e.keyCode
    /*+ "\n which = " + e.which*/
    /*+ "\n charCode = " + e.charCode*/
    + "\n shiftKey = " + e.shiftKey
    + "\n ctrlKey = " + e.ctrlKey
    + "\n altKey = " + e.altKey
    + "\n metaKey = " + e.metaKey
  );
}

function _action(e,w)
{
  // Actions of the keys:
  if( e.keyCode == 112 )
  {
    if(w=='u') return 0;
    window.location.href = "/klawisze-dostepu-access-key,pl.html#pomoc";
    return 1;
  }
  else if( (e.shiftKey == false) && (e.ctrlKey == false) && (e.altKey == true) )
  {
    if(e.keyCode == 49)
    {
      return "font-10";
      return 1;
    }
    else if(e.keyCode == 50)
    {
      return "font-12";
      return 1;
    }
    else if(e.keyCode == 51)
    {
      return "font-14";
      return 1;
    }
    else if(e.keyCode == 52)
    {
      return "font-16";
      return 1;
    }
    else if(e.keyCode == 67)
    {
      if(w=='u') return 0;
      layout = readCookie("layout");
      switch(layout)
      {
        case "contrast":
          return "layout-default";
          break;
        default:
          return "layout-contrast";
          break;
      }
    }
    else if(e.keyCode == 18)
    {
      // AltKey
      return 0;
    }
    else
    {
      // Preview keyCode:
      //_preview(e);
    }
  }
  else
  {
  }
  return 0;
}

function _return(e)
{
  if(e.returnValue) e.returnValue = false;
  if(e.preventDefault) e.preventDefault();
  if(e.stopPropagation) e.stopPropagation();
}
