// -----------------------------------------------------------------------------------
//
//	Gallery v1.11 (working title)
//	by Lukas Michna
//	Last Modification: 10.12.2010
//
// -----------------------------------------------------------------------------------


/* create closure */
(function(jQuery) {

	jQuery.fn.animateDivContent = function(id){
	
		jQuery('.ci_slide_item').each(function(){
		
			if( jQuery(this).children().attr('id') == 'ci_content_' + id ){
				var temp = jQuery(this).children().attr('id').substring(11);
				if( temp == id ){
					jQuery(this).children().show(jQuery.fn.gallery.defaults.content_transition_time);
					jQuery(this).children().stop().animate({ opacity: jQuery.fn.gallery.defaults.content_opacity }, jQuery.fn.gallery.defaults.content_transition_time );
				}	
			}else{
				jQuery(this).children().stop().animate({ opacity: 0 }, jQuery.fn.gallery.defaults.content_transition_time );
			}
		});
		
	};


	function animateMenue(id){
		jQuery('.ci_navigation_button').each(function(){
			if( jQuery(this).hasClass('ci_navigation_button_active') ){
				var old_style = jQuery(this).siblings();
				old_style.each(function(){
					if( !(jQuery(this).hasClass('ci_navigation_button_active')) ){
						old_style = jQuery(this);
					}
				});
				
				jQuery(this).removeClass('ci_navigation_button_active');
			}
		});

		jQuery('#ci_nav_button_' + id).addClass('ci_navigation_button_active');
	}
	
	
	function unbindAll(){
		jQuery('.ci_navigation_button').unbind('click');
		jQuery('.ci_backnav').unbind('click');
		jQuery('.ci_forwardnav').unbind('click');
		clearInterval(window.interval);
	}
	
	function bindAll(){
		jQuery('.ci_navigation_button').click( jQuery.fn.jumpTo );
		jQuery('.ci_forwardnav').click( jQuery.fn.next );
		jQuery('.ci_backnav').click( jQuery.fn.prev );
		if( jQuery.fn.gallery.defaults.slideshow == 'true' ){
			window.interval = setInterval ( 'jQuery.fn.next()', jQuery.fn.gallery.defaults.slideshow_timer );
		}
	}
	
	
	// Funktion zur Berechnung einer Zufallszahl
	function GetRandom( min, max ) {
		if( min > max ) {
			return( -1 );
		}
		if( min == max ) {
			return( min );
		}
		return( min + parseInt( Math.random() * ( max-min+1 ) ) );
	}
	
	
	jQuery.fn.jumpTo = function() {
		var next_pic = parseInt( jQuery(this).attr('id').substring(14) );
		var cur_pic = parseInt( jQuery('.ci_current').attr('id').substring(14) );
		var x;
		
		if( next_pic != cur_pic ){
			ci_history.push(cur_pic);
			changeMarkUp(cur_pic, next_pic);
		}
	}
	
	
	jQuery.fn.next = function() {
		var cur_pic = parseInt( jQuery('.ci_current').attr('id').substring(14) );
		var next_pic = cur_pic;

		if( jQuery.fn.gallery.defaults.random == 'off' ){
			next_pic = parseInt(parseFloat(cur_pic)) + 1;
			if( next_pic > jQuery('.ci_slider').children().length ){
				next_pic = 1;
			}
		}else if( jQuery.fn.gallery.defaults.random == 'on' ){
			while( next_pic == cur_pic){
				next_pic = GetRandom( 1, jQuery('.ci_slider').children().length );
			}
		}

		if( next_pic != cur_pic ){
			ci_history.push(cur_pic);
			changeMarkUp(cur_pic, next_pic);
		}

	}
	
	
	jQuery.fn.prev = function() {
		
		var cur_pic = jQuery('.ci_current').attr('id').substring(14);
		var prev_pic = cur_pic;
		
		if( ci_history.length >= 1 ){
			prev_pic = ci_history.pop();
		}else if( jQuery.fn.gallery.defaults.random == 'on' ){
			while( prev_pic == cur_pic){
				prev_pic = GetRandom( 1, jQuery('.ci_slider').children().length );
			}
		}else{
			prev_pic = parseInt(parseFloat(cur_pic)) - 1;
			if( prev_pic < 1 ){
				prev_pic = jQuery('.ci_slider').children().length;
			}
		}
		
		if( prev_pic != cur_pic ){
			changeMarkUp(cur_pic, prev_pic);
		}
	}

		
	function changeMarkUp(cur_pic, next_pic){
		unbindAll();
		
		if( jQuery.fn.gallery.defaults.transition == 'diashow' ){
			var width = jQuery('.ci_slide_item').css('width');
			width = width.replace(/px/i, "");
			
			var x;
			if( jQuery.fn.gallery.defaults.diashow_style == 'loop' ){
				x = -(next_pic * width - width);
			}else if( jQuery.fn.gallery.defaults.diashow_style == 'no-loop' ){
				if( next_pic > cur_pic ){
					jQuery('.ci_slide_item:first').after(jQuery('#ci_slide_item_' + next_pic));
					x = -width;
				}else{
					x = 0;
					jQuery('.ci_slide_item:first').before(jQuery('#ci_slide_item_' + next_pic));
					jQuery('.ci_slider').css('left', -width + 'px');
				}
			}
			jQuery('#ci_slide_item_' + next_pic).toggleClass('ci_current');
			jQuery('#ci_slide_item_' + cur_pic).toggleClass('ci_current');
		}
		AnimateContent(x, next_pic, cur_pic);
	}
		
		
	function AnimateContent(x, next_pic, prev_pic){
		//jQuery.fn.animateDivContent(next_pic);
		animateMenue(next_pic);

		if( jQuery.fn.gallery.defaults.transition == 'overlay'){
			jQuery('#ci_slide_item_' + next_pic).stop().animate({ opacity: 1 });
			jQuery('.ci_current').stop() .animate({ opacity: 0 }, jQuery.fn.gallery.defaults.transition_time, function() {
				/* Animation complete */
				bindAll();
				
				jQuery.fn.animateDivContent(next_pic);
				
				jQuery('#ci_slide_item_' + next_pic).toggleClass('ci_current');
				jQuery('#ci_slide_item_' + prev_pic).toggleClass('ci_current');
				jQuery('.ci_navigation_button').click( jQuery.fn.jumpTo );
				
				var test = jQuery('#ci_slide_item_' + next_pic).css('class');
				
			});
		}else if( jQuery.fn.gallery.defaults.transition == 'diashow'){
			jQuery('.ci_slider').stop().animate({ left: x }, jQuery.fn.gallery.defaults.transition_time, function() {
				/* Animation complete */
				jQuery.fn.animateDivContent(next_pic);
				
				bindAll();

				if( jQuery('.ci_slider').css('left') != '0px' && jQuery.fn.gallery.defaults.diashow_style == 'no-loop'){
					jQuery('#ci_slide_item_' + next_pic).after( jQuery('#ci_slide_item_' + prev_pic) );
					jQuery('.ci_slider').css('left', '0px');
				}
			});
		}
	}
	
	
	jQuery.fn.gallery = function(options) {
	
		jQuery.fn.gallery.defaults = {
			slideshow: 'true',
			slideshow_timer: 5000,
			transition_time: 1000,
			transition: 'overlay',
			content_transition_time: 500,
			content_opacity: 1,
			diashow_style: 'no-loop',
			navigation: 'hide',
			random: 'on',
			width: 500,
			height: 400,
			zoom: 'in',
			zoom_factor: 0,
			zoom_time: 3000,
			zoom_corner: 'middleleft',
			random_first_pic: 'random',
			loading_color: 'none',
			loading_pic: 'none'
		};
		var options = jQuery.extend( jQuery.fn.gallery.defaults, options );

		ci_history = new Array();
		
		jQuery('.ci_slideshow_container').children().wrapAll('<div class="ci_slider" />');
		var slide_items = jQuery('.ci_slider').children();
		
		var first_pic = 1;
		if( options.random == 'on' ){
			if( options.random_first_pic == 'random' ){
				first_pic = GetRandom( 1, slide_items.length );
			}else if( parseInt( options.random_first_pic ) > 0 && options.random_first_pic <= slide_items.length){
				first_pic = options.random_first_pic;
			}
		}
		jQuery('.ci_slider').children().addClass("ci_slide_item");
		jQuery('.ci_slideshow_container').css("position","absolute");
		
		/* Den einzelnen Item IDs zuweisen */
		var id = 1;
		slide_items.each(function(){
			jQuery(this).attr('id', 'ci_slide_item_' + id);
			id++;
		});
		
		if( options.transition == 'overlay' ){
			jQuery('.ci_slide_item').css('position','absolute');
			jQuery('.ci_slide_item').each(function(){
				if( !(jQuery(this).attr('id') == 'ci_slide_item_' + first_pic) ){
					jQuery(this).css('opacity', '0' );
				}
			});
		}else if( options.transition == 'diashow' ){
				jQuery('.ci_slide_item').css('position','relative');
				jQuery('.ci_slide_item').css('float','left');
		}
		
		/* Startbildschirm laden */
		jQuery('.ci_slideshow_container').prepend('<div class="ci_loading"></div>');
		if( options.loading_pic != 'none' ){
			jQuery('.ci_loading').css('background', 'url(' + options.loading_pic + ') no-repeat center');
			//jQuery('.ci_loading').css('background', 'url(' + jQuery('#ci_slide_item_' + first_pic + ' img').attr('src') + ') no-repeat center');
		}
		if( options.loading_color != 'none' ){
			jQuery('.ci_loading').css('background-color', options.loading_color);
		}
		
		if( options.transition == 'diashow'){
			jQuery('.ci_slideshow_container').prepend('<div class="ci_shadows"></div>');
			jQuery('.ci_shadows').append('<div class="ci_shadow_right"></div>');
			jQuery('.ci_shadows').append('<div class="ci_shadow_left"></div>');
		}
		
		if( options.slideshow_timer < options.transition_time ){
			jQuery.fn.gallery.defaults.slideshow_timer = options.transition_time + 150;
		}
		
		/* Galeriegröße zuweisen */
		jQuery('.ci_slideshow_container').css('height', options.height + 'px');
		jQuery('.ci_slideshow_container').css('width', options.width + 'px');
		jQuery('.ci_slider').css('height', options.height + 'px');
		if( options.transition == 'overlay'){
			jQuery('.ci_slider').css('width', options.width + 'px');
		}else if( options.transition == 'diashow'){
			jQuery('.ci_slider').css('width', options.width * slide_items.length + 'px');
		}
		
		/* Navigation bauen */
		if( options.navigation == 'show' ){
			jQuery('.ci_slideshow_container').append('<div class="ci_navigation"></div>');
			jQuery('.ci_navigation').append('<div class="ci_navigation_button_active" style="display: none"></div>');
		}
		
		/* Größe der einzelnen Bilder und bg image zuweisen */
		id = 1;
		slide_items.each(function(){
			
			var slide_items_pics = jQuery(this).children();
			jQuery(this).css('height', options.height + 'px');
			jQuery(this).css('width', options.width + 'px');
			
			if( options.zoom_factor == 0 ){
				
				slide_items_pics.each(function(){
					if( jQuery(this).attr("src") ){
						jQuery(this).parent().css('background-image', 'url('+ jQuery(this).attr("src") +')' );
						jQuery(this).remove();
					}
					jQuery(this).attr('id', 'ci_content_' + id);
					jQuery(this).css('opacity', '0');
					
				});
			}else{
				slide_items_pics.each(function(){
					jQuery(this).css('width', (100 + options.zoom_factor) + '%');
				});
			}
			id++;
			/* Navigation erstellen */
			if( options.navigation == 'show' ){
				jQuery('.ci_navigation').append('<div class="ci_navigation_button"></div>');
			}
			
		});

		/* Nav Buttons mit ID versehen */
		if( options.navigation == 'show' ){
			id = 1;
			jQuery('.ci_navigation_button').each(function(){
				jQuery(this).append('<a href="#">' + id + '</a>');
				jQuery(this).attr('id', 'ci_nav_button_' + id);
				jQuery(this).click( jQuery.fn.jumpTo );
				id += 1;
			});
		}

		jQuery(document).ready(function() {
			jQuery('.ci_backnav').click( jQuery.fn.prev );
			jQuery('.ci_forwardnav').click( jQuery.fn.next );
			
			if( options.slideshow == 'true' ){
				interval = setInterval ( 'jQuery.fn.next()', options.slideshow_timer );
			}

			/* Startbild angeben */
			jQuery('#ci_slide_item_' + first_pic).addClass('ci_current');
			animateMenue(first_pic);
			jQuery.fn.animateDivContent(first_pic);
			
			if( options.transition == 'diashow' && options.diashow_style == 'no-loop' && first_pic != 1){
				jQuery('#ci_slide_item_1').before( jQuery('#ci_slide_item_' + first_pic ));
			}else if( options.transition == 'diashow' && options.diashow_style == 'loop' ){
				var x = (-(first_pic * options.width) + options.width) + 'px';
				jQuery('.ci_slider').css('left',  x);
			}
			
			jQuery('.ci_loading').animate({ opacity: 0 }, 800, function(){
				jQuery('.ci_loading').remove();
			} );
		});
	};
	

/* end of closure */
})(jQuery);
