PictureThis.Comments = function()
{
	
	var YUE = YAHOO.util.Event;
	var YUD = YAHOO.util.Dom;
    var PTA = PictureThis.Ajax;
	
	return {
		
		init: function(root_id)
		{
			var root = document.getElementById(root_id);
			var controlLinks = YUD.getElementsByClassName('comment_reply_control', 'a', root);

			for(var i=0; i<controlLinks.length; i++)
			{
				YUE.on(controlLinks[i], 'click', this.showForm);
			}
			
			// init the rating stuff
			this.thumbsUp = YAHOO.util.Dom.getElementsByClassName('thumb_up','li',root);
			this.thumbsDown = YAHOO.util.Dom.getElementsByClassName('thumb_down','li',root);
			this.flags = YAHOO.util.Dom.getElementsByClassName('report','li',root);
			this.attachEvents();
		},
		
		attachEvents: function()
		{
			for(var i=0; i<this.thumbsUp.length; i++)
			{
                YUE.on(this.thumbsUp[i], 'click', this.handleClick, this);
			}
			for(var i=0; i<this.thumbsDown.length; i++)
			{
                YUE.on(this.thumbsDown[i], 'click', this.handleClick, this);
			}
			for(var i=0; i<this.flags.length; i++)
			{
                YUE.on(this.flags[i], 'click', this.handleClick, this);
			}
		},
		
		getTarget: function(t)
		{
			if(t.nodeName != 'LI')
			{
				while(t.nodeName != 'LI')
				{
					t = t.parentNode;
				}
			}

			return t;
		},
		
		
		handleSuccess: function(ob)
        {
            eval("r = " + ob.responseText);
            if(r != null && r.success == true){
                var rtype   = ob.argument.rtype;
                var scope   = ob.argument.that;
				var li 		= scope.getTarget(YUE.getTarget(ob.argument.e));
				var ul 		= li.parentNode;
				var div 	= ul.parentNode;
                var pcount  = String(r.pcount);
                var rcount  = String(r.rcount);

				if(rtype != "report"){
					// kill the thumbs			
					var thumbsUp = YAHOO.util.Dom.getElementsByClassName('thumb_up','li',ul);
					var thumbsDown = YAHOO.util.Dom.getElementsByClassName('thumb_down','li',ul);
					
					ul.removeChild(thumbsUp[0]);
					ul.removeChild(thumbsDown[0]);
					
					// find the p tags
					var ps = div.getElementsByTagName("p");
					ps[1].parentNode.removeChild(ps[1]);
					ps[0].parentNode.removeChild(ps[0]);
					
					var p1 = PTA.createChildElement(ul.parentNode,"p",null,"Comment rating:");
					if(rcount>0){
						PTA.createChildElement(p1,"span",{c:"positive"},"+"+rcount);
					}else if(rcount<0){
						PTA.createChildElement(p1,"span",{c:"negative"},""+rcount);
					}else{
						PTA.createChildElement(p1,"span",{c:"neutral"},rcount);
					}
					
					var p2 = PTA.createChildElement(ul.parentNode,"p");
					var span = PTA.createChildElement(p2,"strong",null,pcount);
					// dirty dirty innerHTML, oh well
					if(pcount != 1){
						p2.innerHTML += " people have rated this comment.";
					}else{
						p2.innerHTML += " person has rated this comment.";
					}
				}else{
					var lis = YAHOO.util.Dom.getElementsByClassName('report','li',ul);
					ul.removeChild(lis[0]);
				}
            }else{
                PictureThis.Comments.handleFailure(ob);
            }
            
        },
        
        handleFailure: function(ob)
        {
            var rtype   = ob.argument.rtype;
			var scope   = ob.argument.that;
			var li 		= scope.getTarget(YUE.getTarget(ob.argument.e));
			var ul 		= li.parentNode;
            var div 	= ul.parentNode;
            PTA.createChildElement(div,"p",{style:"font-weight:bold"},"Error, please try later");
        },
		
		
		handleClick: function(e, that)
		{
			YUE.preventDefault(e);
            YUE.stopPropagation(e);
            var li = that.getTarget(YUE.getTarget(e));
			var rtype = li.className;
			// get the url that would have been used
			for(var i = 0; i<li.childNodes.length; i++){
				if(li.childNodes[i].href != undefined) var href = li.childNodes[i].href;
			}
			var urlSplit = href.split("/");
			if(rtype != "report"){
				var cID = urlSplit[urlSplit.length-3];
			}else{
				var cID = urlSplit[urlSplit.length-2];
                if(!confirm("Are you sure you want to report this comment?")){
                    return null;
                }
			}
			
			var sUrl = "/aj?mode=cRate&cID="+cID+"&rtype="+rtype;
            var callback = {
                success:PictureThis.Comments.handleSuccess,
                failure:PictureThis.Comments.handleFailure,
                timeout:5000,
                argument:{
					e:e,
                    rtype:rtype,
                    that:that
                }
            };
            var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null);
		},
		
		
		
		showForm: function(e)
		{
			
			var forms = YUD.getElementsByClassName('comment_reply_form box', 'div');
			var href = this.href.split('#');

			for(var i=0; i<forms.length; i++)
			{
				if(forms[i].id == href[1])
				{
					forms[i].style.display = 'block';
				}
				else
				{
					forms[i].style.display = 'none';
				}
			}
		}
		
	}
	
}();
YAHOO.util.Event.onAvailable('footer', PictureThis.Comments.init, 'module_comments', PictureThis.Comments);