var nxcGallery= new Class({
	options:{
		'scrollNum': '10px',
		'scrollPiscture': false,
		'showHideTime':300,
		'showHideImageTime':300,
		'resizeTime':500,
		'slideshowTime':3000,
		'loopGallery': false
	},
	slideshowPeriodical: false,
	gallery:		'',
	lightBox: 	'',
	galleryShowed: false,
	showHideLightbox: false,
	imageConteinerResize: false,
	showHideImage:false,
	scrollImages:false,
	clonedElement:false,
	numOfElem: 0,
	currentElement:	0,
	galletyWidth: 0,
	tempImagePos:false,
	showGallery:	function(item){
		$clear(this.slideshowPeriodical);
		this.galleryShowed=true;
		this.removeTempImage('none');
		this.setGallerySize(item);
    	this.showHideLightbox.cancel();
    	this.showHideLightbox.start({
      		'opacity': [0.2, 0.8]
    	});
    	document.id( document.body ).getElement( this.lightBox+ ' div.lightbox-box').setStyle('display', 'block');
    	document.id( document.body ).getElement( this.lightBox+' div#gallery-images-lightbox-back' ).setProperty('height',document.id( document.body ).getSize().y);
    	document.id( document.body ).getElement( this.lightBox+' div.lightbox-box' ).setStyle('top',document.id( document.body ).getScroll().y + 10);
    	document.id( document.body ).getElement( this.lightBox).setStyle('display', 'block');
    	this.showImage(item);
	},
	hideGallery:	function(){
		$clear(this.slideshowPeriodical);
		this.slideshowPeriodical=this.scrollRight.periodical(this.options.slideshowTime,this);
		this.galleryShowed=false;
    	this.showHideLightbox.cancel();
		document.id( document.body ).getElement(this.lightBox+' #lightbox-image-conteiner img.light-box-image').setProperty('src','');
    	this.imageConteinerResize.set({
      		'width': '400px',
      		'height': '400px'
    	});
		document.id( document.body ).getElement( this.lightBox+ ' div.lightbox-box').setStyle('display', 'none');
		this.showHideLightbox.cancel();
		this.showHideLightbox.start({
      		'opacity': 0
    	}).chain(function(){
            document.id( document.body ).getElement( this.lightBox).setStyle('display', 'none');
			if( this.options.loopGallery) this.galleryLooping(document.id( document.body ).getElement(this.gallery+" div.gallery-images-box").getStyle('left').toInt());
        }.bind(this));
	},
	showImage: function(el){ 
	/*	if(this.currentElement<=1) document.id( document.body ).getElement(this.lightBox+' a#lightbox-nav-btnPrev').setStyle('display','none');
		  else */  document.id( document.body ).getElement(this.lightBox+' a#lightbox-nav-btnPrev').setStyle('display','block');
	/*	if(this.currentElement>=this.numOfElem) document.id( document.body ).getElement(this.lightBox+' a#lightbox-nav-btnNext').setStyle('display','none');
		  else */  document.id( document.body ).getElement(this.lightBox+' a#lightbox-nav-btnNext').setStyle('display','block');
	    document.id( document.body ).getElement(this.lightBox+' #lightbox-image-conteiner img.light-box-image').setProperty('src',el.getProperty('href'));
    	this.showHideImage.set({ 'display':'none' });
    	document.id( document.body ).getElement( this.lightBox+' #lightbox-image-details-currentNumber' ).set('text', 'Image '+this.currentElement+' of '+this.numOfElem);
    	document.id( document.body ).getElement( this.lightBox+' div#lightbox-container-image-data-box' ).setStyle('display','none');
    	document.id( document.body ).getElement( this.lightBox+' div#lightbox-navigation' ).setStyle('display','none');
    	document.id( document.body ).getElement( this.lightBox+' div#lightbox-loading' ).setStyle('display','block');
  	},
	imageResize: function(){
    	this.imageConteinerResize.cancel();
    	this.showHideImage.cancel();
    	this.showHideImage.set({
    	                'display': 'inline',
    	                'opacity': '0'
    	                });
    	image=document.id( document.body ).getElement(this.lightBox+' #lightbox-image-conteiner img.light-box-image');
    	imageSize=image.getSize();
    	this.imageConteinerResize.start({
    		'width': imageSize.x,
      		'height': imageSize.y
    	}).chain(function(){
        	document.id( document.body ).getElement( this.lightBox+' div#lightbox-loading' ).setStyle('display','none');
        	document.id( document.body ).getElement( this.lightBox+' div#lightbox-container-image-data-box' ).setStyles({
                                                                                                                      'width' : imageSize.x,
                                                                                                                      'display' : 'block'
                                                                                                                      });
          this.showHideImage.set({'display': 'inline'});
        	this.showHideImage.start({
        	      'opacity': [0.2,1]
         	}).chain(function(){
            	      document.id( document.body ).getElement( this.lightBox+' div#lightbox-navigation' ).setStyle('display','block');
         	}.bind(this));
    	}.bind(this));
	},
	previousImage: function(){
		if(this.galleryShowed){
			if (this.currentElement==1) {this.currentElement=16}
			this.currentElement--;
			this.showImage(document.id( document.body ).getElement(this.gallery+' div.gallery-images-box div.gallery-image:nth-child('+this.currentElement+') a'));
		}
	},
	nextImage: function(){
		if(this.galleryShowed){
			if (this.currentElement==15) {this.currentElement=0}
			this.currentElement++;
			this.showImage(document.id( document.body ).getElement(this.gallery+' div.gallery-images-box div.gallery-image:nth-child('+this.currentElement+') a'));
		}
	},
	scrollLeft: function(){
		$clear(this.slideshowPeriodical);
		if(this.options.loopGallery && this.clonedElement ){
			document.id( document.body ).getElements(this.gallery+" div.previous-image a").setStyle('display','none');
			document.id( document.body ).getElements(this.gallery+" div.next-image a").setStyle('display','none');
			var direction='none';
			if( this.clonedElement.getElement('a').getProperty('href')==document.id( document.body ).getElement(this.gallery+" div.gallery-image:last-child a").getProperty('href')){
				 this.clonedElement=document.id( document.body ).getElement(this.gallery+" div.gallery-image:last-child");
				direction='left';
			}
			this.removeTempImage(direction);
		}
		var moveTo=document.id( document.body ).getElement(this.gallery+" div.gallery-images-box").getStyle('left').toInt() + this.options.scrollNum.toInt();
		this.scrollTo(moveTo);
	},
	scrollRight: function(){
		$clear(this.slideshowPeriodical);
		if(this.options.loopGallery && this.clonedElement ){
			document.id( document.body ).getElements(this.gallery+" div.previous-image a").setStyle('display','none');
			document.id( document.body ).getElements(this.gallery+" div.next-image a").setStyle('display','none');
			var direction='none';
			if( this.clonedElement.getElement('a').getProperty('href')==document.id( document.body ).getElement(this.gallery+" div.gallery-image:first-child a").getProperty('href')){
				 this.clonedElement=document.id( document.body ).getElement(this.gallery+" div.gallery-image:first-child");
				direction='right';
			}
			this.removeTempImage(direction);
		}
		var moveTo=document.id( document.body ).getElement(this.gallery+" div.gallery-images-box").getStyle('left').toInt() - this.options.scrollNum.toInt();
		this.scrollTo(moveTo);
	},
	scrollTo: function(moveTo){
		if( !this.options.loopGallery )
			moveTo=this.checkPosition(moveTo);
		//else moveTo=this.checkPosition(moveTo);
		//this.scrollImages.cancel();
		this.scrollImages.start({'left' : moveTo}).chain(function(){
			if(this.options.loopGallery) this.galleryLooping(moveTo);
			document.id( document.body ).getElements(this.gallery+" div.previous-image a").setStyle('display','block');
			document.id( document.body ).getElements(this.gallery+" div.next-image a").setStyle('display','block');
			$clear(this.slideshowPeriodical);
			this.slideshowPeriodical=this.scrollRight.periodical(this.options.slideshowTime,this);
        }.bind(this));
	},
	galleryLooping: function(moveTo){
		if(this.clonedElement===false){
			galleryShowBox=document.id( document.body ).getElement(this.gallery+" div.gallery-images");
			galleryBox=galleryShowBox.getElement('div.gallery-images-box');
			galleryShowBoxSize=galleryShowBox.getSize().x - galleryShowBox.getStyle('padding-left').toInt() -  galleryShowBox.getStyle('padding-right').toInt();
			if(moveTo>=0){
				this.clonedElement=galleryShowBox.getElement('div.gallery-image:last-child').clone().inject(galleryBox,'top'); 
				this.scrollImages.set({
					'width': (this.galleryWidth+this.options.scrollNum),
					'left': (galleryBox.getStyle('left').toInt()-this.options.scrollNum)
				});
				this.setImageEvents(this.clonedElement.getElement('a'));
				this.tempImagePos='left';
			}
			if( moveTo<=(galleryShowBoxSize - (this.galleryWidth))){
				this.clonedElement=galleryShowBox.getElement('div.gallery-image:first-child').clone().inject(galleryBox,'bottom'); 
				galleryBox.setStyles({
					'width': (this.galleryWidth+this.options.scrollNum)
				});
				this.setImageEvents(this.clonedElement.getElement('a'));
				this.tempImagePos='right';
			}
		}
	},
	removeTempImage: function(direction){
		if(this.clonedElement){
			this.clonedElement.dispose();
			if( ((direction+this.tempImagePos)=='rightleft')  || ((direction+this.tempImagePos)=='rightright') || ((direction+this.tempImagePos)=='noneleft')){
				var temp=(document.id( document.body ).getElement(this.gallery+" div.gallery-images-box").getStyle('left').toInt()+this.options.scrollNum);
				this.scrollImages.set({'left': temp});
			}
			this.scrollImages.set({'width': this.galleryWidth});
			this.clonedElement=false;
		}
	},
	checkPosition: function(moveTo){
		var displayT='none';
		var passiveCol='#FFFFFF';
		if(this.options.loopGallery) {
			displayT='block';
			passiveCol='#ECECEC';
		}
		galleryShowBox=document.id( document.body ).getElement(this.gallery+" div.gallery-images");
		galleryShowBoxSize=galleryShowBox.getSize().x - galleryShowBox.getStyle('padding-left').toInt() -  galleryShowBox.getStyle('padding-right').toInt();
		if(moveTo>=0){
			moveTo=0;
			document.id( document.body ).getElements(this.gallery+" div.previous-image a").setStyle('display',displayT);
			document.id( document.body ).getElements(this.gallery+" div.previous-image").setStyle('background-color',passiveCol);
		}
		else{
			document.id( document.body ).getElements(this.gallery+" div.previous-image a").setStyle('display',displayT);
			document.id( document.body ).getElements(this.gallery+" div.previous-image").setStyle('background-color','#ECECEC');
		}
		if( galleryShowBoxSize>this.galleryWidth || (moveTo<=(galleryShowBoxSize - this.galleryWidth))){
			moveTo=galleryShowBoxSize-this.galleryWidth;
			document.id( document.body ).getElements(this.gallery+" div.next-image a").setStyle('display',displayT);
			document.id( document.body ).getElements(this.gallery+" div.next-image").setStyle('background-color',passiveCol);
		}
		else{
			document.id( document.body ).getElements(this.gallery+" div.next-image a").setStyle('display','block');
			document.id( document.body ).getElements(this.gallery+" div.next-image").setStyle('background-color','#ECECEC');
		}
		return moveTo;
	},
	setImageEvents: function(el){
		el.addEvent("click",function(e){ e.stop(); });
		el.addEvent("click",this.showGallery.bind(this,[el]));
		el.addEvent("mouseover",function(){ $clear(this.slideshowPeriodical); }.bind(this));
		el.addEvent("mouseout",function(){ 
																		if(!this.galleryShowed){
																							$clear(this.slideshowPeriodical);
																							this.slideshowPeriodical=this.scrollRight.periodical(this.options.slideshowTime,this);} }.bind(this));
	},
	setGallerySize: function(el){
		galleryImages=document.id( document.body ).getElements(this.gallery+" div.gallery-image a");
		if(el){
			this.currentElement=galleryImages.indexOf(el)+1;
		}
		imageExample=document.id( document.body ).getElement( this.gallery + ' div.gallery-images-box div.gallery-image:nth-child(first)');
		this.numOfElem=galleryImages.length;
		this.galleryWidth=(imageExample.getSize().x + imageExample.getStyle('margin-left').toInt()+imageExample.getStyle('margin-right').toInt())*this.numOfElem;
		if( this.options.scrollPiscture )this.options.scrollNum=imageExample.getSize().x + imageExample.getStyle('margin-left').toInt()+imageExample.getStyle('margin-right').toInt();
		return galleryImages;
	},
	initialize: function(gallery, lightbox, options){
//setting class variables
		this.lightBox=lightbox;
		this.gallery=gallery;
		if(options.scrollNum) this.options.scrollNum=options.scrollNum;
		if(options.showHideTime) this.options.showHideTime=options.showHideTime;
		if(options.showHideImageTime) this.options.showHideTime=options.showHideImageTime;
		if(options.resizeTime) this.options.resizeTime=options.resizeTime;
		if(options.scrollPiscture) this.options.scrollPiscture=options.scrollPiscture;
		if(options.slideshowTime) this.options.slideshowTime=options.slideshowTime;
		galleryImages=this.setGallerySize();

//getting elements
		var lightboxback=document.id( document.body ).getElement( this.lightBox+' div#gallery-images-lightbox-back' );
		var lightbox=document.id( document.body ).getElement( this.lightBox+' div.lightbox-box' );
		var lightBoxNext=lightbox.getElement('a#lightbox-nav-btnNext' );
		var lightBoxPrev=lightbox.getElement( 'a#lightbox-nav-btnPrev' );
		var lightboxImage=lightbox.getElement( 'div.#lightbox-image-conteiner img.light-box-image' );
		var lightboxClose=lightbox.getElement( 'a#lightbox-secNav-btnClose' );
		var scrollLeft=document.id( document.body ).getElement(gallery+" div.previous-image a");
		var scrollRight=document.id( document.body ).getElement(gallery+" div.next-image a");
		
		
//morphs for showing/hiding lightbox background, lightbox image, resizing lightbox
		this.imageConteinerResize= new Fx.Morph('lightbox-image-conteiner', {duration: this.options.resizeTime, transition: Fx.Transitions.Quad.easeIn});
    	this.showHideLightbox = new Fx.Morph(lightboxback, {duration: this.options.showHideTime, transition: Fx.Transitions.Quad.easeIn});
    	this.showHideImage = new Fx.Morph(lightboxImage, {duration: this.options.showHideImageTime, transition: Fx.Transitions.Quad.easeIn});
		this.scrollImages = new Fx.Morph(document.id( document.body ).getElement(this.gallery+" div.gallery-images-box"), {duration: 200, link: 'ignore', transition: Fx.Transitions.Quad.easeIn});
		
//  lightbox events
		lightboxback.addEvent("click",function(e){ e.stop(); });
		lightboxback.addEvent("click",this.hideGallery.bind(this));
		lightboxClose.addEvent("click",function(e){ e.stop(); });
		lightboxClose.addEvent("click",this.hideGallery.bind(this));
		lightbox.addEvent("click",function(e){ e.stop(); });
		lightbox.addEvent("click",this.hideGallery.bind(this));
		lightBoxPrev.addEvent("click",function(e){ e.stop(); });
		lightBoxPrev.addEvent("click",this.previousImage.bind(this));
		lightBoxNext.addEvent("click",function(e){ e.stop(); });
		lightBoxNext.addEvent("click",this.nextImage.bind(this));
		lightboxImage.addEvent("load",this.imageResize.bind(this));
		lightboxback.setStyle('height',document.id( document.body ).getScrollSize().y);

//  gallery events
		galleryImages.each(function(item, index){
			this.setImageEvents(item);
		},this);
		document.id( document.body ).getElement(this.gallery+" div.gallery-images-box").setStyle('width',this.galleryWidth);
		scrollLeft.addEvent("click",function(e){ e.stop(); });
		scrollRight.addEvent("click",function(e){ e.stop(); });
		scrollLeft.addEvent("click",this.scrollLeft.bind(this));
		scrollRight.addEvent("click",this.scrollRight.bind(this));
		galleryShowBox=document.id( document.body ).getElement(this.gallery+" div.gallery-images");
		galleryShowBoxSize=galleryShowBox.getSize().x - galleryShowBox.getStyle('padding-left').toInt() -  galleryShowBox.getStyle('padding-right').toInt();
		if(options.loopGallery && (this.galleryWidth>galleryShowBoxSize)) this.options.loopGallery=options.loopGallery;
		if( this.options.loopGallery) this.galleryLooping(document.id( document.body ).getElement(this.gallery+" div.gallery-images-box").getStyle('left').toInt());
			else this.checkPosition(document.id( document.body ).getElement(this.gallery+" div.gallery-images-box").getStyle('left').toInt());
		$clear(this.slideshowPeriodical);
		this.slideshowPeriodical=this.scrollRight.periodical(this.options.slideshowTime,this);
		//$clear(this.slideshowPeriodical);
	}
});


