/**
 * Parses markdown tables that are in the following format:
 * |Testing | Testing | Testing |
 * | - | - | - |
 * | row 1 | row 1 | row 1 |
 */

function parse_tables($text)
{

  var $newtext = '';
  var $lines = '';
  var $newlines = new Array();
  
  //Break the file into lines
  var $lines  = $text.split("\n");
  
  //Add the tags
  for ( var i in $lines )
  {
    //See if this is the start of line is a table
    if($lines[i].substring(0,1) == '|' || $lines[i].substring(0,4) == '<p>|')
    {
      //Make sure it also ends with a pipe
      if($lines[i].substring($lines[i].length-1) == '|' || $lines[i].substring($lines[i].length-5) == '|</p>')
      {
        
        //Remove the beginning and ending pipe and add the start and end of the row
        if($lines[i].substring(0,1) == '|') { $lines[i] = $lines[i].substring(1); }
        if($lines[i].substring(0,4) == '<p>|') { $lines[i] = $lines[i].substring(4);}
        if($lines[i].substring($lines[i].length-1) == '|') {$lines[i] = $lines[i].substring(0,$lines[i].length-1);}
        if($lines[i].substring($lines[i].length-5) == '|</p>') {$lines[i] = $lines[i].substring(0,$lines[i].length-5);}
        $lines[i] = '<tr><td>'+$lines[i]+'</td></tr>';
        
        //Add the column separators
        $lines[i] = $lines[i].replace('|', '</td><td>');
        
        //See if this is the start of the table because the next line would have | -
        try 
        {
          var next = parseInt(i)+1; //Conver to number and add 1
          next = next+''; //Convert back to string
          if ($lines[next].substring(0, 3) == '| -' || $lines[next].substring(0, 2) == '|-') 
          {
            $lines[i] = '<table>'+$lines[i];
                        
            //Remove that line because we don't want to actually parse it
            $lines[next] = '';
          }
        } 
        catch (e){};
        
        //See if this is the end of the table because the next line would have no starting and ending pipe or is undefined
        try 
        {
          if ($lines[next] == undefined) {
            $lines[i] = $lines[i]+'</table>';
          }
          else if ($lines[next] == '')
          {
            //Do nothing because this is the line we just wiped out after starting the table
          }
          else if ($lines[next].substring(0, 1) == '|' || $lines[next].substring(0, 4) == '<p>|') {
             //Make sure it also ends with a pipe
            if ($lines[next].substring($lines[next].length - 1) == '|' || $lines[next].substring($lines[next].length - 5) == '|</p>') {
            }
            else {
              $lines[i] = $lines[i]+'</table>';
            }
          }
          else {
            $lines[i] = $lines[i]+'</table>';
          }
        } 
        catch (e) {};
      }
      
    }
    
    
    $newlines[i] = $lines[i];
  } 
  
  for (var i in $newlines) 
  {
    $newtext += $newlines[i] + "\n";
  }
 
 return $newtext;
}

/**
 * Parses smileys live - Acts like the parse_smileys function in codeigniter
 */

function parse_smileys($text, $smiley_image_url)
{
  var $smileys_array = get_smileys_array();
	for($key in $smileys_array)
	{
		$text = $text.replace($key, "<img src=\""+$smiley_image_url+$smileys_array[$key][0]+"\" width=\""+$smileys_array[$key][1]+"\" height=\""+$smileys_array[$key][2]+"\" alt=\""+$smileys_array[$key][3]+"\" style=\"border:0;\" />");
	}
  
  return $text;
}

