/*!
 * jewsyBox 0.1.3 - jQuery News Widget
 * http://www.janus-wa.de
 *
 * Copyright 2010, JANUS DIE WERBEMANUFAKTUR
 * Author: Ralf Heumann
 *
 * inspired by: James Padolsey (http://james.padolsey.com/)
 * Release: Wed Jul 23 2009
 *
 * Date: Tue Feb 02 11:47:01 2010
 */

function showNewContent() {
	$('#jewsyInput').fadeIn('normal',hideLoader());
}
function hideLoader() {
	$('#jewsyLoader').fadeOut(200);
}
function getJewsIds() {
	var getJewsIds = $('#jewsyInput code').html();
	var getJewsIds = getJewsIds.split(",");

	return getJewsIds;
}

$(document).ready(function() {

	// Content file
	jewsyFile = 'jews.php';

	// Load the first content
	$('#jewsyInput').load(jewsyFile,'',function() {
		$(this).hide();

		// Fetch all ID's, put them into an array and set first hash value
		var jewsyIDString = getJewsIds();
		var lastJews = jewsyIDString[jewsyIDString.length-1];

		window.location.hash = 'j'+lastJews;

		if(lastJews == jewsyIDString[0]) {
			$('#jewsyPrevious').hide();
		}
		$('#jewsyNext').hide();

		// Hide Loader an show the first entry
		$('#jewsyLoader').hide();
		$(this).fadeIn(200);
	});

	// Switch input onClick
	$('#jewsyNav li a').click(function(){
		var href = jewsyFile;
		var jewsyIDString = getJewsIds();

		for (var key in jewsyIDString) {
			if (jewsyIDString[key] == window.location.hash.substr(2)) {
				var last = parseInt(key)-1;
				var next = parseInt(key)+1;
			}
		}

		// Which button clicked?
		navButton = $(this).parents('li').attr('id');

		if(navButton == 'jewsyPrevious') {
			if(jewsyIDString[last] != undefined) {
				toLoad = href+'?id='+jewsyIDString[last];
				window.location.hash = 'j'+jewsyIDString[last];
			}
		}
		if(navButton == 'jewsyNext') {
			if(jewsyIDString[next] != undefined) {
				toLoad = href+'?id='+jewsyIDString[next];
				window.location.hash = 'j'+jewsyIDString[next];
			}
		}

		if(window.location.hash.substr(2) == jewsyIDString[0]) {
			$('#jewsyPrevious').fadeOut(500);
		} else {
			$('#jewsyPrevious').fadeIn(500);
		}

		if(window.location.hash.substr(2) == jewsyIDString[jewsyIDString.length-1]) {
			$('#jewsyNext').fadeOut(500);
		} else {
			$('#jewsyNext').fadeIn(500);
		}

		$('#jewsyInput').fadeOut('fast', function () {
			$('#jewsyInput').load(toLoad,'',showNewContent());
		});
		$('#jewsyLoader').fadeIn(200);

		return false;

	});
});