/* The Script is Dead
   Feel free to steal all this stuff as long as you keep
   your hands off my graphics and layout.
   
   Not that I believe this script will be very useful to
   anyone except me because it's totally tuned for my
   particular site. */


/********************************************************
 *
 * CJL_RegisterEvent()
 *
 * Copyright (C) 2002-2003, CodeHouse.com. All rights reserved.
 * CodeHouse(TM) is a registered trademark.
 *
 * THIS SOURCE CODE MAY BE USED FREELY PROVIDED THAT
 * IT IS NOT MODIFIED OR DISTRIBUTED, AND IT IS USED
 * ON A PUBLICLY ACCESSIBLE INTERNET WEB SITE.
 * 
 * CodeHouse.com JavaScript Library Module: Register Event Class
 *
 * You can obtain this script at http://www.codehouse.com
 ********************************************************/
 
function CJL_RegisterEvent(elem, type, listener, useCapture, noAutoStart)
{
   var proto = arguments.callee.prototype;

   this.e = elem;
   this.type = type;
   this.cap = useCapture;
   this.l = listener;


   proto.start = function()
   {
      if( this.e.attachEvent )
      {
         this.e.attachEvent("on" + this.type, this.l);
      }
      else if( this.e.addEventListener )
      {
         this.e.addEventListener(this.type, this.l, this.cap);
      }
   }

   if( ! noAutoStart )
   {
      this.start(elem, type, listener);
   }

   proto.stop = function()
   {
      if( this.e.detachEvent )
      {
          this.e.detachEvent("on" + this.type, this.l);
      }
      else if( this.e.removeEventListener )
      {
          this.e.removeEventListener(this.type, this.l, this.cap);
      }
   }
}


var isRunning = false;

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && document.all)? document.all : 
    oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

function makeActive(theTab)  {
    activeTab = getElementsByClassName(document, 'div', 'menutab');
    var FF = (document.getElementById && !document.all);
    for(var i = 0; i < activeTab.length; i++) {
    	  if(activeTab[i].style.display != 'none')  {
    	  	  Effect.Fade(activeTab[i], {queue:{scope:'myscope', position:'end'}, duration:0.5});
    	  }
      }
    Effect.Appear(theTab, {queue:{scope:'myscope', position:'end'}, duration:0.5});  
}

function resetTabs()  {
	$('menulink').style.background = "url(/weblog/pivot/templates/images/tab_main.jpg) top";
	$('archivelink').style.background = "url(/weblog/pivot/templates/images/tab_archives.jpg) top";
	$('commentarylink').style.background = "url(/weblog/pivot/templates/images/tab_commentary.jpg) top";
	$('contactlink').style.background = "url(/weblog/pivot/templates/images/tab_contact.jpg) top";	
	$('searchlink').style.background = "url(/weblog/weblog/pivot/templates/images/tab_search.jpg) top";	
}

function doMainMenu()  {
	resetTabs();
	makeActive('mainmenu');
	$('menulink').style.background = "url(/weblog/pivot/templates/images/tab_main.jpg) bottom";
}

function doCommentary()  {
	resetTabs();
	makeActive('commentary');
	$('commentarylink').style.background = "url(/weblog/pivot/templates/images/tab_commentary.jpg) bottom";
}

function doArchive()  {
	resetTabs();
	makeActive('archives');
	$('archivelink').style.background = "url(/weblog/pivot/templates/images/tab_archives.jpg) bottom";
}

function doContact()  {
	resetTabs();
	makeActive('contactme');
	$('contactlink').style.background = "url(/weblog/pivot/templates/images/tab_contact.jpg) bottom";
}

function doSearch()  {
	resetTabs();
	makeActive('search');
	$('searchlink').style.background = "url(/weblog/pivot/templates/images/tab_search.jpg) bottom";
}

function doSocial(e)  {
  if(!e) var e = window.event;
  if (e.target) targ = e.target;
  else if (e.srcElement) targ = e.srcElement;
  if (targ.nodeType == 3) // defeat Safari bug
  targ = targ.parentNode;
  Effect.toggle(targ.previousSibling, "blind", {duration:0.5});  
}

