/*
    SNOOK.CA v6e


This is loading at the bottom of each page

window.onload = function()
{
    SNOOK.prepareCommentForm();
    SNOOK.prepareLivePreview();
}
*/

Date.prototype.addYear = function()
{
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0) this.setTime(this.getTime() - skew);
    this.setTime(this.getTime() + 365 * 24 * 60 * 60 * 1000);
}

/* =======================================================
		SNOOK NAMESPACE
		you gotta keep em separated.
*/

var SNOOK = {};

SNOOK.prepareCommentForm = function()
{
    // ========================================
		// load cookie info
		// sets default values for form fields
		if($('comments_form'))
		{
      this.prepareField({field:'email',text:'E-mail address',record:true,clear:true});
      this.prepareField({field:'author',text:'Name',record:true,clear:true});
      this.prepareField({field:'url',text:'http://',record:true,clear:false});
      this.prepareField({field:'CommentBody',record:false,clear:true});

      /*
      var lp = $('livepreview');
      lp.style.left = '-9999px';
      lp.style.display = 'block';
      var cfx = new fx.Height(lp,{duration:250});
      cfx.hide();
      $('btnpreview').onclick = function()
      {
         lp.style.left = lp.style.left == '50%' ? '-9999px' : '50%';
         cfx.toggle();
         SNOOK.prepareLivePreview();
      }
      */

      $('comments_form').onsubmit = function()
			{
			  var els = this.getElementsByTagName("*");
			  for(var i=0,j=els.length;i<j;i++)
			  {
			    if(els[i].noValue)
			    {
			      if(els[i].value == els[i].noValue) els[i].value = '';
			      if(els[i].record) SNOOK.remember(els[i]);
			    }
			  }
			}
		}

    // ========================================
		// set up fixed/static switching
		var el = $('postcomment');
		var isie = false;
		/*@cc_on @*/
    /*@if(true){isie=parseInt(navigator.appVersion.split(' ')[3])<7};
    /*@end @*/
		if(el && !isie)
		{
  		var btn = document.createElement('div');
  		btn.id = 'commentswitcher';
  		btn.onclick = function()
  		{
  		  var now = new Date();
        now.addYear();

   		  if(Element.hasClassName(this,'static'))
   		  {
   		      this.className = 'fixed';
   		      // this.style.top = '0';
   		      SNOOK.setCookie('snook-cp', 'fixed', now);
   		  } else {
   		      this.className = 'static';
   		      // this.style.top = 0;
   		      this.style.left = 0; /* reset position */
   		      SNOOK.setCookie('snook-cp', 'static', now);
   		  };
   		}.bind(el);
  		el.appendChild(btn);
  		el.className = SNOOK.getCookie('snook-cp') == 'static' ? 'static':'fixed';
  	}

}



SNOOK.prepareLivePreview = function()
{
      // return; // feature disabled for the time being
      var el = $('livepreview');
      var txten;
      if(!el)return;
			el.style.display = 'block'; // bring the livepreview to life!

			var txt = $("CommentBody");
			var auth = $("author");
			var url = $("url");
			var ison = false;

			// $('lpswitch').innerHTML = "Turn off Live Preview";
			$('lpswitch').onclick = function()
			{
			  if(ison)
			  {
			    url.onkeyup=auth.onkeyup=txt.onkeyup = null;
			    $('lpswitch').innerHTML = "Turn on Live Preview";
			    ison = false;
			  }else{
          url.onkeyup=auth.onkeyup=setMetaData;
          txt.onkeyup=setCommentText;
          setCommentText();
          setMetaData();
			    $('lpswitch').innerHTML = "Turn off Live Preview";
			    ison = true;
			  }

			}

      function setCommentText()
      {
	txten = txt.value.replace(/\n/g, "<br/>");
	txten = txten.replace(":)", "<img src=\"/repti2/img/s-smile.png\"/>");
	txten = txten.replace(":D", "<img src=\"/repti2/img/s-grin.png\"/>");

	$('lptext').innerHTML = '<p>' + txten  + '</p>';


        //$('lptext').innerHTML = '<p>' + txt.value.replace(/\n/g, "<br/>") + '</p>';
      }

			function setMetaData()
			{

      	var pre = '', post = '';
      	if(url.value.length > 0 && url.value != 'http://')
      	{
      		pre += '<a href="'+url.value+'">';
      		post += '</a>';
      	}

      	var dt = new Date();
  			var ms = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
  			var hr = (dt.getHours()==0?12:(dt.getHours()>12?dt.getHours()-12:dt.getHours()));

  			var cpd = $('lpdate');
  			cpd.innerHTML = ms[dt.getMonth()] + ' ' + dt.getDate() + ', ' + dt.getFullYear();

      	var lp = $("lpposter");
      	lp.innerHTML = pre + auth.value + post;
			}

			$('lpswitch').onclick();

}


SNOOK.prepareField = function(options)
{
  var opt = {
      field: '',
      text: '',
      record: false, /* record value to a cookie on submit */
      clear: false, /* clear field on focus */
      restore: true /* restore field on blur */
    };
  Object.extend(opt, options);

  var el = $(opt.field);
  if(el) // make sure field exists
  {
      if(opt.record) el.value = this.getCookie("snook" + opt.field);
      el.previousSibling.style.display = 'none';
      el.noValue = opt.text;
      el.record = opt.record;
      if(!el.value) el.value = opt.text;
      if(opt.clear){ el.onfocus = function(){ if(this.value == this.noValue){this.value = '';} }; };
      if(opt.restore){ el.onblur = function(){ if(this.value == ''){this.value = this.noValue;} }; };
  }
}

// Cookie code Copyright (c) 1996-1997 Athenia Associates.
// http://www.webreference.com/js/
// License is granted if and only if this entire
// copyright notice is included. By Tomer Shiran.
// modifed for Snook namespace


SNOOK.setCookie = function (name, value, expires) {
    var domain = 'snook.ca';
    var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((domain) ? "; domain=" + domain : "") + "; path=/";
    document.cookie = curCookie;
}

SNOOK.getCookie = function(name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)
        return nullstring;
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}


SNOOK.remember = function(fld) {
    var now = new Date();
    now.addYear();
    this.setCookie('snook' + fld.id, fld.value, now);
}

