var vmix_commenter = Class.create();
vmix_commenter.prototype = {
        initialize: function(){
        },
        comment: function(req){
                this.media_type = req.media_type;
                this.media_id = req.media_id;
		this.email = req.email;
		this.name = req.name;
		this.comment = req.comment;
                var add_comment_url = '/'+this.media_type+'/'+this.media_id+'/add_comment?email='+this.email+'&name='+this.name+'&comment='+this.comment;
                var ajax = new Ajax.Request(add_comment_url, {method:'get', onComplete:this.finish.bind(this)});
        },
	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.add_comment_login_required == 1){
			// check for valid login
			var ajax = new Ajax.Request('/check_ext_user', {method:'get', onComplete:this.check_login_cb.bind(this)});
                } else {
			$('add_comment').show();
			try {
				show_comment_box(); // cb in case we need to do other things when this event happens
			} catch (e) {
			}
		}
	},
        check_login_cb: function(o){
                var data = o.responseText.evalJSON();
                if (!data.username){
			// user not logged in, so see if they want to login now
                        if (confirm('You must login before you can post a comment.  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+'='+ encodeURIComponent(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 {
                        try {
                                hiding_comment_box();
                        } catch (e) {
                        }
			} 
                } else {
                        if (data.username && document.add_comment_form.name){ // pre populate the form for the user
				document.add_comment_form.name.value = data.username;
			}
                        if (data.email && document.add_comment_form.email){
                                document.add_comment_form.email.value = data.email;
			}
			$('add_comment').show();
                        try {
                                show_comment_box();
                        } catch (e) {
                        }
		}
        },
        finish: function(o){
                // should probably check return first
	        $('add_comment').hide();
                try {
			hide_comment_box(); // cb in case we need to do other things when this event happens
                } catch (e) {
                }
	        alert('Thank you for your comment!  Your comment will be reviewed and once approved will be made visible.');
        }
}


// ajax calls
function add_comment(form, media_id){
                var media_type = 'video';
                if (!form.comment){
                        alert('No "comment" field in form');
                        return false;
                }
	        var comment = form.comment.value;

		if (!form.name){
			alert('No "name" field in form');
			return false;
		}
	        var name = form.name.value;

		if (form.email){
	        	var email = form.email.value;
		}

                if (!form.media_id && !media_id){
                        alert('No "media_id" field in form');
                        return false;
                }

		if (!media_id){
		        var media_id = form.media_id.value;
		}

		if (!commenter){
			var commenter = new vmix_commenter();
		}
		commenter.comment({'media_type':media_type, 'media_id':media_id, 'email':email, 'name':name, 'comment':comment});
		vmix_hide('add_comment');
}



