var vmix_rater = Class.create();
vmix_rater.prototype = {
	initialize: function(){
	},
	rate: function(req){
		this.rating = req.rating;
		this.media_id = req.media_id;
		this.media_type = 'media';
                this.dont_display_casting_vote = req.dont_display_casting_vote;

		if (jQuery('#your_rating_text').css('display') == 'block' && !this.dont_display_casting_vote){ // let's only swap the "casting vote" text if we have space allocated for it
	                this.prev_your_rating_text = $('your_rating_text').innerHTML;
			jQuery('#your_rating_text').html('Casting vote...');
		}

		this.rate_url = '/'+this.media_type+'/'+this.media_id+'/rate?rating='+this.rating;
		this.check_login_required();
	},
	check_login_required: function(){
                var ajax = new Ajax.Request('/get_ads', {method:'get', onComplete:this.check_login_required_cb.bind(this)});
	},
	check_login_required_cb: function(o){
		var data = o.responseText.evalJSON();
		this.login_url = data.login_url;
		this.redir_var_name = data.redir_var_name;
		if (data.rating_login_required == 1){
			// check login
			var ajax = new Ajax.Request('/check_ext_user', {method:'get', onComplete:this.check_login_cb.bind(this)});
		} else {
			// log rating
			var ajax = new Ajax.Request(this.rate_url, {method:'get', onComplete:this.finish.bind(this)});
		}
	},
	check_login_cb: function(o){
		var data = o.responseText.evalJSON();
		if (data.username){
			var ajax = new Ajax.Request(this.rate_url, {method:'get', onComplete:this.finish.bind(this)});
		} else {
	                if (confirm('You must login before you can rate media.  Would you like to login now?')){
                                if (this.login_url){
                                        var redir_url = this.login_url;
                                        if (this.redir_var_name){
                                                redir_url += this.redir_var_name+'='+location.href; // should probably do some error handling here at some point
                                        }
                                        location.href = redir_url;
                                } else {
                                        alert('Unable to get login url.  Sorry.');
                                }
	                } else {
				if (this.prev_your_rating_text){
					$('your_rating_text').innerHTML = this.prev_your_rating_text;
				}
			}
		}
	},
	finish: function(o){
		// should probably check return first
                set_cookie('rating_'+this.media_id, this.rating, 365);

                $('current_rating').style.width = this.rating*15+'px';
		if (this.prev_your_rating_text){
			$('your_rating_text').innerHTML = this.prev_your_rating_text;
		}

                var response = o.responseText.evalJSON();

	        if ($('total_votes')){
	                $('total_votes').innerHTML = response.total_count;
	        }
	        if ($('avg_rating')){
	                $('avg_rating').style.width = response.average*15+'px';
	        }
	}
}

// ajax calls
function rate(media_type, media_id, rating, dont_display_casting_vote){
	media_type = 'media';
	if (!dont_display_casting_vote){
		var dont_display_casting_vote = 0;
	}
	if (get_cookie('rating_'+media_id)){
		alert("Sorry, you've already voted on this media.  Changing your vote is not supported at this time");
		return false;
	}
	if (!rater){
		var rater = new vmix_rater();
	}
	rater.rate({'media_id':media_id, 'rating':rating, 'dont_display_casting_vote':dont_display_casting_vote});
}


