//Friends Slider class
FriendsSlider = function(config){	
	this.init(config)
}

FriendsSlider.prototype = {
	// constructor	
	init: function(config){
	
		this.sliderInnerContainer 	= $('#' + config.sliderInnerId)
		
		this.leftControl			= $('#' + config.leftArrowId)
		this.rightControl			= $('#' + config.rightArrowId)
		
		this.sliderTemplate			= $('#' + config.slideTemplateContainerId).html()
		this.thumbTemplate			= $('#' + config.thumbTemplateContainerId).html()

		this.slideshowContainer		= $('#' + config.slideshowContainerId)
		this.loadingContainer		= $('#' + config.loadingContainerId)
		this.noFriendsContainer		= $('#' + config.noFriendsContainerId)
		this.errorContainer			= $('#' + config.errorContainerId)
		this.askFriendsWhoBoughtSelectbox = $('#' + config.askFriendsWhoBoughtSelectboxId)
		this.slideshowContainer.css({'position' : 'absolute', 'visibility' : 'hidden', 'display' : 'block'});
		this.pageWidth				= $('#' + config.sliderOuterId).width();
		this.slideshowContainer.css({'position' : '', 'visibility' : '', 'display' : ''});
		this.hoverClass				= config.hoverClass;
		// defaults
		this.currentPosition		= 0
		this.slideTime				= 500
		
		this.leftControlClassEnabled = config.leftControlClassEnabled
		this.leftControlClassDisabled = config.leftControlClassDisabled
		
		this.rightControlClassEnabled = config.rightControlClassEnabled
		this.rightControlClassDisabled = config.rightControlClassDisabled
		this.currentProductId = config.productId;
		
		// constants
		this.MODE_SLIDER		= 0
		this.MODE_LOADING		= 1
		this.MODE_NO_FRIENDS	= 2
		this.MODE_ERROR		= 3
		
		// modes array
		this.modesContainers = {
			0	: this.slideshowContainer,
			1	: this.loadingContainer,
			2	: this.noFriendsContainer,
			3	: this.errorContainer
		}
		
		this.setMode(this.MODE_LOADING)
		this.getFriends()
	},
	
	// getting friends from dedicated application
	getFriends: function(){
		var self = this
		$.ajax({
		    url: window.location.href.replace(/#/gi, ''),
			data: 'ajax_get_friends_with_same_purchase=1&product_id='+this.currentProductId,
		    dataType : "json",
		    success: function (data) {
                        var friends = data.friends;
		       	self.updateFriendsWithPurchaseData(friends);
                        self.setCountReplies(data.countMessage);
		    },
			error: function() {
		    	self.setMode(self.MODE_ERROR)
			}
		});
	},


        setCountReplies: function(count) {
                if (count > 0)
                {
                        $('#repliesLink').html('');
                        $('#repliesTab').html('');
                        $('#repliesTab').append('(' + count + ')');
                        $('#repliesLink').append('(' + count + ')');
                }
        },

	
	// updating friends block
	updateFriendsWithPurchaseData: function(friendsData){
		var slidesCount = friendsData.length
		if(friendsData.length > 0) {
			for(var i in friendsData) {
				this.appendSlide(friendsData[i]);
			}
			this.buildPane(slidesCount)
			this.setMode(this.MODE_SLIDER)
		}
		else {
			this.setMode(this.MODE_NO_FRIENDS);
			//askFriends.askFriendsWhoBoughtSelectbox.style.display = 'none';
		}
	},

	// adding friends slide
	appendSlide: function(templateVars){
		
		var templateEngine = new xtTemplate({
			'template'	: this.sliderTemplate,
			'tv'		: templateVars
		})
		
		var html = templateEngine.parse()
		this.sliderInnerContainer.append(html)
	},
	
	// binding
	buildPane: function(slidesCount) {
		this.slidesCount		= slidesCount
		this.pageCount			= Math.ceil(slidesCount/2)
		this.slidersCanvasWidth	= this.pageWidth * this.pageCount
		
		this.sliderInnerContainer.css('width', this.slidersCanvasWidth)
		
		this.toggleControls()
	},
	
	// setting 
	setMode: function(mode){
		for(var modeIndex in this.modesContainers) {
			if (modeIndex == mode) {
				this.modesContainers[modeIndex].show()
			}
			else {
				this.modesContainers[modeIndex].hide()
			}
		}
	},
	
	// making a slide right or left
	slide: function(){
		var self = this
		this.toggleControls()
		this.sliderInnerContainer.animate({
				'marginLeft' : -this.currentPosition*this.pageWidth
			},
			this.slideTime
		)
	},
	
	// slide to the left
	slideLeft: function () {
		if(this.currentPosition > 0) {
			this.currentPosition--
			this.slide()
		}
	},
	
	// slide to the right
	slideRight: function() {
		this.currentPosition++
		this.slide()
	},
	
	// toggle commerce
	toggleControls: function() {
		
		// left control
		if(this.currentPosition == 0)
			this.enableLeftControl(false)
		else 
			this.enableLeftControl(true)
			
		// right control
		if(this.currentPosition == (this.pageCount-1))
			this.enableRightControl(false)
		else 
			this.enableRightControl(true)
	},
	
	enableLeftControl: function(enableFlag){
		var control = this.leftControl
		$(control).unbind('click')
		if(enableFlag){
			var self = this
			$(control).removeClass(this.leftControlClassDisabled)
			$(control).addClass(this.leftControlClassEnabled)
			$(control).click(function() {
				self.slideLeft()
			})
		}
		else {
			$(control).removeClass(this.leftControlClassEnabled)
			$(control).addClass(this.leftControlClassDisabled)
		}
	},
	
	enableRightControl: function(enableFlag){
		var control = this.rightControl
		
		$(control).unbind('click')
		if(enableFlag){
			var self = this
			$(control).removeClass(this.rightControlClassDisabled)
			$(control).addClass(this.rightControlClassEnabled)
			$(control).click(function() {
				self.slideRight() 
			})
		}
		else {
			$(control).removeClass(this.rightControlClassEnabled)
			$(control).addClass(this.rightControlClassDisabled)
		}
	}
}

