//Creado con Ardora - www.webardora.net
//bajo licencia Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)
//para otros usos contacte con el autor
var zIndex = 0;allJobs=new Array();
$(document).ready(function(){document.addEventListener("touchstart", touchHandler, true);document.addEventListener("touchmove", touchHandler, true);document.addEventListener("touchend", touchHandler, true);document.addEventListener("touchcancel", touchHandler, true);
createBar();getJob();
});
function make_draggable(elements){elements.draggable({containment:"parent",start:function(e,ui){ ui.helper.css("z-index",++zIndex); },stop:function(e,ui){}});}
function getJob(){$.ajax({data: {"action": "getJob"},type: "POST",dataType: "json",url: "php/ardoraPublic.php",
success: function(data, textStatus, jqXHR) {
allJobs=data;
sortResults(allJobs,"tit",true);
var notes="";var rot=0;if (data.length > 0) {for (i=0;i<data.length;i++){
if (isLog) {if (data[i]["usu"] == userN || userType == "profe" || userType == "admin") {notes = notes+'<div class="marco">';}}
notes = notes + '<div id="pic-'+i.toString()+'" class="pic" style="background:url('+data[i]["ima"]+') no-repeat 50% 50%;transform:rotate(' + rot.toString()+'deg);">';
notes = notes+'<a id="P_'+i.toString()+'" title="'+allJobs[i]["tit"]+'">link</a>';
notes = notes+'<div class="comment">'+allJobs[i]["tit"]+'</div>';notes = notes + "</div>";
if (isLog) {if (data[i]["usu"] == userN || userType == "profe" || userType == "admin") {notes = notes+'<div class="divNoteBut"><p id="del'+i.toString()+'" title="" class="butLe butNote">b</p><p id="edit'+i.toString()+'" title="" class="butLe butNote">P</p></div></div>';}}
}$("#ardoraAct").html(notes);
for (i = 0; i < data.length; i++) {$("#P_"+i.toString()).on("touchstart click", function(e) {var num=parseInt($(this).attr("id").substring(2,200).trim());showJob(num);});
if (isLog) {if (data[i]["usu"] == userN || userType == "profe" || userType == "admin") {
$("#del"+i.toString()).on("touchstart click", function(e) {idNote = $(this).attr("id").substring(3, 10).trim();$("#modal-content").empty();$("#modal-content").css("width", "auto");$("#modal-content").css("height", "auto");
var newHtml='<div id="modalTit">Delete</div>';newHtml=newHtml+'<div id="modalBody"><br>Do you really want to delete? <b>'+ allJobs[idNote]["tit"]+'?</b><br><br>';
newHtml=newHtml+'<div id="modalBar"><p id="cancelBut" class="butfont butLe red" title="">E</p><p id="delBut" class="butfont butRi green" title="">g</p></div></div>';
$("#modal-content").append(newHtml);$("#modal-content,#modal-background").toggleClass("active");$("#modal-background, #modal-close, #cancelBut").click(function() {$("#modal-content,#modal-background").toggleClass("active");});$("#modal-content").draggable();
$("#delBut").on("touchstart click", function(e) {delJob(allJobs[idNote]["usu"], allJobs[idNote]["tit"], allJobs[idNote]["dat"]);$("#modal-content,#modal-background").toggleClass("active");});});
$("#edit" + i.toString()).on("touchstart click", function(e) {idNote = $(this).attr("id").substring(4,10).trim();$("#modal-content").empty();$("#modal-content").css("height", "auto");$("#modal-content").css("width", "600px");
var newHtml='<div id="modalTit">Edit</div>';
newHtml=newHtml+'<div id="area_form" ><form id="entraDatos" class="formLogin" name="entraDatos" method="post" action="" enctype="multipart/form-data">';
newHtml=newHtml+'<div id="traballos"><p>Insert the data</p></div>';
newHtml=newHtml+'<div id="datos_form"><label for="titulo">Name</label><input type="text" name="titulo" id="titulo" placeholder="NAME" required value="'+allJobs[idNote]["tit"]+'" maxlength="30"/>';
newHtml=newHtml+'<div id="clear"></div>';
newHtml=newHtml+'<label for="descri">Country</label><input type="text" name="descricion" id="descricion" placeholder="COUNTRY" required value="'+allJobs[idNote]["des"]+'"/>';
newHtml=newHtml+'<div id="clear"></div>';
newHtml = newHtml+'<label for="fileinput">File</label><input type="file" id="fileinput" name="fileinput" accept="image/*" />';
newHtml=newHtml+'<br><div id="clear"></div>';newHtml=newHtml+'<textarea class="ckeditor" name="editor1" id="editor1" rows="100" cols="80">';newHtml=newHtml+'<div id="clear"></div>';newHtml=newHtml+'</div></form></div>';$("#modal-content").append(newHtml);
CKEDITOR.replace( "editor1", {customConfig: "ardora/ckeditor_config.js",
toolbar:[{ name:"clipboard",items:["Cut","Copy","Paste","PasteText","-","Undo","Redo"]},{ name:"editing",items:["Find"]},{ name:"insert",items:["Table","HorizontalRule","Smiley","SpecialChar"]},{ name:"basicstyles",items:["Bold","Italic","Underline","Strike","Subsrcipt","Superscript"]},{ name:"paragraph",items:["NumberedList","-","Blockquote","-","JustifyLeft","JustifyCenter","JustifyRight","JustifyBlock"]},{ name:"styles",items:["Font","FontSize"]},{ name:"colors",items:["TextColor","BGColor"]},{ name:"links",items:["Link","Unlink"]},],toolbarGroups:[{ name: "clipboard"},{ name: "editing"},{ name: "insert"},{ name: "basicstyles"},{ name: "paragraph"},{ name: "styles"},{ name: "colors"},{ name: "links"},]});CKEDITOR.config.height=100;
newHtml='<div id="clear"></div>';newHtml=newHtml+'<div id="clear"></div>';
newHtml=newHtml+'<div id="modalBar"><p id="cancelBut" class="butfont butLe red" title="">E</p><p id="oKBut" class="butfont butRi green" title="">g</p></div></div>';
$("#modal-content").append(newHtml);$("#modal-content").css("left", ($("#ardoraMain").width() - $("#modal-content").width()) / 2).toString() + "px";
$("#modal-content").css("top", "50px");$("#modal-content,#modal-background").toggleClass("active");
CKEDITOR.instances['editor1'].setData(allJobs[idNote]["htm"]);
$("#modal-background, #modal-close, #cancelBut").click(function() {$("#modal-content,#modal-background").toggleClass("active");});
$("#modal-content").draggable();$("#titulo").on("touchstart click", function(e) {$("#titulo").select();});
$("#descricion").on("touchstart click", function(e) {$("#descricion").select();});
$("#fileinput").on("touchstart click", function(e) {$("#fileinput").select();});
$("#oKBut").on("touchstart click", function(e) {$("#alertDiv").remove();var input_Tit = $("#titulo").val();var input_Des = $("#descricion").val();var input_Fil = $("#fileinput").val();
if (input_Tit.length > 0) {if (input_Fil.length > 0) {input=document.getElementById("fileinput");file = input.files[0];if (file.type.match(/image.*/)) {
var scaleWidth=300;var scaleHeight=0;Lector = new FileReader();Lector.onloadend = function(e) {var origen;origen=e.target;var img=document.createElement("img");
img.setAttribute("id", "IMAXEPARASUBIR");img.src = origen.result;img.onload = function() {var canvas = document.getElementById("myCanvas");var ctx = canvas.getContext("2d");
if (img.width > scaleWidth) {scaleHeight = scaleWidth*img.height / img.width;} else {scaleWidth = img.width;scaleHeight = img.height;}
canvas.width = scaleWidth;canvas.height = scaleHeight;$("#myCanvas").css("top", ((-1)*scaleHeight-20));ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, scaleWidth, scaleHeight);var input_Ima = canvas.toDataURL();
input_Htm = CKEDITOR.instances.editor1.getData();updateItem(allJobs[idNote]["tit"], allJobs[idNote]["usu"], input_Tit, input_Des, input_Ima, input_Htm);}};Lector.readAsDataURL(input.files[0]);$("#oKBut").hide();
} else {$("#datos_form").append("<div id='alertDiv'><p id=alertIco>a</p><p id='errorMen'>Wrong type of file</p></div>");$("#fileinput").focus();}}
else{input_Htm = CKEDITOR.instances.editor1.getData();updateItem(allJobs[idNote]["tit"], allJobs[idNote]["usu"], input_Tit, input_Des,"0", input_Htm);}
} else {$("#datos_form").append("<div id='alertDiv'><p id=alertIco>a</p><p id='errorMen'>You must insert a text or a sentence</p></div>");$("#titulo").focus();}
});$("#startBut").click();});}}}
var tmp;
$(".pic").each(function() {tmp = $(this).css("z-index");if (tmp > zIndex) zIndex=tmp;})
$(".pic").draggable({containment: "parent",start: function(e, ui) {ui.helper.css("z-index", ++zIndex);},stop: function(e, ui) {}})
$(".pic").hover(function(){$(this).children(".comment").stop().css("top", "0px");}, function(){$(this).children(".comment").stop().animate({"top": "400px"},600);});}
}
});
}
function normalize(item) {var alc = item.toLowerCase();alc = alc.replace(/á/g, "a");alc = alc.replace(/é/g, "e");alc = alc.replace(/í/g, "i");alc = alc.replace(/ó/g, "o");alc = alc.replace(/ú/g, "u");alc = alc.replace(/à/g, "a");alc = alc.replace(/è/g, "e");alc = alc.replace(/ì/g, "i");alc = alc.replace(/ò/g, "o");
alc=alc.replace(/ù/g, "u");alc = alc.replace(/â/g, "a");alc = alc.replace(/ê/g, "e");alc = alc.replace(/î/g, "i");alc = alc.replace(/ô/g, "o");alc = alc.replace(/û/g, "u");alc = alc.replace(/ë/g, "e");alc = alc.replace(/ï/g, "i");alc = alc.replace(/ü/g, "u");alc = alc.replace(/ă/g, "a");return alc;}
function closeIFrame(){$("#modal-content,#modal-background").toggleClass("active");$.ajax({data: {"action": "login"},type: "GET",dataType: "json",async: false,url: "php/ardoraXML.php",
success: function(data, textStatus, jqXHR) {userN=data["usu"];userName=data["nom"];userGru=data["gru"];userCur=data["cur"];userType=data["typ"]}});
$("#loginBut").hide();isLog=true;$("#modal-content").removeClass("widthAuto").addClass("widthFix");getJob();}
function delJob(userJob, wordJob, dataJob) {if (userJob==userN || userType=="profe" || userType=="admin") {$.ajax({type: "POST",dataType: "json",async: false,url: "php/ardoraXML.php",data:{"action":"deleteItem","tit":wordJob,"nameUser":userJob,"dat":dataJob}});getJob();}}
function updateItem(oldItem, userJob, wordJob, comJob, imaJob, htmJob) {if (userJob == userN || userType == "profe" || userType == "admin") {$.ajax({
data:{"action": "updateItem","oldItem": oldItem.trim(),"tit": wordJob,"des": comJob,"ima": imaJob,"htm": htmJob,"usu": userJob},type: "POST",dataType: "json",url: "php/ardoraXML.php",success: function(data, textStatus, jqXHR) {getJob();$("#modal-content,#modal-background").toggleClass("active");}});};}
function sortResults(vArray,prop,asc) {vArray=vArray.sort(function(a,b){var alc=a[prop].toLowerCase();var blc=b[prop].toLowerCase();
alc=alc.replace(/á/g,"a");blc=blc.replace(/á/g,"a");alc=alc.replace(/é/g,"e");blc=blc.replace(/é/g,"e");alc=alc.replace(/í/g,"i");blc=blc.replace(/í/g,"i");alc=alc.replace(/ó/g,"o");blc=blc.replace(/ó/g,"o");alc=alc.replace(/ú/g,"u");blc=blc.replace(/ú/g,"u");
alc=alc.replace(/ñ/g,"nzz");blc=blc.replace(/ñ/g,"nzz");alc=alc.replace(/ç/g,"czz");blc=blc.replace(/ç/g,"czz");
alc=alc.replace(/à/g,"a");blc=blc.replace(/à/g,"a");alc=alc.replace(/è/g,"e");blc=blc.replace(/è/g,"e");alc=alc.replace(/ì/g,"i");blc=blc.replace(/ì/g,"i");alc=alc.replace(/ò/g,"o");blc=blc.replace(/ò/g,"o");alc=alc.replace(/ù/g,"u");blc=blc.replace(/ù/g,"u");
alc=alc.replace(/â/g,"a");blc=blc.replace(/â/g,"a");alc=alc.replace(/ê/g,"e");blc=blc.replace(/ê/g,"e");alc=alc.replace(/î/g,"i");blc=blc.replace(/î/g,"i");alc=alc.replace(/ô/g,"o");blc=blc.replace(/ô/g,"o");alc=alc.replace(/û/g,"u");blc=blc.replace(/û/g,"u");
alc=alc.replace(/ë/g,"e");blc=blc.replace(/ë/g,"e");alc=alc.replace(/ï/g,"i");blc=blc.replace(/ï/g,"i");alc=alc.replace(/ü/g,"u");blc=blc.replace(/ü/g,"u");
alc=alc.replace(/ă/g,"a");blc=blc.replace(/ă/g,"a");
if (asc) return (alc>blc) ? 1 : ((alc<blc) ? -1 : 0);else return (blc>alc) ? 1 : ((blc < alc) ? -1 : 0);});}
function touchHandler(event) {
var touch = event.changedTouches[0];var simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent({touchstart: "mousedown",touchmove: "mousemove",touchend: "mouseup"}[event.type], true, true, window, 1,touch.screenX, touch.screenY, touch.clientX, touch.clientY, false, false, false, false, 0, null);
touch.target.dispatchEvent(simulatedEvent);event.preventDefault();};
function showJob(num) {$("body").css("overflow", "hidden");$("#ardoraShowJob").css({"width": $(window).width(),"height": $(window).height()});
var txtComent=allJobs[num]["htm"].replace(/\\"/g,"");
 $("#ardoraShowJob").append("<div id='job'><div id='contenDiv'><div id='titleContenDiv'>"+allJobs[num]["tit"]+
"</div><div id='comentContenDiv'><img src='"+allJobs[num]["ima"]+"'/>"+txtComent+"<div id='foot'>"+allJobs[num]["usu"]+" - "+allJobs[num]["dat"]+"</div></div></div><div id='headerDiv'><p>"+allJobs[num]["des"]+"</p></div></div>")
$("#contenDiv").css({"margin-top":(($(window).height()-300)/2)-50});
$("#job").draggable();document.getElementById("ardoraShowJob").style.zIndex = 5000;document.getElementById("ardoraShowJob").style["visibility"] = "visible";$("#ardoraShowJob").css("cursor", "pointer");
$("#ardoraShowJob").click(function(e) {if (!$(e.target).is("#comentContenDiv")){$("#ardoraShowJob").html("");
document.getElementById("ardoraShowJob").style.zIndex=0;document.getElementById("ardoraShowJob").style["visibility"]="hidden";$("#ardoraShowJob").css({"width": 2,"height": 2});}});}
