var rsrmP = "";
var rsrmClickable = false;

function showRadial(e, param)
{
        rsrmClickable = false;
        if(!e) e = window.event;
        if(window.Event && e.which == 3) return;
        if(window.event && e.button == 2) return;
	rsrmP = param;
        var p = mousePos(e);
        var rm = document.getElementById("radialMenu");
        rm.style.left=(p.x-100)+"px";
        rm.style.top=(p.y-100)+"px";
        rm.centerCoords = p;
        rm.className="tiny";
        window.setTimeout(function() { rm.className="small" } ,65);
        window.setTimeout(function() { rm.className="mediumSize" } ,110);
        window.setTimeout(function() { rm.className=""; }, 175);
        addEvent(document, "mousemove", checkMouseToRadialDistance);
        cancelEvent(e);
}

function upToRadial(e)
{
        if(!e) e = window.event;
        if(window.Event && e.which == 3) return;
        if(window.event && e.button == 2) return;
        rsrmClickable = true;
}

function hideRadial()
{
        var rm = document.getElementById("radialMenu");
        rm.className="small";
        window.setTimeout(function() { rm.className="tiny" } ,40);
        window.setTimeout(function() { rm.className="off" } ,80);
        removeEvent(document, "mouseup", hideRadial);
	removeEvent(document, "mousemove", checkMouseToRadialDistance);
}
function highlight(nr)
{
    var ra = document.getElementById('radialActive'+nr);
    if(ra)
        ra.style.visibility="visible";
    var rt = document.getElementById('radialText'+nr);
    if(rt)
        rt.className="active";
}
function unhighlight(nr)
{
    var ra = document.getElementById('radialActive'+nr);
    if(ra)
        ra.style.visibility="hidden";
    var rt = document.getElementById('radialText'+nr);
    if(rt)
        rt.className="";
}
function getXmlHttp() {
  var xmlHttp;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e) {
    // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
        return false;
      }
    }
  }
  return xmlHttp;
}
function yfade(id, r, g, b, holdtime)
{
  var div = document.getElementById(id);
  if(!div) return;
  div.style.background="rgb("+r+","+g+","+b+")";
  if(r < 255 || g < 255 || b < 255) setTimeout("yfade('"+id+"',"+Math.min(r+1, 255)+","+Math.min(g+1, 255)+","+Math.min(b+5, 255)+",50)", holdtime);
}

function ajaxVote(vote)
{
  var xmlHttp = getXmlHttp();
  if(!xmlHttp) return;

  var id = 'votes' + parseInt(rsrmP);

  xmlHttp.onreadystatechange=function() {
      if(xmlHttp.readyState==4) {
        var div = document.getElementById(id);
        if(div) div.innerHTML = xmlHttp.responseText;
        yfade(id, 255, 253, 160, 700);
      }
  }
  xmlHttp.open("GET","/vote/"+vote+"/"+rsrmP,true);
  xmlHttp.send(null);
}

function radialClick(sectorNumber)
{
    if(sectorNumber == 0 && !rsrmClickable) {
        rsrmClickable = true;
	return;
    }

    if(sectorNumber == 0)
        window.location = "/play/" + rsrmP;
    else if(sectorNumber == 1)
        window.location = "/talk/" + rsrmP;
    else if(sectorNumber == 2)
        ajaxVote(2);
    else if(sectorNumber == 3)
        ajaxVote(3);
    else if(sectorNumber == 4)
        window.location = "/like/" + rsrmP;
    else if(sectorNumber == 5)
        showPlaylistControls(rsrmP);
    else if(sectorNumber == 6)
        ajaxVote(1);

    hideRadial();
}
function checkMouseToRadialDistance(e)
{
        var rm = document.getElementById("radialMenu");
    var pos = mousePos(e);
    if (distance(pos.x,pos.y,rm.centerCoords.x,rm.centerCoords.y) > 130)
    {
        hideRadial();
    }
}
function distance(x1,y1,x2,y2)
{
        return Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
}
function scrollPos()
{
    var pos = {'x':0, 'y':0};

    var b = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ?
    window.document.documentElement : window.document.body || null;

    if (b)
    {
        pos.x = b.scrollLeft;
        pos.y =  b.scrollTop;
    }
    return pos;
}
function mousePos(e)
{
    if(!e) e = window.event;
    var pos = { 'x': e.clientX, 'y':e.clientY };

    var b = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ?
    window.document.documentElement : window.document.body || null;

    if (b)
    {
        pos.x += b.scrollLeft;
        pos.y +=  b.scrollTop;
    }
    return pos;
}
function cancelEvent(e) {
    if (!e) e = window.event;
    if (e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
}
function stopEvent(e) {
    if (!e) e = window.event;
    if (e.stopPropagation) {
        e.stopPropagation();
    } else {
        e.cancelBubble = true;
    }
}
function addEvent(obj, evType, fn, useCapture) {
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    return obj.attachEvent("on"+evType, fn);
  }
}
function removeEvent(obj, evType, fn, useCapture) {
  if (obj.removeEventListener) {
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent) {
    return obj.detachEvent("on"+evType, fn);
  }
}



