function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

// Get All elements by class name
// Taken from http://snipplr.com/view/88/getelementsbyclassname/
// http://www.dustindiaz.com/top-ten-javascript/
function getElementsByClassName(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 (var i = 0, j = 0; i < elsLen; i++)
	{
		if (pattern.test(els[i].className))
		{
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

// function for showing and hiding elements
function detail(element, method, next) {
        
    // Get id number from html
    var matches = element.innerHTML.match(">(.*?)<");
    var thisEvent = matches[0];
    thisEvent = thisEvent.substring(1, (thisEvent.length-1));
        
    // Get the detail element using the Id
    thisEvent = document.getElementById("day" + thisEvent);
    
    // Show or hide the div
    if(method == "show") {
        next.className = "detail hide";
        thisEvent.className = "detail show";
    } else if(method == "hide") {
        next.ClassName = "detail";
        thisEvent.className = "detail hide";
    }
}

function calendar() {
    if(document.getElementById("next")) {
    	// Get next event div
    	var next = document.getElementById("next");
    	
    	// Get detail divs
    	var details = getElementsByClassName("detail", document, "div");
    	
    	// Get days of events from list
    	var calEvents = getElementsByClassName("concert", document, "li");
    	
    	// Hide all detail divs and then show the next event
    	for(var i=0; i<details.length; i++) {
    		// hide divs
    		details[i].className = "detail hide" ;
    	}
    	next.className = "detail";
    	
    	// When event hovered over, call detail function to show the detail
    	for(var i=0; i<calEvents.length; i++) {
    		calEvents[i].onmouseover = function() { detail(this, "show", next); }
    		calEvents[i].onmouseout = function() { 
    			next.className = "detail";
    			detail(this, "hide", next);
    		}
    	}
    }
}

// JQuery AJAX
function updateCal(month) {
    $.post("/ajax.php", {month: month}, function(data){
        $("div.calendar").html(data);
        
        //Run calendar function to hide multiple event divs
        calendar();
    });
}

addLoadEvent(calendar);

// Populates label text into an input field (i.e. search box)
function populateSearchFieldText (field_id, label) {
    if (!document.getElementById) return false;
    
    // On startup copy label text to field
    var field = document.getElementById(field_id);
    if(field) {
        field.value = label;
    
        // On focus blank out field if label text is value
        field.onfocus = function () {
            if (this.value == label) {
                this.value = '';
            }
        }
    
        // On blur copy label text if value is blank
        field.onblur = function () {
            if (this.value == '') {
                this.value = label;
            }
        }
    }
}

addLoadEvent(function() {
    populateSearchFieldText('signup', 'Enter your email address');
});