/*

----#------#---#--
---#-#----#-#-#-#-
--#####---#--#--#-
-#-----#--#-----#-
Created By Aaron Machin
2018

*/


//Creates the confirm box
function createConfirm(title,content, button1, button2, button3){
    var confirmBody = document.createElement("div");
    confirmBody.className = "confirmBody";
    confirmBody.setAttribute("id", "confirmBody");
        var confirmContainer = document.createElement("div");
        confirmContainer.className = "confirmContainer";
            var confirmInner = document.createElement("div");
            confirmInner.className = "confirmInner";
                var cfTop = document.createElement("div");
                cfTop.className = "cfTop";
                    var cfTitle = document.createElement("div");
                    cfTitle.className = "cfTitle";
                    cfTitle.innerHTML = title;
                var cfContent = document.createElement("div");
                cfContent.className = "cfContent";
                cfContent.innerHTML = content;
                var cfBottom = document.createElement("div");
                cfBottom.className="cfBottom";
                    var cfSubmitContainer = document.createElement("div");
                    cfSubmitContainer.className = "cfSubmitContainer";
                        var cfYes = document.createElement("div");
                        cfYes.className = "cfSubmit_bt cfYes";
                        cfYes.innerHTML = button1;
                        var cfNo = document.createElement("div");
                        cfNo.className = "cfSubmit_bt cfCancel";
                        cfNo.innerHTML = button2;
                        if (button3 != "")
                        {
							var cfCancel = document.createElement("div");
							cfCancel.className = "cfSubmit_bt cfCancel";
							cfCancel.innerHTML = button3;
						}

    confirmBody.appendChild(confirmContainer);
        confirmContainer.appendChild(confirmInner);
            confirmInner.appendChild(cfTop);
                cfTop.appendChild(cfTitle);
            confirmInner.appendChild(cfContent);
            confirmInner.appendChild(cfBottom);
                cfBottom.appendChild(cfSubmitContainer);
                    cfSubmitContainer.appendChild(cfYes);
                    cfSubmitContainer.appendChild(cfNo);
                    if (button3 != "") cfSubmitContainer.appendChild(cfCancel);


    document.body.appendChild(confirmBody);

    var elements = [cfYes, cfNo, cfCancel, confirmBody, confirmContainer];
    return elements;
}

function removeConfirm(e){

    e.parentNode.removeChild(e);
}


function confirmThis(title,content,button1Text, button2Text, button3Text, yesCallback,noCallback,cancelCallback){
    var elements = createConfirm(title,content, button1Text, button2Text, button3Text);
    var yes = elements[0];
    var no = elements[1];
    var cancel = elements[2];
    var cfbody = elements[3];
    var cfContainer = elements[4];
    cfContainer.style.top = "-200px";
    setTimeout(function(){ cfContainer.style.top = "200px"; },100);


    window.onclick = function(event) {
        if (event.target == cfbody) {
            cfbody.style.backgroundColor = "rgb(192, 192, 192, 0.6)";
            setTimeout(function(){ cfbody.style.backgroundColor = "transparent"; },100);

        }
    }

    yes.addEventListener("click", function(){
        cfContainer.style.top = "-200px";
        setTimeout(function(){ removeConfirm(cfbody);},200);
        yesCallback();
    });

    no.addEventListener("click", function(e){
        cfContainer.style.top = "-200px";
        setTimeout(function(){ removeConfirm(cfbody);},200);
        noCallback();
    });

    if (cancelCallback)
    {
		cancel.addEventListener("click", function(){
			cfContainer.style.top = "-200px";
			setTimeout(function(){ removeConfirm(cfbody);},200);
			cancelCallback();
		});
	}
}

//First function: Yes, Second: No
/*

Function call for a confirm message

confirmThis("Title here", "Content here","yes", "no", "cancel",function(){
  console.log("Yes");
},function(){
    console.log("No");
},function(){
    console.log("Cancel");
});


*/
