// SlideShow with Navigation  base on Prototype Library
//var sw= new SMC.SlideShow( 'main-banner',{ animSpeed: 3, speed: 8000});
// sw.install(sw);
if (typeof SMC == 'undefined') {
 SMC = {};
}
SMC.SlideShow = Class.create();
SMC.SlideShow.prototype = {
  initialize:  function (container, options){
      this.container       = container;
	  this.start = true;
      this.setOptions(options);
	  this.init();
	  if(!container) return;
},
   setOptions: function(options) {
      this.options = {
			animSpeed :5,
			speed: 1000,
			pattern: ' .slide',
			autoplay: true,
			navBar: true
			
      }
      Object.extend(this.options, options || {});
}, 
   play: function() {
   		 if (this.options.autoplay) { 
         		if (!this.start)  this.next(); else this.start =false;
				this.clear();   		       
		 		slides = this.getSlides();
				 var _this = this;   
		 		setTimeout(function(){_this.play()}, _this.options.speed);		
		 		Effect.run( slides[slides.length-1],false, this.options.animSpeed, 100); 
		  	 }
},
   back: function() {
   		var container = $(this.container);
   		slides = this.getSlides();
		nodeRemoved = container.removeChild(slides[0]); 
		container.appendChild(nodeRemoved); 		 
},
	buttomsCtrl: function (e) {
  		if (e!= 'undefined' && e!=false) {	  Event.stop(e); ele= Event.element(e);   }	
		    this.clear();
		if (ele.id=='sw-back' || ele.id=='sw-next') {		
		    this.start = true;
			this.options.autoplay= false;
			this.swapClassNames('sw-play','pause','play');
			}  			
		if (ele.id=='sw-back')
			{
			this.back();	
			}
		else if	(ele.id=='sw-next')
			{
			this.next(); 
			}
		else {
		      if (this.options.autoplay) {
    		        this.options.autoplay= false; 
					this.swapClassNames('sw-play','pause','play');
			  } else {
			        this.start = true;
					this.options.autoplay= true;		
					this.play();	
					this.swapClassNames('sw-play','play','pause');
					}

			 }				
	
},
   swapClassNames: function (id, oldclass, newclass) {
			Element.removeClassName(id, oldclass); 
			Element.addClassName(id, newclass); 
},
   next: function() {
   		 var container = $(this.container);		
   		 slides = this.getSlides();
		 nodeRemoved = container.removeChild(slides[slides.length-1]); 
		 container.insertBefore(nodeRemoved,slides[0]); 
},
   clear: function () {
      		slides = this.getSlides();
			slides.each( function (item) {
				item.style.display = '';			
			 });
},

   init: function() {
         this.container.oncontextmenu = function() {return false;}  /* Avoid right click */ 
		 slides = this.getSlides();
		 slides.each( function (item) {
				item.oncontextmenu = function() {return false;}					
			 });

			_this=this;
		 	if (slides.length > 1 ) setTimeout(function(){_this.play()}, _this.options.speed);
 

}, 
     getSlides: function (){
		 return $$('#'+ this.container + this.options.pattern);
},
     install: function (myObject)  {
	 var htmlnav = '<div class="slide-nav-bar"><a  id="sw-back"  class="back" href="#"></a> <a id="sw-play" class="pause"  href="#"></a> <a id="sw-next" class="next"  href="#"></a></div>';
	  new Insertion.Top (this.container, htmlnav);
	   $$('#'+ this.container + ' .slide-nav-bar a').each( function (item) {
		 item.onclick= myObject.buttomsCtrl.bindAsEventListener(myObject);
	 	});
	 
	 }	  
}
	
