(function($){
var winLoaded = false;
$(window).load(function(){
	winLoaded = true;
});
var close = '<div class="close" style="display: none;"> \
                   <a href="#" role="button" tabindex="0">Schließen</a> \
               </div>'
;
function GetRandom( min, max ) {
	if( min > max ) {
	return( -1 );
	}
	if( min == max ) {
	return( min );
	}
	return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}
$.widget('aperto.stagegallery', {
	options: {},
	_create: function(){
	$('div.teaser-box', this.element).append(close);
	$.ajax({
		url: $.location.coremedia.changeQuery( $('em.gallery-more a', this.element).attr('href'), 'templateId', 'renderSnippet' ),
		dataType: 'text',
		success: $.proxy(this, '_handleData')
	});
	
	this.element.delegate('div.close > a','ariaclick', $.proxy(this, '_hideDetails'));
	this.element.delegate('em.more > a', 'ariaclick', $.proxy(this, 'showDetails'));
	//this.element.inOut($.proxy(this, 'showTeaser'), $.proxy(this, 'hideTeaser'), {mouseDelay: 300});
	},
	_handleData: function(data){
	this.loadTeasers = $(data).find('div.teaser:not(:first)');
	this.teasers = $('div.teaser', this.element).add(this.loadTeasers);
	this._buildGui();
	this._addTeaser();
	this.loadIndex( GetRandom(0, this.teasers.length - 1), true );
	this.element
		.css({opacity: 0})
		.addClass('stage-gallery-ready')
		.fadeTo(300, 1)
	;
	},
	loadPrev: function(){
	var prev = this.index - 1;
	if( prev < 0 ){
		prev = this.teasers.length - 1;
	}
	this.loadIndex(prev);
	return false;
	},
	loadNext: function(){
	var next = this.index + 1;
	if( next >= this.teasers.length ){
		next = 0;
	}
	this.loadIndex(next);
	return false;
	},
	loadIndex: function(index, fast){
	if(this.index === index){return;}
	this._addTeaser();
//	this.teasers.filter(':eq('+ index +')').show();
//	this.teasers.filter(':eq('+ this.index +')').hide();
	this.teasers.filter(':eq('+ index +')')
		.css({opacity: 0})
		.crossFade({
		hideElement: this.teasers.filter(':eq('+ this.index +')'),
		animateHeight: false,
		animOpts: {
			duration: (fast) ? 0 : 400
		}
		})
	;
	this.index = index;
	this.pagination.html('<span>'+ (index + 1) +'/'+ this.teasers.length +'</span>');
	},
	
	showDetails: function(e){
	var teaser = this.teasers.eq(this.index);
	$('div.short-decription', teaser).slideUp();
	$('div.long-decription', teaser)
		.each(function(){
		var height = Math.min(260, $(this).height());
		$(this)
			.css({height: 0, display: 'block'})
			.animate({
			height: height
			}, {
			complete: function(){
				var close = $('div.close', teaser);
				close[ (this.scrollHeight > this.offsetHeight) ? 'addClass' : 'removeClass' ]('has-scroll');
				close[ ($.support.opacity) ? 'fadeIn' : 'show' ] ();
				if(height < 260){
				$(this).css({height: ''});
				}
			}
			})
		;
		})
	;
	$('div.next, div.prev', this.element).fadeOut();
	$('h1', teaser).fadeOut();
	$('> div.close', teaser).fadeIn();
	this.isDetailsVisible = true;
	return false;
	},
	_hideDetails: function(){
	if( !this.isDetailsVisible ){return;}
	var teaser = this.teasers.eq(this.index);
	$('div.close', teaser)[ ($.support.opacity) ? 'fadeOut' : 'hide' ] ();
	$('div.short-decription', teaser).slideDown();
	$('div.long-decription', teaser).slideUp(400, function(){
		$(this).css('height', '');
	});
	$('div.next, div.prev', this.element).fadeIn();
	$('h1', teaser).fadeIn();
	this.isDetailsVisible = false;
	return false;
	},
	
	_buildGui: function(){
	//pagination
	this.pagination = $('div.pagination', this.controls).html('<span>1/'+ this.teasers.length +'</span>');
	this.index = 0;
	//prev next
	$('div.prev a', this.element)
		.attr('tabindex', '0')
		.bind('ariaclick', $.proxy(this, 'loadPrev'))
	;
	$('div.next a', this.element)
		.attr('tabindex', '0')
		.bind('ariaclick', $.proxy(this, 'loadNext'))
	;
	if($.support.waiAria){
		$('div.prev a, div.next a', this.controls).attr('role', 'button').removeAttr('href');
	}
	this.pagination = $('div.pagination', this.element).html('<span>'+ (this.index + 1) +'/'+ this.teasers.length +'</span>');
	$('div.next, div.prev', this.element).fadeIn( 800 );
	},
	_addTeaser: function(){
	if(this.isTeaserBuild){return;}
	this.loadTeasers.addClass('load')
	this.element.find('div.rack-design').append(this.loadTeasers.hide());
	$('div.teaser-box', this.loadTeasers).append(close);
	this.pagination = $('div.pagination', this.element).html('<span>'+ (this.index + 1) +'/'+ this.teasers.length +'</span>');
	
	this.teasers = $('div.teaser', this.element);
	
	this.teasers.addClass(function(i){
		return 'teaser-'+i;
	})
	this.isTeaserBuild = true;
	}
});
})(jQuery);
