/******************************************/
/*Author: Werner de Bruin
/*Date: Aug 2009
/*Libraries used:JQuery
/******************************************/

/* <![CDATA[ */

$(document).ready(function(){

	$(".homeTabs").hide();
	$(".homeTabs:first").show().addClass("currTab");	
	$("#midClmn2 div:first").addClass("current");
	
	$('.introLnk').hover(function(){//when you click on the interchanging image
		var tab = $('.introLnk').index(this);	
		$(".introLnk").not(":eq("+tab+")").children('div').removeClass("current").attr('style', '').css('background', '#fff');
		$(".introLnk").filter(":eq("+tab+")").children('div').addClass("current");
		$(".currTab").hide().removeClass("currTab");
		$(".homeTabs").eq(tab).show().addClass("currTab");
	},
	function() {
		var tab = $('.introLnk').index(this);
		$(".introLnk").filter(":eq("+tab+")").children('div').css('background', '#D7EAE3');
	});

	//slider boxes / carousel
	if($('body').attr('id')=='home') {
		
		slider1('/oasis/category/latest-news/featured/xml/');			
		$('#sliders div').hover(function() {		
			$(this).css({'background-color':'#eaf4f0'});
		},function() {		
			$(this).css({'background-color':'#f5f5f5'});
		});
	 }

});

//slider boxes / carousel function
function slider1(url) {
	$.ajax({
		type: "GET",
		//url: "/scribe/themes/oasis/data/"+url+"?cachebuster=" + 1*new Date(),
		url: url,
		dataType: "xml",
		success: function(xmlData)
		{					
			xmlDataSet = xmlData;
			carousel();
		},
		error: function()
		{
			$('#slide1').html("<p>There's been an error loading the page. Please <a href='./'>try again</a></p>");
		}	
	});
}

var curPos = 1;
function carousel()
{
	var itemsPerPage = 1;
	var resultSetLength = $("item",xmlDataSet).length + 1;
	var strToAppend = "<div class='slideBtns'>";
	if (itemsPerPage != resultSetLength)
	{
		if (curPos == 1) // First page. Go forward only.
		{
			strToAppend += "<a href='#' onclick='curPos=1;carousel();return false;'><img src='/scribe/themes/oasis/images/icon.prev.png' width='23' height='12' border='0' /></a>";
			strToAppend += "<a href='#' onclick='curPos += "+itemsPerPage+";carousel();return false;'><img src='/scribe/themes/oasis/images/icon.next.png' width='19' height='12' border='0' /></a>";			
		}
		if (curPos > 1 && parseInt(curPos + itemsPerPage) < resultSetLength) // Somewhere inbetween.
		{
			strToAppend += "<a href='#' onclick='curPos -= "+itemsPerPage+";carousel();return false;'><img src='/scribe/themes/oasis/images/icon.prev.png' width='23' height='12' border='0' /></a>";
			strToAppend += "<a href='#' onclick='curPos += "+itemsPerPage+";carousel();return false;'><img src='/scribe/themes/oasis/images/icon.next.png' width='19' height='12' border='0' /></a>";
		}
		if (parseInt(curPos + itemsPerPage) >= resultSetLength) // Last page. Go back only.
		{
			strToAppend += "<a href='#' onclick='curPos -= "+itemsPerPage+";carousel();return false;'><img src='/scribe/themes/oasis/images/icon.prev.png' width='23' height='12' border='0' /></a>";
			strToAppend += "<a href='#' onclick='curPos=1;carousel();return false;'><img src='/scribe/themes/oasis/images/icon.next.png' width='19' height='12' border='0' /></a>";
		}
		
	} 
	strToAppend += "</div><div class='clear'></div>";
	$('#slide1').html(strToAppend);


	var html = "";
	$("title:lt(" + parseInt(curPos + itemsPerPage) + ")",xmlDataSet).filter(":gt(" + parseInt(curPos - 1) + ")").each(function(i) {
		var title = $(this).text();	
		var description = $("description:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text();
		var image = $(this).parent().find('[nodeName="enclosure"]').attr('url');
		var linkd = $("link:eq(" + parseInt(curPos + i) + ")",xmlDataSet).text();
		var linkTxt = $(this).parent().find('[nodeName="dc:creator"]').text();
		
		html += '<div class="slideContent"><div id="slideImg"><img src="'+image+'" border="0" /></div>'
		+ '<div id="slideTxt"><div class="h2">' + title + '</div>'
		+ '<p>'+description.substring(0,50)+'...&nbsp;<a href="'+linkd+'">More<img src="/scribe/themes/oasis/images/icon.arrow.png" width="8" height="8" border="0" /></a>'
		//+ '<p><a href="'+linkd+'">'+linkTxt+'</a><img src="images/icon.arrow.png" width="8" height="8" border="0" /></p>'
		+'</div></div>';		
		
	});
	

/*
	Populate our DIV with the HTML we have constructed.
*/
	$(".slideContent").fadeOut();
	setTimeout("finishAjax('slide1', '"+escape(html)+"')", 700);

}

//slider boxes / carousel function
function slider2(url2) {
	$.ajax({
		type: "GET",
		url: "data/"+url2+"?cachebuster=" + 1*new Date(),
		dataType: "xml",
		success: function(xmlData)
		{					
			xmlDataSet = xmlData;
			carouse2();
		},
		error: function()
		{
			$('#slide2').html("<p>There's been an error loading the page. Please <a href='index.htm'>try again</a></p>");
		}	
	});
}

function finishAjax(id, response) {
  $('#slide1').append(unescape(response));
  $('#slide1').fadeIn();
}



/* ]]> */ 

