addArrowBehaviors = new Class
({
    initialize: function(arrow, pixels, mode, container, scrollBack, handle)
    {
    	this.arrow = arrow;
    	this.pixels = pixels;
    	this.mode = mode;
		this.container = container;
		this.scrollBack = scrollBack;
		this.handle = handle;
		
		this.arrowScroller = new Fx.Scroll(this.container);
		
		$(this.arrow).addEvent('click', function(e)
		{
			this.containerSizeArray = $(this.container).getSize();
			this.currentScrollX = this.containerSizeArray.scroll.x;
			this.currentScrollY = this.containerSizeArray.scroll.y;
			this.contentWidth = this.containerSizeArray.scrollSize.x;
			this.contentHeight = this.containerSizeArray.scrollSize.y;
			
			if (this.mode == 'horizontal') 
			{
				this.arrowScroller.scrollTo(this.currentScrollX + this.pixels, this.currentScrollY);
			}
			
			if (this.mode == 'vertical') 
			{
				this.arrowScroller.scrollTo(this.currentScrollX, this.currentScrollY + this.pixels);
			}
			
			if (this.mode == 'horizontal') 
			{
				this.scrollBackSizeArray = $(this.scrollBack).getSize();
				this.scrollBackWidth = this.scrollBackSizeArray.size.x;
			}
			
			if (this.mode == 'vertical') 
			{
				this.scrollBackSizeArray = $(this.scrollBack).getSize();
				this.scrollBackHeight = this.scrollBackSizeArray.size.y;
			}
			
			if (this.mode == 'horizontal') 
			{
				this.handlescrollX = ((this.currentScrollX + this.pixels) / (this.contentWidth - this.scrollBackWidth)) * this.scrollBackWidth;
				if (this.handlescrollX > this.scrollBackWidth) 
				{
					this.handlescrollX = this.scrollBackWidth - 14;
				}
				if (this.handlescrollX < 0) 
				{
					this.handlescrollX = 0;
				}
				
				$(this.handle).setStyle('left', this.handlescrollX);
			}
			if(mode == 'vertical') 
			{
				this.handlescrollY = ( (this.currentScrollY + this.pixels) / (this.contentHeight - this.scrollBackHeight)) * this.scrollBackHeight;
				
				if (this.handlescrollY > this.scrollBackHeight) 
				{
					this.handlescrollY = this.scrollBackHeight -14;
				}
				if (this.handlescrollY < 0) 
				{
					this.handlescrollY = 0;
				}
				$(this.handle).setStyle('top', this.handlescrollY);
			}
			
	    }.bind(this));
    }
});

scrollBarClass = new Class
({
    initialize: function(container,scrollBarBack,handle,xy,mode,topLeft)
    {
        this.container = container;
        this.scrollBarBack = scrollBarBack;
        this.handle = handle;
        this.mode = mode;
        this.xy = xy;
        this.topLeft = topLeft;
        
        this.containerTd = $(this.scrollBarBack).getParent();
		this.containerTr = this.containerTd.getParent();
		this.containerSection = this.containerTr.getParent();
		this.containerTable = this.containerSection.getParent();
		
        this.SizeArray = $(this.container).getSize();
        
        if(this.xy == 'y')
        {
            this.ScrollLength = this.SizeArray.scrollSize.y;
            this.BoxLength = this.SizeArray.size.y;
		}
        
        if(this.xy == 'x')
        {
            this.ScrollLength = this.SizeArray.scrollSize.x;
            this.BoxLength = this.SizeArray.size.x;
        }
        
		if(this.ScrollLength > this.BoxLength)
		{
			
			$(this.containerTable).setStyle('display', 'block');
			
			this.ScrollBarSlider = new Slider(this.scrollBarBack, this.handle, 
			{
				steps: 100,
				offset: 0,
				mode: this.mode,
			 
			    onChange: function(step)
			    {
			    	if(this.mode == 'vertical')
        			{
        				
			    		$(this.container).scrollTo(0,step * ( (this.ScrollLength - this.BoxLength) /100) );
			    	}
			    	
			    	if(this.mode == 'horizontal')
        			{
			    		$(this.container).scrollTo(step * ( (this.ScrollLength - this.BoxLength) /100),0 );
			    	}
			    	
        		}.bind(this),
			    
			    onTick: function(pos)
			    {
					this.knob.setStyle(this.Topleft, pos);
			    }.bind(this)
		    
			});
		}
		else
		{
			this.containerTable.setStyle('display', 'none');
		};
	}
    
});

