function setTabEvents(slidesContainer) {
    
	// Get the current slide ID
	clickedSlide = slidesContainer.parent("li").attr("id");
	current_slide = $("#slides li.current");
	notes = "";

    previous_tab = current_slide.attr("id").substring(4);
		
	// Set which tab is next in line
    var new_tab = null;
    if (clickedSlide == "prev-tab" || clickedSlide == "next-tab") {                
		if (clickedSlide == "prev-tab") {
			var previous_slide = $("#slides li.current").prev();
			if (previous_slide.attr("id") != "prev-tab") {
				new_tab = previous_slide;
			}
		}
		else if (clickedSlide == "next-tab") {
			var next_slide = $("#slides li.current").next();
			if (next_slide.attr("id") != "next-tab") {
				new_tab = next_slide;
			}
		}
	}

	if (new_tab != null) {
		clickedSlide = new_tab.attr("id");
	}
		
	// Set the selected tab as current
	if (clickedSlide != "prev-tab" && clickedSlide != "next-tab") {
		$("#" + clickedSlide).addClass("current");
		$("#" + clickedSlide).siblings("li").removeClass();
	}

    current_slide = clickedSlide.substring(4);
    $(".view-fullscreen").attr({href: "/gt-larger/?slide=" + current_slide});
    
    $(".pic-fullscreen").attr({
        href: "/gt-larger/?slide=" + current_slide
    });

    current_margin = $("#tabs ul").css("marginLeft");
    current_margin = parseInt(current_margin.substring(0, current_margin.length - 2));
    last_tab = parseInt($("#tabs ul li:last").attr("id").substring(4));
    last_margin = (161 * (last_tab - 4));
    
    if ( ((Math.abs(current_margin / 161) + 4 == current_slide) || (current_margin == 0 && current_slide == 4)) ||
         (Math.abs(current_margin / 161) + 1 == current_slide) )
    {
        if (parseInt(previous_tab) < parseInt(current_slide))
        {
            if (current_margin != -last_margin)
                $("#tabs ul").animate({"marginLeft": "-=322px"}, 300);
        }
        if (parseInt(previous_tab) > parseInt(current_slide))
        {
            if (current_margin != 0)
                $("#tabs ul").animate({"marginLeft": "+=322px"}, 300);
        }
    }

    if ( parseInt(current_slide) != 1 && $("#prev-tab a").attr("class") == "inactive" )
        $("#prev-tab a").removeClass();
    else if ( parseInt(current_slide) == 1 && $("#prev-tab a").attr("class") == "")
        $("#prev-tab a").addClass("inactive");
    else if ( parseInt(current_slide) == last_tab && $("#next-tab a").attr("class") == "")
        $("#next-tab a").addClass("inactive");
    else if (  parseInt(current_slide) != last_tab &&  $("#next-tab a").attr("class") =="inactive" )
        $("#next-tab a").removeClass();
    
	// Set all the slides with current process data
	data = { clicked_slide: clickedSlide };
	$.getJSON("/change_identification/", data, function(json) {
		notes = json['notes'];
        
        $("#id-" + clickedSlide).show();
        $("#id-" + clickedSlide).siblings("div").hide();
        $("#id-notes").html("<h3>Notes on this view:</h3><p>" + notes + "</p>");

		/*switch(clickedSlide)
		{
			case "object-tab":
				$("#id-object-view").show();
				$("#id-object-view").siblings("div").hide();
				$("#id-notes").html("<h3>Notes on this view:</h3><p>" + notes + "</p>");
				break;    

			case "surface-tab":
				$("#id-surface-view").show();
				$("#id-surface-view").siblings("div").hide();
				$("#id-notes").html("<h3>Notes on this view:</h3><p>" + notes + "</p>");
				break;    
	
			case "ten-tab":
				$("#id-ten-view").show();
				$("#id-ten-view").siblings("div").hide();
				$("#id-notes").html("<h3>Notes on this view:</h3><p>" + notes + "</p>");
				break;    

			case "thirty-tab":
				$("#id-thirty-view").show();
				$("#id-thirty-view").siblings("div").hide();
				$("#id-notes").html("<h3>Notes on this view:</h3><p>" + notes + "</p>");
				break;

			default:

		}*/
	});

}

function setIDTabs() {
	$("#slides a").click(function(event) {
		setTabEvents($(this));
        return false;
	});
}

