$(document).ready(
    function () {
        if ($("#workgroup").length > 0) {
            HideRightColumn();
            DisplayWorkgroup();
        }
		if ($("#workgroups").length > 0) {
            HideRightColumn();
            DisplayWorkgroups();
        }
		
		
    });
	
$(".Group").live("click", function(event) {
	var wgId = $(this).attr('id');
	var url = "/default/en-us/workinggroups/workinggroup" + (OFFLINE ? ".off" : "") + ".aspx?wg=" + wgId;
    $(location).attr('href', url);
});

$(".Group").live("mouseover mouseout", function (event) {
    if (event.type == "mouseover") {
        $(this).addClass("HighLighted");
    } else {
        $(this).removeClass("HighLighted");
    }
});

function DisplayWorkgroup() {
    var params = getUrlVars();
    var workgroupId = params["wg"];
    var messageId = params["msg"];

    $.ajax({
        type: "GET",
		cache: false,
        url: KEY_MESSAGE_FILE,
        dataType: "xml",
        success: function (xml) {
            LoadWorkgroup(xml, workgroupId, messageId)
        }
    });
}


function DisplayWorkgroups() {
	$.ajax({
        type: "GET",
        url: KEY_MESSAGE_FILE,
        dataType: "xml",
        success: function (xml) {
            LoadWorkgroups(xml)
        }
    });
}

function LoadWorkgroup(xml, workgroupId, messageId) {
    var wg = $(xml).find("WebItem").filter(function (index) {
        return $(this).attr("Id") == workgroupId;
    }).first();

    var wgTitle = $(wg).find("Title").first().text();
    $("h2").first().html($("h2").first().text() + "&nbsp;" + wgTitle);

    $(wg).find("Representative").each(function () {
        if ($(this).attr('Position') == "Chairman") {
            //$("#chairmanRow").css("Display", "block");
            $("#chairman").text($(this).text());
        }
        else if ($(this).attr('Position') == "Convenor") {
            //$("#convenorRow").css("display", "block");
            $("#convenor").text($(this).text());
        }
        else if ($(this).attr('Position') == "Contact") {
            //$("#contactRow").css("display", "block");
            $("#contact").text($(this).text());
        }
    });
	
	if ($(wg).attr('Status') == "Past") $("#contact").text("ERT Secretariat");

    $("#wgMission").html($(wg).find("Mission").text());

    $(wg).find("Message").each(function () {
        CreateMessageHtml($(this), messageId);
    });
	
	ScrollToElement(messageId);
}

function LoadWorkgroups(xml)
{
	var currentGroups = "";
	var pastGroups = "";
	var idxCurrent  = 0, idxPast = 0;
	$(xml).find("WebItem").each(function() {
		var title = $(this).find("Title").first().text();
		var id = $(this).attr("Id");
		
		if ($(this).attr('Status') == 'Current')
		{
			var clss = idxCurrent % 2 == 0 ? "AltGroupListItem" : "GroupListItem";
			currentGroups += '<div class="Group ' + clss + '" id="' + id + '">' + title + '</div>';
			idxCurrent++;
		}
		else if ($(this).attr('Status') == 'Past')
		{
			var clss = idxPast % 2 == 0 ? "AltGroupListItem" : "GroupListItem";
			pastGroups += '<div class="Group ' + clss + '" id="' + id + '">' + title + '</div>';
			idxPast++;
		}		
	});
	
	$(currentGroups).appendTo("#workgroupsCurrent");
	$(pastGroups).appendTo("#workgroupsPast");
	
}

function CreateMessageHtml(message, currentMsg) {
    var msgclass = $(message).attr("Id") == currentMsg ? "Msg CurrentMsg" : "Msg";
    var tab = '<a id="' + $(message).attr("Id") + '" name="' + $(message).attr("Id") + '"></a>';
	tab += '<div class="' + msgclass + '">'; 
	
    tab += '<table class="wgMessagesTab" cellspacing="0" cellpadding="0"><tbody>';
    tab += '<tr><td class="MsgHdr">' + $(message).find("Title").first().text() + '</td></tr>';
    tab += '<tr><td class="MsgDate">' + $(message).attr('DisplayDate') + '</td></tr>';
    tab += '<tr><td class="MsgDescr">' + $(message).find("Description").first().text() + '</td></tr>';
    tab += '</tbody></table>';
    
	// add document links
	
	tab += '<ul class="wgDocs">';
    $(message).find("Document").each(function() {
		tab += '<li><a href="' + DOCS_DIR + "/" + $(this).attr("FileName") + '">' + $(this).attr("Title") + '</a></li>';
	});
	tab += '</ul>';
	
	tab += '</div>';
	$(tab).appendTo("#wgMessages");
}


