var SimpleSlideShowDemo = new Class({
	options: {
		slides: [],
		startIndex: 0,
		wrap: true,
		onShow: Class.empty, //Mootools 1.2: $empty
		onRotate: Class.empty,
		onStop: Class.empty,
		onAutoPlay: Class.empty,
		onShowSlide: Class.empty,
		slideInterval: 2500,
		auto:false,
		transitionDuration: 700,
		splitter:"&middot;",
		prev:"",
		next:"",
		play:"&infin;",
		stop:"&phi;",
		bigNavigation : true,
		smallNavigation: true,
		browser:true,
		addBarInside:true,
		hideText:false
	},
	initialize: function(container,options){

	  if (container[0].getParent().getTag()=='a') {
     this.containerNest = container[0].getParent().getParent();
    } else {
      this.containerNest = container[0].getParent();
    }
    this.container = container;
		this.setOptions(options);
		this.slides = [];
		this.title = [];
		this.descr = [];
		this.effects = [];
		this.browserArr = [];
		this.menuIndex = [];
		this.playStatus = false;
		if (this.options.auto) this.playStatus = true;

    this.createStructure();
    // ************* get parameter from url ***************************** //
    this.index_img = parseInt(getQuerystring('index'));
    var list = $$('.current li.isfirst');//.getElements('li.isfirst');
    var myString = ''+list.getProperty('id');
    var arr = myString.split('_');
    this.firstId = arr[1];
    this.con_firstid= arr[1];
		this.addSlides(this.container);
		//if(this.slides.length) this.showSlide(this.options.startIndex);
		if(this.slides.length) this.showSlide(this.index_img);
		if (this.options.auto) {
      this.autoplay();
     // this.playStatus = true;
    }
    this.cycleTo(this.index_img);
	},
	createFx: function(){
		this.slideFx = new Fx.Elements(this.slides, {duration: this.options.transitionDuration});
		this.slides.each(function(slide){
			slide.setStyle('opacity',0);
		});
	},	
	createStructure: function() {

	  var oldClass = this.containerNest.getProperty("class");
    this.containerNest.setProperty('class', '');
	  this.containerNest.addClass('rgslideshow');
	  
	  this.test = new Element('div').addClass(oldClass).injectBefore(this.containerNest);
	  this.containerNest.injectInside(this.test);

	  if (!this.options.hideText) {
	  
      if (this.options.addBarInside) {
        this.nestAll = new Element('div').setProperty('class', 'rgsBar').injectAfter(this.containerNest); 
        this.description = new Element('div').setProperty('class', 'description').injectInside(this.nestAll);   
        //this.title = new Element('div').setProperty('class', 'imgtitle').injectInside(this.nestAll);
      }
      
      //	  this.nestAll = new Element('div').setProperty('class', 'rgslideshow').injectAfter(this.containerNest);
      
      //if (this.options.addBarInside) this.titleOp = new Element('div').setProperty('class', 'optitle').injectInside(this.nestAll);
    //  this.nestDescription = new Element('div').setProperty('class', 'descr').injectInside(this.nestAll);
    //  if (this.options.addBarInside) this.descriptionOp = new Element('div').setProperty('class', 'opdescr').injectInside(this.nestAll);
      
   
       if(this.options.browser) {
       
        this.navigation = new Element('div').setProperty('class', 'rgslideshownav').injectAfter(this.containerNest);
        this.innerNavigation = new Element('div').setProperty('class', 'rgsnav2').injectInside(this.navigation);
        
        if(this.options.smallNavigation) this.prevEl = new Element('span').setProperty('class', 'rgsprev').injectInside(this.innerNavigation).setHTML(this.options.prev).addEvent('click', this.cycleBack.bind(this));
        this.browser = new Element('div').setProperty('class', 'rgsbrowser').injectInside(this.innerNavigation);
        if(this.options.smallNavigation)  this.nextEl = new Element('span').setProperty('class', 'rgsnext').injectInside(this.innerNavigation).setHTML(this.options.next).addEvent('click', this.cycleForward.bind(this));

       } 
    }   
    
    this.createFx();

  },
	addSlides: function(slides){
    var i =1;
		slides.each(function(slide){
			this.slides.include($(slide));
      
			this.effects[this.slides.indexOf(slide)] = new Fx.Style(slide, 'opacity');
      slide.addClass('rgssimg');
        var title="&nbsp;";
        var descr="";
        if (slide.getProperty("title")) {
          split = slide.getProperty("title").split("|");
          if (split[0]) {
            title = split[0];
            slide.setProperty("title",title);
          }
          if (split[1]) descr = split[1];
        }

  			this.descr[this.slides.indexOf(slide)] = descr;      
  			//this.title[this.slides.indexOf(slide)] = title;
      if(this.options.browser) {  			
        this.browserEl = new Element('span').setProperties({'class': 'rgsbrowserEl', 'title': title}).setHTML(this.slides.indexOf(slide)+1).injectInside(this.browser);
    //    this.browserEl.setProperty('id',this.options.paginationSelector+i);
        /*
        this.m_li = new Element('li').injectInside(this.m_ul);
        this.m_a = new Element('a').setProperties({'href': "javascript:void(0);"}).setHTML(title).injectInside(this.m_li).addEvent('click', this.cycleTo.bind(this,this.slides.indexOf(slide)));
        */
        try {
          this.ele_class = $$('#item_'+this.firstId);
          var getHref = $('anch_'+this.firstId).getProperty('href');
          var getIndex = getHref.split("=");
          this.menuIndex[this.slides.indexOf(slide)] = parseInt(getIndex[1]);
          this.ele_class.addEvent('click', this.cycleTo.bind(this,parseInt(getIndex[1])));          
          $('anch_'+this.firstId).removeProperty('href');                      
          this.firstId = parseInt(this.firstId) + 1;
          this.ele_class.setStyle('cursor','pointer');       
        }catch(err){}
  			//this.browserEl.addEvent('click', this.cycleTo.bind(this,this.slides.indexOf(slide)));
  			this.browserArr[this.slides.indexOf(slide)] = this.ele_class;
  		}
			
			if (i!=1) {
      	//if(this.options.browser)		this.splitter = new Element('span').setProperty('class', 'rgssplitEl').setHTML(this.options.splitter).injectBefore(this.browserEl);
      	slide.setStyle('display','none');
      } else {

        if (!this.options.hideText && this.options.addBarInside) {
          /////var nestDiv = this.nestAll.getSize();
        } else {
          var nestDiv = this.containerNest.getSize();

        }
        
        var img = slide.getSize();

    		if(this.options.bigNavigation) {          
          this.bigPrev.setStyle('height',(slide.getProperty("height")-nestDiv.size.y)+'px');
          this.bigNext.setStyle('height',(slide.getProperty("height")-nestDiv.size.y)+'px');
          if(!this.options.addBarInside) {
	          this.bigPrev.setStyle('height',(slide.getProperty("height"))+'px');
	          this.bigNext.setStyle('height',(slide.getProperty("height"))+'px');

					}					 
        }
    		if(this.options.addBarInside) { 
    		  /////this.nestAll.setStyle('width',slide.getProperty("width")+'px');
    		}
    	  if(this.options.browser) {
          var width =  this.containerNest.getSize();
    		  this.navigation.setStyle('width',width.size.x-1+'px');
        }
      
      }
			i++;
		}, this);
		this.browser_slide_Nav = new Element('span').setProperties({'class': 'browser_slide'}).setHTML("01").injectInside(this.browser);
		this.browser_slide = new Element('span').setProperties({'class': 'browser_slide'}).setHTML("|").injectInside(this.browser);
		this.browser_slide = new Element('span').setProperties({'class': 'browser_slide'}).setHTML(numberFormat(this.slides.length,"0")).injectInside(this.browser);
		this.containerNest.addEvent('click', this.cycleForward.bind(this));
	  this.containerNest.addEvent('mouseover', this.transperency_brow_90.bind(this));
	  this.containerNest.addEvent('mouseout', this.transperency_brow_30.bind(this));	
    this.navigation.addEvent('mouseover', this.transperency_brow_90.bind(this));
    this.navigation.addEvent('mouseout', this.transperency_brow_30.bind(this));
    this.transperency_brow_30();
	},
	addSlide: function(slide){
		this.addSlides([slide]);
	},
	/* -------------- add transperacy browser Navigation ---------------- */
  transperency_brow_30: function(){
    this.navigation.style.opacity = ".30";
    this.navigation.style.filter = "alpha(opacity=30)";
  },
   transperency_brow_90: function(){
    this.navigation.style.opacity = ".90";
    this.navigation.style.filter = "alpha(opacity=90)";
  },
	cycleForward: function(){
		if($chk(this.now) && this.now < this.slides.length-1) this.showSlide(this.now+1);
		else if ((this.now) && this.options.wrap) this.showSlide(0);
		else if(!$defined(this.now)) this.showSlide(this.options.startIndex);
		this.browser_slide_Nav.innerHTML =numberFormat(this.now+1,"0");
	},
	cycleBack: function(){
		if(this.now > 0) this.showSlide(this.now-1);
		else if(this.options.wrap) this.showSlide(this.slides.length-1);
		this.browser_slide_Nav.innerHTML = numberFormat(this.now+1,"0");
	},
	cycleTo: function(i){
		if(this.playStatus) {
      this.togglePlay();
    }	
    this.showSlide(i);
    this.browser_slide_Nav.innerHTML = numberFormat(i+1,"0");
	},
	togglePlay: function() {
    if (this.playStatus) {
      this.stop();
      this.playStatus = false;
      this.play.innerHTML = this.options.play;
      this.play.setProperty('title',"Play");
    }
    else {
      this.autoplay();
      this.playStatus = true;
      this.play.innerHTML = this.options.stop;
      this.play.setProperty('title',"Stop");
    }
  },
	autoplay: function(){
		this.slideshowInt = this.rotate.periodical(this.options.slideInterval, this);
		this.fireEvent('onAutoPlay');
	},
	stop: function(){
		clearInterval(this.slideshowInt);
		this.fireEvent('onStop');
	},  	
	rotate: function(){
		this.cycleForward();
		this.fireEvent('onRotate');
	},	
	showSlide: function(iToShow){

		var now = this.now;		
		var currentSlide = this.slides[now];
		var slide = this.slides[iToShow];

		
		function fadeIn(s){
			s.setStyles({
				display:'block',
				visibility: 'visible',
				opacity: 0
			});
			this.effects[this.slides.indexOf(s)].start(1);
			this.fireEvent('onShow', [slide, iToShow]);
		};
    
    if (!this.options.hideText) {
      //if (this.options.addBarInside) this.description.innerHTML = this.title.innerHTML = "&nbsp;";
      if(this.options.browser) {
        this.browserArr.each(function(el) {
          el.removeClass("current");
        });
      }
    }

		if(slide) {    
			if($chk(now) && now != iToShow){
				this.effects[now].start(0).chain(function(){
					this.slides[now].setStyle('display', 'none');
					fadeIn.apply(this, [slide]);
          if (!this.options.hideText && this.options.addBarInside) {
            this.description.innerHTML  = this.descr[iToShow];
            //this.title.innerHTML  = this.title[iToShow];            
          }
          if(this.options.browser) {          
            for ( var i=0;i< this.menuIndex.length;i++) {
              if (this.now >= this.menuIndex[i]) {
                if (this.menuIndex.length == i + 1 ||
                    this.now < this.menuIndex[i+1]) {
                  this.browserArr[i].addClass("current");
                }
                else {
                  this.browserArr[i].removeClass("current");
                }  
              }
            }
          }
				}.bind(this));
			} else {
        fadeIn.apply(this, [slide]);
          if (!this.options.hideText && this.options.addBarInside) {
  		      this.description.innerHTML  = this.descr[iToShow];
            //this.title.innerHTML  = this.title[iToShow];
            //if(this.options.browser) this.browserArr[1].addClass("current");
            if(this.options.browser) {        
            for ( var i=0;i< this.menuIndex.length;i++) {
              if (this.index_img >= this.menuIndex[i]) {
                if (this.menuIndex.length == i + 1 ||
                    this.index_img < this.menuIndex[i+1]) {
                  this.browserArr[i].addClass("current");
                }
                else {
                  this.browserArr[i].removeClass("current");
                }  
              }
            }
          }
        }
      }
			this.now = iToShow;
		}
	}
});


// This function formats numbers by adding its prefix
function numberFormat(nStr,prefix){
    var prefix = prefix || '';
    nStr += '';
    var con ='';
    if(nStr.length < 2){con=prefix + nStr} else{con= nStr;}
    return con;
}
function getQuerystring(key, default_)
{
  if (default_==null) default_="0";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}
SimpleSlideShowDemo.implement(new Options, new Events);