preloaderClass = new Class
({
    initialize: function(host,galleryFolder,imageFolder,imgFileNameArray,numPics)
    {
    	this.host = host;
    	this.galleryFolder = galleryFolder;
    	this.imageFolder = imageFolder;
    	this.imgFileNameArray = imgFileNameArray;
    	this.numPics = numPics;
    	
    	this.counter = 0;
    	this.preloaderVar = this.preloaderFunction.periodical(750, this);
    },
    
    preloaderFunction : function()
	{
    	new Asset.images([this.host + 'images/stories/igallery/' + this.galleryFolder + '/' + this.imageFolder + '/' + this.imgFileNameArray[this.counter] ],
		{
		    onComplete: function()
		    {
				
		    }
		});
		
		this.counter++;
		if(this.counter == this.numPics)
		{
			$clear(this.preloaderVar);
		}
		
    }
    
});


commentsClass = new Class
({
	initialize: function(guest,idArray,host,commentsAmountDiv,commentsDataContainer,commentsContainer,commentsForm,commentsTextarea,
	commentsImgId,commentsGif,imgIndex,darkDiv,lightbox)
    {
    	this.guest = guest;
    	this.idArray = idArray;
    	this.host = host;
    	
    	this.commentsAmountDiv = commentsAmountDiv;
    	this.commentsDataContainer = commentsDataContainer;
    	this.commentsContainer = commentsContainer;
    	this.commentsForm = commentsForm;
    	this.commentsTextarea = commentsTextarea;
    	this.commentsImgId = commentsImgId;
    	this.commentsGif = commentsGif;
    	this.imgIndex = imgIndex;
    	this.darkDiv = darkDiv;
		this.lightbox = lightbox;
    	
    	
    	
    	
		if(this.guest == 0)
		{
			$(this.commentsImgId).setProperty('value', this.idArray[this.imgIndex]);
			$(this.commentsForm).removeEvents();
		}
		
		this.caculateComments();
		
		//ajax comments bit
		if(this.guest == 0)
		{	
			$(this.commentsForm).addEvent('submit', function(e) 
			{
				new Event(e).stop();
				this.commentToSend = $(this.commentsTextarea).getValue();
				
				if(this.commentToSend.length < 1)
				{
					$(this.commentsGif).setHTML('Comment Field Was Empty');
				}
				else
				{
					$(this.commentsGif).setHTML('<img src="' + this.host + 'components/com_igallery/images/loader.gif');
					
					$(this.commentsForm).send
					({
						onComplete: function(response) 
						{
							if(response == 0)
							{
								$(this.commentsGif).setHTML('Please Login Again');
							}
							else
							{
								this.newCommentDiv = new Element('div', 
								{
							    	'class': 'main_comments_div_' + this.idArray[this.imgIndex]
								});
								this.newCommentDiv.injectInside(this.commentsDataContainer);
								
								
								
								this.newCommentDiv.setStyles({'border-bottom': '1px solid #cccccc',padding: '8px 0px',display: 'none'});
								
								
								this.responseWithLineBreak = response.replace(new RegExp("\\n","g"),"<br />" );
								
								this.newCommentDiv.setHTML(this.responseWithLineBreak);
								$(this.commentsGif).setHTML('<span style="color:green">Comment Successfully Added</span>');
								$(this.commentsTextarea).value = '';
								this.caculateComments();
							}
						}.bind(this)
					});
					
				}
				
			}.bind(this));
			
		}
    },
    
    caculateComments: function()
    {
    	
    	this.commentDataDivs = $(this.commentsDataContainer).getElements('div');
    	this.commentsContainerDiv = $(this.commentsContainer);
    	
    	this.commentsContainerChildDivs = $(this.commentsContainer).getElements('div');
    	this.commentsContainerChildDivs.each(function(el,index)
    	{
    		el.remove();
    	});
    	
		this.commentsCounter = 0;
		this.commentDataDivs.each(function(el,index)
		{
			this.commentDivId = el.getProperty('class');
			this.commentDivImgIdArray = this.commentDivId.split("_");
			this.commentDivImgId = this.commentDivImgIdArray.pop();
			
			if(this.commentDivImgId == this.idArray[this.imgIndex])
			{
				//el.setStyle('display','block');
				this.clone = el.clone().injectInside(this.commentsContainerDiv);
				this.clone.setStyle('display','block')
				this.commentsCounter++
			}
		}.bind(this));
		
		if(this.commentsCounter == 0)
		{
			$(this.commentsAmountDiv).setHTML('There are no comments for this picture');
		}
		if(this.commentsCounter == 1)
		{
			$(this.commentsAmountDiv).setHTML('1 comment left for this picture:');
		}
		if(this.commentsCounter > 1)
		{
			$(this.commentsAmountDiv).setHTML(this.commentsCounter + ' comments left for this picture:');
		}
		
		this.totalScrollHeight = Window.getScrollHeight();
		if(this.lightbox == 1)
		{
			$(this.darkDiv).setStyle('height',this.totalScrollHeight + 30);
		}
    }
    
    
});

