PictureThis.Rating = function()
{
	
	var YUE = YAHOO.util.Event;
    var PTA = PictureThis.Ajax;
	
	return {
		
		init: function(root_id)
		{
			var root = document.getElementById(root_id);
			this.root = root.getElementsByTagName('ul')[0];
			this.lis = this.root.getElementsByTagName('li');

			this.attachEvents();
		},
		
		attachEvents: function()
		{
			for(var i=0; i<this.lis.length; i++)
			{
				YUE.on(this.lis[i], 'mouseover', this.handleOver, this);
				YUE.on(this.lis[i], 'mouseout', this.handleOut, this);
                YUE.on(this.lis[i], 'click', this.handleDown, this);
			}
		},
		
		getTarget: function(t)
		{
			if(t.nodeName != 'LI')
			{
				while(t.nodeName != 'LI')
				{
					t = t.parentNode;
				}
			}

			return t;
		},
		
		handleOver: function(e, that)
		{
			var li = that.getTarget(YUE.getTarget(e));
			var num = li.className.split('_')[1];
			var images = li.parentNode.getElementsByTagName('img');

			for(var i=0; i<num; i++)
			{
				images[i].src = images[i].src.replace(/_off/, '_on');
			}
		},
		
		handleOut: function(e, that)
		{
			var li = that.getTarget(YUE.getTarget(e));
			var num = li.className.split('_')[1];
			var images = li.parentNode.getElementsByTagName('img');

			for(var i=0; i<num; i++)
			{
				images[i].src = images[i].src.replace(/_on/, '_off');
			}
		},
        
        handleSuccess: function(ob)
        {
            eval("r = " + ob.responseText);
            if(r != null && r.success == true){  
                var rtype   = ob.argument.rtype;
                var el      = ob.argument.that;
                var rating  = r.rating;
                var rcount  = r.rcount;
                var div     = document.getElementById("rating_"+rtype);
                
                // empty the current div
                div.innerHTML = "";
                PTA.createChildElement(div,"p",{style:"color:#666;"},"You have rated this photo");
                var h4 = PTA.createChildElement(div,"h4");
                var ul = PTA.createChildElement(div,"ul",{c:"star-rating clearfix"});
                
                // is it technical or creative?
                if(rtype == "technical"){
                    PTA.createChildElement(h4,"img",{src:"/images/copy/technical_skill.gif",alt:"Average Technical Rating"});
                }else{
                    PTA.createChildElement(h4,"img",{src:"/images/copy/creativity.gif",alt:"Average Creativity Rating"});
                }
                var li = PTA.createChildElement(ul,"li");
                PTA.createChildElement(li,"img",{src:"/images/rating_"+rating+".gif",alt:"Rating " + rating});
                var p = PTA.createChildElement(ul,"p",{style:"color: #666; display: block; float: left; margin: 9px 0px 0 0"});
                var span = PTA.createChildElement(p,"span",{style:"color: #333; font-weight: bold;"},rcount+"&nbsp;");
                // dirty dirty innerHTML, oh well
                p.innerHTML += "people have rated this photo.";
            }else{
                PictureThis.Rating.handleFailure(ob);
            }
        },
        
        handleFailure: function(ob)
        {
            var rtype   = ob.argument.rtype;
            var div = document.getElementById("rating_"+rtype);
            PTA.createChildElement(div,"p",{style:"font-weight:bold"},"Error saving rating, please try later");
        },
        
        handleDown: function(e, that)
        {
            YUE.preventDefault(e);
            YUE.stopPropagation(e);
            var li = that.getTarget(YUE.getTarget(e));
			var num = li.className.split('_')[1];
            // work out the photo id and rating type from the url
            var urlSplit = li.firstChild.href.split("/");
            var rtype = urlSplit[urlSplit.length-2];
            var photoID = urlSplit[urlSplit.length-4];
            var sUrl = "/aj?mode=rate&photoID="+photoID+"&rtype="+rtype+"&rating="+num;
            var callback = {
                success:PictureThis.Rating.handleSuccess,
                failure:PictureThis.Rating.handleFailure,
                timeout:5000,
                argument:{
                    rtype:rtype,
                    that:that
                    
                }
            };
            var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null);
        }
	}
	
}();
YAHOO.util.Event.onAvailable('footer', PictureThis.Rating.init, 'rating_technical', PictureThis.Rating);
YAHOO.util.Event.onAvailable('footer', PictureThis.Rating.init, 'rating_creative', PictureThis.Rating);
