// JavaScript Document

// Global variables
var where_the_bar_is;
var opbar_visible = true;
var opbar_active = true;
var opbar_handle;
var img_bar;
var img_cursor;
var dragging = false;
var startXpos;
var curXpos;
var cursorPos = 0.5;
var img_cursor_X;
var img_bar_X = 20;
var green_limit;
var red_limit;
var imgBarWidth = 400;
var imgBarHeight = 26;
var imgCursorWidth = 14;
var imgCursorHeight = 54;
var Message;
var green_l;
var red_l;
var opbar_display = "debate";
var curarg = -1;
var explarea;

///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
function findPos(obj) { // Thanks QuirksMode.org!
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function CreateOpBar(name_of_div_to_attach_to, greenLabel, redLabel, visible, initial_value)
{
	where_the_bar_is = document.getElementById(name_of_div_to_attach_to);
	
	// Creating the bar div container
	opbar_handle = document.createElement("div");
	opbar_handle.id = "voting";
		
	opbar_wrapper = document.createElement("div");
	opbar_wrapper.id = "opbar_wrapper";
	
	
	// Attaching the opbar
	where_the_bar_is.appendChild(opbar_handle);
	
	// Title 
	opbar_handle.appendChild(opbar_wrapper);
	
	// Labels
	green_l = document.createElement("span");
	green_l.innerHTML = greenLabel;
	//opbar_wrapper.appendChild(green_l);
	//green_l.className= "ob_label";
	
	// Explanation area (initially empty)
	explareac = document.createElement("p");
	explareac.id = "explarea";
	explareac.className = "normal";
	opbar_wrapper.appendChild(explareac);
	
	// Images
	img_bar = document.createElement("img");
	img_bar.id ="votebar";
	img_bar.src="http://www.debarena.com/fr/img/opinion_bar.jpg";
	opbar_wrapper.appendChild(img_bar);
	img_bar.width= imgBarWidth;
	img_bar.height = imgBarHeight;
	bar_location = findPos(img_bar);
	//img_bar.style.position="relative";
	//img_bar.style.left="-50px";
	//img_bar.style.top="0px";
	//img_bar.style.marginTop="30px";
	img_bar.style.zindex=-1;
	
	img_cursor = document.createElement("img");
	img_cursor.id="votecursor";
	img_cursor.src="http://www.debarena.com/fr/img/cursor.gif";
	opbar_wrapper.appendChild(img_cursor);
	img_cursor.width = imgCursorWidth;
	//img_cursor_X = img_bar_X + (imgBarWidth * cursorPos) - (imgCursorWidth / 2);
	//green_limit=img_cursor_X- imgBarWidth/2;
	//red_limit=img_cursor_X+ imgBarWidth/2;
	img_cursor.style.position="relative";
	img_cursor.style.left= (-imgBarWidth/2 -6) +"px";
	img_cursor.style.top= "13px";
	//img_cursor.style.top=bar_location[1];
	img_cursor.style.zindex=0;
	img_cursor.onmousedown= DragStart;
	//img_cursor.style.top="13px";
	
	red_l = document.createElement("span");
	red_l.innerHTML = redLabel;
	//opbar_wrapper.appendChild(red_l);
	//red_l.className= "ob_label";
	
	Message= document.createElement("h2");
		Message.className= "obmessage";
		Message.innerHTML = cursorPos2message(cursorPos,greenLabel,redLabel);
		opbar_wrapper.appendChild(Message);
	
	voteBut = document.createElement("a");
	voteBut.type="submit";
	voteBut.innerHTML="<span class='icone_valider'>Valider</span>";
	voteBut.className= "bouton valider";
	voteBut.href = "javascript:castVote(Math.round((cursorPos*2 - 1) * 100),curarg, ('na' + curarg));";
	opbar_handle.appendChild(voteBut);
	
	//opbar_handle.style.display="none";
	
	document.onmouseup= DragStop;
	document.onmousemove= Dragging;
	
	if (initial_value !=0)
	{
		cursorPos = initial_value;
		Message.innerHTML = cursorPos2message(cursorPos,green_l.innerHTML,red_l.innerHTML);
		var val = Math.abs(cursorPos*2 - 1)*255;
		if (cursorPos<0.5) var color = "rgb("+ Math.round(255-val) + ',250,' + Math.round(255-val) +")";
		else var color = "rgb(250," + Math.round(255-val) + ','+ Math.round(255-val) +")";
		Message.style.backgroundColor= color;
		img_cursor.style.left= ((-imgBarWidth/2 -6) + (initial_value-0.5)*imgBarWidth) +"px";
	}
}

function DragStart(ev)
{
	dragging = true;
	ev = ev || window.event;
	var mousePos = mouseCoords(ev);
	startXpos = mousePos.x;
	curXpos= mousePos.x;
	img_cursor.src="http://www.debarena.com/fr/img/cursor_active.gif";
	return false;
}

function DragStop()
{
	dragging = false;
	//img_cursor_X= img_cursor_X + (curXpos-startXpos);
	img_cursor.src="http://www.debarena.com/fr/img/cursor.gif";
	return false;
}

function Dragging(ev)
{
	if (dragging==true)
	{
		ev = ev || window.event;
		var mousePos = mouseCoords(ev);
		curXpos = mousePos.x;
		var D = (curXpos-startXpos)/350;
		//if (D>0) D = 0.005; else D=-0.005;
		startXpos=curXpos;
		if (cursorPos>=0 && cursorPos<=1) cursorPos+=D;
		else if (cursorPos<0 && D>0) cursorPos+=D;
		else if (cursorPos>1 && D<0) cursorPos+=D;
		if (cursorPos<0) cursorPos=0;
		if (cursorPos>1) cursorPos=1;
		img_cursor_X = (-imgBarWidth -6) + (imgBarWidth * cursorPos);
		img_cursor.style.left= eval('"'+img_cursor_X+'px"');
		Message.innerHTML = cursorPos2message(cursorPos,green_l.innerHTML,red_l.innerHTML);
		var val = Math.abs(cursorPos*2 - 1)*255;
		if (cursorPos<0.5) var color = "rgb("+ Math.round(255-val) + ',250,' + Math.round(255-val) +")";
		else var color = "rgb(250," + Math.round(255-val) + ','+ Math.round(255-val) +")";
		Message.style.backgroundColor= color;
	}
	
	return false;
}

function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

function cursorPos2message(cpos,gl,rl)
{
	var result="";
	if (cpos<0.5) x=gl; else x=rl;
	var pct= Math.abs((cpos*2 - 1) * 100);
	if( pct>95) result= x+" ,"+x+" et re-"+x;
	else if( pct>90) result="Radicalement " + x;
	else if( pct>80) result="Absolument " + x;
	else if( pct>60) result=x;
	else if( pct>40) result="Plutôt " + x;
	else if( pct>25) result="Légèrement " + x;
	else if( pct>15) result=x  + " ,peut-être...";
	else if( pct>5) result="Neutre, tendance " + x;
	else result="Neutre";
	result += " (" + Math.round(pct) + "%)";
	return result;
}