$(document).ready(function () {
	
		slide(".categories", 10, 0, 150, .8);
	
        $('.boardDisplay').each(function () {
            var distance = 10;
            var time = 250;
            var hideDelay = 500;

            var hideDelayTimer = null;

            var beingShown = false;
            var shown = false;
			
			
		           
            var trigger = $('.trigger', this);
            var info = $('.links', this).css('opacity', 0);
			var rateSuccessEl = $('.rateSuccess', this).css('opacity', 0);
			var rateFailEl = $('.rateFail', this).css('opacity', 0);

            var ratingDown = $('.ratingDown', this);
            var ratingUp   = $('.ratingUp', this);
            var boardDown  = $('.boardDown',this);
            var boardUp    = $('.boardUp',this);
            var id         = this.id.replace('b','');
            var voted      = 0;
			
			
  			$('.rateUpGx').hover(function () {
    		this.src = '/assets/images/thumb_up_over.png';
 			 }, function () {
   			 this.src = '/assets/images/thumb_up.png';
 		 	});
			
			$('.rateDownGx').hover(function () {
    		this.src = '/assets/images/thumb_down_over.png';
 			 }, function () {
   			 this.src = '/assets/images/thumb_down.png';
 		 	});
			
            
            $([ratingDown.get(0)]).click(function() {
            	if(voted==0) {
            		rateMe('negative');
            	} else {
            		rateDupe();
            	}
            });

            $([ratingUp.get(0)]).click(function() {
            	if(voted==0) {
            		rateMe('positive');
            	} else {
            		rateDupe();
            	}
            });
            
            function rateMe(type) {
            	$.getJSON("/board/rate?id="+id+"&type="+type, function(data){
            		if(data) {
						rateSuccess();
						voted = 1;
						boardUp.html(data.up);
						boardDown.html(data.down);
            		} else {
            			rateDupe();
            		}
            	});    
            }
			
			function rateSuccess(){
				rateSuccessEl.css({
                        top: 170,
                        left: 480,
                        display: 'block'
                    }).animate({
                        top: '-=' + distance + 'px',
                        opacity: 1
                    }, 250)
					.animate({
                        top: '-=' + distance + 'px',
                        opacity: 0
                    }, 500);
				
			}
			
			function rateDupe(){
				rateFailEl.css({
                        top: 170,
                        left: 480,
                        display: 'block'
                    }).animate({
                        top: '-=' + distance + 'px',
                        opacity: 1
                    }, 250)
					.animate({
                        top: '-=' + distance + 'px',
                        opacity: 0
                    }, 500);
				
			}
			
			
            $([trigger.get(0), info.get(0)]).mouseover(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                if (beingShown || shown) {
                    // don't trigger the animation again
                    return;
                } else {
                    // reset position of info box
                    beingShown = true;
                    info.css({
                        top: 200,
                        left: 225,
                        display: 'block'
                    }).animate({
                        top: '-=' + distance + 'px',
                        opacity: 1
                    }, time, 'swing', function() {
                        beingShown = false;
                        shown = true;
                    });
                }

                return false;
            }).mouseout(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                hideDelayTimer = setTimeout(function () {
                    hideDelayTimer = null;
                    info.animate({
                        top: '-=' + distance + 'px',
                        opacity: 0
                    }, time, 'swing', function () {
                        shown = false;
                        info.css('display', 'none');
                    });

                }, hideDelay);

                return false;
            });
        });
    });


function slide(navigation_id, pad_out, pad_in, time, multiplier) {  
    // creates the target paths  
    var list_elements = navigation_id + " li.";  
    var link_elements = list_elements + " a";  
      
    // creates the hover-slide effect for all link elements           
    $(link_elements).each(function(i)  
    {  
        $(this).hover(  
        function()  
        {  
            $(this).animate({ paddingLeft: pad_out }, 150);  
        },        
        function()  
        {  
            $(this).animate({ paddingLeft: pad_in }, 150);  
        });  
    });  
}  
