﻿function Slider(sliderId, handleId, minValue, maxValue)
{
    this.slider = document.getElementById(sliderId);
    this.handle = document.getElementById(handleId);
    this.handle.sliderCtrl = this;
    this.handle.onmousedown = this.handleMouseDown;
    this.position = 0;
    this.minPos = 0;
    this.maxPos = this.slider.offsetWidth - this.handle.offsetWidth;
    this.minValue = minValue;
    this.maxValue = maxValue; 
}

Slider.prototype.handleMouseDown = function(evt)
{
    if (this.sliderCtrl)
        return this.sliderCtrl.handleMouseDown(evt ? evt : event);
    
    Slider.activeSlider = this;
    if (window.attachEvent)
    {
        document.attachEvent("onmousemove", this.handleMouseMove);
        document.attachEvent("onmouseup", this.handleMouseUp);
    }
    else
    {
        window.addEventListener("mousemove", this.handleMouseMove, false)
        window.addEventListener("mouseup", this.handleMouseUp, false);
    }
    this.dragX = evt.screenX;
    this.dragY = evt.screenY;
    this.dragPos = this.handle.offsetLeft;
    return false;
};

Slider.prototype.handleMouseUp = function(evt)    
{
    if (!(this instanceof Slider))
        return Slider.activeSlider.handleMouseUp(evt);
        
    if (window.attachEvent)
    {
        document.detachEvent("onmousemove", this.handleMouseMove);
        document.detachEvent("onmouseup", this.handleMouseUp);
    }
    else
    {
        window.removeEventListener("mousemove", this.handleMouseMove, false)
        window.removeEventListener("mouseup", this.handleMouseUp, false);
    }        
    return false;
};

Slider.prototype.handleMouseMove = function(evt)
{
    if (!(this instanceof Slider))
        return Slider.activeSlider.handleMouseMove(evt);
    
    var maxPos = this.slider.offsetWidth - this.handle.offsetWidth;
    var position = Math.max(-10, Math.min(maxPos, this.dragPos + evt.screenX - this.dragX)+5);
    this.value = Math.round(Math.min(this.maxValue, Math.max(this.minValue, this.minValue + position / maxPos * (this.maxValue - this.minValue))));
    this.handle.style.left = ((this.value * 28.5)-4) + "px";
    if (this.onchange) this.onchange(this, this.value);
    return false;
};

function init()
    {
        // Create the slider, give it the IDs of the two divs and set min and max value
        var slider = new Slider("slider", "handle", 0, 10);
        
        // Default to 5
        var slider2;
        slider2 = document.getElementById("handle");
        update(slider2,5);
        
        // Attach onchange event
        slider.onchange = update;
        
    }
    
function update(slider, value)
    {
        document.getElementById("vote").value = value;
    }
    
function clickslider(e) 
    {
        var x=0;
        var slider = document.getElementById("slider");
        
        e=e||window.event;
        
       // alert(e.target.id);
        
        if (e.target.id == "slider")
        {        
            if(e.layerX) {
                x=e.layerX;
                //alert(x);
            }
            else if(typeof(e.clientX)=='number') {
                var dE=document.documentElement;
                x=e.clientX+document.body.scrollLeft+(dE?dE.scrollLeft:0);
            }
            var handle;
            //alert(x);
            handle = document.getElementById("handle");
            //x = Math.round((x-160)/300*10);
            x = Math.round(x/300*10);
            //alert(x);
            handle.style.left = ((x*28.5)-4) + "px";
            update(handle,x);
            return x;
        }
        
    }
    
// End of Slider Code

//Start of Voting Ajax    

//New Functions to solve auto-voting bug

    function ajaxShowFunction(){
    
    recordstat('SHOWFUNCTION','LOBORDOG')
	var ajaxRequest;  // The variable that makes Ajax possible!
	var storyId;
	var vote;
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Voting requires an Ajax compatible browser");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	
	var ajaxDisplay = document.getElementById('ajaxDiv');
	
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){				

			ajaxDisplay.innerHTML = ajaxRequest.responseText;

		}
	}


    storyId = document.getElementById('storyId').value;
	vote = document.getElementById('vote').value;	
		
	var queryString = "?storyId=" + storyId + "&vote=" + vote;
	           //alert( queryString );  
	ajaxRequest.open("GET", "/include/lobordog/showlobordog.asp" + queryString, true);
	ajaxRequest.send(null); 

}

    function ajaxVoteFunction(){
    recordstat('VOTEFUNCTION','LOBORDOG')
	var ajaxRequest;  // The variable that makes Ajax possible!
	var storyId;
	var vote;
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Voting requires an Ajax compatible browser");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	
	var ajaxDisplay = document.getElementById('ajaxDiv');
	
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){				

			ajaxDisplay.innerHTML = ajaxRequest.responseText;

		}
	}


    storyId = document.getElementById('storyId').value;
	vote = document.getElementById('vote').value;	
		
	var queryString = "?storyId=" + storyId + "&vote=" + vote;
	           //alert( queryString );  
	ajaxRequest.open("GET", "/include/lobordog/votelobordog.asp" + queryString, true);
	ajaxRequest.send(null); 

}

//End of Voting Ajax

function gotime() {
    //alert('gotime');
    document.getElementById("LODAverageValue").style.color = "#d8dde0";
}
