var tabList = [];
tabList.push(new tab('catalogue','li_cat','a_cat'));
tabList.push(new tab('google_scholar','li_gs','a_gs'));

function tab(divName, liName, aName){
	this.theDiv = divName;
	this.theLI  = liName;
	this.theA   = aName;

	this.div_name = function(){
		return this.theDiv;
	}
	this.li_name = function(){	
		return this.theLI;
	}
	this.a_name = function(){
		return this.theA;
	}
}
    
function switchTab(theEvent){
	var which = theEvent.id;
	if (which == ''){ return;}

	for (var foo=0; foo<tabList.length; foo++){
		if (tabList[foo].a_name() == which){
			$('#' + tabList[foo].div_name()).show();
			$('#' + tabList[foo].li_name()).addClass('selected');
			$('#' + tabList[foo].a_name()).unbind('click',function(){});
		} else {
			$('#' + tabList[foo].div_name()).hide();
			$('#' + tabList[foo].li_name()).removeClass('selected');
      $('#' + tabList[foo].a_name()).click(function() { switchTab(this);});
    }
	}
}	

function selectMenu(theA){
	if (theA.parentNode != null)
		$(theA.parentNode).addClass('hover');
}

function unselectMenu(theA){
	if (theA.parentNode != null)
		$(theA.parentNode).removeClass('hover');
}

function doMenu(){
	var tmp = $('#myLeftNav');
	if ($('#myLeftNav') != null){
		var currLoc = location.href;
//		if (!document.all)
			currLoc = currLoc.replace(/^http:\/\/[a-z\.]+\//,'/');
		currLoc = currLoc.replace(/index\.(html|shtml|php|htm)$/,'');
		var theLI = $('#myLeftNav li');
		if (theLI != null){
			for (var foo=0;foo<theLI.length;foo++){
				var theA = $('a',theLI[foo]);
				if (theA[0] != null){
					theA = theA[0];
					var tempLoc = $(theA).attr('href');
					tempLoc = tempLoc.replace(/index\.(html|shtml|php|htm)$/,'');
					if (currLoc == tempLoc){
							$(theLI[foo]).addClass('selected');
							$(theLI[foo]).unbind('mouseout',function(){});
							$(theLI[foo]).unbind('mouseover',function(){});
					} else {
							$(theLI[foo]).removeClass('selected');
							$(theA).mouseover(function(){selectMenu(this);});
							$(theA).mouseout(function(){ unselectMenu(this);});
					}
				}
			}
		}
	} // if leftNav
}

function loadNewsEvents(){
	$('#news_items').empty();
	$('#event_items').empty();
	$('#news_items').append($('<li><p>Loading....</p></li>'));	
	$('#event_items').append($('<li><p>Loading....</p></li>'));	
	var url = '/libraries/includes/php/rsscache.php?http://myuminfo.umanitoba.ca/Feeds/newsFeed.asp?sec=13&typ=2';
	var params = {};
	$.get(url, params, showNews);
	url = '/libraries/includes/php/rsscache.php?http://myuminfo.umanitoba.ca/Feeds/eventsFeed.asp?sec=13&typ=2';
	$.get(url, params, showEvents);
}

function showNews(data, textStatus){
	showNewsEvents(data,'news_items');
}
function showEvents(data, textStatus){
	showNewsEvents(data,'event_items');
}

function showNewsEvents(data, ne){
	var news = $('item',data);
	// Limit is the lesser of the number of news/events stories or 3
	var limit = (news.length < 3? news.length : 3); 
	$('#' + ne).empty();
	if (limit == 0){
		var msg = ne.replace('_items','');
		if (msg == 'event'){ msg+= 's';}
		$('#'+ ne ).append($('<li>No ' + msg + '</li>'));
	} else {
		var container = $('#' + ne)[0];
		for (var foo=0; foo<limit; foo++){
			makeLI(news[foo],container);
		}	
	}		
	$('#' + ne + ' li:first-child').css('display','block');
	$('#' + ne + ' li:first-child').addClass('curr');
	if (limit > 1){
		$('#' + ne ).cycle({fx : 'scrollHorz', pause: 1, speed: 2000, timeout: 7000});
	}
}

function makeLI(item,container){
	var link = $('link',item).text();
	var title = $('title',item).text();
	var theLI = document.createElement('li');
	var theH = document.createElement('h4');
	var theA = document.createElement('a');
	$(theA).attr('href',link);
	theA.appendChild(document.createTextNode(title));
	theH.appendChild(theA);
	theLI.appendChild(theH);
	var sDate = $('ev\\:startdate, startdate',item);
	if (sDate.length > 0){	
		sDate = sDate.text();
		var dateV = new Date(sDate.substr(0,4),sDate.substr(5,2),sDate.substr(8,2),sDate.substr(11,2),sDate.substr(14,2));
		$(theLI).append($('<p>' + dateV.format("M j, Y @ h:i a") + '</p>'));
	}
	container.appendChild(theLI);
}

