/**
* Set the background size
*
* Resize the background when the browser window gets resized
* Written by Pascal Beyeler (anvio.ch)
*
* @param		e		event object
* @return		void
*/

function setBackgroundSize(e) {

	var background = YAHOO.util.Selector.query('div#background img')[0];
	var viewportWidth = YAHOO.util.Dom.getViewportWidth();
	var viewportHeight = YAHOO.util.Dom.getViewportHeight();

	if(viewportWidth > 960) {
		document.body.style.width = viewportWidth + 'px';
	} else {
		document.body.style.width = '960px';
	}
	if(viewportHeight > 800) {
		document.body.style.height = viewportHeight + 'px';
	} else {
		document.body.style.height = '840px';
	}

}

YAHOO.util.Event.onDOMReady(setBackgroundSize);
YAHOO.util.Event.addListener(window, 'resize', setBackgroundSize);


/**
* Add the onmouseover and onmouseout events to the icons
*
* Written by Pascal Beyeler (anvio.ch)
*
* @param		e		event object
* @return		void
*/

function setMouseEvents(e) {

	var iconIds = ['itunes_logo', 'exlibris_logo', 'musicload_logo', 'sevendigital_logo', 'weltbild_logo', 'amazon_logo', 'caritas_icon', 'add_icon', 'facebook_icon', 'twitter_icon'];

	for(var i = 0; i < iconIds.length; i++) {

		var element = YAHOO.util.Selector.query('div#' + iconIds[i] + ' img')[0];

		var mouseOverEvent = function(e) {
			var target = YAHOO.util.Event.getTarget(e);
			YAHOO.util.Dom.addClass(target, 'button_mouseover');
		};

		var mouseOutEvent = function(e) {
			var target = YAHOO.util.Event.getTarget(e);
			YAHOO.util.Dom.removeClass(target, 'button_mouseover');
		};

		YAHOO.util.Event.addListener(element, 'mouseover', mouseOverEvent);
		YAHOO.util.Event.addListener(element, 'mouseout', mouseOutEvent);

	}

}

YAHOO.util.Event.onDOMReady(setMouseEvents);


/**
* Get the Twitter RSS Feed
*
* Written by Pascal Beyeler (anvio.ch)
*
* @param		url		url to fetch
* @return		void
*/

Twitter = function(url) {

	//default vars
	this.url = url;
	this.items = [];
	this.currIndex = 0;
	this.displayCount = 2;
	this.arrowUp = YAHOO.util.Selector.query('div#arrow_up img')[0];
	this.arrowDown = YAHOO.util.Selector.query('div#arrow_down img')[0];

	//load and render the feed
	this.loadFeed();

};


/**
* Load Feed
*
* Load the feed
*
* @return		void
*/

Twitter.prototype.loadFeed = function() {

	var callbacks = {

		success: function (o) {

			try {

				//parse the json response
				this.items = YAHOO.lang.JSON.parse(o.responseText);

				//set the arrow button handlers
				this.setHandlers();

				//render the feed
				this.render(this.currIndex, this.displayCount);

			} catch (x) {
				return;
			}

		},
		scope: this
	};

	YAHOO.util.Connect.asyncRequest('GET', this.url, callbacks);

}


/**
* Set Handlers
*
* Sets the button callbacks for previous and next post
*
* @return		void
*/

Twitter.prototype.setHandlers = function() {

	//set the arrow onclick handler
	YAHOO.util.Event.addListener(this.arrowUp, 'click', function(event) {
		this.currIndex = this.currIndex - 1;
		this.render(this.currIndex, this.displayCount);
	}, this, true);

	YAHOO.util.Event.addListener(this.arrowDown, 'click', function(event) {
		this.currIndex = this.currIndex + 1;
		this.render(this.currIndex, this.displayCount);
	}, this, true);

}


/**
* Render
*
* Render the feed
*
* @param		from	start index
* @param		count	amount of posts to display
* @return		void
*/

Twitter.prototype.render = function(from, count) {

	//show or hide the arrows
	if(this.currIndex > 0) {
		this.arrowUp.style.display = 'inline';
	} else {
		this.arrowUp.style.display = 'none';
	}

	if((this.items.length - this.currIndex - this.displayCount) > 0) {
		this.arrowDown.style.display = 'inline';
	} else {
		this.arrowDown.style.display = 'none';
	}

	//render the items
	var rssFeedNode = YAHOO.util.Dom.get('rss_news');
	rssFeedNode.innerHTML = '';
	for (var i = from; i < this.items.length && i < (from + count); i++) {

		rssFeedNode.innerHTML += '\
			<div class="rss_news_entry">\
				<b>' + this.items[i].title + '</b>, <i>' + this.items[i].date + ':</i><br />\
				' + this.items[i].description + '\
				 <a href="' + this.items[i].link + '" target="_blank">more</a>\
			</div>\
		';

	}

}

//instantiate the rss feed laoder
YAHOO.util.Event.onDOMReady(function() {
	var twitter = new Twitter('/twitter');
});
