/*
	Observers
*/

// search and subscribe buttons
$(document).ready(function() {
	
	prepNavbar();
	initNav();
	initSubNav();
	prepPrintLinks();
	protectDownloadLinks();
	
});	

function prepNavbar()
{
	// brute force :last-child
	$('ul#nav li:last').addClass('last');
	$('ul#nav li:first').addClass('first');
	
	$('ul#nav li').hover(
		function() {
			$(this).addClass('active');
		},
		function() {
			if (this.id != "nav_"+section) {
				$(this).removeClass('active');
			}			
		}		
	);

}

function initNav()
{
	if (haveSection()) {
		$("#nav_"+section).addClass('active');
	}
}

function haveSection()
{
	return (section != "{embed:section}");
}

function initSubNav()
{
	if (haveSubSection()) {
		$("#sub_"+subsection).addClass('active');
	}
}

function haveSubSection()
{
	return (subsection != "{embed:subsection}");
}

function prepPrintLinks()
{
	$("span.print a").click(function() {
		window.location = window.location + "?print=yes";
		return false;
	});
}

function protectDownloadLinks() 
{
	if (typeof(logged_in) == "undefined"){
		$('a.protected').each(function() {
			if ($(this).hasClass("demo")) {
				$(this).attr("href", "/index.php/contact/sales?d=demo");				
			}
			else {
				var current_href = $(this).attr("href");			
				$(this).attr("href", "/index.php/contact/general?d="+current_href);				
			}			

		});
	}
}