ratingsClass = new Class
({
	initialize: function(guest,idArray,host,imgIndex,ratingsContainer,ratingsInfoDiv,ratingForm,
	ratingFormImgId,ratingFormImgRating,ratingsMessageContainer,ratedContainer,numRatingsSpan)
	{	
		this.guest = guest;
    	this.idArray = idArray;
    	this.host = host;
    	this.imgIndex = imgIndex;
    	
    	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.newRatedArray = new Array();
    	
    	this.ratedDivs = $(this.ratedContainer).getElements('div');
		this.ratedCounter = 0;
		this.ratedDivs.each(function(el,index)
		{
			this.ratedDivValue = el.innerHTML;
			this.ratedDivInt = parseInt(this.ratedDivValue);
			this.newRatedArray[index] = this.ratedDivInt;
		}.bind(this));
    	
    	this.caculateRating();
    	
    	this.ratingMessages = $(this.ratingsMessageContainer).getElements('span');
    	this.ratingMessages.each(function(el,index){el.setStyle('display','none')});
    	if(this.guest == 1)
		{
    		this.ratingMessages[0].setStyle('display','inline');
		}
		if(this.guest == 0 && this.newRatedArray[this.imgIndex] == 0)
		{
    		this.ratingMessages[1].setStyle('display','inline');
		}
		if(this.guest == 0 && this.newRatedArray[this.imgIndex] == 1)
		{
    		this.ratingMessages[2].setStyle('display','inline');
		}
		
		if(this.guest == 0)
		{
			//set the mouse enter and mouse leave
			this.ratingStars.each(function(el,index)
			{
				el.removeEvents();
				if(this.newRatedArray[this.imgIndex] == 0)
				{
				    el.addEvent('mouseenter', function()
				    {
				    	for(this.i = 0; this.i<5; this.i++)
						{
							if(this.i <= index)
							{
								this.ratingStars[this.i].setProperty('src', this.host + 'components/com_igallery/images/star-blue.gif');
							}
							else
							{
								this.ratingStars[this.i].setProperty('src', this.host + 'components/com_igallery/images/star-white.gif');
							}
						}
				    }.bind(this));
				    
				    el.addEvent('mouseleave', function()
				    {
				    	for(this.i = 0; this.i<5; this.i++)
						{
							if(this.i < Math.floor(this.imgRating))
							{
								this.ratingStars[this.i].setProperty('src', this.host + 'components/com_igallery/images/star-blue.gif');
							}
							else
							{
								this.ratingStars[this.i].setProperty('src', this.host + 'components/com_igallery/images/star-white.gif');
							}
							if(this.imgRating - Math.floor(this.imgRating) > 0)
							{
								this.ratingStars[Math.floor(this.imgRating)].setProperty('src', this.host + 'components/com_igallery/images/star-half.gif');
							}
						}
				    }.bind(this));
					
				    
				    //submit the form
				    el.addEvent('click', function()
				    {	
				    	this.ratingMessages.each(function(el,index){el.setStyle('display','none')});
				    	this.ratingMessages[3].setStyle('display','inline');
				    	
				    	$(this.ratingFormImgId).setProperty('value', this.idArray[this.imgIndex]);
				    	$(this.ratingFormImgRating).setProperty('value', index + 1);
				    	
				    	$(this.ratingForm).send
						({
							onComplete: function(response) 
							{
								
								this.ratingMessages.each(function(el,index){el.setStyle('display','none')});
				    			if(response == 0)
				    			{
									this.ratingMessages[5].setStyle('display','inline');
				    			}
				    			if(response == 1)
				    			{
									this.ratingMessages[2].setStyle('display','inline');
				    			}
				    			if(response == 2)
				    			{
									this.ratingMessages[4].setStyle('display','inline');
									this.newRatingDiv = new Element('div', 
									{
								    	'class': 'ratings_div_' + this.idArray[this.imgIndex]
									});
									this.newRatingDiv.setStyle('display','none');
									this.newRatingDiv.injectInside(this.ratingsContainer);
									this.newRatingDiv.setHTML(index + 1);
									
									this.ratedDivs[this.imgIndex].setHTML(1);
									
									this.caculateRating();
				    			}
								
							}.bind(this)
						});
				    	
				    }.bind(this));
			    
				}
			
			}.bind(this));
		
		}
	},
	
	caculateRating: function()
	{
		//caculate the rating
		this.ratingDivs = $(this.ratingsContainer).getElements('div');
		this.ratingCounter = 0;
		this.ratingSum = 0;
		this.ratingDivs.each(function(el,index)
		{
			this.ratingDivId = el.getProperty('class');
			this.ratingDivImgIdArray = this.ratingDivId.split("_");
			this.ratingDivImgId = this.ratingDivImgIdArray.pop();
			
			if(this.ratingDivImgId == this.idArray[this.imgIndex])
			{	
				this.ratingNumber = parseInt(el.innerHTML);
				this.ratingSum = this.ratingSum + this.ratingNumber;
				this.ratingCounter++
			}
		}.bind(this));
		
		if(this.ratingCounter == 1)
		{
			$(this.numRatingsSpan).setHTML(this.ratingCounter + ' Vote');
		}
		else
		{
			$(this.numRatingsSpan).setHTML(this.ratingCounter + ' Votes');
		}
		
		if(this.ratingSum == 0 && this.ratingCounter == 0)
		{
			this.imgRating = 0;
		}
		else
		{
			this.imgRatingtoRound = this.ratingSum/this.ratingCounter;
			this.imgRating = Math.round( (this.imgRatingtoRound * 2) ) /2;
		}
		
		//set the rating
		this.ratingStars = $(this.ratingsInfoDiv).getElements('img');
		for(this.k = 0; this.k<5; this.k++)
		{
			this.ratingStars[this.k].setProperty('src', this.host + 'components/com_igallery/images/star-white.gif');
		}
		
		for(this.k = 0; this.k<Math.floor(this.imgRating); this.k++)
		{
			this.ratingStars[this.k].setProperty('src', this.host + 'components/com_igallery/images/star-blue.gif');
		}
		
		if(this.imgRating - Math.floor(this.imgRating) > 0)
		{
			this.ratingStars[Math.floor(this.imgRating)].setProperty('src', this.host + 'components/com_igallery/images/star-half.gif');
		}
	}
	
});

