// JavaScript Document

// Constants
var maxWidth;
var maxHeight;

// Variables
var isShowVote;
var voteWidth;
var voteHeight;

function showVote(startShowing, width, height) {
	maxWidth = width;
	maxHeight = height;
	isShowVote = startShowing;
	voteWidth = width;
	voteHeight = height;
        $(".voteButton").css("opacity", "1.0");
	
	if(startShowing) {
		$("#pollVindu").css("display", "block");	
	} else {
		$("#pollVindu").css("display", "none");	
	}
}

function removeVoteField() {
    $('#voteHeader').remove();
    $('#voteForm').remove();
}

$(function() {
    // Click show button
    $(".voteButton").click(function() {
        removeVoteField();
        drawVote($(this).attr("id"));
                
            // Animate show vote
        $("#pollVindu").animate({
            width: voteWidth,
            height: voteHeight
        }, 1000, function() {
                isShowVote = !isShowVote;
                if(isShowVote) {
                    voteWidth = 0;
                    voteHeight = 0;
                    $("#pollVindu").css("display", "block");
                    $("#voteForm").css("display", "block");
                    $("#voteHeader").css("display", "block");
                } else {
                    voteWidth = maxWidth;
                    voteHeight = maxHeight;
                    $("#pollVindu").css("display", "none");
                    $("#voteForm").css("display", "none");
                    $("#voteHeader").css("display", "none");
                    removeVoteField();
                }
        });
    });
});

function drawVote(id) {
    $(".voteButton").css("opacity", "0.0");
    $.ajax({
        type: "POST",
        url: "php/ajax/voteHandler.php",
        data: "pollId=" + id,

        success: function(html){
            if(html.toString() == "ERROR") {
                $('<h2 id="voteHeader">Beklager, men du har allerede stemt.</h2>').appendTo("#pollVindu");
                removeWindow();
            } else {
                $(html).appendTo("#pollVindu");
            }
            

            // Make a listener
            $("#doAWote").click(function() {
                var voteId = jQuery('#voteForm input:radio:checked').val();
                $.ajax({
                    type: "POST",
                    url: "php/ajax/voteHandler.php",
                    data: "p_id=" +id+ "&el_id=" + voteId,

                    success: function(html){
                        removeVoteField();
                        $(html).appendTo("#pollVindu");
                        $("#voteHeader").css("display", "block");
                        removeWindow();
                    }
                });
            });
            closeButton();
        }
    });

function removeWindow() {
    $("#pollVindu").animate({
        opacity: 0
    }, 3000, function() {
        $("#pollVindu").css("opacity", "1.0");
        $("#pollVindu").css("width", "0");
        $("#pollVindu").css("height", "0");
        showVote(false, maxWidth, maxHeight);
    });
}

function closeButton() {
    // Make a listener
    $("#doClose").click(function() {
        $("#pollVindu").animate({
        opacity: 0
    }, 500, function() {
        $("#pollVindu").css("opacity", "1.0");
        $("#pollVindu").css("width", "0");
        $("#pollVindu").css("height", "0");
        showVote(false, maxWidth, maxHeight);
        });
    });
}

}