function set_primary_process(process_id) {
    window.location = "/guidedtour/?process_id=" + process_id;    
/*    data = { process_id : process_id };
    $.getJSON("/change_process_identification/", data, function(json) {
        
		$("#id-object-view > img").attr("src", json['object_view_data']);
		$("#id-surface-view param:first").attr("value", json['surface_view_data']);        
        $("#id-surface-view embed").attr("src", json['surface_view_data']);
        $("#id-ten-view > img:first").attr("src", json['ten_x_data']);
        $("#id-thirty-view > img:first").attr("src", json['thirty_x_data']);

        var tab_tags = '<li id="prev-tab"><a href="#prev">Prev</a></li>';
        var current = "current"
        for (i = 1; i < 5; i++)
            if (json['tab_id' + i]) {
                tab_tags = tab_tags + '<li id="' + json['tab_id' + i] + '" class="' + current  + '"><a href="#">' + json['tab_name' + i] + '</a></li>';
                if (current == "current")
                    current = "";
            }
        
        tab_tags = tab_tags + '<li id="next-tab"><a href="#next">Next</a></li>';

        $("#slides").children().remove();
        $("#slides").html(tab_tags);

        $("#" + json['first_id']).addClass("current");
        $("#" + json['first_id']).siblings("li").removeClass();
        $("#" + json['first_view_id']).show();
        $("#" + json['first_view_id']).siblings("div").hide();
        $("#id-notes").html("<h3>Notes on this view:</h3><p>" + json['object_view_notes'] + "</p>");

        var div_id = "primary-selection";
        var select_id = "primary-select";
        $("#" + div_id + " .selection-wrap > input").remove();
        $("#" + div_id + " .selection-wrap > div:first").remove();
        $("#" + div_id + " .selection-wrap > select").remove();
        
        var tag = '<select id="' + select_id  + '" name="' + select_id + '" tabindex="1">' +
                  '<option value="">(Select a Process)</option>';

        var i = 0;
        while (json['process_id_' + i] != null) {
            tag = tag + '<option value="' + json['process_id_' + i] + '"';
            if (json['process_id_' + i] == json['process_id'])
                tag = tag + " selected";
            tag = tag + '>' + json['process_name_' + i] + '</option>';
            i++;
        }
        
        tag = tag + '</select>';
        $("#" + div_id + " p:first").before(tag);
        $("#" + select_id).selectbox({debug: true});

        $("#" + div_id + " strong").html(json['group_name']);
        
        setIDTabs();
        
    }); */
}


//--------- Groups Box ----------------//

function openWindow(linky) {
	thisParent = $(linky).parent().attr("id");
	if(thisParent == "primary-group-select"){
		$("#primary-group-select").next("div.groups").show();
	}else {
		$("#secondary-group-select").next("div.groups").show();
	}
 	openStatus=1;
}
 
function closeWindow(linky) {
	$("div.groups").hide();
 	openStatus=0;
}
 
function setGroups() {
	openStatus=0; 	
 	$("div.groups").hide();
}

$(document).ready(function() {
	setGroups();
	
    $(".browse li a").click(function() {
        var process_id = $(this).attr("href");
        //set_primary_process(process_id);
        window.location = "/theatlas/guidedtour/?process_id=" + process_id;
        
        closeWindow();

        return false;
    });

	$("a.groups-toggle").click(function() {
		$("div.browse").show();
        if(openStatus==0){
			openWindow($(this));
		}else {
			closeWindow();
		}

		return false;
	});
	
	$("div.groups p > a").click(function() {
		closeWindow($(this));
		return false;
	});

	/*$("div.groups ul > li > a").click(function() {
        var group_id = $(this).attr("href");
        var _parent = $(this).parents().get(2);
        var parent_id = $(_parent).attr("id");

        var div_id = "secondary-selection";
        var select_id = "secondary-select";
        if (parent_id == "primary-group-select-box") {
            var div_id = "primary-selection";
            var select_id = "primary-select";
        }

        var data = { group_id:group_id };
        $.getJSON("/change_select_group/", data, function(json) {
            $("#" + div_id + " .selection-wrap > input").remove();
            $("#" + div_id + " .selection-wrap > div:first").remove();
            $("#" + div_id + " .selection-wrap > select").remove();

            var tag = '<select id="' + select_id  + '" name="' + select_id + '" tabindex="1">' +
                '<option value="">(Select a Process)</option>';

            var i = 0;
            while (json['process_id_' + i] != null) {
                tag = tag + '<option value="' + json['process_id_' + i]  + '">' +
                    json['process_name_' + i] + '</option>';
                i++;
            }

            tag = tag + '</select>';
            $("#" + div_id + " p:first").before(tag);
            $("#" + select_id).selectbox({debug: true});

            $("#" + div_id + " a.groups-toggle").html(json['group_name']);
        });
         
		closeWindow();
		return false;
    });*/

    //Browsing drop-downs
	$("div.cats ul").hide();
	$("div.cats h5").click(function(event) {
		$(this).next("ul").toggle();
		
		if($(this).children("span").hasClass("collapse")){
			$(this).children("span").removeClass();
		}else {
			$(this).children("span").addClass("collapse");
		}
	});
});