/*
 * 
 * 
 */

var objReview;
var Reviews = Class.create();
window.Reviews=Reviews;
window.Reviews.prototype = {
	initialize: function(opts) {
		if (typeof(opts) != 'object') return false;
		this.appState = opts.appState;		
		this.loadedBody = false;		
		this.cid = opts.cid;
		this.uid = opts.uid;
		this.type = opts.uid;
		this.onlyUser = (this.redirect = opts.redirect) ? true : false;
		this.mine = opts.mine ? true : false;
		this.reviewType = opts.reviewType ? '&UserReviews_reviewType='+opts.reviewType : '';
		this.count = opts.count ? opts.count : 0;
		if (typeof(opts.patterns) != 'object') {
			this.patterns = {
				show: 'View reviews (%c)',
				hide: 'Hide reviews (%c)',
				none: 'No reviews',
				load: 'Loading data...',
				up:   'Updating reviews...'
			};
		} else {
			this.patterns = opts.patterns;
		}
		
		this.check();
	},
	
	check: function() {
		this.updateBtnText(false);
		if (this.count>0) {
			$('reviews_'+this.cid).className = 'reviewsBox';
		} else {
			$('reviews_'+this.cid).className = 'reviewsBox reviewsNull';
			$('reviews_body_'+this.cid).hide();
		}
		if (!this.mine) {
			$('reviews_btn_addform_'+this.cid).show();
		} else {
			$('reviews_btn_addform_'+this.cid).hide();
		}
		$('reviews_addform_'+this.cid).hide();
	},
	
	guest: function() {
		if (this.onlyUser && this.uid == -1) {
			window.location.href = this.redirect;
			return true;
		}
		return false;
	},
	
	toggle: function(mode, suffix) {
		if (mode == 'body' && this.count<1 || mode != 'body' && this.guest()) {
			return 'closed';
		}
		
		var sfx = suffix ? '_'+this.uid : '';
		var id = 'reviews_' + mode + '_' + this.cid + sfx;
		if ($(id).offsetHeight) {
			if (mode == 'body') {
				this.updateBtnText(false);
			} else if (mode == 'editform') {
				$('reviews_text_' + this.cid + sfx).show();
			}
			$(id).hide();
			return 'closed';
		} else {
			if (mode == 'body') {
				if (!this.loadedBody) {
					this.get();
				}
				this.updateBtnText(true);
			} else if (mode == 'editform') {
				$('reviews_text_' + this.cid + sfx).hide();
			}
			$(id).show();
			return 'opened';
		}
	},
	
	updateBtnText: function(trigger) {
		var btn = $('reviews_btn_body_' + this.cid);
		var visible = typeof(trigger == 'boolean') ? trigger : ('reviews_body_' + this.cid).offsetHeight;
		var text = this.count<1 ? this.patterns.none : (visible ? this.patterns.hide.replace('%c',this.count) : this.patterns.show.replace('%c',this.count));
		btn.innerHTML = text;
	},
	
	get: function() {
		$('reviews_body_' + this.cid).innerHTML = this.patterns.load;
		this.ajax({postVars: 'UserReviews_action=reviewGetList',
							onSuccess: function(t) {
								$('reviews_body_' + objReview.cid).innerHTML = t.responseText;
								objReview.loadedBody = true;
							}});
	},
	
	add: function() {
		if (!(text = $('reviews_add_' + this.cid).value.escapeHTML())) {
			$('reviews_btn_addform_' + this.cid).className = this.toggle('addform');
			return;
		}		
		this.ajax({postVars: 'UserReviews_action=reviewAdd&UserReviews_text=' + text,
							onSuccess: function(t) {
								objReview.count++;
								objReview.mine = true;
			  			  objReview.check();
								objReview.get();
					    }});
	},
	
	edit: function() {
		if (!(text = $('reviews_edit_' + this.cid + '_' + this.uid).value.escapeHTML())) {
			this.toggle('editform',1);
			return;
		}
		$('reviews_text_' + this.cid + '_' + this.uid).innerHTML = this.patterns.up;
		this.toggle('editform',1);
		this.ajax({postVars: 'UserReviews_action=reviewUpdate&UserReviews_text=' + text,
							onSuccess: function(t) {
								$('reviews_text_' + objReview.cid + '_' + objReview.uid).innerHTML = text;
					    }});
	},
	
	remove: function() {
		this.ajax({postVars: 'UserReviews_action=reviewDel',
							onSuccess: function(t) {
								objReview.count--;
								objReview.mine = false;
			  			  objReview.check();
								objReview.get();
					    }});
	},
	
	vote: function(review, vote) {
		if (this.guest()) {
			return;
		}
		this.lastReview = review;
		this.ajax({postVars: 'UserReviews_action=reviewVote&UserReviews_reviewId=' + review + '&UserReviews_voteType=' + vote,
							onSuccess: function(t) {
								$('reviews_actions_' + objReview.lastReview).innerHTML = t.responseText;
					    }});
	},
	
	ajax: function(opts) {
		objReview = this;
		opts.postBody = this.appState+'mode_ajax=true&UserReviews_cid=' + this.cid + this.reviewType + '&' + opts.postVars ;
		opts.method = 'post';
		opts.onFailure = function(t) { };//alert('Error ' + t.status + ' -- ' + t.statusText); };
		request = new Ajax.Request('userreviews.php?', opts);
	}
	
}
