// File: readXML.js

// Start function when DOM has completely loaded 
$(document).ready(function(){ 

	/* xml data using XMLHttpRequest subject to same origin browser policy */ 
	//$.ajax({url: "http://virtual.newtonfreelibrary.net/webapps/facts/repeaterXML.aspx", cache: false, success: function(xml){HTMLfromXML(xml,'fact');showDivs('fact');}});
	
	/* json data using XMLHttpRequest subject to same origin browser policy */ 
	//$.getJSON("http://virtual.newtonfreelibrary.net/webapps/facts/repeaterJSON.aspx", {}, function(json){HTMLfromJSON(json.facts.fact,"fact");showDivs('fact');});
	
	/* JSONP data retrived as parameters to somefunction that gets executed */ 
	$.getJSON("http://virtual.newtonfreelibrary.net/webapps/facts/factdata.aspx?output=jsonp&callback=?", {}, function(json){HTMLfromJSON(json.facts.fact,"fact");showDivs('fact');});

	
	$.get("./includes/blogs.xml",{},function(xml){
		HTMLfromXML(xml,'blog');
		var rand = Math.floor(Math.random()*9+1)
		//var element = $(xml).find('blogs blog[id='+rand+']');
		
		var elem = $("div.blog:eq("+rand+")");
		$("div.blog:eq("+rand+")").remove();
		
		$("div.blog").wrapAll("<div id='blogrest'></div>");
		$("#blog").prepend(elem);

	});
});
 
 function ToggleExpansion(div, minHeight){
 	if($(div).css('height') == 'auto'){ $(div).css('height',minHeight);}
 	else{ $(div).css('height','auto');}
 	return false;
 }

function HTMLfromJSON(json, type){
	myHTMLOutput = '';
	
	// Run the function for each 'type' tag in the XML file
	$.each(json,function(f,ft) {
		//alert(ft.id);
		id = ft.id;
		
		title = ft.title;
		url = ft.url;

		fulltext = ft.fulltext;

		// Build row HTML data and store in string
		mydata = BuildHTML(id, title, url, fulltext, "fact");
		myHTMLOutput = myHTMLOutput + mydata;
	});
	 
 	$("#"+type).append(myHTMLOutput);
}
 
 function HTMLfromXML(xml,type){
	myHTMLOutput = '';

	// Run the function for each 'type' tag in the XML file
	$(type,xml).each(function(i) {
		
		id = ""+$(this).attr("id");

		title = ""+$(this).find("title").text();
		url = $(this).find("title").attr("url") || "";

		fulltext = ""+$(this).find("fulltext").text();

		// Build row HTML data and store in string
		mydata = BuildHTML(id, title, url, fulltext, type);
		myHTMLOutput = myHTMLOutput + mydata;
	});
 
 	$("#"+type).append(myHTMLOutput);
 		 			
	
}
 
 function BuildHTML(id, title, url, fulltext, divclass){
	output = "<div class='"+divclass+"'>";
	
	if (url.length > 1) output += "<a href='"+url+"'>" + title + "</a>"; // Check to see if there is a "url" attribute in the name field
	else output += title;	
	if (fulltext.length > 1) output += " <a style='font-style:italic; font-weight:normal;' href='http://virtual.newtonfreelibrary.net/webapps/facts/fact.aspx?showId="+id+"'>more...</a>";
	output += "</div>";
	
	return output;
}


function showDivs(type){
	$(document).ready(function(){
		
	$("div." + type + ":eq(0)").css('top','5px');
	
	var runtimeInterval = "rotation_interval_"+type;
	var blogId = 0;
	var factId = 0;
	
	var blogCount;
	var factCount;
	
	if(type == "blog"){
		blogCount = $("div." + type).size();
		rotation_interval_blog = setInterval(function(){rotate(type, blogCount, blogId++);},5000); //time in milliseconds
		$("#"+type).hover(
			function() { clearInterval(rotation_interval_blog);}, 
			function() { rotation_interval_blog = setInterval(function(){rotate(type, blogCount, blogId++);},5000); rotate(type, blogCount, blogId++); });
	}
	
	else if(type == "fact"){
		factCount = $("div." + type).size();
		rotation_interval_fact = setInterval(function(){rotate(type, factCount, factId++);},5000); //time in milliseconds
		$("#"+type).hover(
			function() { clearInterval(rotation_interval_fact);}, 
			function() { rotation_interval_fact = setInterval(function(){rotate(type, factCount, factId++);},5000); rotate(type, factCount, factId++); });
	}
	});

}
 
 function rotate(type, div_count, div_previous) {
   div_current = (div_previous + 1) % div_count;
   $("div." + type + ":eq(" + (div_previous % div_count) + ")").animate({top: -205},"slow", function() {
	 $(this).css('top','210px');
   });
   $("div." + type + ":eq(" + div_current + ")").show().animate({top: 5},"slow");
}