//forum.js - JavaScriptove vychytavky pro forum
function insertAtCursor(myField, myValue) {
  //IE support
  if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
  }
  //MOZILLA/NETSCAPE support
  else if (myField.selectionStart || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)
                  + myValue
                  + myField.value.substring(endPos, myField.value.length);
  } else {
    myField.value += myValue;
  }
}


function vlozSmajlika(tag) {
	textArea = document.getElementById('text');
	insertAtCursor(document.getElementById('text'), tag);
	return false;
}


function vypisSmajliky () {
	smajliky = new Array();
	
	//dost obskurni seznam smajliku	
	smajliky[":-)"] = "happy.gif"
	smajliky[":-))"] = "veryhappy.gif"
	smajliky[":-D"] = "bigsmile.gif"
	smajliky[":-P"] = "tonguestickingout.gif"
	smajliky[";-)"] = "wink.gif"
	smajliky[":-("] = "sad.gif"
	smajliky[":-/"] = "confused.gif"
	smajliky["B-)"] = "wearingsunglasses.gif"
	smajliky[":-B"] = "wearingregularglassesisanerd.gif"
	smajliky["X-("] = "angry.gif"
	smajliky[":-X"] = "inlove.gif"
	smajliky["8-X"] = "skull.gif"
	smajliky[":(|)"] = "monkey.gif"
	smajliky["~:>"] = "chicken.gif"
	smajliky["%%-"] = "luckyclover.gif"
	smajliky["b-("] = "beatup.gif"
	smajliky[":)>-"] = "peace.gif"
	smajliky[":-&"] = "sick.gif"
	smajliky[":^o"] = "liar.gif"
	
	smajliky.length;
	document.write("<div><label>Smajlíky</label>");
	for (var smajlik in smajliky) {
		
		document.write("<img src=\"images/smajliky/" + smajliky[smajlik] + "\" onclick=\"javascript: vlozSmajlika('" + smajlik + "');\"/>");
	}
	document.write("</div>")
}

function askForDelete() {
	return window.confirm("Opravdu si přejete smazat váš příspěvek?");
}
    
function getServerRoot () {
    var re = /^(http[s]*:\/\/[^\/]+)\/.*$/;
    return document.location.href.match(re)[1];
}

var guestbook = {
    submitComment: function (evt) {
        var data = {'autor': $('#prispevekAutor').val(),
            'text': $('#prispevekText').val(),
            'otazkaProPobaveni': $('#prispevekOtazkaProPobaveni').val(),
            'odpoved': $('#prispevekOdpoved').val(),
            'homepage':  $('#prispevekHomepage').val()};
        jQuery.ajax({
            type: 'POST',
            data: data,
            url: 'guestbook_submit_ajax.php',
            timeout: 2000,
            error: guestbook.onSubmitCommentError,
            success: guestbook.onSubmitCommentOk,
            dataType: 'json'
        });

        // Zobrazit div s nacitanim
        $('#prispevekSubmitBox').html('<p>Odesílám...</p>').removeClass('error').addClass('loading'); 

        // Nastav odesilaci tlacitko jako disabled
        $('#prispevek button[type=submit]').attr('disabled', 'disabled');
        evt.preventDefault();
    },

    onSubmitCommentError: function (aRequest, aMessage, aException) {
        guestbook.showErrorMessage('Server neodpovídá.');
        $('#prispevek button[type=submit]').removeAttr('disabled');
    },

    onSubmitCommentOk: function (aResponse) {
        if (aResponse.stat == "ok") {
            document.location.href = getServerRoot() + "/guestbook.php";
        } else {
            // Zobraz chybovou hlasku
            guestbook.showErrorMessage(aResponse.message);
            $('#prispevek button[type=submit]').removeAttr('disabled');
        }
    },

    showErrorMessage: function (aMessage) {
        $('#prispevekSubmitBox').html('<p>' + aMessage + '</p>').removeClass('loading').addClass('error'); 
    }
}

$(document).ready(function () {
    $('#prispevek').bind('submit', guestbook.submitComment);
});
	

