ajaxSlide = Class.create(Abstract, {
	    initialize: function (scroller,mainEl,next,prev, options) {
	     
		this.scrolling	= false;
		this.requestRunning = false;
		this.firstLoaded = false;
		this.scroller	= scroller;
		this.mainEl = mainEl;
		this.nextSlide = mainEl;
        this.next = next;
        this.prev = prev;		
        this.options  = Object.extend({url:'/works/view.json',inc:'incoming',preloader:'carousel_loader' }, options || {});
        
        this.id = 0;
        this.pr = 0;
        
        this.options.preloader = $(this.options.preloader);
		this.updateHandler = this.update.bind(this);
		this.updateAndScrollHandler = function(trigger, transport, json) {
		    this.update(trigger,transport, json);
		    this.preLImages(trigger);
		    //this.moveTo(trigger);
	     }.bind(this);
	  
	    this.runRequest.bind(this).defer({parameters: {id:this.id,pr:this.pr}, onSuccess:this.updateHandler.curry('null').bind(this)});
       
	    this.next.observe('click',this.click.bind(this));
		this.prev.observe('click',this.click.bind(this));	
				
	   },
	   click: function (event) {

	   var trigger = event.findElement('div');
	      
	   if (!trigger.hasClassName('button_disabled')) {
			this.deactivateControls();
			if(!this.requestRunning){ 
				if(!this.scrolling){	
				  //$('carousel_loader').show();
				  this.options.preloader.show();	 
				  this.runRequest.bind(this).defer({parameters: {id:this.id,pr:this.pr,position:trigger.identify()}, onSuccess:this.updateAndScrollHandler.curry(trigger).bind(this)});
				}
			}
		 }
		event.stop();
	   },	
	   
	   moveTo: function (trigger) {
		 
		  var trigerName = trigger.identify();
		  
		  var moveTo = 0;	
		     
		  if(trigerName == this.next.identify()) { moveTo = -855; } else { moveTo = 855; }
		 
		  this.scrolling = true;
		  	 
			 new Effect.Parallel([
	                  new Effect.Move(this.mainEl,{x: moveTo, y: 0,sync: true,
					           afterFinish: (function () {     
					  }).bind(this)}),
					  
				      new Effect.Move(this.nextSlide,{x: moveTo,y: 0,sync: true,
					  afterFinish: (function () { 
						    this.scrolling	= false; 
							this.activateControls(); 
							this.mainEl.remove();
							this.mainEl = this.nextSlide;
							this.scroller.setOpacity("1");  
							//$('carousel_loader').hide();
							this.options.preloader.hide();
					    }).bind(this)})		
			  ], { duration: 0.5,delay: 0.5});
	
		  return false;		 
		},
		
		runRequest: function(options) {	  
          this.requestRunning = true;
          this.scroller.setOpacity("0.5"); 
          new Ajax.Request(this.options.url, Object.extend({method: "GET"}, options)); 
          return this;
        },
        
		update: function(trigger,transport,json){
		  
		  this.id = json.id;
		  this.pr = json.pr;
		   
		   
		  if(this.firstLoaded) {		   
		        	        
		      var newSlidePos = 0;				
			  var trigerName = trigger.identify();				
			  if(trigerName == this.next.identify()) { newSlidePos = 855;  } else { newSlidePos = -855; }
			  var newElId = this.randomNumber(10,99999);							
			  this.nextSlide = Builder.node('div', { id: newElId ,className: this.options.inc,style: 'left:'+newSlidePos+'px; float:left;'});				
			  this.scroller.insert(this.nextSlide); 				
			  this.nextSlide.update(json.html);				
			  this.createWorkActions();
							
		  }else{
		     
		      this.mainEl.update(json.html);
		      this.mainEl.hide();
		      if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
		      	 this.scroller.setOpacity("1");
				 this.firstLoaded = true;
				 this.requestRunning = false;
				 this.options.preloader.hide();
				 this.mainEl.show();
				 this.createWorkActions();
		      }else{
			     var elName = this.nextSlide.identify();
			     this.preload = $$('#'+elName+' img');
			     this.NumImageLoaded = this.preload.size();
			       this.preload.each(function(_s){	 	  
				   _s.onload = function(){
				      this.NumImageLoaded--; 
				      if(this.NumImageLoaded == 0){
					      	 this.scroller.setOpacity("1");
					      	 this.firstLoaded = true;
						     this.requestRunning = false;
					         this.options.preloader.hide();
					         this.mainEl.show();
					         this.createWorkActions();
				       }
				   }.bind(this); 	    
		         }.bind(this)); 
		      }        
		   }  
		},
		
		randomNumber:function(min,max){
          return Math.floor(Math.random() * (max - min + 1)) + min;	
		},
		
		preLImages:function(trigger){
		  if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
		  	  this.requestRunning = false;
			  this.moveTo(trigger);
		  }else{
		    this.NumImageLoaded = 0;	
		    var elName = this.nextSlide.identify();
		    this.preload = $$('#'+elName+' img');	
		    this.NumImageLoaded = this.preload.size();		
			this.preload.each(function(_s){	 	  
				  _s.onload = function(){
				      this.NumImageLoaded--; 
				      if(this.NumImageLoaded == 0){
					     this.requestRunning = false;
				         this.moveTo(trigger);
				       }
				   }.bind(this); 	    
		      }.bind(this));
		  }
		}, 
		preLWork:function(){
		  if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
		  	$('loaderWork').hide();
		  	Effect.Appear('work_desc',{ duration: 0.5 });
		  }else{	
			  this.NumImageLoaded = 0;	
		      this.preload = $$('#work_conts img');	
			  this.NumImageLoaded = this.preload.size();		
			  this.preload.each(function(_s){	 	  
				   _s.onload = function(){
				      this.NumImageLoaded--; 
				      if(this.NumImageLoaded == 0){
				      	 $('loaderWork').hide();
				      	 
					     Effect.Appear('work_desc',{ duration: 0.5 });	
					     
				       }
				   }.bind(this); 	    
		      }.bind(this));
		  }
		
			
		},
        createWorkActions:function(){
         if($('works_surround')){   
            var elName = this.nextSlide.identify();

            $$('#'+elName+' a[id]').each(function(id){
       		   
       		    var link = $(id).readAttribute('id');	//nuorodos id
    	        var href = $(id).readAttribute('href');       
    	        id.observe('click',this.clickWork.bind(this));
    	           
        	}.bind(this));
         }
        	
        },
        
        clickWork:function(event){        	
        
        	var href = event.findElement('a');
        	$('loaderWork').show();
        	new Ajax.Updater('work_conts',href,{	    	              
	    	              onComplete:function(request){  
	    	              	this.preLWork();
	    	              }.bind(this),	
	    	requestHeaders:['X-Update', 'work_conts']});
        	event.stop();
        },
        
		deactivateControls: function () {
		  this.next.addClassName('button_disabled');
		  this.prev.addClassName('button_disabled');
		},

	    activateControls: function () {
		  this.next.removeClassName('button_disabled');
		  this.prev.removeClassName('button_disabled');
		}
	  
	});