/**
 * Made by Thomas Manson
 * http://mansonthomas.com
 * 2008-02-16
 */

/*
Add flags to my link list
*/
function linkListCustomize()
{
  $$('#LinkList1 a').each(function(item){
    if(item.innerHTML.indexOf('#{')>-1)
      item.innerHTML = (new Template(item.innerHTML).evaluate({en:'<img class="clickable" src="http://mansonthomas.com/myicons/flag/gb.png" alt="English written website"/>',fr:'<img class="clickable" src="http://mansonthomas.com/myicons/flag/fr.png" alt="French written website"/>'}));
    });

  $$('#LinkList2 a').each(function(item){
    if(item.innerHTML.indexOf('#{')>-1)
      item.innerHTML = (new Template(item.innerHTML).evaluate({en:'<img class="clickable" src="http://mansonthomas.com/myicons/flag/gb.png" alt="English written website"/>',fr:'<img class="clickable" src="http://mansonthomas.com/myicons/flag/fr.png" alt="French written website"/>'}));
    });
}

/*
Add expand/collapse functionnality to <pre></pre>
*/
function preCustomize()
{
  var i=0;
  $$('pre').each(function(item){
    item.id="pre_"+i;
    if(!(item.className.indexOf('noexpand')>-1))
    {
      item.style.position='relative';
      item.insert({top:'<img class="clickable" style="display:block;" id="pre_img_'+i+'" src="http://mansonthomas.com/myicons/icons/arrow_out.png" alt="Expand this zone !" onClick="preToggle('+i+')"/>'});
      item.insert({after:'<div id="div_'+item.id+'" style="display:none;">&nbsp;</div>'});
    }
    i++;
  });
}
var daggableList=Array();
/*Toggle between expanded and collapsed mode on pre*/
function preToggle(i)
{
  var img = $('pre_img_'+i);
  var div = $('div_pre_'+i);
  var pre = $('pre_'    +i);

  if(img.src.indexOf('arrow_out')>-1)
  {
    img.src="http://mansonthomas.com/myicons/icons/arrow_in.png";
    img.alt='Collapse this zone';
    pre.style.zIndex='9999';

    if( pre.original == null)
    {
      pre.original={
        top   :pre.cumulativeOffset().top,
        left  :pre.cumulativeOffset().left,
        width :pre.getWidth(),
        height:pre.getHeight()
      };
    }
    div.style.display="block";
    div.style.height=pre.getHeight()+'px';

    pre.absolutize();
    var newWidth       = document.viewport.getWidth()-100;
    new Effect.Morph('pre_'+i,{style:'top:'+pre.original.top+'px;left:50px;width:'+newWidth+'px;height:'+pre.original.height+'px;'});//top & height for ie6 that sucks as usual
    daggableList["pre_"+i]=new Draggable("pre_"+i);
  }
  else
  {
    img.src="http://mansonthomas.com/myicons/icons/arrow_out.png";
    img.alt='Expand this zone';
    
    pre.style.position='';
    div.style.display="none";
    
    new Effect.Morph('pre_'+i,{style:'left:'+pre.original.left+'px;width:'+pre.original.width+'px;'});
    daggableList["pre_"+i].destroy();
  }
}

function preSyntaxHighlighting()
{
/*
  if (!window.shr) window.shr = new SWFHttpRequest();
  
    shr.open( 'GET', 'http://mansonthomas.com/blogger/syntaxHighlight.php');
    shr.onreadystatechange = function(){
        console.log(this);
        $('status').innerHTML = 'readyState = ' + this.readyState + ', status = ' + this.status;
        $('status').style.display = '';
        if (this.readyState!=4) return;
        if (this.status==200) {
            $('result').innerHTML = this.responseText.
                replace(new RegExp('<','g'),'&lt;').
                replace(new RegExp('>','g'),'&gt;').
                replace(new RegExp('(\\r)?\\n','g'),"<br />");
        } else {
            $('result').innerHTML = '<span style="color:red">Oops - something went horribly wrong :(</span>';
        
      }
    };
  
  
  var shr = new SWFHttpRequest();
shr.open( 'GET', 'http://mansonthomas.com/blogger/syntaxHighlight.php' );
shr.onreadystatechange = function(){
    if (this.readyState!=4) return;
    if (this.status==200) {
        alert('success '+this.responseText);// Success! Take appropriate action with this.responseText
    } else {
      alert('Oops - something went wrong :(');
    }
};
shr.send( 'code=phpcode&language=php' );
       */

 /*  
  new Ajax.Request('http://mansonthomas.com/blogger/syntaxHighlight.php',
  {
    method:'post',
    parameters:{code:'<?php phpinfo(); ?>', language:'php'},
    onSuccess: function(transport){
      var response = transport.responseText || "no response text";
      alert("Success! \n\n" + response);
    },
    onFailure: function(){ alert('Something went wrong...') }
  });
   */

}


function aboutMeCustumize()
{
    $$('#Profile1 .profile-textblock').each(function(item){
    if(item.innerHTML.indexOf('#{')>-1)
      item.innerHTML = (new Template(item.innerHTML).evaluate({p:'<p>',pe:'</p>',b:'<b>',be:'</b>', target:'target="_new"'}));
    }); 
}

function init()
{
 linkListCustomize    ();
 preCustomize         ();
 preSyntaxHighlighting();  
 aboutMeCustumize     ();
}

function callOnLoad(init)
{
  if (window.addEventListener)
    window.addEventListener("load", init, false);
  else if (window.attachEvent)
    window.attachEvent("onload", init);
  else
    window.onload = init;
}
callOnLoad(init);