(function($) {
    $.fn.extend({
		slideshow: function(options) {
			
			var config = {};
			config.slideWidth = 1600;
			config.current = 0;
			config.scrollspeed = 800;
			config.speed = 8000;
			config.touchDevice = 'ontouchstart' in document;
			config.doupdate = true;
			
			return this.each(function() { 	
                            
				if(options) {
					$.extend(config, options);
				}
			
				var o = {
					obj: null,
					opts: null,
					totalSlides: 0,
					current: 0,
					sliderPos: 0,
					startDrag: 0,
					maxLeft: 0,
					init : function(obj, opts) {
                                            
						o.opts = opts;
						o.current = o.opts.current;
						o.obj = obj
						o.slideshow = $(obj); 
						o.slideshow.after('<a href="javascript:void(0);" id="scrollerLeft">Left</a><a href="javascript:void(0);" id="scrollerRight">Right</a>');
						o.slider = $('#landing-feature ul');
							o.slides = $('li', o.slider);
						o.total = o.slides.length;
						o.controls = $('#slider-controls');
							o.links = $('li:not(first-child) a:not(.browse-left,.browse-right)',o.controls);
							o.left = $('#scrollerLeft').hide();
							o.right = $('#scrollerRight');
							o.bLeft = $('.browse-left',o.controls);
							o.bRight = $('.browse-right',o.controls);
							if(config.touchDevice == false) {
								o.right.hide();
							};
						o.sliderPos = 0;
						
						//o.slider.append(o.slides.filter('li').clone());
						o.totalSlides = o.slides.length;
						
						o.maxLeft = -((o.totalSlides - 1) * o.opts.slideWidth);
						o.events();
						if(o.slider.find('.selected').html()) {
							o.slider.css('left','-1600px');
							o.slide(1);
						};
					},
					
					/* animate slider */
					slide : function(int) {
						if(int >= o.total || int < 0) { return false; }
						if(config.doupdate!= false && o.current != int)
						{
							var imgClass = $(o.slides[int]).find('img').attr('class');
							redrawGalleries(imgClass);
						}
						o.current = int;
						o.sliderPos = -(o.current * o.opts.slideWidth);
						o.slider.stop().animate({'left' : o.sliderPos}, o.opts.scrollspeed, 'easeOutQuint', o.update());
						if(o.current == o.total-1 ) {
							o.right.hide();
							o.bRight.addClass('disabled');
						}else if(o.current == 0){
							o.left.hide();	
							o.bLeft.addClass('disabled');
						};
						if(o.left.is(':hidden') && o.current!=0){
							o.left.stop().fadeTo(300, 1, 'easeOutQuad');
							o.bLeft.removeClass('disabled');
						};
						if(o.right.is(':hidden') && o.current!=o.total-1){
							o.right.stop().fadeTo(300, 1, 'easeOutQuad');
							o.bRight.removeClass('disabled');
						};
					},
					
					/* update selected link */
					update : function(){
						
						o.current == o.totalSlides ? o.current = 0 : null;
						o.links.filter('.selected').removeClass('selected')
							.end().filter('[rel="' + (o.current +1) + '"]').addClass('selected');
						
					},
					
					/* bind events */
					events : function() {
                                              	o.links.click(function(e){
                                                        //alert("ceva");
							e.preventDefault();
							o.slide($(this).attr('rel')-1);
						});
						o.slideshow.parent().hoverIntent(function(){
							if(o.current!=0) o.left.stop().fadeTo(100, 1, 'easeOutQuad');
							if(o.current!= o.total-1) o.right.stop().fadeTo(300, 1, 'easeOutQuad');
						}, function(){
							o.left.hide();
							o.right.hide();
						});
						o.left.click(function(e){ o.slide(o.current-1);	});
						o.right.click(function(e){ o.slide(o.current+1); });
						o.bLeft.click(function(e){ e.preventDefault(); o.slide(o.current-1); });
						o.bRight.click(function(e){ e.preventDefault(); o.slide(o.current+1); });
						o.slideshow.mousedown(o.onMouseDown);
						//if(o.obj.addEventListener != null) o.obj.addEventListener('touchstart', o.onMouseDown, true);
					},
					
					onMouseDown : function (event)
					{
						var pageX = event.pageX;
						if(event.type == "touchstart")
						{
							var touches = event.touches,
							first = touches[0];
							
							event.preventDefault();
							event.stopPropagation();
							pageX = first.clientX;		
						}
						
						o.slider.addClass('dragging');
						
						$(document).mousemove(o.onDragging);
						//if(o.obj.addEventListener != null) o.obj.addEventListener('touchmove', o.onDragging, true);
						$(document).mouseup(o.onMouseUp);
						//if(document.addEventListener != null) document.addEventListener('touchend', o.onMouseUp,true);
						
						o.startDrag = pageX;
						o.lastDragValue = pageX;
						event.preventDefault();
						return false;
					},
					onDragging : function (event) 
					{
						var pageX = event.pageX;
						if(event.type == "touchmove")
						{
							event.preventDefault();
							event.stopPropagation();
							//event = convertToMouseEvent(evt);
							var touches = event.touches,
							first = touches[0];
							
							pageX = first.clientX;
						}
						var dif = (pageX - o.lastDragValue);
						o.sliderPos += dif;
						if(o.sliderPos > 0) o.sliderPos = 0;
						if(o.sliderPos < o.maxLeft) o.sliderPos = o.maxLeft;
						
						o.slider.css('left',o.sliderPos + "px");
						o.lastDragValue = pageX;
						//alert(o.sliderPos);
						return false;
					},
					onMouseUp : function (event)
					{
						/*if(event.type == "touchend")
						{
							event.preventDefault();
							event.stopPropagation();
							//event = convertToMouseEvent(evt);
						}*/
						
						$(document).unbind('mousemove',o.onDragging);
						$(document).unbind('mouseup',o.onMouseUp);
						//if(document.addEventListener != null) document.removeEventListener('touchmove',o.onDragging,true);
						//if(document.addEventListener != null) document.removeEventListener('touchend',o.onMouseUp,true);
						
						var dif = (o.startDrag - o.lastDragValue);
						if(dif > 400 && o.current < o.totalSlides -  1)
						{
							o.slide(o.current + 1);
						}
						else if (dif < -400 && o.current > 0)
						{
							o.slide(o.current - 1);
						}
						else
						{
							o.slide(o.current);
						}
						
						o.slider.removeClass('dragging');
						
						//--------------						
						
					}
				}
				o.init(this,config);
			});
		}
	});
})(jQuery);
