// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function displayBadge(badge_id) {
	alert('hi');
  if (window.ActiveXObject) //IE
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
  else if (window.XMLHttpRequest) //other
    xhr = new XMLHttpRequest();
  else
    alert("Two Brains cannot get your goals (your browser doesn't support AJAX)");
  
	var base_url = "http://twobrains.liftlabs.com";
	var url = base_url + "/badges/goals/" + badge_id;
  xhr.open("GET", url, true);

	xhr.setRequestHeader("Cache-Control", "no-cache");
  xhr.setRequestHeader("Pragma", "no-cache");

  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
      if (xhr.status == 200) {
        if (xhr.responseText != null)
          processRSS(xhr.responseXML);
        else {
    	    alert("Failed to receive RSS file from Two Brains (file not found)");
          return false;
        }
      }
      else
    	  alert("Error code " + xhr.status + " received: " + xhr.statusText);
    }
  }
  xhr.send(null);
}

function processRSS(rssxml) {
  RSS = new RSS2Channel(rssxml);
  showRSS(RSS);
}

function RSS2Channel(rssxml) {
  // required string properties
  this.title;
  this.link;
  this.description;

  // optional string properties
  this.language;
  this.pubDate;

  // array of RSS2Item objects
  this.items = new Array();

	// collect RSS channel
	// collect RSS item elements
  var chanElement = rssxml.getElementsByTagName("channel")[0];
  var itemElements = rssxml.getElementsByTagName("item");

  for (var i=0; i<itemElements.length; i++)
  {
    Item = new RSS2Item(itemElements[i]);
    this.items.push(Item);
  }

  var properties = new Array("title", "link", "description", "language", "pubDate");
  var tmpElement = null;
  for (var i=0; i<properties.length; i++)
  {
    tmpElement = chanElement.getElementsByTagName(properties[i])[0];
    if (tmpElement != null)
      eval("this." + properties[i] + "=tmpElement.childNodes[0].nodeValue");
  }
}

function RSS2Item(itemxml) {
  // required properties
  this.title;
  this.link;
  this.description;

  // optional properties
  this.pubDate;

  var properties = new Array("title", "link", "description", "pubDate");
  var tmpElement = null;
  for (var i=0; i<properties.length; i++)
  {
    tmpElement = itemxml.getElementsByTagName(properties[i])[0];
    if (tmpElement != null)
      eval("this." + properties[i] + "=tmpElement.childNodes[0].nodeValue");
  }
}

function showRSS(RSS) {
	var item_html = "";
	item_html += "<h4 id='badge-hdr'>Goals from TwoBrains</h4>";
	item_html += "<ul id='badge-list'>";
  if (RSS.items != null) {
  	for (var i=0; i<RSS.items.length; i++) {
  		item_html += "<li class='badge-item'><a class='tb-badge' href='" + RSS.items[i].link	+ "' title='Two Brains Goal'>" + RSS.items[i].title + "</a></li>";
  	}
	}	
  else {
  	item_html = "<li class='badge-item'>No goals to display.</li>";
  }
  item_html += "</ul>";
  item_html += "<div id='badge-ftr'><a class='ftr-link' href='http://twobrains.liftlabs.com'>Join TwoBrains</a> today!</div>";
  document.getElementById("badge").innerHTML = item_html;
  styleBadge();
}

function getElementsByClassName(oElm, strTagName, strClassName) {
	var arrElements = (strTagName == "*" && oElm.all) ? oElm.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 styleBadge() {
	// styling badge container
	var container = document.getElementById('badge');
	container.style.width 						= '190px';
	container.style.padding 					= '8px';
	container.style.border		 				= '1px solid #36596F';
	container.style.backgroundColor		= '#f7f7f7';
	// styling badge heading
	var hdr = document.getElementById('badge-hdr');
	hdr.style.margin									= '-8px -8px 5px -8px';
	hdr.style.padding 								= '6px';
	hdr.style.fontSize 								= '16px';
	hdr.style.color 									= '#fff';
	hdr.style.backgroundColor 				= '#36596F';
	hdr.style.textAlign								= 'center';
	// styling badge list
	var list = document.getElementById('badge-list');
	list.style.marginLeft							= '15px';
	// styling the list items
	var items = getElementsByClassName(document, "li", "badge-item");
	for(var j=0; j<items.length; j++) {
		items[j].style.listStyleType 		= 'square';
	}
	// styling goal links
	var links = getElementsByClassName(document, "a", "tb-badge");
	for(var i=0; i<links.length; i++) {
		links[i].style.color 						= '#36596F';
		links[i].style.fontSize					= '11px';
	}
	// styling the badge footer
	var ftr = document.getElementById('badge-ftr');
	ftr.style.background							= '#fff';
	ftr.style.margin									= '10px -8px -8px -8px';
	ftr.style.borderTop								= '1px solid #ccc';
	ftr.style.color										= '#3c3c3c';
	ftr.style.padding									= '6px';
	ftr.style.textAlign								= 'center';
	// styling the footer links
	var ftr_links = getElementsByClassName(document, "a", "ftr-link");
	for(var k=0; k<ftr_links.length; k++) {
		ftr_links[i].style.color				= '#3c3c3e';
		ftr_links[i].style.fontWeight		= '700';
	}
}