//Objects, Properties and Variables Oh My!
var p = {};
p.repeats = {};
p.special_chars = {
	"\\+"		:	"&#43;"
};

//Methods - Alphabetized
p.empty = function(str)
{
	return (str === null || str === undefined || str == "" || !str || str.length < 1) ? true : false;
};

p.replaceSpecial = function(str, dir)
{
	if (p.empty(str)) { return str; }
	var re = new RegExp();
	for (var i in p.special_chars) {
		re = new RegExp((dir === true) ? p.special_chars[i] : i, 'gi');
		str = str.replace(re, (dir === true) ? i.replace(/\\/,'') : p.special_chars[i]);
	}
	return str;
};

p.trim = function(str)
{
	return (p.empty(str)) ? str : str.replace(/^(\s*|\n*|\r*|\t*)|(\s*|\n*|\r*|\t*)$/g,'');
};

p.urlDecode = function(str)
{
	if (p.empty(str)) { return str; }
	return p.replaceSpecial(decodeURIComponent(str), true);
};

p.urlEncode = function(str)
{
	if (p.empty(str)) { return str; }
	return encodeURIComponent(p.replaceSpecial(str));
};

p.catbox = function()
{
  var h = p.catHeight();
  $('div.catbox').each(function()
  {
    if (parseInt($(this).css('height')) > h) {
      //$(this).css('height', h + 'px');
    }
    $(this).click(function()
    { 
      var cat = ($(this).html() == 'M') ? 'music' : ($(this).html() == 'L') ? 'life' : 'code';
      location.href = '/category/' + cat + '/';
    });
    
    $(this).mouseover(function()
    {
      $(this).fadeTo('fast', 0.5);
    });
    $(this).mouseout(function()
    {
      $(this).fadeTo('fast', 1);
    });
  });
};

p.catHeight = function()
{
  var h = 0;
  $('div.date').each(function()
  {
    h = parseInt($(this).css('height'));
  });
  return h;
};

p.equalSize = function()
{
  var ch = $('#content').css('height');
  var sh = $('#sidebar').css('height');
  
  if (parseInt(ch) < parseInt(sh)) {
    $('#content').css('height', sh);
  }
};

p.highlight = function()
{
  if (SyntaxHighlighter !== undefined) {
    SyntaxHighlighter.defaults['tab-size'] = 2;
    SyntaxHighlighter.defaults['toolbar'] = false;
    SyntaxHighlighter.defaults['gutter'] = false;
    SyntaxHighlighter.all();
  }
};

p.submitComment = function()
{
  $('#submit-comment').click(function()
	{
		$(this).html('Processing...');
		var n = $('#your_name').val();
		var e = $('#email').val();
		var c = $('#comment').val();
		
		if (p.empty(n) || p.empty(e) || p.empty(c)) {
			alert('You must fill in all the required fields.');
			$('#submit-comment').html('Submit Comment');
			return false;
		}
		
		$.ajax({
			type: "POST",
			cache: false,
			url: "/process/contact.php",
			dataType: "text",
			data: 'name=' + p.urlEncode(n) + '&email=' + p.urlEncode(e) + '&comment=' + p.urlEncode(c),
			success: function(msg)
			{
				$('#submit-comment').html('Submit Comment');
				msg = (msg == "ok") ? "Your message has been successfully sent." : "There was an error sending your message, please try again.";
				$('#comment-message').html(msg).fadeIn('fast', function()
				{
					setTimeout(function()
					{
						$('#comment-message').fadeOut('fast');
					}, 5000);
				});

			}
		});
		
		return false;
	});
};

p.tags = function()
{
  $('#sidebar div.tag').each(function()
  {
    $(this).click(function() { location.href = '/tag/' + $(this).attr('id') + '/'; });
    $(this).mouseover(function()
    {
      $(this).fadeTo('fast', 0.5);
    });
    $(this).mouseout(function()
    {
      $(this).fadeTo('fast', 1);
    });
  });
};





$(document).ready(function()
{
  p.highlight();
  p.catbox();
  p.tags();
  p.equalSize();
  p.submitComment();
});