/*
	custom image cross slide
	adhan duhriatna singgih - 31 oktober 2011
*/

jQuery.fn.customQ = function(opt){
	var self = this,
			self_width = this.width(),
			self_height = this.height();
	
	(function(proceed) {
		var n_loaded = 0;
		var plan = self.find('img');
		function loop(i, img) {
			img.onload = function(e) {
				n_loaded++;
				if (n_loaded == plan.length)
					proceed();
			}
			img.src = plan[i].src;
			if (i + 1 < plan.length)
				loop(i + 1, new Image());
		}
		loop(0, new Image());

	})(function() {	
		var imgs = self.find('img').css({
			position: 'absolute',
			visibility: 'visible',
			opacity: '0',
			top: 0,
			left: 0,
			border: 0
		});		
		
		self.css({
			overflow: 'hidden',
			padding: 0,
			position: 'relative'
		});
		
		function recur(count , e, limit, tanda){
			
			
			if (limit != undefined && limit == 0){ 
				imgs.eq(0).animate({ opacity: '1' }, 500);
				return;
			}
			
			if(opt.repeat==true || opt.repeat==undefined){
			
				imgs.eq(e).animate	({"left": "+="+(self_width-imgs.eq(e).width())+"px"}, opt.speed,  
									function(){
										imgs.eq(e).animate	( {visibility: 'visible'}, (opt.delay/2),
											function(){
												imgs.eq(e).animate	({"left": "-="+(self_width-imgs.eq(e).width())+"px"}, opt.speed,
													function(){
														tmp = e+1;
														if(tmp<count) next = tmp; else next = 0;													
														imgs.eq(next).animate({ opacity: '1' }, 2500);
														imgs.eq(e).animate({ opacity: '0' }, 2500, function(){
															tmp = e+1;														
															if(tmp==count) limit--;														
															if(tmp<count) e = tmp; else	e = 0;																												
															recur(count, e, limit);
														})																										
													}
												)
											}										
										)	
									}
								);
			}else{
			
				if(tanda == undefined) var tanda = "+";
				
				imgs.eq(e).animate	({"left": tanda+"="+(self_width-imgs.eq(e).width())+"px"}, opt.speed,  
										function(){
											imgs.eq(e).animate	( {visibility: 'visible'}, (opt.delay/2),
												function(){
													if(tanda=="+") tanda = "-"; else tanda = "+";
													
													tmp = e+1;
													if(tmp<count) next = tmp; else next = 0;				
													
													if(tanda=="-")
															imgs.eq(next).css({"left": (self_width-imgs.eq(next).width())+"px"});
													
													imgs.eq(next).animate({ opacity: '1' }, 2500);
													imgs.eq(e).animate({ opacity: '0' }, 2500, function(){
														if(tanda=="-")
															imgs.eq(e).css({"left":"0"});
														
														tmp = e+1;														
														if(tmp==count) limit--;														
														if(tmp<count) e = tmp; else	e = 0;																												
														recur(count, e, limit, tanda);
													})																										
												}									
											)	
										}
									);
			}
		}
		
		imgs.eq(0).css({ opacity: '1' });	
		var count = self.find('img').length;
		recur(count, 0, opt.loop);
	});
	return self;
} 
