$(document).ready(function() {



    addMouseOvers();



});



function addMouseOvers()

{

	// main menu

    $(".main_menu a>img").mouseover(

		function() {

		    $(this).addClass("menu_underline");

		}

	).mouseout(

		function() {

		    if (!$(this).hasClass("selected")) {

		        $(this).removeClass("menu_underline");

		    }

		}

	);

	

	// top nav and thumb mouseovers

	$(".mouseover a>img").mouseover(

		function() 

		{

			$(this).attr("src", $(this).attr("src").replace("-off", "-on"));						 

		}

	).mouseout(

		function() 

		{

			if (!$(this).hasClass("selected"))

			{

				$(this).attr("src", $(this).attr("src").replace("-on", "-off"));						 

			}

		}

	);

	

	if (pageType != "") {

        $("#" + pageType + "_nav").addClass("selected");

        $(".selected").mouseover();

    }



}



/////////////////////////////////////////////////////////////////////



var selectedId = "";

var xmlDoc = "";

var pageType = "";

var imagePath = "";

var thumbPath = "";



if (document.location.href.indexOf("?") > 0)

{

	var qs = document.location.href.split("?"); 

	if (qs.length > 0 && qs[1].indexOf("id=") >= 0)

	{

		selectedId = qs[1].substring(3); 

	}

}



function loadClients()

{

	$("#" + pageType + "_nav").addClass("selected").mouseover();



	$.ajax({

		url: "data/" + pageType + ".xml", 

		success: function(data){ 

			var xml = "";            

			if ($.browser.msie && data.xml == null) 

			{                

				xml = new ActiveXObject("Microsoft.XMLDOM");                

				xml.async = false;                

				xml.loadXML(data);                            

			}

			else

			{

				xml = data;	

			}

			parseXml(xml); 

		},

		error: function(XMLHttpRequest, textStatus, errorThrown) { alert("error: " + textStatus + " - " + errorThrown); }

	});

}



function parseXml(xml)

{	

	$(xml).find("client").each( function() {

			var id = $(this).attr("id");

			var name = $(">name", this).text();

			if (pageType != "bs")

			{

				var html = '<img src="images/' + thumbPath + id + '-off.jpg" alt="' + name + '"/>';

				if (id.indexOf("blank") == -1)

				{

					html = '<a id="' + id + '" href="">' + html + '</a>';

				}

				$(".thumb_container").append(html);

			}

			else

			{

				$("work", this).each( function() {

					var workId = $(this).attr("id"); 

					var html = '<img src="images/' + thumbPath + id + '-' + workId + '-off.jpg" alt="' + name + '"/>';

					if (id.indexOf("blank") == -1)

					{

						html = '<a id="' + id + '-' + workId + '" href="">' + html + '</a>';

					}				

					$(".thumb_container").append(html);

				});

			}

		});



	xmlDoc = xml;



	// Add events

	addMouseOvers();

	

	$(".thumb_container a").click( function(e) { 

		e.preventDefault();

		showItem(this.id); 

	});	

	

	

	// select one

	if (selectedId == "")

	{

		$(".thumb_container a:first").click();	

	}

	else

	{

		$(".thumb_container a[id='" + selectedId + "']").click();	

	}

}



function showItem(id)

{

	// reset

	if (selectedId != "")

	{

		$("#" + selectedId + " img").removeClass("selected").mouseout();

	}

	selectedId = id;



	// set selected thumb

	$("#" + selectedId + " img").addClass("selected").mouseover();



	var workId = "";

	if (id.indexOf("-") > 0) {

		workId = id.substring(id.indexOf("-") + 1);	

	    id = id.substring(0, id.indexOf("-"));

	}

	

	// get details

	var xmlNode = $(xmlDoc).find("client[id='" + id + "']");

	var name = $("name", xmlNode).text();

	var logo = $(xmlNode).find("work[id='logo']");

	var work_print = $(xmlNode).find("work[id='work_print']");

	var web = $(xmlNode).find("work[id='web']");

	

	if (workId != "")

	{

		xmlNode = $(xmlNode).find("work[id='" + workId + "']");	

	}

	

	var title = $(">title", xmlNode).text();

	var desc = $(">description", xmlNode).text();



	// main image

	$("#main_image").html("").hide();

	$("#sub_image").html("").hide();



	if (pageType == "work_identities")

	{

		var ext = "gif"; 

		$("#main_image").append("<img src='images/" + imagePath + id + "-logo" + "." + ext + "'/>");	

	}

	else

	{

		var count = $("images>imagePath", xmlNode).length;  

		if (count > 0)

		{

			var style = "";

			if ($(xmlNode).find("images").attr("imageClass") != null)

			{

				style = $("images", xmlNode).attr("imageClass");

			}

			else if (pageType == "web") 

			{

				style = "sub"; 

			}

			$("#main_image").append("<img src='images/" + $("images>imagePath:first", xmlNode).text() + "' class='" + style + "'/>");

			if (count > 1)

			{

			    $("images>imagePath:gt(0)", xmlNode).each(function() {

			        $("#sub_image").append("<img src='images/" + $(this).text() + "' class='" + style + "'/>");

			        if (style != "horiz") {

			            $("#sub_image").append("<br/>");

			        }

			    });

			}

		}

	}

	$("#main_image").fadeIn("slow");

	$("#sub_image").fadeIn("slow");

	

	// name and description

	$("#item_identity").text(name);

	$("#item_desc").html(desc);

}	





function addSpacer()

{

	if ($("#item_links").html().length > 0)

	{

		$("#item_links").append("&nbsp;&nbsp;|&nbsp;&nbsp;");

	}	

}
