addBehaviors = new Class
({

initialize: function(thumbContainer,scrollBoundry,scrollSpeed,thumbTable,fade,main,
largeImgDiv,host,galleryFolder,imageFolder,imgFileNameArray,lightbox,desContainer,
lightboxWidth,scrollBack,scrollHandle,darkDiv,whiteDiv,closeImage,lboxLargeImgDiv,preload,
upArrow,downArrow,leftArrow,rightArrow,showUpDown,showLeftRight,thumbScrollBack,thumbScrollHandle,
thumbVScrollBack,thumbVScrollHandle,lboxObject,magnify,scrollLeftArrow,
scrollRightArrow,scrollUpArrow,scrollDownArrow,desUpArrow,desDownArrow,numPics,
lboxPreload,lboxTScrollHandle,lboxTVScrollHandle,lboxDesContainer,allowComments,
allowRating,idArray,guest,ratingsContainer,ratingsInfoDiv,ratingForm,
ratingFormImgId,ratingFormImgRating,ratingsMessageContainer,ratedContainer,numRatingsSpan,
commentsAmountDiv,commentsDataContainer,commentsContainer,commentsForm,commentsTextarea,commentsImgId,
commentsGif,lboxRatingsInfoDiv,lboxRatingForm,lboxRatingFormImgId,lboxRatingFormImgRating,
lboxRatingsMessageContainer,lboxNumRatingsSpan,lboxCommentsForm,lboxCommentsAmountDiv,lboxCommentsContainer,
lboxCommentsForm,lboxCommentsTextarea,lboxCommentsImgId,lboxCommentsGif,lboxAllowComments,lboxAllowRating)
{
	
	
	
	
	this.thumbContainer = thumbContainer;
	this.scrollBoundry = scrollBoundry;
	this.scrollSpeed = scrollSpeed;
	this.thumbTable = thumbTable;
	this.fade = fade;
	this.main = main;
	this.largeImgDiv = largeImgDiv;
	this.host = host;
	this.galleryFolder = galleryFolder;
	this.imageFolder = imageFolder;
	this.imgFileNameArray = imgFileNameArray;
	this.lightbox = lightbox;
	this.desContainer = desContainer;
	this.lightboxWidth = lightboxWidth;
	this.desScrollBack = scrollBack;
	this.desScrollHandle = scrollHandle;
	this.darkDiv = darkDiv;
	this.whiteDiv = whiteDiv;
	this.closeImage = closeImage;
	this.lboxLargeImgDiv = lboxLargeImgDiv;
	this.preload = preload;
	this.upArrow = upArrow;
	this.downArrow = downArrow;
	this.leftArrow = leftArrow;
	this.rightArrow = rightArrow;
	this.showUpDown = showUpDown;
	this.showLeftRight = showLeftRight;
	this.thumbScrollBack = thumbScrollBack;
	this.thumbScrollHandle = thumbScrollHandle;
	this.thumbVScrollBack = thumbVScrollBack;
	this.thumbVScrollHandle = thumbVScrollHandle;
	this.lboxObject = lboxObject;
	this.magnify = magnify;
	this.scrollLeftArrow = scrollLeftArrow;
	this.scrollRightArrow = scrollRightArrow;
	this.scrollUpArrow = scrollUpArrow;
	this.scrollDownArrow = scrollDownArrow;
	this.desUpArrow = desUpArrow;
	this.desDownArrow = desDownArrow;
	this.numPics = numPics;
	this.lboxPreload = lboxPreload;
	this.lboxTScrollHandle = lboxTScrollHandle;
	this.lboxTVScrollHandle = lboxTVScrollHandle;
	this.lboxDesContainer = lboxDesContainer;
	this.allowComments = allowComments;
	this.allowRating = allowRating;
	this.idArray = idArray;
	this.guest = guest;
	
	this.ratingsContainer = ratingsContainer;
	this.ratingsInfoDiv = ratingsInfoDiv;
	this.ratingForm = ratingForm;
	this.ratingFormImgId = ratingFormImgId;
	this.ratingFormImgRating = ratingFormImgRating;
	this.ratingsMessageContainer = ratingsMessageContainer;
	this.ratedContainer = ratedContainer;
	this.numRatingsSpan = numRatingsSpan;
	
	this.commentsAmountDiv = commentsAmountDiv;
	this.commentsDataContainer = commentsDataContainer;
	this.commentsContainer = commentsContainer;
	this.commentsForm = commentsForm;
	this.commentsTextarea = commentsTextarea;
	this.commentsImgId = commentsImgId;
	this.commentsGif = commentsGif;
	
	
	
	this.lboxRatingsInfoDiv = lboxRatingsInfoDiv;
	this.lboxRatingForm = lboxRatingForm;
	this.lboxRatingFormImgId = lboxRatingFormImgId;
	this.lboxRatingFormImgRating = lboxRatingFormImgRating;
	this.lboxRatingsMessageContainer = lboxRatingsMessageContainer;
	this.lboxNumRatingsSpan = lboxNumRatingsSpan;
	
	this.lboxCommentsForm = lboxCommentsForm;
	this.lboxCommentsAmountDiv = lboxCommentsAmountDiv;
	this.lboxCommentsContainer = lboxCommentsContainer;
	this.lboxCommentsForm = lboxCommentsForm;
	this.lboxCommentsTextarea = lboxCommentsTextarea;
	this.lboxCommentsImgId = lboxCommentsImgId;
	this.lboxCommentsGif = lboxCommentsGif;
	
	this.lboxAllowComments = lboxAllowComments;
	this.lboxAllowRating = lboxAllowRating;

	
	
	this.thumbScroller = new iScroller(this.thumbContainer, {area: this.scrollBoundry, velocity: this.scrollSpeed,
	thumbTable: this.thumbTable, scrollBack: this.thumbScrollBack, scrollHandle: this.thumbScrollHandle,
	scrollBack: this.thumbScrollBack, scrollHandle: this.thumbScrollHandle, scrollVBack: this.thumbVScrollBack, 
	scrollVHandle: this.thumbVScrollHandle,
		onChange : function(x,y)
		{
			this.scrollBackSizeArray = $(this.options.scrollBack).getSize();
			this.scrollBackWidth = this.scrollBackSizeArray.size.x;
			
			this.scrollBackVSizeArray = $(this.options.scrollVBack).getSize();
			this.scrollBackHeight = this.scrollBackVSizeArray.size.y
			
			this.thumbTableSizeArray = $(this.options.thumbTable).getSize();
			this.thumbTableWidth = this.thumbTableSizeArray.size.x;
			this.thumbTableHeight = this.thumbTableSizeArray.size.y;
			
			
			this.handlescrollX = (x/(this.thumbTableWidth - this.scrollBackWidth) ) * this.scrollBackWidth * 1.25;
			if(this.handlescrollX > (this.scrollBackWidth -14))
			{
				this.handlescrollX = (this.scrollBackWidth -14);
			}
			if(this.handlescrollX < 0)
			{
				this.handlescrollX = 0;
			}
			
			this.handlescrollY = (y/(this.thumbTableHeight - this.scrollBackHeight) ) * this.scrollBackHeight * 1.25;
			if(this.handlescrollY > (this.scrollBackHeight -14))
			{
				this.handlescrollY = (this.scrollBackHeight -14);
			}
			if(this.handlescrollY < 0)
			{
				this.handlescrollY = 0;
			}
			
			this.element.scrollTo(x,y);
			$(this.options.scrollHandle).setStyle('left',this.handlescrollX);
			$(this.options.scrollVHandle).setStyle('top',this.handlescrollY);
	    } 
	});
	$(this.thumbTable).addEvent('mouseenter', this.thumbScroller.start.bind(this.thumbScroller));
	$(this.thumbTable).addEvent('mouseleave', this.thumbScroller.stop.bind(this.thumbScroller));
	
	this.linksArray = $(this.thumbTable).getElements('a');
	
	if(this.preload == 1 && this.main == 1)
	{
		this.counter = 0;
		this.preloaderClass = new preloaderClass(this.host,this.galleryFolder,'large',this.imgFileNameArray,this.numPics);
	}

	
	if(this.showUpDown == 1)
	{
		this.upArrowScrollClass = new addArrowBehaviors(this.upArrow, -150,'vertical',this.thumbContainer,this.thumbVScrollBack,this.thumbVScrollHandle);
		this.downArrowScrollClass = new addArrowBehaviors(this.downArrow, 150,'vertical',this.thumbContainer,this.thumbVScrollBack,this.thumbVScrollHandle);
	}	    
			
	if(this.showLeftRight == 1)
	{
		this.rightArrowScrollClass = new addArrowBehaviors(this.rightArrow, 250,'horizontal',this.thumbContainer,this.thumbScrollBack,this.thumbScrollHandle);
		this.leftArrowScrollClass = new addArrowBehaviors(this.leftArrow, -250,'horizontal',this.thumbContainer,this.thumbScrollBack,this.thumbScrollHandle);
	}
	
	this.rightThumbScrollClass = new addArrowBehaviors(this.scrollRightArrow, 250,'horizontal',this.thumbContainer,this.thumbScrollBack,this.thumbScrollHandle);
	this.leftThumbScrollClass = new addArrowBehaviors(this.scrollLeftArrow, -250,'horizontal',this.thumbContainer,this.thumbScrollBack,this.thumbScrollHandle);
	
	this.upThumbScrollClass = new addArrowBehaviors(this.scrollUpArrow, -250,'vertical',this.thumbContainer,this.thumbVScrollBack,this.thumbVScrollHandle);
	this.downThumbScrollClass = new addArrowBehaviors(this.scrollDownArrow, 250,'vertical',this.thumbContainer,this.thumbVScrollBack,this.thumbVScrollHandle);
	this.upDesScrollClass = new addArrowBehaviors(this.desUpArrow, -250,'vertical',this.desContainer,this.desScrollBack,this.desScrollHandle);
	this.downDesScrollClass = new addArrowBehaviors(this.desDownArrow, 250,'vertical',this.desContainer,this.desScrollBack,this.desScrollHandle);
	
	
	
	this.enlarge = this.magnify;
	
	this.linksArray.each(function(el,index)
	{	
		el.addEvent('click', function(e)
		{	
			e = new Event(e).stop();
			
			if(this.allowRating == 1)
			{
				this.mainRatings = new ratingsClass(this.guest,this.idArray,this.host,index,this.ratingsContainer,
				this.ratingsInfoDiv,this.ratingForm,this.ratingFormImgId,this.ratingFormImgRating,this.ratingsMessageContainer,
				this.ratedContainer,this.numRatingsSpan);
			}
			
			if(this.allowComments == 1)
			{
				this.mainComments = new commentsClass(this.guest,this.idArray,this.host,this.commentsAmountDiv,
				this.commentsDataContainer,this.commentsContainer,
				this.commentsForm,this.commentsTextarea,this.commentsImgId,this.commentsGif,index,this.darkDiv,this.lightbox);
			}
			
			if(this.fade == 1)
			{
				if(this.main == true)
				{
					if (this.enlarge == 1) 
					{
						$('magnifygif').remove();
					}
				}
				
				this.imageToFadeOut = $(this.largeImgDiv).getElement('img[class=large_img]');
				this.imageToFadeLeft = this.imageToFadeOut.getLeft();
				this.containerLeft = $(this.largeImgDiv).getLeft();
				this.leftMargin = this.imageToFadeLeft - this.containerLeft;
				
				this.containerTr = $(this.largeImgDiv).getParent();
				this.containerSection = this.containerTr.getParent();
				this.containerTable = this.containerSection.getParent();
			
				this.mainImageTop = this.imageToFadeOut.getTop();
				this.containerTop = this.containerTable.getTop();
				this.topMargin = this.mainImageTop - this.containerTop;
				
				this.imageToFadeOut.setStyles
				({
					position: 'absolute',
					top: this.topMargin,
					left: this.leftMargin
				});
				
				this.imageFadeAway = new Fx.Style(this.imageToFadeOut, 'opacity').start(1,0).chain(function()
				{
					this.imageToFadeOut.remove();
				}.bind(this));
				
				this.imageToFadeIn = new Asset.images([this.host + 'images/stories/igallery/' + this.galleryFolder + '/' + this.imageFolder + '/' + this.imgFileNameArray[index] ], 
				{
					onComplete: function()
					{	
						this.imageToFadeInEl = new Element('img', 
						{
					    	'src': null
						});
						
						this.imageToFadeInEl.setStyle('opacity',0);
						this.imageToFadeInEl.injectTop(this.largeImgDiv);
						this.imageToFadeInEl.setProperty('src', this.host + 'images/stories/igallery/' + this.galleryFolder + '/' + this.imageFolder + '/' + this.imgFileNameArray[index]);
						this.imageToFadeInEl.setProperty('class', 'large_img');
						this.imageFadeIn = new Fx.Style(this.imageToFadeInEl, 'opacity').start(0,1).chain(function()
						{
							if(this.main == true)
							{
								if (this.enlarge == 1) 
								{
									this.insertMagnify();
								}
							}
						}.bind(this));
						
					}.bind(this)
		    	});
			}
			else
			{
				$(this.largeImgDiv).setHTML('<img src="' + this.host + '/components/com_igallery/images/loader.gif" />');
				
	
				new Asset.images([ this.host + 'images/stories/igallery/' + this.galleryFolder + '/' + this.imageFolder + '/' + this.imgFileNameArray[index] ], 
				{
					onComplete: function()
					{
						$(this.largeImgDiv).setHTML('<img src="' + this.host + 'images/stories/igallery/' + this.galleryFolder + '/' + this.imageFolder + '/' + this.imgFileNameArray[index] +  '" />');
						
						this.imgToChain = $(this.largeImgDiv).getElement('img');
						this.imgChain = new Fx.Style(this.imgToChain, 'opacity',{duration:200}).start(1,1).chain(function()
						{
							if(this.main == true)
							{
								if (this.enlarge == 1) 
								{
									this.insertMagnify();
								}
							}
						}.bind(this));
						
					}.bind(this)
		    	});
			}
			
			if (this.main == true)
			{
				
				this.linkClass = el.getProperty('class');
				this.linkTarget = el.getProperty('target');
				if(this.linkClass == 'picture_link')
				{	
					$(this.largeImgDiv).setStyle('cursor', 'pointer');
					$(this.largeImgDiv).removeEvents('click');
					$(this.largeImgDiv).addEvent('click', function(e)
					{
						if (this.linkTarget == '_blank') 
						{
							window.open(el);
						}
						else
						{
							window.location = el;
						}
						
					}.bind(this));
				}
				
				if(this.lightbox == 1 && this.linkClass == 'no_link' )
				{
					$(this.largeImgDiv).removeEvents('click');
					
					$(this.largeImgDiv).setStyle('cursor', 'pointer');
					
					$(this.largeImgDiv).addEvent('click', function(e)
					{	
						this.igniteLightBox(index);
					}.bind(this));
				}
			}

			
			this.descriptionDivs = $(this.desContainer).getElements('div[class=des_div]');
			
			this.descriptionDivs.each(function(el,index)
			{
				el.setStyle('display', 'none');
			});
			
			
			$(this.desContainer).scrollTo(0,0);
			this.descriptionDivs[index].setStyle('display', 'block');
			this.desScrollbar = new scrollBarClass(this.desContainer,this.desScrollBack,this.desScrollHandle,'y','vertical','top');
					
		}.bind(this));
	}.bind(this));
},


insertMagnify : function()
{
	this.mainImage = $(this.largeImgDiv).getElement('img');
	this.mainImageLeft = this.mainImage.getLeft();
	this.containerLeft = $(this.largeImgDiv).getLeft();
	this.leftMargin = this.mainImageLeft - this.containerLeft;
	
	this.imgSizeArray = this.mainImage.getSize();
	this.imgWidth = this.imgSizeArray.size.x;
	this.imgHeight = this.imgSizeArray.size.y;
	
	this.containerTr = $(this.largeImgDiv).getParent();
	this.containerSection = this.containerTr.getParent();
	this.containerTable = this.containerSection.getParent();

	this.mainImageTop = this.mainImage.getTop();
	this.containerTop = this.containerTable.getTop();
	this.mainImageTopMargin = this.mainImageTop - this.containerTop;
	
	this.magnifyMarginLeft = this.leftMargin + this.imgWidth - 27;
	this.magnifyMarginTop = this.imgHeight + this.mainImageTopMargin - 20;
	
	
	this.magnify = new Asset.images([this.host + 'components/com_igallery/images/magnify.gif' ], 
	{
		onComplete: function()
		{
			this.magnify[0].injectTop(this.largeImgDiv).setStyles
			({
				position: 'absolute',
				top: this.magnifyMarginTop,
				left: this.magnifyMarginLeft,
				'z-index': 1020,
				opacity: 0
			});
			this.magnify[0].setProperty('id', 'magnifygif');
			
			if (this.fade == 1) 
			{
				this.magnifyFadeIn = new Fx.Style(this.magnify[0], 'opacity').set(0.5);
			}
			if (this.fade == 0)
			{
				this.magnifyFadeIn = new Fx.Style(this.magnify[0], 'opacity').set(0.5);
			}
		}.bind(this)
	});
},

igniteLightBox : function(index)
{
	if(this.lboxPreload == 1)
	{
		this.counter = 0;
		this.lboxPreloaderClass = new preloaderClass(this.host,this.galleryFolder,'lightbox',this.imgFileNameArray,this.numPics);
	}
	
	$(this.lboxTVScrollHandle).setStyle('top', 0);
	$(this.lboxTScrollHandle).setStyle('left', 0);
	
	this.bodyTag = document.getElementsByTagName("body").item(0);
	this.scrolledDown = Window.getScrollTop();
	this.totalScrollHeight = Window.getScrollHeight();
	this.totalWidth = Window.getWidth();
	this.whiteDivLeftMargin = (this.totalWidth/2) - ( (this.lightboxWidth)/2);
	
	
	$(this.whiteDiv).injectTop(this.bodyTag);
	
	$(this.whiteDiv).setStyles
	({
		'top': this.scrolledDown + 30,
        'left': this.whiteDivLeftMargin,
        'opacity': '0',
		'display': 'block',
		'z-index': 1040
	});
	
	this.scrolledDown = Window.getScrollTop();
	this.totalScrollHeight = Window.getScrollHeight();
	this.totalWidth = Window.getWidth();
	$(this.darkDiv).injectTop(this.bodyTag);
	
	$(this.darkDiv).setStyles
	({
		'width': this.totalWidth,
        'height': this.totalScrollHeight + 30,
        'top': '0px',
        'left': '0px',
        'opacity': '0',
		'display': 'block',
		'z-index': 1030
	});
	
	this.darkDivFadeIn = new Fx.Style($(this.darkDiv), 'opacity').start(0,.7);
	this.whiteDivFadeIn = new Fx.Style($(this.whiteDiv), 'opacity').start(0,1);
	
	this.lboxImageToFadeIn = new Asset.images([ this.host + 'images/stories/igallery/' + this.galleryFolder + '/lightbox/' + this.imgFileNameArray[index] ], 
	{
		onComplete: function()
		{
			this.lboxImageToFadeIn[0].injectTop(this.lboxLargeImgDiv).setStyles('opacity: 0;');
			this.lboxImageFadeIn = new Fx.Style(this.lboxImageToFadeIn[0], 'opacity').start(0,1);
			this.lboxImageToFadeIn[0].setProperty('class', 'large_img');
		}.bind(this)
	});
	
	this.lboxDescriptionDivs = $(this.lboxDesContainer).getElements('div[class=des_div]');
	this.lboxDescriptionDivs.each(function(el,index)
	{
		el.setStyle('display', 'none');
	});
	$(this.lboxDesContainer).scrollTo(0,0);
	this.lboxDescriptionDivs[index].setStyle('display', 'block');
	
	$(this.closeImage).addEvent('click', function(e)
	{
		this.darkFadeOut = new Fx.Style($(this.darkDiv), 'opacity').start(0.7,0).chain(function()
		{
			$(this.darkDiv).setStyle('display','none');
		}.bind(this));
		
		this.whiteFadeOut = new Fx.Style($(this.whiteDiv), 'opacity').start(1,0).chain(function()
		{
			$(this.lboxLargeImgDiv).setHTML('');
			$(this.whiteDiv).setStyle('display','none');
			
		}.bind(this));
		
		if(this.allowRating == 1)
		{
			this.mainRatings = new ratingsClass(this.guest,this.idArray,this.host,index,this.ratingsContainer,
			this.ratingsInfoDiv,this.ratingForm,this.ratingFormImgId,this.ratingFormImgRating,this.ratingsMessageContainer,
			this.ratedContainer,this.numRatingsSpan);
		}
		
		if(this.allowComments == 1)
		{
			this.mainComments = new commentsClass(this.guest,this.idArray,this.host,this.commentsAmountDiv,
			this.commentsDataContainer,this.commentsContainer,
			this.commentsForm,this.commentsTextarea,this.commentsImgId,this.commentsGif,index,this.darkDiv,this.lightbox);
		}
		
	}.bind(this));
	
	
	
	if(this.lboxAllowRating == 1)
	{
		this.mainRatings = new ratingsClass(this.guest,this.idArray,this.host,index,this.ratingsContainer,
		this.lboxRatingsInfoDiv,this.lboxRatingForm,this.lboxRatingFormImgId,this.lboxRatingFormImgRating,this.lboxRatingsMessageContainer,
		this.ratedContainer,this.lboxNumRatingsSpan);
	}
	
	if(this.lboxAllowComments == 1)
	{
		this.mainComments = new commentsClass(this.guest,this.idArray,this.host,this.lboxCommentsAmountDiv,
		this.commentsDataContainer,this.lboxCommentsContainer,
		this.lboxCommentsForm,this.lboxCommentsTextarea,this.lboxCommentsImgId,this.lboxCommentsGif,index,this.darkDiv,this.lightbox);
	}
	
	
}

});
