$(document).ready(function(){
	
	$('div.banners, div.portfolio_block, div.example').each(function(){
		var _slides = $('> div.banner, img.preview, div.slide', this),
			_holder = $(this),
			_loader = $('#loader'),
			_next = $('a.next', this),
			_prev = $('a.prev', this),
			_curren = 0,
			_duration = 800,
			_autoRotation = 5000,
			_length = _slides.length - 1,
			_width = _holder.width(),
			_this = this,
			_loadNum = 0,
			_isImg = false,
			_isSlide = false,
			_timer = false;
			
			if (_slides.is('img')) _isImg = true;
			if (_slides.is('div.slide')) _isSlide = true;
						
		var _ul = '<ul class="nav" id="banners-nav">';

		_slides.each(function(){
			var _url = $(this).css('backgroundImage').replace('url("','').replace('")','').replace('url(','').replace(')','');
			if (_isImg) _url = this.src;
			if (_isSlide) _url = $('img',this).attr('src');
			var _newImage = new Image();
			_newImage.src = _url;
			if (_newImage.complete) {
				loadIMG();
			} else {
				_newImage.onload = function(){
					loadIMG();
					this.onload = function(){};
				}
			}
			_ul += '<li><a title="'+$(this).find('em').text()+'" href="#"></a></li>';
		});
		
		function loadIMG(){
			_loadNum++;
			if (_loadNum >= _length+1) {
				_loader.hide();
				_slides.show().css('left',9999);
				_slides.eq(0).css('left',0).hide().fadeIn(_duration).addClass('active');
				if (_length > 1 && _slides.is('div.banner')) _timer = setInterval(switchFade, _autoRotation);
			}
		}
					
		_holder.bind('slideNext', function(){
			if (_slides.is(':animated')) return false;
			if (_timer) { clearInterval(_timer);}
			_curren++;
			if (_curren > _length) _curren = 0;
			_slides.filter('.active').animate({'left':-_width},_duration);
			_links.parent().removeClass('active');
			_links.eq(_curren).parent().addClass('active');
			_slides.eq(_curren).css('left',_width).animate({'left':0},{duration:_duration, complete:function(){
				_slides.removeClass('active').not(this).css('left',9999);
				$(this).addClass('active');
			}});
			return false;
		});
		_holder.bind('slidePrev', function(){
			if (_slides.is(':animated')) return false;
			if (_timer) { clearInterval(_timer);}
			_curren--;
			if (_curren < 0) _curren = _length;
			_slides.filter('.active').animate({'left':_width},_duration);
			_links.parent().removeClass('active');
			_links.eq(_curren).parent().addClass('active');
			_slides.eq(_curren).css('left',-_width).animate({'left':0},{duration:_duration, complete:function(){
				_slides.removeClass('active').not(this).css('left',9999);
				$(this).addClass('active');
			}});
			return false;
		});
		
		_holder.touchwipe({
			wipeLeft: function() { _holder.trigger('slideNext') },
 			wipeRight: function() { _holder.trigger('slidePrev') },
			preventDefaultEvents: false
		});
		
		_ul += '</ul>';
		
		if (_length+1 > 1) {
			if ($('div.page',_holder).length) {
				$('div.page',_holder).prepend(_ul);
			} else {
				_holder.append(_ul);
			}
		}
		var _links = $('#banners-nav a');
		_links.eq(0).parent().addClass('active');
		
		_links.click(function(){
			if (_loader.is(':hidden')) {
				var _index = _links.index(this);
				if (_curren != _index) {
					if (_timer) { clearInterval(_timer);}
					_links.parent().removeClass('active');
					$(this).parent().addClass('active');
					_slides.eq(_curren).fadeOut(_duration/3, function(){
						$(this).show().css('left',9999);
					});
					_curren = _index;
					_slides.eq(_curren).hide().css('left',0).fadeIn(_duration);
				}
			}
			return false;
		});
		
		function switchFade(){
			_links.parent().removeClass('active');
			_slides.eq(_curren).fadeOut(_duration/3, function(){
				$(this).show().css('left',9999);
			});
			_curren++;
			if (_curren > _length) _curren = 0;
			_slides.eq(_curren).hide().css('left',0).fadeIn(_duration);
			_links.eq(_curren).parent().addClass('active');
		}
		
	});
	
});



(function($) { 
   $.fn.touchwipe = function(settings) {
     var config = {
    		min_move_x: 20,
    		min_move_y: 20,
 			wipeLeft: function() { },
 			wipeRight: function() { },
 			wipeUp: function() { },
 			wipeDown: function() { },
			preventDefaultEvents: true
	 };
     
     if (settings) $.extend(config, settings);
 
     this.each(function() {
    	 var startX;
    	 var startY;
		 var isMoving = false;

    	 function cancelTouch() {
    		 this.removeEventListener('touchmove', onTouchMove);
    		 startX = null;
    		 isMoving = false;
    	 }	
    	 
    	 function onTouchMove(e) {
    		 if(config.preventDefaultEvents) {
    			 e.preventDefault();
    		 }
    		 if(isMoving) {
	    		 var x = e.touches[0].pageX;
	    		 var y = e.touches[0].pageY;
	    		 var dx = startX - x;
	    		 var dy = startY - y;
	    		 if(Math.abs(dx) >= config.min_move_x) {
	    			cancelTouch();
	    			if(dx > 0) {
	    				config.wipeLeft();
	    			}
	    			else {
	    				config.wipeRight();
	    			}
	    		 }
	    		 else if(Math.abs(dy) >= config.min_move_y) {
		    			cancelTouch();
		    			if(dy > 0) {
		    				config.wipeDown();
		    			}
		    			else {
		    				config.wipeUp();
		    			}
		    		 }
    		 }
    	 }
    	 
    	 function onTouchStart(e)
    	 {
    		 if (e.touches.length == 1) {
    			 startX = e.touches[0].pageX;
    			 startY = e.touches[0].pageY;
    			 isMoving = true;
    			 this.addEventListener('touchmove', onTouchMove, false);
    		 }
    	 }    	 
    	 if ('ontouchstart' in document.documentElement) {
    		 this.addEventListener('touchstart', onTouchStart, false);
    	 }
     });
 
     return this;
   };
 
 })(jQuery);