/*
 * Returns the smileys array
*/
function get_smileys_array()
{
  var smileys_array = new Array();
  
  //	smiley			         image name						width	height	alt 
  smileys_array[':-)'] = new Array('grin.gif',			'19',	'19',	'grin');
	smileys_array[':lol:'] = new Array('lol.gif',			'19',	'19',	'LOL');
	smileys_array[':cheese:'] = new Array('cheese.gif',			'19',	'19',	'cheese');
	smileys_array[':)'] = new Array('smile.gif',			'19',	'19',	'smile');
	smileys_array[';-)'] = new Array('wink.gif',			'19',	'19',	'wink');
	smileys_array[';)'] = new Array('wink.gif',			'19',	'19',	'wink');
	smileys_array[':smirk:'] = new Array('smirk.gif',			'19',	'19',	'smirk');
	smileys_array[':roll:'] = new Array('rolleyes.gif',		'19',	'19',	'rolleyes');
	smileys_array[':-S'] = new Array('confused.gif',		'19',	'19',	'confused');
	smileys_array[':wow:'] = new Array('surprise.gif',		'19',	'19',	'surprised');
	smileys_array[':bug:'	] = new Array('bigsurprise.gif',	'19',	'19',	'big surprise');
	smileys_array[':-P'	] = new Array('tongue_laugh.gif',	'19',	'19',	'tongue laugh');
	smileys_array['%-P'] = new Array('tongue_rolleye.gif',	'19',	'19',	'tongue rolleye');
	smileys_array[';-P'] = new Array('tongue_wink.gif',	'19',	'19',	'tongue wink');
	smileys_array[':P'	] = new Array('raspberry.gif',		'19',	'19',	'raspberry');
	smileys_array[':blank:'] = new Array('blank.gif',			'19',	'19',	'blank stare');
	smileys_array[':long:'] = new Array('longface.gif',		'19',	'19',	'long face');
	smileys_array[':ohh:'] = new Array('ohh.gif',			'19',	'19',	'ohh');
	smileys_array[':grrr:'] = new Array('grrr.gif',			'19',	'19',	'grrr');
	smileys_array[':gulp:'] = new Array('gulp.gif',			'19',	'19',	'gulp');
	smileys_array['8-/'	] = new Array('ohoh.gif',			'19',	'19',	'oh oh');
	smileys_array[':down:'] = new Array('downer.gif',			'19',	'19',	'downer');
	smileys_array[':red:'	] = new Array('embarrassed.gif',	'19',	'19',	'red face');
	smileys_array[':sick:'] = new Array('sick.gif',			'19',	'19',	'sick');
	smileys_array[':shut:'	] = new Array('shuteye.gif',		'19',	'19',	'shut eye');
	smileys_array[':-/'	] = new Array('hmm.gif',			'19',	'19',	'hmmm');
	smileys_array['>:('	] = new Array('mad.gif',			'19',	'19',	'mad');
	smileys_array[':mad:'	] = new Array('mad.gif',			'19',	'19',	'mad');
	smileys_array['>:-('	] = new Array('angry.gif',			'19',	'19',	'angry');
	smileys_array[':angry:'] = new Array('angry.gif',			'19',	'19',	'angry');
	smileys_array[':zip:'	] = new Array('zip.gif',			'19',	'19',	'zipper');
	smileys_array[':kiss:'] = new Array('kiss.gif',			'19',	'19',	'kiss');
	smileys_array[':ahhh:'] = new Array('shock.gif',			'19',	'19',	'shock');
	smileys_array[':coolsmile:'] = new Array('shade_smile.gif',	'19',	'19',	'cool smile');
	smileys_array[':coolsmirk:'	] = new Array('shade_smirk.gif',	'19',	'19',	'cool smirk');
	smileys_array[':coolgrin:'] = new Array('shade_grin.gif',		'19',	'19',	'cool grin');
	smileys_array[':coolhmm:'	] = new Array('shade_hmm.gif',		'19',	'19',	'cool hmm');
	smileys_array[':coolmad:'	] = new Array('shade_mad.gif',		'19',	'19',	'cool mad');
	smileys_array[':coolcheese:'] = new Array('shade_cheese.gif',	'19',	'19',	'cool cheese');
	smileys_array[':vampire:'] = new Array('vampire.gif',		'19',	'19',	'vampire');
	smileys_array[':snake:'	] = new Array('snake.gif',			'19',	'19',	'snake');
	smileys_array[':exclaim:'	] = new Array('exclaim.gif',		'19',	'19',	'excaim');
	smileys_array[':question:'] = new Array('question.gif',		'19',	'19',	'question');
  
  return smileys_array;
}
 