$(document).ready(function() {

	$("#sidebar").height($("body").height());

	$("#slider").scrollFollow({
		speed: 300,
        offset: 0
	});

	var target = "#gallery";
	var sidebar = "#slider";

    $('#gallery span.pic').live('mouseover', function() {
        var topAdjust = ($.browser.msie) ? 130 : -61;
        var leftAdjust = ($.browser.msie) ? 0 : 11;
        var span = $(this).find("span.j");
        span.css("top", $(this).position().top + topAdjust).css("left", $(this).position().left + leftAdjust);
        span.show();
    }).live('mouseout', function() {
        $(this).find("span").hide();
    });	
    
    $.ajax({
		type: "GET",
		url: "press.xml",
		dataType: "xml",
		success: parseXml
	});
	
	function parseXml(xml) {
	
		$(target).empty();
		$(sidebar).empty();
		
		var c = 1; // category iterator			
		$(xml).find("category").each(function() {
		
			$(sidebar).append("<h1>" + $(this).attr("name") + "</h1>");
			$(target).append("<h1>" + $(this).attr("name") + "</h1>");
			
			var p = 1; // product iterator
			$(this).find("product").each(function(){
				$(sidebar).append("<h2><a href='#p"+c+p+"'>" + $(this).attr("name") + "</a></h2>");
				$(target).append("<h3 id='p"+c+p+"' class='p"+c+p+"'>" + $(this).attr("name") + "</h3>");
				if ($(this).attr("number")) {
					$(target + " > h3.p"+c+p).append("<span class='number'>(" + $(this).attr("number") + ")</span><br style='clear:both;' />");
				}
				
				var d = 1; // docs iterator
				$(this).find("docs").each(function() { 
					$(target).append("<ul class='docs p"+c+p+d+"'>"); 
					if ($(this).attr("kind") == "pr") {
						$(target + " ul.p"+c+p+d).append("<li>Press Texts:</li>"); 
					} else if ($(this).attr("kind") == "tech") {
						$(target + " ul.p"+c+p+d).append("<li>Tech Specs:</li>"); 
					} else if ($(this).attr("kind") == "sd") {
						$(target + " ul.p"+c+p+d).append("<li>Download SD Video:</li>"); 
					} else if ($(this).attr("kind") == "hd") {
						$(target + " ul.p"+c+p+d).append("<li>Download HD Video:</li>"); 
					}
					
					$(this).find("doc").each(function(){
						$(target + " ul.p"+c+p+d).append("<li><a href='"+$(this).find("url").text()+"'>"+$(this).find("language").text()+"</a></li>");
					});
					$(target).append("</ul><br style='clear:both;' />");
					d++;	
				}); // if docs
				
				// images iterator
				$(this).find("images").each(function(){
					var i = 1; 
					$(this).find("image").each(function(){
						$(target).append("<span class='pic i"+c+p+d+i+"'></span>");
						$(target +" span.i"+c+p+d+i).append("<img src='"+$(this).find("thumb").text()+"' class='productimg' alt='Press Image' title='"+$(this).find("caption").text()+"' width='190' height='190'/>");
						$(target +" span.i"+c+p+d+i).append("<span class='j j"+c+p+d+i+"' style='display:none;'></span>");
						$(this).find("url").each(function(){
							$(target + " span.j"+c+p+d+i).append("<a href='"+$(this).text()+"'>"+$(this).attr("kind")+"</a>");
						});
					if (i%3 == 0) {
						$(target).append("<br/>");
					}
					i++;
					});
				});
				
				p++;
			});
		
			c++;
		});
			
		thumbs = $("img.productimg");
		var z = 0;
		var refreshId = setInterval(function() {
			 thumbs.eq(z).animate({ opacity:1 },300);
			 z++;
			 if (z > thumbs.length) {
			 	clearInterval(refreshId);
			 }
		}, 50);	
	
		
	} // fn parseXml


});