var nodeID;
$(document).ready(function() {
    $.ajax({
        url: "/usercontrols/SelectionGuide.ashx",
        type: "POST",
        async: false,
        data: "Node=1",
        dataType: "text",
        success: function(data) {
            if (data != "") {
	        $(".Loading-White").hide('slow');
	        $("#SelectionGuideTree").show('slow');
                $("#SelectionGuideMain").html(data);
                LinkDescEvent();
            }
        },
 	error:function (xhr, ajaxOptions, thrownError){
		alert(xhr.statusText);
	} 
    });
    $("#PreviousButton").click(function() { PreviousButton() });
    $("#NextButton").click(function() { NextButton() });
});

function PreviousButton() {
    $("#NextButton").show();
    $(".Loading-White").show('slow');
    $("#SelectionGuideTree").hide('slow');
    if ($("#History").html() != "0,1") {
        setTimeout("PrevFunc()", 1000);
    }
}

function NextButton() {
    var id = null;
    $('input[name="Selection"]:radio[checked=true]').each(function() {
       id = $(this).attr("id");
    });
    if (id == null) {
        alert("Please make a selection from the available options");
        return;
    }
    nodeID = id;
    $(".Loading-White").show('slow');
    $("#SelectionGuideTree").hide('slow');
    setTimeout("NextFunc()", 1000);
}

function PrevFunc()
{
	$.ajax({
            url: "/usercontrols/SelectionGuide.ashx",
            type: "POST",
            async: false,
            data: "History=" + $("#History").html() + "&action=prev",
            dataType: "text",
            success: function(data) {
                if (data != "") {
	       	    $(".Loading-White").hide('slow');
	            $("#SelectionGuideTree").show('slow');
                    $("#SelectionGuideMain").html(data);
                    LinkDescEvent();
                }
            },
 	error:function (xhr, ajaxOptions, thrownError){
		alert(xhr.statusText);
	} 
        });
}

function ShowFunc()
{
   $.ajax({
        url: "/usercontrols/SelectionGuide.ashx",
        type: "POST",
        async: false,
        data: "Node=" + nodeID + "&History=" + $("#History").html() + "&SpecificNode=true",
        dataType: "text",
        success: function(data) {
            if (data != "") {
	        $(".Loading-White").hide('slow');
	        $("#SelectionGuideTree").show('slow');
                $("#SelectionGuideMain").html(data);
                if ($("#HideNext").html() == "True") {
                    $("#NextButton").hide();
                }
                LinkDescEvent();
            }
        },
 	error:function (xhr, ajaxOptions, thrownError){
		alert(xhr.statusText);
	} 
    });
}

function NextFunc()
{
	$.ajax({
        url: "/usercontrols/SelectionGuide.ashx",
        type: "POST",
        async: false,
        data: "Node=" + nodeID + "&History=" + $("#History").html(),
        dataType: "text",
        success: function(data) {
            if (data != "") {
	        $(".Loading-White").hide('slow');
	        $("#SelectionGuideTree").show('slow');
                $("#SelectionGuideMain").html(data);
                if ($("#HideNext").html() == "True") {
                    $("#NextButton").hide();
                }
                LinkDescEvent();
            }
        },
 	error:function (xhr, ajaxOptions, thrownError){
		alert("No data available for this selection");
		$(".Loading-White").hide('slow');
	        $("#SelectionGuideTree").show('slow');
	} 
    });
}

function ShowNode(id) {
    nodeID = id;
    $(".Loading-White").show('slow');
    $("#SelectionGuideTree").hide('slow');
    $("#NextButton").show();
    setTimeout("ShowFunc()",1000);
}

function LinkDescEvent() {    
    $('input[name="Selection"]:radio').each(function() {
        $(this).click(function() { ShowDesciption(this) });
    });
}

function ShowDesciption(selectedNode) {
    $('div[type="Desc"]').each(function() {
        $(this).hide('slow');
    });
    if ($("#Desc_" + $(selectedNode).attr("id")).text() != "")
    $("#Desc_" + $(selectedNode).attr("id")).show('slow');
}
