$(document).ready(function(){initStuff();});
var newscarcurrent = 0;
var imgcarcurrent = 0;
var tm = new Array();
var imgtm= new Array();
//Initalization Function
function initStuff(){
	for(var i=0;i<document.images.length;i++){
		if(document.images[i].id=="navroll"){
			setupRollover(document.images[i]);
		}
	}
	var newslength=0;
	var imglength=$(".imgcar").length;
	$(".imgcar").each(function(index){
		if(index==0){
			$("#imgcarbuttons").append('<img id="imgsquare0" src="/images/orangesquare.gif" alt="Orange Square" width="8" height="8" />');
			var imgsqmargleft=Math.floor((750-imglength*16)/2);
			$("#imgsquare0").css("margin-left",imgsqmargleft);
			$("#imgsquare0").css("cursor","pointer");
			$("#imgsquare0").click(function(){imgCar(imglength,imgcarcurrent,0);});
		}else{
			$("#imgcarbuttons").append('<img id="imgsquare'+index+'" src="/images/bluesquare.gif" alt="Blue Square" width="8" height="8" />');
			$("#imgsquare"+index).css("margin-left",8);
			$("#imgsquare"+index).css("cursor","pointer");
			$("#imgsquare"+index).click(function(){imgCar(imglength,imgcarcurrent,index);});
		}
	});
	imgtm[imgtm.length]=setTimeout(function(){imgCar(imglength,0);},5000);
	$.get("/content/news.xml", function(news){
										newslength=$("article",news).length;
										$("article",news).each(function(index){
																		if(index==0){
																			$("#news").append('<p id="newscar'+index+'"><span class="bold"><span class="bluetext">'+$(this).find("title").text()+'</span></span><br /><span style="font-size:11px;">'+$(this).find("content").text()+'</span></p>');
																			$("#displaynews").append('<h2>'+$(this).find("title").text()+'</h2><p>'+$(this).find("content").text()+'</p>');
																			$("#newsbuttons").append('<img id="square0" src="/images/orangesquare.gif" alt="Orange Square" width="8" height="8" />');
																			var sqmargleft=Math.floor((574-newslength*16)/2);
																			$("#square0").css("margin-left",sqmargleft);
																			$("#square0").css("cursor","pointer");
																			$("#square0").click(function(){newsCar(newslength,newscarcurrent,0);});
																		}else{
																			$("#news").append('<p id="newscar'+index+'" style="display:none;"><span class="bold"><span class="bluetext">'+$(this).find("title").text()+'</span></span><br /><span style="font-size:11px;">'+$(this).find("content").text()+'</span></p>');
																			$("#displaynews").append('<h2>'+$(this).find("title").text()+'</h2><p>'+$(this).find("content").text()+'</p>');
																			$("#newsbuttons").append('<img id="square'+index+'" src="/images/bluesquare.gif" alt="Blue Square" width="8" height="8" />');
																			$("#square"+index).css("margin-left",8);
																			$("#square"+index).css("cursor","pointer");
																			$("#square"+index).click(function(){newsCar(newslength,newscarcurrent,index);});
																		}
																}
										);
										tm[tm.length]=setTimeout(function(){newsCar(newslength,0);},5000);
								},"xml");
	$.get("/content/archive.xml", function(news){
										newslength=$("article",news).length;
										$("article",news).each(function(index){
																			$("#displaynews").append('<h2>'+$(this).find("title").text()+'</h2><p>'+$(this).find("content").text()+'</p>');
																		});
										   },"xml");
}
function setupRollover(thisImage){
	thisImage.outImage=new Image();
	thisImage.outImage.src=thisImage.src;
	thisImage.onmouseout=rollOut;
	thisImage.overImage=new Image();
	thisImage.overImage.src =thisImage.src.substring(0,(thisImage.src.length-4))+"roll.png";
	thisImage.onmouseover=rollOver;
}
function rollOver(){
	this.src=this.overImage.src;
}
function rollOut(){
	this.src=this.outImage.src;
}
function newsCar(newslength,x,fadeto){
	if(typeof fadeto != "undefined"){
		for(i=0;i<tm.length;i++){
			clearTimeout(tm[i]);
		}
		$("#newscar"+x).fadeOut("slow", function() {
			$("#square"+(x)).attr("src","/images/bluesquare.gif");
			$("#newscar"+fadeto).fadeIn("slow");
			$("#square"+fadeto).attr("src","/images/orangesquare.gif");
			newscarcurrent=fadeto;
			tm[tm.length]=setTimeout(function(){newsCar(newslength,fadeto);},8000);
		});
	}else{
		if(x<newslength-1){
			for(i=0;i<tm.length;i++){
				clearTimeout(tm[i]);
			}
			$("#newscar"+x).fadeOut("slow", function() {
				$("#square"+(x)).attr("src","/images/bluesquare.gif");
				$("#newscar"+(x+1)).fadeIn("slow");
				$("#square"+(x+1)).attr("src","/images/orangesquare.gif");
				newscarcurrent=x+1;
				tm[tm.length]=setTimeout(function(){newsCar(newslength,(x+1));},5000);
			});
		}else{
			for(i=0;i<tm.length;i++){
				clearTimeout(tm[i]);
			}
			$("#newscar"+x).fadeOut("slow", function() {
				$("#square"+(x)).attr("src","/images/bluesquare.gif");
				$("#newscar0").fadeIn("slow");
				$("#square"+(0)).attr("src","/images/orangesquare.gif");
				newscarcurrent=0;
				tm[tm.length]=setTimeout(function(){newsCar(newslength,0);},5000);
			});
		}
	}
}
function imgCar(imglength,x,fadeto){
	if(typeof fadeto != "undefined"){
		for(i=0;i<imgtm.length;i++){
			clearTimeout(imgtm[i]);
		}
		$("#imgcar"+x).fadeOut("slow", function() {
			$("#imgsquare"+(x)).attr("src","/images/bluesquare.gif");
			$("#imgcar"+fadeto).fadeIn("slow");
			$("#imgsquare"+fadeto).attr("src","/images/orangesquare.gif");
			imgcarcurrent=fadeto;
			imgtm[imgtm.length]=setTimeout(function(){imgCar(imglength,fadeto);},8000);
		});
	}else{
		if(x<imglength-1){
			for(i=0;i<imgtm.length;i++){
				clearTimeout(imgtm[i]);
			}
			$("#imgcar"+x).fadeOut("slow", function() {
				$("#imgsquare"+(x)).attr("src","/images/bluesquare.gif");
				$("#imgcar"+(x+1)).fadeIn("slow");
				$("#imgsquare"+(x+1)).attr("src","/images/orangesquare.gif");
				imgcarcurrent=x+1;
				imgtm[imgtm.length]=setTimeout(function(){imgCar(imglength,(x+1));},5000);
			});
		}else{
			for(i=0;i<imgtm.length;i++){
				clearTimeout(imgtm[i]);
			}
			$("#imgcar"+x).fadeOut("slow", function() {
				$("#imgsquare"+(x)).attr("src","/images/bluesquare.gif");
				$("#imgcar0").fadeIn("slow");
				$("#imgsquare"+(0)).attr("src","/images/orangesquare.gif");
				imgcarcurrent=0;
				imgtm[imgtm.length]=setTimeout(function(){imgCar(imglength,0);},5000);
			});
		}
	}
}
