function gallery_slices() {
	var current = $("#wgallery dt.selected");
	var slice_width = 20;


	var post_slices = current.nextAll("dt");
	var post_count = post_slices.length;
	var pre_slices = current.prevAll("dt");
	var pre_count = pre_slices.length;

	if(pre_count) {
		for(var i = 0; i < pre_count; i++) {
			pre_slices.eq(i).addClass("slice");
			pre_slices.eq(i).css({ left:((pre_count - i - 1) * slice_width), right:'auto' });
			pre_slices.eq(i).click(function(event){ gallery_jump(this); } );
		}
	}

	if(post_count) {
		for(var i = 0; i < post_count; i++) {
			post_slices.eq(i).addClass("slice");
			post_slices.eq(i).css({ right:((post_count - i - 1) * slice_width), left:'auto' });
			post_slices.eq(i).click(function(event){ gallery_jump(this); } );
		}
	}


}

function gallery_jump(slice) {
	var slice = $(arguments[0]);
	var gallery_panel = $("#wgallery");
	var current = gallery_panel.find("dt.selected");

	current.removeClass("selected");
	gallery_panel.find("dd.selected").removeClass("selected")

	slice.removeClass("slice").addClass("selected").unbind('click');
	slice.nextAll("dd").eq(0).addClass("selected");
	gallery_slices();
}

function gallery_prev() {
	var gallery_panel = $("#wgallery");
	var current = gallery_panel.find("dt.selected");
	var prev = current.prevAll("dt");
	var anim_params = {};

	if( prev.length ) {
		gallery_panel.find("dd.selected").removeClass("selected").prevAll("dd").eq(0).addClass("selected");
		current.removeClass("selected");
		prev.eq(0).removeClass("slice").addClass("selected").unbind('click');;

		gallery_slices();

	}
};

function gallery_next() {

	var gallery_panel = $("#wgallery");
	var current = gallery_panel.find("dt.selected");
	var next = current.nextAll("dt")
	var anim_params = {};

	if( next.length ) {
		gallery_panel.find("dd.selected").removeClass("selected").nextAll("dd").eq(0).addClass("selected");
		current.removeClass("selected");
		next.eq(0).removeClass('slice').addClass("selected").unbind('click');;

		gallery_slices();


	}

}

$(document).ready(function(){

	// Check if the third level navigation is selected
	var nav_level3 = $("#nav ul.level3 > li.selected");
	if(nav_level3.length) {
		var nav3_list = nav_level3.parent();
		nav3_list.parent('li').find("li.selected").prevAll("li").addClass('nav_border');
		$("#nav_level3").html( $("<div id=\"nav3_spacer\"></div><ul>"+nav3_list.html()+"</ul>") );
	}

	// Unobtrusive JavaScript Popup Windows
	$("a.popup_radio").click(function() {
     	var popup = window.open( $(this).attr("href"), 'popup_radio', 'width=250, height=212,scrollbars=no');
		if(popup) {return false; }
	});


	if($("#wgallery")) { // Click actions for the gallery

		gallery_slices();

		$("#gallery_prev").click( function() {
			gallery_prev();
			return false;
		});

		$("#gallery_next").click( function() {
			gallery_next();
			return false;
		});

	}

});

