// JavaScript Document
var cibox1 = 0;
var cibox2 = 0;
var cibox3 = 0;

function rssitem(id, title, titlelink, description, pubdate) {
	this.id = id;
	this.pubdate = pubdate;
	this.title = title;
	this.description = description;
	this.titlelink = titlelink;
}
function setup() {
	$(function () {
		setuprss();
		setimagequote();
		$('#mainmenu > ul > li').each(function () {
										 	$(this).hover(
														  function () {
															 $(this).find('div').show();
														  },
														  function () {
															  $(this).find('div').hide();
														  }
														  );
										 });
		var pageTitle = 'Northwest+Workforce+Development+Council';
		$('#sharefacebook').attr('href', 'http://www.facebook.com/sharer.php?u=' + document.URL);
		$('#sharestumbleupon').attr('href', 'http://www.stumbleupon.com/submit?url=' + document.URL + '&title=' + pageTitle);
		$('#sharedigg').attr('href', 'http://digg.com/submit?url=' + document.URL + '&title='+ pageTitle);
		$('#sharegoogle').attr('href', 'http://www.google.com/bookmarks/mark?op=edit&bkmk=' + document.URL + '&title='+ pageTitle);
		$('#sharereddit').attr('href', 'http://www.reddit.com/submit?url=' + document.URL + '&title='+ pageTitle);
		$('#sharetwitter').attr('href', 'http://twitter.com/home?status=Currently+reading+' + document.URL);
	});
}

function setuprss() {
	$.ajaxSetup({
		error:function(x,e){
			if(x.status==1){
			alert('You are offline!!\n Please Check Your Network.');
			}else if(x.status==404){
			alert('Requested URL not found.');
			}else if(x.status==500){
			alert('Internel Server Error.');
			}else if(e=='parsererror'){
			alert('Error.\nParsing JSON Request failed.');
			}else if(e=='timeout'){
			alert('Request Time out.');
			}else {
			alert('Unknow Error.\n'+x.responseText);
			}
		}});
		get_xml();
		
		$('#rss1prev').click(function() {
				cibox1 = showitem(cibox1 - 2, newsArray, 'rss1story');
		});
		$('#rss1next').click(function() {
				cibox1 = showitem(cibox1, newsArray, 'rss1story');
		});
		$('#rss2prev').click(function() {
				cibox2 = showitem(cibox2 - 2, publicationsArray, 'rss2story');
		});
		$('#rss2next').click(function() {
				cibox2 = showitem(cibox2, publicationsArray, 'rss2story');
		});
		$('#rss3prev').click(function() {
				cibox3 = showitem(cibox3 - 2, noticesArray, 'rss3story');
		});
		$('#rss3next').click(function() {
				cibox3 = showitem(cibox3, noticesArray, 'rss3story');
		});
}
function showitem(itemnumber, feed, areaid) {
	if (feed.getCount() > 0) {
		if (feed.getCount() == 1) {
		itemnumber = 0;	
		}
		if (itemnumber < 0) {
			itemnumber = feed.getCount() + itemnumber;
		}		
		var area = areaid.substring(0,4);
	
		$('#' + areaid).empty();
		var selecteditem = feed.getAt(itemnumber);
		// grab the post title
	
		var description = selecteditem.description;
		if (description.length > 250) {
			description = description.substring(0,250) + "...";
		}
 	
		var itemtitle = selecteditem.title;
		// grab the post's URL
		var titlelink = selecteditem.titlelink;
		
		// now create a var 'html' to store the markup we're using to output the feed to the browser window
		var html = '';
		$('#' + area + 'link').attr('href',titlelink.htmlDecode());
		html += '<div class="rssstoryheading">' + itemtitle.htmlDecode() + '</div>';
		html += '<p class="description">' + description.htmlDecode() + '</p>';
 
 
		//put that feed content on the screen!
		$('#' + areaid).append($(html));
		$('#' + area + 'current' ).text(itemnumber + 1);
		// increment item counter
		return (itemnumber + 1) % feed.getCount();
	}
	return 0;
}
function get_xml() {
	// setup default containers for the 3 different categories of items in the RSS feed.
	newsArray = new DP_ObCollectionOrdered('id', rssitem);
	publicationsArray = new DP_ObCollectionOrdered('id', rssitem);
	noticesArray = new DP_ObCollectionOrdered('id', rssitem);
	var category = '';
	
	$.get('rss/rss.xml?rnd=' + Math.floor(Math.random()*10000), function(d) {
				if (typeof d == "string") {
					var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
       				xmldoc.async = false;
       				xmldoc.loadXML(d);
				} else {
					var xmldoc = d;	
				}
				$(xmldoc).find('item').each(function (i) {
					category = $(this).find('category').text();
					switch(category) {
						case 'notices':
							noticesArray.add(new rssitem(i, $(this).find('title').text(), $(this).find('link').text(), $(this).find('description').text(), new Date($(this).find('pubDate').text())));
						break;
						case 'publications':
							publicationsArray.add(new rssitem(i, $(this).find('title').text(), $(this).find('link').text(), $(this).find('description').text(), new Date($(this).find('pubDate').text())));
						break;
						default:
							newsArray.add(new rssitem(i, $(this).find('title').text(), $(this).find('link').text(), $(this).find('description').text(), new Date($(this).find('pubDate').text())));
					}
				});
				$('#rss1total').text(newsArray.getCount());
				$('#rss2total').text(publicationsArray.getCount());
				$('#rss3total').text(noticesArray.getCount());
				newsArray.sortByProp('pubdate','Alpha','desc');
				publicationsArray.sortByProp('pubdate','Alpha','desc');
				noticesArray.sortByProp('pubdate','Alpha','desc');
				cibox1 = showitem(0,newsArray,'rss1story');
				cibox2 = showitem(0,publicationsArray,'rss2story');
				cibox3 = showitem(0,noticesArray,'rss3story');
				$('.rssstory').show();
	},($.browser.msie?"text":"xml"));
}
function setimagequote() {
	$.get('imagequote.html', function (d) {
		var dr = $(d).find('tr.datarow');
		var randquote = Math.floor(Math.random()*$(dr).size());
		var himage = $(dr).eq(randquote).find('img');
		var himagesrc = $(himage).attr('src');
		var himagealt = $(himage).attr('alt');
		var hquote = $(dr).eq(randquote).find('.hquote').text();
		$('#headerimage').attr('src',himagesrc);
		$('#headerimage').attr('alt',himagealt);
		$('#quote').text(hquote.stripHTML().trim());
		$('#headerquoteimagewrapper').fadeIn('slow');
	},'html');
}
function setfontsize(basesize) {
	$('.rssstory').css('font-size',basesize);
	$('#quote').css('font-size',basesize);
	$('#footer').css('font-size',basesize);
}

	