var iScroller = new Class({

	options: 
	{
		area: 20,
		velocity: 1,
		thumbTable: null,
		scrollBack: null,
		scrollHandle: null,
		scrollVBack: null,
		scrollVHandle: null,
		onChange: function(x, y)
		{
			this.element.scrollTo(x, y);
		}
	},

	initialize: function(element, options){
		this.setOptions(options);
		this.element = $(element);
		this.mousemover = ([window, document].contains(element)) ? $(document.body) : this.element;
	},

	start: function(){
		this.coord = this.getCoords.bindWithEvent(this);
		this.mousemover.addListener('mousemove', this.coord);
	},

	stop: function(){
		this.mousemover.removeListener('mousemove', this.coord);
		this.timer = $clear(this.timer);
	},

	getCoords: function(event){
		this.page = (this.element == window) ? event.client : event.page;
		if (!this.timer) this.timer = this.scroll.periodical(50, this);
	},

	scroll: function(){
		var el = this.element.getSize();
		var pos = this.element.getPosition();

		var change = {'x': 0, 'y': 0};
		for (var z in this.page){
			if (this.page[z] < (this.options.area + pos[z]) && el.scroll[z] != 0)
				change[z] = (this.page[z] - this.options.area - pos[z]) * this.options.velocity;
			else if (this.page[z] + this.options.area > (el.size[z] + pos[z]) && el.scroll[z] + el.size[z] != el.scrollSize[z])
				change[z] = (this.page[z] - el.size[z] + this.options.area - pos[z]) * this.options.velocity;
		}
		if (change.y || change.x) this.fireEvent('onChange', [el.scroll.x + change.x, el.scroll.y + change.y]);
	}

});

iScroller.implement(new Events, new Options);