/*
Script: slideBlind.js
	slideBlind - jQuery-based blinds slideshow

Version: 
	1.0

License:
	MIT-style license.

Copyright:
	Copyright (c) 2009 [Ciprian Ionescu](http://ciprianionescu.com/).

Dependencies:
	- jquery 1.3.2 or higher [jquery.js](http://www.jquery.com/)
*/

(function($) {
	
	$.slideBlind_items = 0;
	$.cancelSlide = 0;
	$.funcToCall = Object;

	$.fn.slideBlind = function(options) {

		var opts = $.extend( {}, $.fn.slideBlind.defaults, options);
		var availableImagesArray = [];
		$.slideBlind_items++;
		
		return this.each(function(i) {
			$("<div style='position:relative; width: " + $(this).width() + "px;height: " + $(this).height() + "px;'></div>").appendTo($(this));
			var slideBlindContainer = $(this).find('div')[0];
			var slideBlindContainerWidth = $(slideBlindContainer).width();
			var slideBlindContainerHeight = $(slideBlindContainer).height();
			var activeImage = -1;
			var activeElement = $.slideBlind_items;
			var cancel = 0;
			$("#project_active_images").empty();
			$(slideBlindContainer).find('div.main_image').remove();
			$(opts.imagesArray).each(function(i) {
				var img = new Image();
				$(img).load(function() {
					$(this).hide();
					var div = $('<div class="main_image"></div>').css({
						'background-image': "url('"+this.src+"')",
						'background-position': '0 0',
						'width': slideBlindContainerWidth+"px",
						'height': slideBlindContainerHeight+"px",
						'position': 'absolute',
						'top': 0,
						'left': 0
					}).appendTo(slideBlindContainer).hide();
					
				}).error(function() {
					//in case of error
				}).attr( {
					'src' : this.src
				});
				$("<span class=\"project_image\">&nbsp;</span> ").appendTo('#project_active_images');
			});
						
			$('#project_active_images span.project_image').mouseover(function(){
				$(this).addClass("hovered");
			});
			$('#project_active_images span.project_image').mouseout(function(){
				$(this).removeClass("hovered");
			});
			$('#project_active_images span.project_image').click(function(){
				var index = $("span").index(this)-1;
				//animateImage(index);
			});
			
			animateNextImage();
			
			function animateNextImage(){
				var nextImageIndex = -1;
				var imagesArray = $(slideBlindContainer).find('div.main_image');
				
				var totalImages = imagesArray.size();
				
				if(totalImages == 0){
					$(slideBlindContainer).animate({'opacity':$(slideBlindContainer).css('opacity')}, opts.slideDureation, 'linear', function(){animateNextImage()});
					return;
				}else{
					if(totalImages < activeImage+1){
						nextImageIndex = 0;
					}else{
						nextImageIndex = activeImage + 1;
					}
					
					if(nextImageIndex > totalImages) nextImageIndex = 0;
					
					$(slideBlindContainer).find('div.main_image').hide();
					if(activeImage>=0) $(slideBlindContainer).find('div.main_image:eq('+activeImage+')').show();
					
					$(slideBlindContainer).find('div.animation_div_'+activeElement).remove();
					
					$("<div class=\"animation_div_"+activeElement+"\"></div>").css({
						'width': slideBlindContainerWidth+"px",
						'height': slideBlindContainerHeight+"px",
						'position': 'absolute',
						'overflow': 'hidden',
						'top': 0,
						'left': 0
					}).appendTo(slideBlindContainer).show();
					
					activeImage = nextImageIndex;
					
					for (x=0; x<opts.animatePieces; x++ ){
						if(opts.orientation == 'vertical'){
							$(imagesArray[nextImageIndex]).clone().css({
								'width': Math.ceil(slideBlindContainerWidth/opts.animatePieces)+"px",
								'left': x*Math.ceil(slideBlindContainerWidth/opts.animatePieces)+"px",
								'background-position': "-"+(x*Math.ceil(slideBlindContainerWidth/opts.animatePieces))+"px 0",
								'top':0,
								'opacity': 0
							}).removeClass('main_image').show().appendTo($(slideBlindContainer).find('div.animation_div_'+activeElement)[0]).attr('alt', x);
						}else if(opts.orientation == 'horizontal'){
							$(imagesArray[nextImageIndex]).clone().css({
								'height': Math.ceil(slideBlindContainerHeight/opts.animatePieces)+"px",
								'top': x*Math.ceil(slideBlindContainerHeight/opts.animatePieces)+"px",
								'background-position': "0 -"+(x*Math.ceil(slideBlindContainerHeight/opts.animatePieces))+"px",
								'left':0,
								'opacity': 0
							}).removeClass('main_image').show().appendTo($(slideBlindContainer).find('div.animation_div_'+activeElement)[0]).attr('alt', x);
						}

						if(x==opts.animatePieces-1 && activeImage == totalImages-1){
							setTimeout("$('div.animation_div_"+activeElement+">div:eq("+x+")').animate({opacity:1},"+(opts.animatePiecesAtOnce*opts.animatePieceDuration)+",'linear',function(){"+opts.onComplete+"});",x*opts.animatePieceDuration);
						}else{
							setTimeout("$('div.animation_div_"+activeElement+">div:eq("+x+")').animate({opacity:1},"+(opts.animatePiecesAtOnce*opts.animatePieceDuration)+",'linear',function(){});",x*opts.animatePieceDuration);
						}

					}
					
					$('#project_active_images span.project_image').removeClass('active');
					$('#project_active_images span.project_image:lt('+activeImage+')').addClass('passed');
					$('#project_active_images span.project_image:eq('+activeImage+')').addClass('active');
					
					$(slideBlindContainer).animate({'opacity':$(slideBlindContainer).css('opacity')}, opts.slideDuration, 'linear', function(){
						if(cancel==0){
							animateNextImage();
						}
					});
				
					if(activeImage == totalImages-1){
						cancel = 1;
						$('body').animate({'opacity':1}, delayBetweenProjects, function(){
							//opts.onComplete();
						});
					}
					return;
				}
			}
		});
	}
	
	$.stopBlind = function(){
		$.cancelSlide = 1;
	}
	
	$.fn.slideBlind.defaults = {
		animate : false,
		animatePieces : 10,
		animatePiecesAtOnce : 5,
		animatePieceDuration : 100,
		orientation: 'vertical',
		slideDuration : 2000,
		imagesArray : [ ],
		onComplete : ""
	}
})(jQuery);