function initSearch() {
  var t = window.setTimeout("liveSearch()",2000);
}

function finishSearch()  {
	theResults = $('searchresults');
	if (theResults.innerHTML != '')  {
	result = new Effect.Appear('searchresults', {duration:0.5});
	  }
	else  {
		result = new Effect.Fade('searchresults', {duration:0.5});
	}
	  $('spinner').style.visibility = 'hidden';
}

function submitIt()  {
	$('searchme').submit();
}

function checkMail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }
 		 return true					
	}


function liveSearch()  {
	$('spinner').style.visibility = 'visible';
  searchTerm = $('searchbox').value;
  pars = 'search=' + searchTerm;
  theUrl = '/weblog/pivot/search2.php';
  var searchNow = new Ajax.Updater('searchresults', theUrl, {method: 'post', parameters: pars, onComplete: finishSearch});	
}

// Check a comment

function showNewComment()  {
	goComments = new Effect.ScrollTo('ankertje', {duration:0.1});
	$('cbuttons').style.display = 'none';
  $('commentposted').style.display = 'block';
  Effect.BlindDown('commentholder');
  Effect.Pulsate('commentposted');
  $('commentspinner').style.visibility = 'hidden';
}

function doComment()  {
	$('comerror').style.display = 'none';
	if($F('piv_name').length < 2)  {
		$('comerror').style.display = 'block';
		$('comerror').innerHTML = 'Error: please enter your name.';
		Effect.Pulsate('comerror');
		return false;
	}
	if($F('piv_email').length > 0)  {
		if(!checkMail($F('piv_email')))  {
		  $('comerror').style.display = 'block';
		  $('comerror').innerHTML = 'Error: if you enter an email address it should be a valid one.';
		  Effect.Pulsate('comerror');
		  return false;
		}
	}
	if($F('piv_comment').length < 5)  {
		$('comerror').style.display = 'block';
		$('comerror').innerHTML = 'Error: please enter a comment.';
		Effect.Pulsate('comerror');
		return false;		
	}
	$('commentspinner').style.visibility = 'visible';
  $('commentform').removeAttribute('onsubmit');
  
  pars = 'piv_comment=' + $F('piv_comment') +
          '&piv_url=' + $F('piv_url') +
          '&piv_name=' + $F('piv_name') +
          '&piv_comment=' + $F('piv_comment') +
          '&piv_email=' + $F('piv_email') +
          '&piv_spkey=' + $F('piv_spkey') +
          '&piv_code=' + $F('piv_code') +
          '&piv_rememberinfo=' + $F('piv_rememberinfo') +
          '&piv_notify=' + $F('piv_notify') +
          '&spammerspissoff=yes' +
          '&piv_weblog=' + $F('piv_weblog');
	  
          theUrl = '/weblog/pivot/ajax_submit.php';
          sendComment = new Ajax.Updater('commentholder', theUrl, {method: 'post', parameters: pars, onComplete: showNewComment});
}

// Send a mail with the contact form

function doMail()  {
	if($F('cmessage').length < 10)  {
	  $('cerror').style.display = 'block';
		$('cerror').innerHTML = 'Error: You didn\'t enter anything that looks like a message.';
		Effect.Pulsate('cerror');
		return false;		
	}
	if($F('cname') == '')  {
	  $('cerror').style.display = 'block';
		$('cerror').innerHTML = 'Error: please enter your name.';
		Effect.Pulsate('cerror');
		return false;		
	}
	if($F('csubject').length < 1)  {
	  $('cerror').style.display = 'block';
		$('cerror').innerHTML = 'Error: please enter a subject.';
		Effect.Pulsate('cerror');
		return false;		
	}
	if(!checkMail($F('cemail')))  {
	$('cerror').style.display = 'block';
		$('cerror').innerHTML = 'Error: please enter a valid email address.';
		Effect.Pulsate('cerror');
		return false;
	}
	else  {
	$('cspinner').style.visibility = 'visible';
	pars = 'subject=' + $F('csubject') + '&email=' + $F('cemail') + '&name=' + $F('cname') + '&url=' + $F('curl') + '&message=' + $F('cmessage');
  var doMail = new Ajax.Updater('contactform', '/weblog/pivot/mail.php', {method: 'post', parameters: pars, onComplete: finishMail});	
  }
}

