﻿jQuery.voteslider = function(input, maxvalue, txtVoteCount, drpVoteDirection) {



    var debuginfo = $("#DebugInfo");
    var debuginfo2 = $("#DebugInfo2");
    
    var SliderPointer = $(input).children(".SliderPointer");
    var PositiveIndicator = $(input).children(".PositiveIndicator");
    var NegativeIndicator = $(input).children(".NegativeIndicator");
    var VoteCountIndicator = $(SliderPointer).children(".VoteCountIndicator");
    
    var ContainerOffset = $(input).offset();
    var SliderPointerwidth = $(SliderPointer).width();
    
    var VoteCountTextBox = $("#"+txtVoteCount);
    var VoteDirectionDropdown = $("#"+drpVoteDirection);
    
    var SelectedVoteCount;
    
    var MinLeftPosition = ($(input).offset().left - (SliderPointer.width() / 2))
    var MaxLeftPosition = (($(input).offset().left + $(input).width()) - (SliderPointer.width() / 2))
    
    var MOUSE_DOWN = null;

    SliderPointer.mousedown(function (e) {				
        $(document.body).focus();
		MOUSE_DOWN = true;
		return false;
    })
    
    $(input).click(function (e) {
		var SliderLeftPosition = e.pageX - (SliderPointer.width() / 2);

        SelectedVoteCount = setSliderBySliderPointer(SliderLeftPosition);
        
        VoteDirectionDropdown.val(SelectedVoteCount >=0 ? 1 : -1);
        VoteCountTextBox.val(SelectedVoteCount >=0 ? SelectedVoteCount : SelectedVoteCount *-1);

		return false;
    });
    
    $(document).mousemove(function(e) 
	{
		if (MOUSE_DOWN) 
		{			
			var SliderLeftPosition = Math.round(e.pageX - (SliderPointer.width() / 2));
            SelectedVoteCount = setSliderBySliderPointer(SliderLeftPosition);
            VoteDirectionDropdown.val(SelectedVoteCount >=0 ? 1 : -1);
            VoteCountTextBox.val(SelectedVoteCount >=0 ? SelectedVoteCount : SelectedVoteCount *-1);
			return false;
		}
	}).mouseup(function(e) {
		if (MOUSE_DOWN) {
			MOUSE_DOWN = false;
		}
	});
	
	VoteDirectionDropdown.change(function () {
	    SelectedVoteCount = (SelectedVoteCount >=0 ? SelectedVoteCount : SelectedVoteCount *-1) * VoteDirectionDropdown.val();
	    setSliderByValue();
	});
	
	VoteCountTextBox.keyup(function () {
	    SelectedVoteCount = VoteCountTextBox.val() * VoteDirectionDropdown.val();
	    setSliderByValue();
	});
	
	function setSliderByValue()
	{
	    var newposition = ($(input).width() / (maxvalue * 2) * (SelectedVoteCount))
	    newposition = newposition + $(input).offset().left + ($(input).width() / 2) - (SliderPointer.width() / 2)
	    SelectedVoteCount = setSliderBySliderPointer(newposition);
	}
	
	function setSliderBySliderPointer(SliderPointerLeftPosition)
	{		
		if (SliderPointerLeftPosition <= MinLeftPosition) {
		    SliderPointerLeftPosition = MinLeftPosition;
		} else if (SliderPointerLeftPosition >= MaxLeftPosition) {
		    SliderPointerLeftPosition = MaxLeftPosition;
		} 
		
		//deze code werkt niet in IE, vervangen door de volgende regel
        //SliderPointer.offset({ left:  SliderPointerLeftPosition });
        SliderPointer.css('left',(SliderPointerLeftPosition - ContainerOffset.left) + 'px')

        var RelativeLeft = SliderPointerLeftPosition - $(input).offset().left + SliderPointer.width() / 2;
        var RelativeValue = RelativeLeft - ($(input).width() / 2)
        
        var SelectedVoteCount = ((RelativeValue) * maxvalue) / ($(input).width() / 2);
        var output = Math.floor(SelectedVoteCount)
        SelectedVoteCount = output < 0 ? output * -1 : output;

        PositiveIndicator.css('width', '0px')
        NegativeIndicator.css('width', '0px')
        if (RelativeLeft > ($(input).width() / 2)) {
            PositiveIndicator.css('width', RelativeValue + 'px')
        }
        else if (RelativeLeft < ($(input).width() / 2)) {
            NegativeIndicator.css('width', (RelativeValue * -1)+ 'px')
        }
        
        if (SelectedVoteCount != 0) {
            VoteCountIndicator.html(SelectedVoteCount);
            VoteCountIndicator.show();
        }
        else {
            VoteCountIndicator.hide();
        }
	    return output;
	}
}

jQuery.fn.voteslider = function(maxvalue, txtVoteCount, drpVoteDirection) {
	this.each(function() {
		var input = this;
		new jQuery.voteslider(input, maxvalue, txtVoteCount, drpVoteDirection);
	});

	return this;
}


