// Copyright (C) Garrett Murray <garre.tt>

function gebi(e)
{
  return document.getElementById(e);
}

window.onscroll = function()
{
  if (window.XMLHttpRequest)
  {
    e = gebi('sticky');
    s = document.documentElement.scrollTop;
    x = self.pageYOffset;
    if (s > 319 || x > 319)
    {
      e.style.position = 'fixed'; 
      e.style.top = '0px';
      e.style.paddingLeft = '0px';
      e.className = 'shadowed';
    }
    else
    {
      e.style.position = 'absolute'; 
      e.style.top = '320px';
      e.className = '';
    }
  }
}

window.onload = function()
{
  new Image().src = 'http://static.maniacalrage.net/images/highresremix/gm_cl.png';
  rf = gebi('randFact');
  facts = Array(
    'bad dancer', 'gadget freak', 'cat lover',
    'television addict', 'apple fan', 'workaholic',
    'all-around good guy', 'southpaw', 'emotional robot',
    'tall and skinny', 'obsessive', 'likes t-shirts',
    'argumentative', 'entomophobic', 'root beer drinker',
    'avid recycler'
  );
  rf.innerHTML = ', '+facts[Math.floor(Math.random()*facts.length)];

	e = gebi('post_notes');
	if (e)
		clean_notes(e);
}

function clean_notes(notesBlock)
{
	answers = getElementsByClass('answer', null, 'li');
	replies = getElementsByClass('reply', null, 'li');
	likes = getElementsByClass('like', null, 'li');
	reblogs = getElementsByClass('reblog', null, 'li');
	notesBlock.innerHTML = '';

	answerBlock = '';	
	replyBlock = '';
	likeBlock = '';
	reblogBlock = '';
	
	if (answers)
	{
		totalAnswers = answers.length;
		if (totalAnswers > 0)
		{
			for (var ai = 0; ai < totalAnswers; ai++)
				answerBlock += '<li class="note answer">'+answers[ai].innerHTML+'</li>';
		}
	}
	if (replies)
	{
		totalReplies = replies.length;
		if (totalReplies > 0)
		{
			for (var ri = 0; ri < totalReplies; ri++)
				replyBlock += '<li class="note reply">'+replies[ri].innerHTML+'</li>';
		}
	}
	if (likes)
	{
		totalLikes = likes.length;
		if (totalLikes > 0)
		{
			for (var ni = 0; ni < totalLikes; ni++)
				likeBlock += '<li class="note like">'+likes[ni].innerHTML+'</li>';
		}
	}
	if (reblogs)
	{
		totalReblogs = reblogs.length;
		if (totalReblogs > 0)
		{
			for (var bi = 0; bi < totalReblogs; bi++)
			{
				if (!hasClass(reblogs[bi], 'tumblelog_maniacalrage') && !hasClass(reblogs[bi], 'without_commentary'))
					reblogBlock += '<li class="note reblog">'+reblogs[bi].innerHTML+'</li>';
			}
		}
	}
	newInner = (answerBlock+replyBlock+reblogBlock+likeBlock != '' ? '///<br/>' : '');
	newInner += (answerBlock != '' ? '<div class="notes_recent_label answers">Recent answers</div><ol class="notes">' + answerBlock + '</ol>' : '');
	newInner += (replyBlock != '' ? '<div class="notes_recent_label replies">Recent replies</div><ol class="notes">' + replyBlock + '</ol>' : '');
	newInner += (reblogBlock != '' ? '<div class="notes_recent_label reblogs">Recent reblogs</div><ol class="notes">' + reblogBlock + '</ol>' : '');
	newInner += (likeBlock != '' ? '<div class="notes_recent_label likes">Recent likes</div><ol class="notes likes">' + likeBlock + '</ol>' : '');
	notesBlock.innerHTML = newInner;
	notesBlock.style.display = 'block';
}

function hasClass(obj, className)
{
	var re = new RegExp("(^|\\s)" + className + "(\\s|$)");
	if (obj.className)
	  return re.test(obj.className);
	return false;
}

function getElementsByClass(searchClass, node, tag)
{
	var classElements = new Array();
	if (node == null)
		node = document;
	if (tag == null)
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++)
	{
		if (pattern.test(els[i].className))
		{
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function resize_video(v)
{
  try
  {
    var video_width = parseInt(1024);
    v.height = Math.floor(video_width / (v.width / v.height));
    v.width = video_width;
  }
  catch(e)
  {
    console.log(e);
  }
}

function fix_videos()
{
  videos = getElementsByClass('video content', null, 'div');
  for (var v = 0; v < videos.length; v++)
	{
		embeds = videos[v].getElementsByTagName('embed');
		iframes = videos[v].getElementsByTagName('iframe');
		vobjects = videos[v].getElementsByTagName('object');
	}
	if (embeds)
	{
		for (var ve = 0; ve < embeds.length; ve++)
			resize_video(embeds[ve]);
	}
	if (iframes)
	{
		for (var vi = 0; vi < iframes.length; vi++)
			resize_video(iframes[vi]);
	}
	if (vobjects)
	{
		for (var vo = 0; vo < vobjects.length; vo++)
			resize_video(vobjects[vo]);
	}
}

