var rows = new Array();
var perPage = 5;
var pages = {'news':0,'events':0};
var re = /([a-z]+)([0-9]+)/;

function getItems() {
	rows.news = $$('#newsbox_items a');
	rows.events = $$('#eventsbox_items a');
}
function setPage(type, offset) {
	if (pages[type]+offset < 0 || (pages[type]+offset)*perPage >= rows[type].length) return;
	pages[type] = parseInt(pages[type]+offset);
	setVisible(type, pages[type]);
}
function setVisible(type, offset) {
	if (offset < 0 || offset*perPage >= rows[type].length) return;
	for (var i=0; i<rows[type].length; i++)
		rows[type][i].style.display = 'none';
	for (var i=offset*perPage; i<rows[type].length && i<perPage*(offset+1); i++)
		rows[type][i].style.display = 'block';
	$(type+'_page').innerHTML = 'Page '+(offset+1)+'/'+Math.ceil(rows[type].length/perPage);
	if (type == 'news') {
		$('nav_newer').className = (offset-1 < 0)? 'nav_inactive' : 'nav_active';
		$('nav_older').className = ((offset+1)*perPage >= rows[type].length)? 'nav_inactive' : 'nav_active';
	}
	if (type == 'events') {
		$('nav_sooner').className = (offset-1 < 0)? 'nav_inactive' : 'nav_active';
		$('nav_later').className = ((offset+1)*perPage >= rows[type].length)? 'nav_inactive' : 'nav_active';
	}
}
function loadItem(type, id) {
	if (parseInt(id) > 0 || id == 'first') {
		$('whatsnew_page').className = 'bg_'+type;
		$('whatsnew_leftcol').style.visibility = 'hidden';
		$('whatsnew_rightcol').style.visibility = 'hidden';
		new Ajax.Request('feed_item.php?type='+type+'&id='+id, {
			method: 'get',
			onSuccess: function(t) {
				eval('var data = ' + t.responseText);
				if (data.error) {alert('Error: '+data.error); return;}
				var leftcol = '';
				var rightcol = '';
				if (type == 'news') {
					leftcol += '<h2>News Article Date</h2>';
					leftcol += '<p>'+data.news_date_formatted+'</p>';
					rightcol += '<h1>'+data.title+'</h1>';
					rightcol += data.copy;
				} else if (type == 'events'){
					leftcol += '<h2>Event Date &amp; Time</h2>';
					leftcol += '<p>';
					if (data.range_override.length > 0)
						leftcol += data.range_override;
					else
						leftcol += data.ed_month+' '+data.ed_day+', '+data.ed_year+((data.ed_end_day.length > 0 && data.ed_end_day != '0th')?' to<br />'+data.ed_month+' '+data.ed_end_day+', '+data.ed_year:'')+((data.range.length>0)?'<br />'+data.range:'');
					leftcol += '</p>';
					if (data.location)
						leftcol += '<h2>Location Info</h2><p>'+data.location+'</p>';
					else
						leftcol += '<h2>Location Info</h2><p>Willamette Valley Vineyards<br />8800 Enchanted Way SE<br />Turner, OR 97392</p>';
					rightcol += '<h1>'+data.title+'</h1>';
					rightcol += data.details;
				}
				if (data.media && data.media.length > 0) {
					leftcol += '<h2>Related Images</h2>';
					for (var i=0; i<data.media.length; i++)
						leftcol += '<p><a href="images/'+data.media[i]+'" target="_blank"><img src="thumbs/'+data.media[i]+'" border="0" /></a></p>';
				}
				
				$('whatsnew_leftcol').style.visibility = 'visible';
				$('whatsnew_rightcol').style.visibility = 'visible';
				$('whatsnew_leftcol').innerHTML = leftcol;
				$('whatsnew_rightcol').innerHTML = rightcol;
				document.location.replace('#'+type+data.id);
			}
		});
	} else {
		alert('Error retrieving item.');
	}
}
function initialize() {
	if (window.location.hash)
		var matches = window.location.hash.match(re);
	else
		var matches = null;
	if (matches != null && matches[1] == 'news' && parseInt(matches[2]) > 0)
		loadItem('news', parseInt(matches[2]));
	else if (matches != null && matches[1] == 'events' && parseInt(matches[2]) > 0)
		loadItem('events', parseInt(matches[2]));
	else
		loadItem('news','first');
}