function get_cookie( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
	return ( unescape ( results[2] ) );
  else
	return null;
}	

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=/";
}


$(document).ready(function() {
	var tpage = get_cookie('tpage');

	// Hack to make sure we have the right top-nav link selected
	var pageScript = location.href;;

	if (/my_news\.php/.test(pageScript))
	{
		tpage = "my_news_button";
	}
	else if (/all_news\.php/.test(pageScript))
	{
		tpage = "all_news_button";
	}
	else if (/latest_news\.php/.test(pageScript))
	{
		tpage = "latest_news_button";
	}
	else if (/invite\.php/.test(pageScript))
	{
		tpage = "invite_button";
	}
	else if (/user_messages\.php/.test(pageScript))
	{
		tpage = "messages_button";
	}
	else if (/user_blog_view\.php/.test(pageScript))
	{
		tpage = "blog_button";
	}
	else if (/user_friends\.php/.test(pageScript))
	{
		tpage = "network_button";
	}
	else
	{
		tpage = "community_button";
	}

	if (!tpage) {
		tpage="community_button";
		document.cookie = "tpage=" + tpage + ";";
		document.cookie = "tpage=" + tpage + ";path=/;domain=lookabooka.com";
		var el_id = '#' + tpage;
		$(el_id).addClass('active');			
	} else {		
		var el_id = '#' + tpage;
		$(el_id).addClass('active');	
	}
	
	$("#navigation_buttons li a").click(function() {
		var nav_link = $(this);
		$('#navigation_buttons li a').removeClass('active');	
		$("#branding #account_site_links li a").removeClass('active');		
		nav_link.addClass('active');
		document.cookie = "tpage=" + nav_link.attr('id') + ";";
		document.cookie = "tpage=" + nav_link.attr('id') + ";path=/;domain=lookabooka.com";
	});

	$("#account_site_links li a").click(function() {														   
		var nav_link = $(this);
		$('#navigation_buttons li a').removeClass('active');	
		$("#branding #account_site_links li a").removeClass('active');
		nav_link.addClass('active');
		document.cookie = "tpage=" + nav_link.attr('id') + ";";
		document.cookie = "tpage=" + nav_link.attr('id') + ";path=/;domain=lookabooka.com";
	});
	
});

function openPopup(theURL) {
	var windowHandle = window.open(theURL,'popup','width=500,height=500,menubar=0,toolbar=0,location=0,resizable=1,scrollbars=1,titlebar=no,left=0,top=0');

	if (!windowHandle) {
		windowHandle.opener = self;
	}
	
	windowHandle.focus();
}

