function removeWidows() {
	$('h1,h2,h3,li,p, a').each(
		function(){
			$(this).html($(this).html().replace(/\s([^\s<]+)\s*$/,'&nbsp;$1'));
		}
	);
}

function navRollOvers() {
	if(!$.browser.msie) {
		$('ul#navigation a:not(.active)').mouseenter(function() {
			$(this).animate({
				borderBottomWidth: '6px'
			}, 'fast');
		});

		$('ul#navigation a:not(.active)').mouseleave(function() {
			$(this).animate({
				borderBottomWidth: '0px'
			}, 'fast');
		});
	} else {
		$('ul#navigation a').mouseenter(function() {
			$($(this)).css("border-bottom", "6px solid #cc0");
		});
		$('ul#navigation a').mouseleave(function() {
			$($(this)).css("border-bottom", "0px solid #cc0");
		});
	}
}

function cycle() {
	$('div.cycle').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		pager:  'div#pager'
	}); 
}

function parseXML() {
	
	categoryArray = new Array();
	imageArray = new Array();
	
	$.get("xml/global.xml",{},function(xml) {
		$('items',xml).each(function(i) {
			id = $(this).attr("id");
			descrip = $(this).find("description").attr("text");
			title = $(this).find("item").attr("title");
			if(i == 0) {
				$("div#description p:first").text(descrip);
				$("div#description h1").text(title);
			}
			
			categoryArray.push(id);
			display = $(this).attr("display");

			$(this).find("item").each(function(item) {
				linkTitle = $(this).attr("title");
				linkId =$(this).attr("id");
				$("div#projectMenu div#projects ul#" + id).append("<li><a href='#' id='" + linkId + "'>" + linkTitle + "</a></li>");
				imageArray = $(this).find("images");
			});		
			for(s = 0; s < $("div#projectMenu ul#" + id + " li a").length; s++) {
		
			}
			
			$("div#projectMenu ul#" + id + " li a").click(function() {
				mediaId = $(this).attr("id");
				category = $(this).parents("ul").attr("id");
				if(category == "tv") {
					loadVideo(mediaId);
				} else {
					loadSlideshow(category, mediaId);
				}
				
				return false;
			});
		});
		// read X from the URL if it exists then change the tab
		if($.getURLParam("x") == "mail") {
			$('div#projectMenu ul#mainNav li a.mail').click();
		} else if($.getURLParam("x") == "tv") {
			$("div#projectMenu ul#mainNav li a.tv").click();
		} else if($.getURLParam("x") == "online") {
			$("div#projectMenu ul#mainNav li a.online").click();
		}
	});
	
}
		
function loadSlideshow(category, slideshowId) {
	
	$("div#video").css("display", "none");
	$("div#cycleWrapper").css("display", "block");
	$("div#pager").css("display", "block");
	
	$("div#cycle").cycle('stop');
	
	$("div#cycle img").remove();
	$("div#pager a").remove();
	
	$.get("xml/global.xml",{},function(xml) {
		$('items',xml).each(function(i) {
			if($(this).attr("id") == category) {
				$(this).find("item").each(function() {							   
					if($(this).attr("id") == slideshowId) {
						title = $(this).attr("title");
						descrip = $(this).find("description").attr("text");

						$("div#description h1").html(title);
						$("div#description p:first").html(descrip);
						imageArray = $(this).find("images");
						imgArray = imageArray.children();
						$(imgArray).each(function() {
							$("div#cycle").append("<img src='" + $(this).attr('src') + "' height='" + $(this).attr('height') + "' width='" + $(this).attr('width') + "' alt='' />");
						});
						$("div#cycle").cycle({
							fx: 'fade',
							pager:  'div#pager'
						});
					}
				});
			}
		});
	});
}

function loadVideo(videoId) {
	$("div#cycle").cycle('stop');
	$("div#cycleWrapper").css("display", "none");
	$("div#pager").css("display", "none");
	$("div#video").css("display", "block");
	$.get("xml/global.xml",{},function(xml) {
		$("item",xml).each(function(i) {
			
			$("div#description h1").html(title);
			$("div#description p:first").html(descrip);
			if($(this).attr("id") == videoId) {
				title = $(this).attr("title");
				descrip = $(this).find("description").attr("text");
				videoLink = $(this).find("video").attr("src");
				$("div#video param").each(function() {
					if($(this).attr("name") == "movie") {
						$(this).remove();
						$("div#video object").append("<param value='" + videoLink + "' name='movie'></param>");
					}
				});
				$("div#video object embed").remove();
				$("div#video object").append("<embed height='300' width='400' allowscriptaccess='always' allowfullscreen='true' type='application/x-shockwave-flash' src='" + videoLink + "'></embed>");
			}
		});
	});
}

function browserVersion() {
	if($.browser.msie) {
		var version = "text";
	} else {
		var version = "xml";
	}
	return version;
}