var simpleSlide = new Class({

  initialize : function(params){
        this.id = params.id;
        this.parent = $(this.id);
        this.url = params.url;
        this.noPages = 0;
        this.totalHeight = 0;
        this.pages=[];
        this.currentPage = 0;
        this.duration = params.duration || 1000;
        this.maxPages = params.maxPages || -1;
		if ((navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) && (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1))
		{

		}
		else {
	        this.setup();
		}
        
        //this.reload();    

    },
    
    setup : function() {
        
        
        

        
        this.container = new Element("div" , {'class': 'carrouselContainer'});
        
        
        
            
        	size = this.parent.getSize();
            buttonLocation = (size.y - 32) / 2;
        
            if (this.parent.get('html') != '' || this.parent.get('text') != '') {
                
        
                this.addItem(this.parent.get('html'), this.parent.get('text'));
                
                //this.container.inject(this.parent);
                        
                this.parent.set('html','');    
                this.parent.set('text','');    
                
                
            }
	//		this.loading = new Element("div", {'class': 'loader'} );
	//		this.loading.setStyle('height', size.y);
	 
	
	//		this.loading.inject(this.parent);
			                        
            this.nextButton = new Element("div", {'class':'btnright'});
            this.nextButton.set('text', '');
			this.nextButton.setStyle('margin-top', buttonLocation);
			
					
            this.nextButton.inject(this.parent);    
            this.nextButton.addEvent('click', this.next.bind(this));
    
            this.prevButton = new Element("div", {'class':'btnleft'});
            this.prevButton.set('text', '');
			this.prevButton.setStyle('margin-top', buttonLocation);
			
            this.prevButton.inject(this.parent);    
            this.prevButton.addEvent('click', this.prev.bind(this));
        
		this.hideLoader();
            
            this.container.inject(this.parent);
        
                 
    }, 
    
    hideLoader : function() {
  // 	this.loading.hide();
  //  	this.prevButton.show();
   // 	this.nextButton.show();
    },
    
    showLoader : function() {
   // 	this.loading.show();
   // 	this.prevButton.hide();
   // 	this.nextButton.hide();
    },
    
    addItem : function (html, text, adopt) {
        var elm = new Element("div", {'class':'carrouselItem'});

        elm.set('item', this.noPages);

		if (html == '' && text == '' || adopt && html.length == 0 ) {
			this.maxPages = this.noPages;
			this.disableNext();
			return false;
		}
        this.noPages ++;
        
        if (adopt ) {
         elm.adopt(html );
        }
        else if (html) {
            elm.set('html', html );
        } else {
            elm.set('text', text);
        }
       
				if (this.noPages > 1 ) {        
	     		 var totalHeight = 0;

		       for(i=0;i<this.noPages - 1;i++) {
		         		size = this.pages[i].getSize();
		            totalHeight += size.y;
		         }
		        
		       elm.setStyle('top', -1 * totalHeight);
		       elm.setStyle('left',   "101%" );
        } else {
		       elm.setStyle('top' , 0);
		       elm.setStyle('left', 0);
        }
      
        
        this.pages.push(elm);        
        elm.inject(this.container);
        
        if (totalHeight > 0) {
         	this.container.setStyle('height', size.y);
        }
		return true;
    }
    ,
    
    disableNext : function() { 
    	this.nextButton.setStyle('background-position', '-96px 0');
    }
    ,
    completePrev : function(dummy) {
    	this.pages[this.currentPage  ].setStyle('left','1000%');
    	this.currentPage -- ;
    	if ( this.currentPage == 0 ){ 
    		this.prevButton.setStyle('background-position', '-96px 0');
    	}
    	this.nextButton.setStyle('background-position', '0');
    	this.pages[this.currentPage  ].setStyle('left','0');
    },
    
    prev : function () {
    
		if ( this.currentPage == 0 ) return;
        
        var fx = new Fx.Elements([this.pages[this.currentPage - 1],this.pages[this.currentPage  ]],{
        			duration: 1000,
        			onComplete : this.completePrev.bind(this)
        			});
       	size=this.pages[this.currentPage].getSize();
				fx.start({
             "0": { "left": [-1 * size.x, 0]},
             "1": { "left": [0,  size.x]}
         });
       
        

    },
    
    completeNext : function(dummy) {
    	this.pages[this.currentPage].setStyle('left',"-1000%");
    	this.currentPage ++ ;
    	if (this.currentPage > 0 ){
    	 
    		this.prevButton.setStyle('background-position', '0');
    	
    	}
    	if ( this.currentPage == this.maxPages  - 1 ) {
    		this.disableNext();
    	}
    	this.pages[this.currentPage].setStyle('left',"0");
   },
    
    
    
    next : function () {
        //alert('next');
        if ( this.currentPage == this.maxPages - 1) {
        	return;
        }
        else if ( this.currentPage + 1>= this.noPages ) {
     				this.getData( this.currentPage + 1 );    
        }
        else {
     			var fx = new Fx.Elements([this.pages[this.currentPage],this.pages[this.currentPage  + 1 ]], {
     			duration : 1000,
     			onComplete : this.completeNext.bind(this)
     			
     			});
         size=this.pages[this.currentPage].getSize();
					
         fx.start({
            "0": { "left": [0, -1 * size.x]},
            "1": { "left": [size.x,0]}
        	});
         
        }
        
    },
    
    getData: function(index) {
				url = this.url + "&page=" + index
				this.showLoader();
				var req = new Request.HTML({
							url:url, 
							onSuccess: this.process.bind(this), 
							onFailure: this.processfail.bind(this)
					});
				req.send();
		},
		
	processfail : function(responseText) {
			this.hideLoader();
			alert('request failed');
	},
		
	process : function(responseText) {
				this.hideLoader();
				if (this.addItem(responseText, '' , true) == true) {
					this.next();
				}
				
	}
    
});
