//<![CDATA[
$(document).ready(function() {
	
	function parseXml(xml) { //ie can't parse xml
		if (jQuery.browser.msie) {  
			var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");  
			xmlDoc.loadXML(xml);  
			xml = xmlDoc;  
		}  
		return xml;  
	}  
	
	function getRSS() {
		$.ajax({ //get and parse rss events feed (via php proxy)
			type: "GET",
			cache: false,
			url: "/e/index_eventsproxy.php",
			dataType: ($.browser.msie) ? "text" : "xml",
			success: function(rss) {
				$('#include_calendar').empty();
				$('#include_calendar').append('<ul/>');
				rss = parseXml(rss);
				$(rss).find('item').each(function(index){
					if(index<3) { //only get first 3 items
						var t = $(this).find('title').text();
						var l = $(this).find('link').text();
						var d = $(this).find('description').text().replace(/[\s\S]*?Date:<\/strong>/gi,'').replace(/<.*/gi,'').replace(', 2009',''); //get date and regex
						$('#include_calendar ul').append('<li>'+d+': <a href="'+l+'">'+t+'</a></li>');
					}
				});
				$('#include_calendar').show(250);
			}
		});
	}
	
	getRSS();
	
	/*$('dl').wrap('<ul></ul>');// reformat events widget output
	$('#include_calendar ul').find('dt').each(function(){
		var d = $(this).next('.date').text().replace(/<\/?[^>]+>/gi, '');
		var l = $(this).find('a').attr('href');
		var t = $(this).find('a').text();
		$('#include_calendar ul').append('<li>'+d+': <a href="'+l+'">'+t+'</a></li>');
	});
	$('#include_calendar ul dl').remove();
	$('#include_calendar').show(250);*/
	
	/*==================================================================================
	Converts news feed to adequate syntax, eg:
	
	INCLUDE:
	--------
	<div id="include_news">
		<!-- EDITABLE AREA -->
		<!-- com.omniupdate.div label="rss" group="Everyone" button="801" break="break" border="green" padding="2" bgcolor="white" -->
		<!-- com.omniupdate.editor csspath="/we-css/webexhibits-editor.css" -->
			<table>
				<thead>
					<tr>
						<td>Title</td>
						<td>URL</td>
						<td>Description</td>
						<td>Thumbnail</td>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td>Mansueto Cam</td>
						<td>http://mansuetocam.lib.uchicago.edu/view/index.shtml</td>
						<td>Get a bird's-eye view of Mansueto Library construction from our new webcam.</td>
						<td><img src="http://www.lib.uchicago.edu/e/whatsnew/images/ManCam2.jpg"/></td>
					</tr>
					<tr>
						<td>D'Angelo Access</td>
						<td>http://www.lib.uchicago.edu/e/using/access/students.html</td>
						<td>Access to the D'Angelo Law Library for non-law students will be limited from March 7-16 for law school reading and exam periods.</td>
						<td><img src="http://www.lib.uchicago.edu/e/whatsnew/images/dangelo.jpg"/></td>
					</tr>
					<tr>
						<td>Booker Prize</td>
						<td>http://www.lib.uchicago.edu/e/alumnifriends/brooker/</td>
						<td>Apply for the T. Kimball Brooker Book Collecting Prize- deadline April 1, 2009</td>
						<td><img src="http://www.lib.uchicago.edu/e/whatsnew/images/brookernew.gif"/></td>
					</tr>
				</tbody>
			</table>
		<!-- /com.omniupdate.div -->
	</div>
	
	BECOMES:
	--------
	<div id="news">
		<div class="news_item" style="border:0;">
			<img alt="Mansueto Cam" src="http://www.lib.uchicago.edu/e/whatsnew/images/ManCam2.jpg"/>
			<p>
				<a href="http://mansuetocam.lib.uchicago.edu/view/index.shtml">Mansueto Cam</a>
				<br/>
				Get a bird's-eye view of Mansueto Library construction from our new webcam.
			</p>
		</div>
		<div class="news_item">
			<img alt="D'Angelo Access" src="http://www.lib.uchicago.edu/e/whatsnew/images/dangelo.jpg"/>
			<p>
				<a href="http://www.lib.uchicago.edu/e/using/access/students.html">D'Angelo Access</a>
				<br/>
				Access to the D'Angelo Law Library for non-law students will be limited from March 7-16 for law school reading and exam periods.
			</p>
		</div>
		<div class="news_item">
			<img alt="Brooker Prize" src="http://www.lib.uchicago.edu/e/whatsnew/images/brookernew.gif"/>
			<p>
				<a href="http://www.lib.uchicago.edu/e/alumnifriends/brooker/">Brooker Prize</a>
				<br/>
				Apply for the T. Kimball Brooker Book Collecting Prize- deadline April 1, 2009
			</p>
		</div>
	</div>
	==================================================================================*/
	$('#include_news').after('<div id="news"></div>');
	$('#include_news > table > tbody > tr').each(function(i){ //for each row with index i
		var title,href,desc,img;
		$('#news').append('<div class="news_item"></div>');
		$(this).children('td').each(function(j){ //get content of each cell, and append it to row i with desired syntax
			if (j==0) title = $(this).html().replace(/<\/?[^>]+>/gi, '');
			if (j==1) href = $(this).html().replace(/<\/?[^>]+>/gi, '');
			if (j==2) desc = $(this).html();
			if (j==3) {
				img = $(this).children('img:first');
				//get all the syntax together in .news_item if we're dealing with the last cell:
				$('.news_item:eq('+i+')').prepend(img).append('<p><a href="'+href+'">'+title+'</a><br />'+desc+'</p>'); 
			}
		});
	});
	$('.news_item:first').css('border','0'); //now separator for first news item
	$('#include_news').remove(); //cleanup
	
	$('#m_home_tab').html($('#m_main').html());	//copy home tab to main display
	
	$('.ql').livequery('click', function(event) { //quicklinks setup
        $('.quicklinks').toggle();
	});

	var t = $('title').html(); //Library specific help tab setup
	if (t.indexOf('Law') != -1) var law = true; //D'Angelo
	if(law) {
		$('#help_classlib').parent().remove(); //remove class librarians link
		$('#help_ask').text('Ask A Law Librarian'); //ask a librarian -> ask a law librarian
		$('#help_ask').attr('href','http://www.lib.uchicago.edu/e/law/using/reference/ask.html');
		$('#help_workshops').attr('href','https://training.uchicago.edu/categories.cfm?family_id=8'); //workshops link
		$('#help_computing').attr('href','http://www.lib.uchicago.edu/e/law/using/computing/'); //computing link
		$('#footer_dir').attr('href','http://www.lib.uchicago.edu/e/law/using/staff/'); //footer directory link edit
	}
	if (t.indexOf('Crerar') != -1) var crerar = true; //Crerar
	if (t.indexOf('Eckhart') != -1) var eckhart = true; //Eckhart
	if(crerar || eckhart) {
		$('#help_researchguides').text('Science Research Guides'); //subject research guides -> science research guides
		$('#help_researchguides').attr('href','http://www.lib.uchicago.edu/e/su/sci');
	}
		
	$('#tabs a').click(function() { //main page tab setup
		$('#m_main').html($('#' + $(this).attr("id") + '_tab').html()); //replace html in homepagemain 	
		if($(this).attr("id") == 'm_home') getRSS(); //if we're dealing with the home page, reload rss feeds
		
		$('.quicklinks').hide(); //hide quicklinks as default
		
		$(this).parent().siblings().children().css('display','inline'); //show other anchor props
		$(this).parent().siblings().children('img').css('display','none');//hide other images
		
		$(this).next('img').css('display','block'); //show this tab's image
		$(this).hide();//hide this tab's anchor prop
	});
	
	function tabRedir() { //redirect to correct tab on pageload (pass tab via url param (e.g /e/index.html#r=how))
		var tabredir = $.url.param("r");
		if(tabredir=="how") {
			$("#m_how").click();
		}
		if(tabredir=="myacc") {
			$("#m_myaccounts").click();
		}
		if(tabredir=="help") {
			$("#m_help").click();
		}
	}
	
	tabRedir();
	
	$('#linkssq a').hover( //hover functionality for library logo links
		function() {
			i = $('#linkssq a').index(this);
			xpos = (-45)+(i*9);
			pos = xpos+"px 0px";
			$('#linkssq').css("backgroundPosition",pos);
		},
		function() {
			$('#linkssq').css("backgroundPosition","-54px 0px");
		}
	);
});
//]]>

