
var baseURL   = '/zabava/zivot-je-horky-bohudik-2008/';
var voteURL   = baseURL + 'vote/';
var bugdetURL = baseURL + 'budget/';




function Voter()
{
	this.showControl = function(song, element)
	{
		new CleanAjax({
			DataLoadedHandler: function() {
				with(element)  try {
				if(this.status == 200) with(element) {
            				var json = eval("(" + this.responseText + ")");
					var voteBudget = document.createElement('div');
					for(var i = 0; i < json.budget; i++) {
						var a = voteBudget.appendChild(document.createElement('a'));
						a.setAttribute('href', voteURL +'?alias=' + song + '&amount='+(i+1));
						a.appendChild(document.createTextNode(i + 1));
					}
					voteBudget.appendChild(document.createElement('p')).appendChild(document.createElement('p')).appendChild(document.createTextNode(json.message));
					try	{replaceChild(voteBudget, firstChild);}
					catch(e) {appendChild(voteBudget);}
					style.display = 'block';
				}
				else with(document.getElementById('answer')) {
					replaceChild(document.createTextNode(this.responseText), firstChild);
					style.display = 'block';
				}
				}
				catch(e)
				{
				}
			}
		}).postParams(bugdetURL, {'song': song});
	}

	this.sendVote = function(song, amount)
	{
		new CleanAjax({
			DataLoadedHandler: function() {
				if(this.status == 200) with(document.getElementById('vote-status')) {
					replaceChild(document.importNode(this.responseXML.documentElement), firstChild);
				}
				else with(document.getElementById('answer')) {
					replaceChild(document.createTextNode(this.responseText), firstChild);
					style.display = 'block';
				}
			}
		}).postParams(voteURL);
	}

}


var voter = new Voter();

var sendURL = baseURL + 'sendToFriend/';

function Sender()
{
	this.show = function show(element)
	{
		element.style.display = 'block';
		return;
	}

	this.send = function send(email, url)
	{
		new CleanAjax({
			DataLoadedHandler: function() {
				if(this.status == 200) with(document.getElementById('sendtofriend')) {
					replaceChild(document.importNode(this.responseXML.documentElement), firstChild);
				}
				else with(document.getElementById('sendtofriend')) {
					replaceChild(document.createTextNode(this.responseText), firstChild);
					style.display = 'block';
				}
			}
		}).postParams(sendURL, {'email': email, 'url': url});
	}
}

var sender = new Sender();

var x;
var y;

var skid;

window.onload = init;

function getXY(e) {
    x = (window.Event) ? e.pageX : event.clientX;
    y = (window.Event) ? e.pageY : event.clientY;
}

function init() {
  if (window.Event) {
    document.captureEvents(Event.MOUSEMOVE);
  }
  document.onmousemove = getXY;
}

function showVote(id) {
    skid = id;
    var tmp = function() {
		this.onSuccess = function() {
            var inner = document.getElementById("inner");
            var jason = eval("("+ this.getTextResponse()+")");

            switch(jason.response.txt) {
                case 'NONE_LOGGIN' :
                    inner.innerHTML = '<p><strong>Hlasovat může jen přihlášený uživatel!</strong></p>';
                    showFloatDiv("answer");
                    break;
                case 'NONE_ID' :
                    inner.innerHTML = '<p><strong>Chybí povinný parametr!</strong></p>';
                    showFloatDiv("answer");
                    break;
                case 'WRONG_ID' :
                    inner.innerHTML = '<p><strong>Příběh má nesprávné ID!</strong></p>';
                    showFloatDiv("answer");
                    break;
                case 'NONE_VOTE' :
                    inner.innerHTML = '<p><strong>Nelze hlasovat pro tento příběh!</strong></p>';
                    showFloatDiv("answer");
                    break;
                case 'SHOW_VOTE' :
                    document.getElementById("inskladba").innerHTML = jason.response.skladba;
                    document.getElementById("inskupina").innerHTML = jason.response.skupina;
                    document.getElementById("inbody").innerHTML = jason.response.body;
                    clearElement(document.getElementById("voter")); 
                    for (var i=1; i <= jason.response.body; i++) {
                        createVoter(i);
                    }
                    showFloatDiv("vote");
                    break;
            }
		}

		this.getData = function() {
            this.makeRequest("GET", 'cmd=info&id='+id, URL);
 	   }
	}
	tmp.prototype = new myAjax();
	var o = new tmp();
	o.getData();
}

function showFloatDiv(name) {
    var menu = document.getElementById(name);
    var w = 230;
    var h = 70;
    x -= w;
    y -= h;

    menu.setAttribute("style", "display: block; position:absolute;left:"+x+"px;top:"+y+"px;");
    menu.style["display"] = "block";
    menu.style["position"] = "absolute";
    menu.style["left"] = x+"px";
    menu.style["top"] = y+"px";
}

function createVoter(id) {
    var voter = document.getElementById("voter");
    var img;

    if (id < 10) {
        id = '0'+id;
    }

    if (document.all && !window.opera) {
         img = document.createElement('<img src="/microsite/mp3-parada/_clip/vote-'+id+'.gif" onclick="javascript:vote('+id+');">');
    }
    else {//ostatni*/
        img = document.createElement("img");
        img.setAttribute("src", "/microsite/mp3-parada/_clip/vote-"+id+".gif");
        img.setAttribute("onclick", "javascript:vote("+id+");");
    }
    voter.appendChild(img);
}

function vote(bod) {
    bod = parseInt(bod);
    var tmp = function() {
		this.onSuccess = function() {
            var inner = document.getElementById("inner");
            var jason = eval("("+ this.getTextResponse()+")");

            closeDiv("vote");

            switch(jason.response.txt) {
                case 'NONE_LOGGIN' :
                    inner.innerHTML = '<p><strong>Hlasovat může jen přihláený uživatel!</strong></p>';
                    showFloatDiv("answer");
                    break;
                case 'NONE_ID' :
                    inner.innerHTML = '<p><strong>Chybi povinny parametr!</strong></p>';
                    showFloatDiv("answer");
                    break;
                case 'WRONG_ID' :
                    inner.innerHTML = '<p><strong>Skladba má nesprávný ID!</strong></p>';
                    showFloatDiv("answer");
                    break;
                case 'NONE_VOTE' :
                    inner.innerHTML = '<p><strong>Nelze hlasovat pro tuto skladbu!</strong></p>';
                    showFloatDiv("answer");
                    break;
                case 'BUDGET_OVERFLOW' :
                    inner.innerHTML = '<p><strong>Nelze udělit víc než 10 hlasů denně!</strong></p>';
                    showFloatDiv("answer");
                    break;
                case 'BUDGET_SPEND' :
                    inner.innerHTML = '<p><strong>Nelze udělit víc hlasů, než ti zbívá!</strong></p>';
                    showFloatDiv("answer");
                    break;
                case 'VOTE_ERROR' :
                    inner.innerHTML = '<p><strong>Hlasování selhalo, zkuste to znovu!</strong></p>';
                    showFloatDiv("answer");
                    break;
                case 'VOTE_OK' :
                    inner.innerHTML = '<p>Skladbě <strong>'+jason.response.skladba+'</strong><br /> jsi právě udelil hlasů: <strong>'+jason.response.last+'</strong></p><p>Zbívá ti hlasů: <strong>'+jason.response.body+'</strong></p>'
                    showFloatDiv("answer");
                    break;
            }
		}

		this.getData = function() {
            this.makeRequest("POST", 'cmd=vote&id='+skid+'&body='+bod, URL);
 	   }
	}
	tmp.prototype = new myAjax();
	var o = new tmp();
	o.getData();
}
