$.extend($.expr[':'], {
    opacity: function(elem, i, attr){
      return( $(elem).css("opacity") === attr[3] + '' );
    }
});

var newsClass = function() {
	var self = this;
	var fadeSpeed = 600;
	
	this.initGrid = function() {
		$('#site-title').remove();
		$('#header').append('<div id="news-header">' +				
							'	<div id="news-image-slider" class="jcarousel-container">' +
							'		<div class="jcarousel-clip">' +	
							'			<ul class="container"></ul>' +
							'		</div>' +
							'		<div class="shadow-left"></div>' +
							'		<div class="shadow-right"></div>' +
							'		<div disabled="disabled" class="jcarousel-prev jcarousel-prev-disabled"></div>' +
							'		<div class="jcarousel-next"></div>' +
							'		<div id="news-slider-arrow"></div>' +
							'		<div id="raven-wing"></div>' +
							'	</div>' +
							'	<div id="news-title-outer"><div id="news-title-slider"></div></div>' +
							'</div>');
		$('#news-home .item').each( function( i, val ) {
			image_container = $(this).children('.image_container');
			title = $(this).find('.text h2');
			$( image_container ).remove();
			$( title ).remove();
			$('#news-image-slider .container').append( '<li id="news-li-' + i + '"><img src="' +  image_container.children('img').attr('src') + '" width="490" height="230"/></li>' );
			$('#news-title-slider').append( '<div id="news-title-' + i + '" style="display:none;">' + title.text() + '</div>' );
			$(this).attr('id', 'news-text-' + i).hide();
		});
	};
	
	// Docu: http://sorgalla.com/projects/jcarousel/examples/static_circular.html
	this.initCarousel = function() {
		$(function() {
			$('#news-image-slider .container').jcarousel({
				auto		: 6,
				scroll		: 1,
				animation	: news.fadeSpeed,
				wrap		: 'circular',
				/*initCallback			: function(instance, state) {
					$('#news-image-slider img:not(:first)').css('opacity', '0.6');
				},*/
				itemFirstInCallback		: {
					onBeforeAnimation	: function(instance, li, index, state) {
						var id = $(li).attr('id').split('-');
						// show news title and text
						$('#news-home').animate({ height: $('#news-text-' + id[2]).height() + 'px'}, news.fadeSpeed, function() {
							$('#news-title-' + id[2] + ',#news-text-' + id[2]).fadeIn(news.fadeSpeed);
						});
						// opacity
						//$(li).children('img').fadeTo(news.fadeSpeed, 1);		
					},
					onAfterAnimation	: function(instance, li, index, state) {}
				},
				itemFirstOutCallback	: {
					onBeforeAnimation	: function(instance, li, index, state) {
						var id = $(li).attr('id').split('-');
						// hide news title and text
						$('#news-title-' + id[2] + ',#news-text-' + id[2]).fadeOut(news.fadeSpeed);	
						// opacity
						//$(li).children('img').fadeTo(news.fadeSpeed, 0.6);		
					},
					onAfterAnimation	: function(instance, li, index, state) {}
				}
			});
		});
	};
};

var news = new newsClass();

$(function(){
	news.initGrid();
	news.initCarousel();
});
