// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


// Fix navbar widths

Event.observe(window, "load", function(){
    
    var top_nav_lis = $$("#navbar ul li");
    top_nav_lis.each(function(li){
      li.style.width = (100/top_nav_lis.length + '%');
      li.style.textAlign = "center";
    })

  }

)


// Create cookies in JS
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

// Read cookies
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/* Replace the "home" link with "Your Snippets"
   and the "log in" link with "Log Out" if the
   login_menu cookie is set. This should give
   logged-in users the same navbar on the cached
   pages as they have on the dynamic ones.
*/
function fixNavbar(){

	if(readCookie("login_menu") == "1"){
		l = $("home_link");
		l.innerHTML = ("Your Snippets");
		l.href = "/snippets";
		
		l = $("user_link");
		l.innerHTML = ("Edit Account");
		l.href = '/users/' + readCookie("login_user") + "/edit";
		
		l = $("log_in_link");
		l.innerHTML = ("Log Out");
		l.href = '/logout';
		
		
	}
	
}