var Rater = {
	entityID : null,
	allowedRatings : [1, 2, 3, 4, 5],
	voterID : null,
	voterLoggedIn : false,
	requiresLogin : false,
	requestUri : null,
	
	scaleSize : 5,
	items : [],
	
	imgSize : { width : 30, height : 30 },
	scoreKeeper : null,

	init : function( klass, url, entityID, voterID, voterLoggedIn, requiresLogin ) {
		var k = 0;

		this.requestUri = url;
		this.entityID = entityID;
		this.voterID = voterID;
		this.voterLoggedIn = voterLoggedIn;
		this.requiresLogin = requiresLogin;
		
		var ul = $$('ul.' + klass)[0];
		if( !ul ) { return false; }
		
		ul.setStyle('display', '');
		this.items = $$('ul.' + klass + ' a' );
		this.items.each( function(e) {
			e.addEvent( 'mouseover', this.over.bind(this) );
			e.addEvent( 'mouseout', this.out.bind(this) );
			e.addEvent( 'click', this.rate.bind(this) );
			e.setAttribute('score', this.allowedRatings[k]);
			k++;
		}, this);
		
		this.scoreKeeper = $$('ul.' + klass + ' li.score span ' )[0];
		this.scoreKeeper = $defined(this.scoreKeeper) ? this.scoreKeeper : null;
	},
	
	over : function() {
		var current = new Event(arguments[0]).target;
		var start = false, index = 0;
		for(var i = this.items.length - 1; i >= 0; i-- ) {
			if( this.items[i] == current ) { start = true; index = i + 1;}
			if( start ) {
				this.items[i].setStyle( 'background-position', '-' + (this.imgSize.width * i) + 'px -' + this.imgSize.height + 'px' );
			}
		}
		if( index > 0 && this.scoreKeeper ) { this.scoreKeeper.setText( index ); }
	},
	
	out : function() {
		for(var i = this.items.length - 1; i >= 0; i-- ) {
			this.items[i].setStyle( 'background-position', '-' + (this.imgSize.width * i) + 'px 0' );
		}
		if( this.scoreKeeper ) { this.scoreKeeper.setHTML( '&nbsp;' ); }
	},
	
	rate : function(e) {
		e = new Event(e);
		if( !this.requestUri ) { return false; }
		if( this.requiresLogin ) {
			if( !this.voterLoggedIn || !(this.voterID > 0) ) {
				//request quick login form
				if( !$defined(QuickLoginForm) ) { 
					document.location = '/members/login';
					return false;
				}
				
				QuickLoginForm.request(e);
				return false;
			}
		}

		var rating = e.target.getAttribute('score');
		this.gaTrackEvent(this.entityID, rating);
		
		var r =  new Json.Remote( this.requestUri, {method: 'post', onComplete: this.rateCompleted.bind(this)} );
		r.send({entityID: this.entityID, entityType: 'photo', rating: rating});
		
		return true;
	},
	
	gaTrackEvent: function(id, rating) {
		var pageTracker = window.pageTracker || null;
		if( !pageTracker ) { return; }
		if( /^\/photos\/(contest\/)?all\/?$/.test(document.location.pathname) ) {
			path = '/photos/show/' + id + '/a';
		}
		else {
			path = document.location.pathname;
		}
		pageTracker._trackEvent('RatePhoto', 'rate', path, rating);
	},
	
	rateCompleted : function(response) {
		document.location = response.redirect;
		return;
	},
	
	activate : function( requestUri, configObj ) {
		this.requestUri = requestUri;
		this.entityID = $defined(configObj.entityID) ? configObj.entityID : null;
		this.allowedRatings = $defined(configObj.allowedRatings) ? configObj.allowedRatings : [1, 2, 3, 4, 5];
		this.voterID = $defined(configObj.voterID) ? configObj.voterID : null;
		this.voterLoggedIn = $defined(configObj.voterLoggedIn) ? configObj.voterLoggedIn : false;
		this.requiresLogin = $defined(configObj.requiresLogin) ? configObj.requiresLogin : false;
	}
};

/*
Rater.activate('/photos/rate/" . $photo->getId() . "', {
		entityID: " . $photo->getId() . ",
		voterID: " . ($memberID > 0 ? $memberID : "null") . ",
		voterLoggedIn: " . ($memberLoggedIn ? "true" : "false") . ",
		requiresLogin: " . ($photo->isInContest() ? "true" : "false") . "
	});

 */