function finishMail()  {
	$('cspinner').style.visibility = 'hidden';
}

function goHome()  {
	window.location.href = '/weblog/';
}

function doWarpComments()  {
	new Effect.ScrollTo('commentform');
}

// from taggerati

function doList(type, tag)  {if(type == 'delicious')  { ttype = 'del.icio.us'; } else { ttype = type; }$('tgrrsslist').innerHTML = '<img src="/weblog/pivot/../extensions/taggerati/loading_' + ttype + '.gif" alt=""/>';var pars = 'type=' + type + '&tag=' + tag;var url = '/stage/pivot/../extensions/taggerati/ajax_rsslist.php';var myAjax = new Ajax.Updater('tgrrsslist', url, {method: 'get', parameters: pars});}

function doMetaTab(e)  {
  if(!e) var e = window.event;
  if (e.target) targ = e.target;
  else if (e.srcElement) targ = e.srcElement;
  if (targ.nodeType == 3) // defeat Safari bug
  targ = targ.parentNode;
  Effect.toggle(targ.parentNode.previousSibling, "blind", {duration:0.5});
}

// Initialize everything

function attachBehaviors()  {
	if  ($('form'))  {
	  $('form').setAttribute("onsubmit", "return false;");
  }
	if  ($('commentform'))  {
	  $('commentform').setAttribute("onsubmit", "return false;");
  }  
	$('searchbox').setAttribute("autocomplete", "off");
	$('searchbox').value = '';
	new CJL_RegisterEvent($('menulink'), "click", doMainMenu);
	new CJL_RegisterEvent($('archivelink'), "click", doArchive);
	new CJL_RegisterEvent($('commentarylink'), "click", doCommentary);
	new CJL_RegisterEvent($('contactlink'), "click", doContact);		
	new CJL_RegisterEvent($('searchlink'), "click", doSearch);
	if($('commentwarp'))  {	
	  new CJL_RegisterEvent($('commentwarp'), "click", doWarpComments);
	  }
	new CJL_RegisterEvent($('searchbox'), "keypress", initSearch);
	new CJL_RegisterEvent($('caction'), "click", doMail);
	if($('preview'))  {
	  new CJL_RegisterEvent($('preview'), "click", doComment);
	  new CJL_RegisterEvent($('submit'), "click", doComment);
	}
	new CJL_RegisterEvent($('head'), "click", goHome);
	hiddenElements = document.getElementsByTagName('input');
	for(i=0;i<hiddenElements.length;i++)  {
	  if(hiddenElements[i].type == 'hidden')  {
	    if((hiddenElements[i].name == 'piv_spkey') || (hiddenElements[i].name == 'piv_code') || (hiddenElements[i].name == 'piv_weblog'))	{
			hiddenElements[i].setAttribute('id', hiddenElements[i].name);
	    	}
	    }
	  }
	metatabs = document.getElementsByClassName('metalink');
	for(i=0;i<metatabs.length;i++)  {
          new CJL_RegisterEvent(metatabs[i], "click", doMetaTab);
	  }
	stabs = document.getElementsByClassName('stab');
	for(i=0;i<stabs.length;i++)  {
          new CJL_RegisterEvent(stabs[i], "click", doSocial);
	  }
	headers = $('contentleft').getElementsByTagName('h1');
	headers[0].style.backgroundColor = '#161213';
}


function init() {       // quit if this function has already been called       if (arguments.callee.done) return;       // flag this function so we don't do the same thing twice       arguments.callee.done = true;       // create the "page loaded" message	attachBehaviors();   };   /* for Mozilla */   if (document.addEventListener) {       document.addEventListener("DOMContentLoaded", init, false);   }   /* for Internet Explorer */   /*@cc_on @*/   /*@if (@_win32)       document.write("<script defer src=/weblog/pivot/templates/javascript/ie_onload.js><"+"/script>");   /*@end @*/   /* for other browsers */   window.onload = init;




