var activeNavlink;
var activeSubNavlink;
var resourcesLink;

function activeLinkOnOff(s_onOff){
	if ( $(activeNavlink) ){
		$(activeNavlink).writeAttribute("class",s_onOff);
	}
}

function activeSubLinkOnOff(s_onOff){
	if ( $(activeSubNavlink) ){
		$(activeSubNavlink).writeAttribute("class",s_onOff);
	}
}



document.observe("dom:loaded", function() {

	//////////////////////////////////////////////
	// Navigation link js
	//////////////////////////////////////////////

	if ( force_activelink.length == 0 ){
		var current_href = window.location.pathname.toString();
	} else {
		var current_href = force_activelink;
	}

	// Find the current active link
	$('navigation').select('a').each( function(element){
		if ( element.readAttribute("href") ){
			var href = element.readAttribute("href").toString();

			// Ensure both variables have '/' on the end so they will match correctly
			if ( !current_href.endsWith("/") ){ current_href = current_href+"/"; }
			if ( !href.endsWith("/") ){ href = href+"/"; }
	
			// If variables match, set this as the active link
			if ( current_href == href ){
				activeNavlink = element.up('li').readAttribute("id").toString();
			}
		}
	});

	// If one active link already exists, check if it's a sub-level link and if so set activeLink (to it's parent) and activeSubLink
	if ( $(activeNavlink) ) {
		if ( $(activeNavlink).readAttribute("class") != "level1" ){
			activeSubNavlink = activeNavlink;
			activeNavlink = $(activeSubNavlink).up('ul').up('li').readAttribute("id").toString();
		}
	}

	// Inititally make active links active
	activeLinkOnOff("on");
	activeSubLinkOnOff("on");


	//////////////////////////////////////////////
	// Resources - Download/view increment popularity
	//////////////////////////////////////////////

	if ( $('thumbnail') ){

		$('thumbnail').select('a').each( function(element) {

			if ( element.readAttribute("class") != "file_extension_link" && element.readAttribute("class") != "back_to_category" ){

				if ( element.readAttribute("class") == "lightview" ){

					element.observe("click", function(event){

						resourcesLink = element.readAttribute("href").toString();
						
						new Ajax.Request('/data/susa/scripts/ajax_increment_popularity.php', {
							parameters: {id: resource_id},
							method: 'get'
						});
	
					});

				} else {

					element.observe("click", function(event){

						resourcesLink = element.readAttribute("href").toString();
						
						if ( element.readAttribute("target").toString() == "_blank" ){

							new Ajax.Request('/data/susa/scripts/ajax_increment_popularity.php', {
								parameters: {id: resource_id},
								method: 'get'
							});

						} else {

							new Ajax.Request('/data/susa/scripts/ajax_increment_popularity.php', {
								parameters: {id: resource_id},
								method: 'get',
								onComplete: function(response){
									window.location.href = resourcesLink;
								}
							});

							Event.stop(event);
						}
	
					});

				}

			}

		});

	}

});


