var Comments = function(options) {
    this.options = {
            id: 0,
			page: 0,
			object: null,
			name: null,
			parentid: 0,
			folder: './'
        };
		
	this.options = $.extend(this.options, options || {});  	

	this.flag= function(id) {
		var flagged = $("#"+this.options.name + 'flagme' + id);
		var data =  'do=flag&id=' + id + this.cacheDefeat();
		$.ajax({
		   type: "GET",
		   url: this.options.folder + 'backend.php',
		   data: data,
		   success: function(msg){
		     flagged.html('flagged');
		   }
		 });
	};
	
	this.openFlagged= function(obj, id) {
		var obj = obj;
		var data = 'do=getcomment&post=' + id + '&id=' + this.options.id + this.cacheDefeat();
		$.ajax({
		   type: "GET",
		   url: this.options.folder + 'backend.php',
		   data: data,
		   success: function(msg){
		     obj.after('<div>'+msg+'</div>');
			 obj.remove();
		   }
		 });
	};

	this.checkTitle= function() {
		var form = $("#"+this.options.name + 'form').getChildren('div')[0].getChildren('form')[0].each(function (ele) {
			if (ele.name == 'title' && ele.value == 'Title Here') {
				ele.val('');
			}
		});
	};
	
	this.clearText= function(object) {
		if (object.name == 'title' || object.name == 'message') {
			if (object.value == 'Title Here' || object.value == 'Message Here') {
				object.value = '';
			}
		}
	};
	
	this.setReply= function(id) {
		this.options.parentid = id;
	};
	
	this.thumbsUp= function(id) {
		var a = this;
		var data = 'do=thumbs&id=' + id + '&up=true' + this.cacheDefeat();
		$.ajax({
		   type: "GET",
		   url: this.options.folder + 'backend.php',
		   data: data,
		   success: function(msg){
				a.updateThumbs(id);		   
			}
		 });
	};
	
	this.thumbsDown= function(id) {
		var a = this;
		var data = 'do=thumbs&id=' + id + '&up=false' + this.cacheDefeat();
		$.ajax({
		   type: "GET",
		   url: this.options.folder + 'backend.php',
		   data: data,
		   success: function(text){
				if (text == "done")
					a.updateThumbs(id);		   
			}
		 });
	};
	
	this.updateThumbs= function(messageid) {
		var up = $("#"+ this.options.name + 'up' + messageid);
		var down = $("#"+ this.options.name + 'down' + messageid);
		var upndown = $("#"+ this.options.name + 'updown' + messageid);
		var data = 'do=getthumbs&id=' + messageid + this.cacheDefeat();
		$.ajax({
		   type: "GET",
		   url: this.options.folder + 'backend.php',
		   data: data,
		   success: function(text){
				var updown = text.split(";");
				if (up != null)
					up.html(updown[0]);
				if (down != null)
					down.html(updown[1]);
				if (updown != null)
					upndown.html(updown[2]);
			}
		 });
	};	
	
	this.respond= function() {
		var a = this;
		var data = 'do=write&id=' + this.options.id + '&parentid=' + this.options.parentid + '&message=' + $("#"+this.options.name + "formsmessage").val().replace(/&/g,"%26")+ '&title=' + $("#"+this.options.name + "formstitle").val().replace(/&/g,"%26") + this.cacheDefeat();
		var toReplace = $("div ." + this.options.name + "forms")[0];
		$.ajax({
		   type: "POST",
		   url: this.options.folder + 'backend.php',
		   data: data,
		   success: function(text){
				if (text == "") {
					a.getComments(a.page);
				} else {
					form.getChildren('div')[0].setStyle('display', 'block');
					form.getChildren('div')[0].html(text);
				}
				a.insertReply();
			}
		 });
	};
	
	
	this.insertReply= function(mid) {
		var name = this.options.name;
		var mid = mid;
		this.options.parentid = mid;
		var data = 'objid=' + name + '&do=getform&id=' + this.options.id + this.cacheDefeat();
		$.ajax({
		   type: "GET",
		   url: this.options.folder + 'backend.php',
		   data: data,
		   success: function(text){
   				$("div ." + name + "forms").each(function (i,ele) {
					$(ele).html('');
				});
				$("#" + name + 'form').html('');
				if (mid == null)
					$("#" + name + 'form').html(text);
				else
					$("#" + name + 'form' + mid).html(text);
			}
		 });
	};
	
	this.getComments =  function(page) {
		this.options.page = page;
		var object = this.options.object;
		var pager = this.options.page;
		var data = 'objid=' + this.options.name + '&do=read&thumbs=' + $("#"+this.options.name + "sel").val() + '&id=' + this.options.id + '&page=' + this.options.page + this.cacheDefeat();
		$.ajax({
		   type: "GET",
		   url: this.options.folder + 'backend.php',
		   data: data,
		   success: function(msg){
		     object.html(msg);
			 if (pager > 0) { 
				 comtop = $('#comtop');
				 $('html,body').animate({
					//scrollTop: '+=' + $('#comtop').offset().top + 'px'
					scrollTop: comtop.offset().top
					//scrollTop : 0
				 }, 'slow');
			 }
		   }
		 });
	};	
	
	this.showComments= function(id, object) {
		var data = 'objid=' + this.options.name + '&do=comments&id=' + this.options.id + '&pid=' + id + this.cacheDefeat();
		alert(data);	
		$.ajax({
		   type: "GET",
		   url: this.options.folder + 'backend.php',
		   data: data,
		   success: function(text){
		     object.html(text);
		   }
		 });	
	};

		
	
	this.cacheDefeat = function() {
		var myDate = new Date();
		return '&r=' + myDate.getTime();
	};

	this.getComments(this.options.page);
}

function counterUpdate(opt_countedTextBox, opt_countBody, opt_maxSize) {
        var countedTextBox = opt_countedTextBox ? opt_countedTextBox : "counttxt";
        var countBody = opt_countBody ? opt_countBody : "countBody";
        var maxSize = opt_maxSize ? opt_maxSize : 1000;

        var field = document.getElementById(countedTextBox);

        if (field && field.value.length >= maxSize) {
                field.value = field.value.substring(0, maxSize);
        }
        var txtField = document.getElementById(countBody);
                if (txtField) { 
                txtField.innerHTML = maxSize - field.value.length;
        }
}
