(function(){
    Event = YAHOO.util.Event;
    Event.onDOMReady(function(){
        // Only execute if the featured area is shown
        if (!guid) 
            return;
        var voter = new YAHOO.util.Element("vote-area");
        var links = voter.getElementsByTagName("A");
        var value = 0;
        
        // Iterate through all the voting links
        // and assign their value
        for (var i = 0; i < links.length; i++) {
            links[i].voteValue = i + 1;
            
        }
        
        Event.on("vote-area", 'mouseover', function(e){
            if (e) 
                Event.stopEvent(e);
            var target = Event.getTarget(e);
            if (checkTarget(target, 'IMG')) {
                target = target.parentNode;
                if (checkTarget(target, 'A')) {
                    val = target.voteValue;
                    displayValue(val);
                }
            }
        });
        Event.on("vote-area", 'click', function(e){
            if (e) 
                Event.stopEvent(e);
            var target = Event.getTarget(e);
            if (checkTarget(target, 'IMG')) {
                target = target.parentNode;
                if (checkTarget(target, 'A')) {
                    val = target.voteValue;
                    vote(val);
                }
            }
        });
        Event.on("vote-area", 'mouseout', function(e){
            displayValue(value);
        });
        // See if our target element is of type 'el'
        function checkTarget(obj, el){
            if (obj == null) 
                return false;
            if (obj.nodeName == el) {
                return true;
            }
            else {
                return false;
            }
        }
        function displayValue(val){
            document.getElementById('vote-area').className = 'vote'; // Revert back to the base class name
            voter.addClass('vote' + val); // Now add the voted value class
        }
		function displayAvgValue(val) {
			var el = YAHOO.util.Dom.getElementsByClassName('vote-display','span')[0];
			el.className = 'vote-display';
			YAHOO.util.Dom.addClass(el,'vote' + val);			
		}
        function vote(val){
            var url = '/services/ajaxcalls.asmx/SubmitVote';
            var callback = {
                success: function(o){
					displayAvgValue(o.responseXML.firstChild.firstChild.nodeValue);
                }                
            }
			YAHOO.util.Connect.asyncRequest('POST', url, callback, 'contentGuid=' + guid + '&value=' + val); 
            value = val;
        }
    });
    
    
    
})();

