
var Meta = {
  defaultWidth:       'fixed',      // Options: fixed, liquid
  defaultFontSize:    'normal',     // Options: normal, large
  defaultBackground:  'grey',       // Options: green, paper, grey, pink, black
  searchPrompt:       'Search...'
};


// ------------------------------------------------------------------------
// |  MetaTools (preloader for body background only)                      |
// ------------------------------------------------------------------------

if (document.images) {
  preload_image_object = new Image();
  image_url = new Array();
  image_url[0] = "/images/meta_background-green.gif";
  image_url[1] = "/images/meta_background-paper.gif";
  image_url[2] = "/images/meta_background-grey.gif";
  image_url[3] = "/images/meta_background-pink.gif";
  image_url[4] = "/images/meta_background-black.gif";
    var i = 0;
    for(i=0; i<=4; i++) 
      preload_image_object.src = image_url[i];
}


// ------------------------------------------------------------------------
// |  Simple MetaTools class to track user prefs                          |
// ------------------------------------------------------------------------

var MetaTools = Class.create({
  
  initialize: function() {
    this.userWidth = this.readCookie('metawdth') || Meta.defaultWidth;
    this.userFont = this.readCookie('metafnt') || Meta.defaultFontSize;
    this.userBg = this.readCookie('metabg') || Meta.defaultBackground;
    this.enableUserStyle('metawdth',this.userWidth);
    this.enableUserStyle('metafnt',this.userFont);
    this.enableUserStyle('metabg',this.userBg);
  },
  
  enableUserStyle: function(tooltype,name) {
    var styleSheets = $$('link.'+tooltype).partition(function(s){ return s.id == tooltype+'_'+name });
    var onStyle = styleSheets.first().first();
    var offStyles = styleSheets.last();
    onStyle.disabled = false;
    offStyles.each(function(s){ s.disabled = true; });
    this.createCookie(tooltype,name,365);
  },
  
  createCookie: function(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=/";
  },
  
  readCookie: function(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;
  },
  
  eraseCookie: function(name) {
  	this.createCookie(name,"",-1);
  }
  
});

function createMetaTools() {
  window.metaTools = new MetaTools();
  return true;
}

var createdMetaTools = createMetaTools();


// -----------------------------------------------------------------------------------
// |  Safari Styled - MetaTool Search Interface, Inspired By:                        |
// |  http://www.artweb-design.de/2007/4/16/safari-beautiful-search-input-tag-fixed  |
// -----------------------------------------------------------------------------------

var MetaSearch = Class.create({
  
  initialize: function() {
    this.box = $('q');
    if (Prototype.Browser.WebKit) { this.faceLiftWebKit() }
    else { 
      this.setDefaultNonWebKit();
      this.box.writeAttribute('size','21');
      this.box.setStyle({ fontSize:'11px',padding:'1px'});
      this.box.onfocus = this.focusNonWebKit.bindAsEventListener(this);
      this.box.onblur = this.blurNonWebKit.bindAsEventListener(this);
    };
  },
  
  faceLiftWebKit: function() {
    this.box.type = 'search';
    this.box.writeAttribute('placeholder', Meta.searchPrompt);
    this.box.writeAttribute('autosave', window.location.host);
    this.box.writeAttribute('results','5');
    this.box.writeAttribute('size','22');
  },
  
  focusNonWebKit: function() {
    if (this.box.value == Meta.searchPrompt) {
      this.box.value = '';
      this.box.className = 'focus';
    }
  },
  
  setDefaultNonWebKit: function() {
    this.box.value = Meta.searchPrompt;
    this.box.className = '';
  },
  
  blurNonWebKit: function() {
    if (this.box.value == '') { this.setDefaultNonWebKit() };
  }
  
});

function createMetaSearch() { window.metaSearch = new MetaSearch(); }
Event.observe(window,'load',createMetaSearch);


// ------------------------------------------------------------------------
// |  MetaContent For:                                                    |
// |    * XHTML Strict (rel="external" to target="_blank" function)       |
// |      http://www.sitepoint.com/article/standards-compliant-world/     |
// |    * Reformat CodyRay to fit in content area.                        |
// ------------------------------------------------------------------------

var MetaContent = Class.create({
  
  initialize: function() {
    this.codeRays = $$('.CodeRay td.code pre');
    this.externalLinks = $$('a.external-link');
    this.setCodeRayWidths();
    this.setExternalLinks();
    window.onresize = this.setCodeRayWidths.bindAsEventListener(this);
  },
  
  setCodeRayWidths: function() {
    this.codeRays.each(function(cr){
      var contentWidth = $('contentarea-middle2').getWidth();
      var cntWidthPx = ((contentWidth-112)+'px');
      cr.ondblclick = '';
      cr.setStyle({width:cntWidthPx});
    });
  },
  
  setExternalLinks: function() {
    this.externalLinks.each(function(link){
      if (link.readAttribute('href')) { link.writeAttribute('target','blank') };
    });
  }
  
});

function createMetaContent() { window.metaContent = new MetaContent(); }
Event.observe(window,'load',createMetaContent);







