var Global = {
	init : function() {

	}
}
var likeLove = (function(){
	return {
		init: function() {
			this.images = [];
			this.choices = $('#choices li img');
			this.largeview = $('#largeview');
			this.initChoices();
			this.initRating();
		},
		initChoices: function() {
			$.each(this.choices, function(i,e) {
				likeLove.images[i] = {};
				likeLove.images[i].name = 'image_' + (i + 1);
				likeLove.images[i].thumb = $(e);
				likeLove.images[i].set = false;
				
				
				$(e).click(function(e) {
					likeLove.setChoice(i);
				});
			});
			
			likeLove.setChoice(0);
		},
		initRating: function () {
			$('#rating li a:eq(0)').click(function(e){
				likeLove.choose('like');
				return false;
			});
			$('#rating li a:eq(1)').click(function(e){
				likeLove.choose('love');
				return false;
			});
			$('#rating li a:eq(2)').click(function(e){
				likeLove.choose('dislike');
				return false;
			});
		},
		setChoice: function(i) {
			this.currentIndex = i;
			this.largeview[0].src = 'img/likelove/'+this.images[i].name+'.jpg';
			console.log(this.images[i])
		},
		choose: function(rating) {
			this.images[this.currentIndex].thumb[0].src = 'img/likelove/'+likeLove.images[this.currentIndex].name+'_inactive.jpg';
			this.images[this.currentIndex].thumb.unbind('click');
			this.images[this.currentIndex].rating = rating;
			this.images[this.currentIndex].set = true;
			this.findNext();
			console.log(rating)
		},
		findNext: function() {
			for (i=this.currentIndex;i<=this.images.length-1;i++) {
				if(i !== this.currentIndex && this.images[i].set == false) {
					this.largeview[0].src = 'img/likelove/'+this.images[i].name+'.jpg';
					this.currentIndex = i;
					console.log(this.images[i]);
					break;
				}
				if(i == this.images.length-1) {
					for(i2=0; i2<=this.currentIndex; i2++) {
						if(i2 !== this.currentIndex && this.images[i2].set == false) {
							this.largeview[0].src = 'img/likelove/'+this.images[i2].name+'.jpg';
							this.currentIndex = i2;
							console.log(this.images[i2]);
							break;
						}
						if(i2 == this.currentIndex) {
							console.log('no options');
							this.outputResults();
							break;
						}
					}
				}
			  
			} 
		},
		outputResults: function() {
			$.each(this.images, function(i,e) {
				if(e.set) {
					$('#form').append('<input name="'+e.name+'" value="'+e.rating+'" type="hidden" />');
				}
				console.log(i,e)
			})
			
			document.likelove.submit();
		}
	}
})();

$(document).ready(function() { 
	Global.init();
	if($('#rating').length > 0) likeLove.init();
});
