/**
 * 
 */
//παράμετροι καμβά
var canvas;
var cntx;
var minimumCanvasWidth=300;
var minimumCanvasHeight=150;
var originalCanvasWidth=580;
var originalCanvasHeight=450;
var canvasWidth=originalCanvasWidth;
var canvasHeight=originalCanvasHeight;

var canvasFont="17px Georgia";
//παράμετροι αλληλεπίδρασης
var mouseX=0;
var mouseY=0;
var mouseButton=-1;

function initialiseCanvas(){
    canvas=document.getElementById("canvas2D");
    cntx=canvas.getContext("2d");
    defineCanvasSize();
    canvas.addEventListener("dblclick",function(evt){
        handleDoubleClick(evt);
    }, false);  
    canvas.addEventListener("mousemove",function(evt){
        handleMouseMove(evt);
    }, false);  
    canvas.addEventListener("mousedown",function(evt){
        handleMouseDown(evt);
    }, false);  
    canvas.addEventListener("mouseup",function(evt){
        handleMouseUp(evt);
    }, false);  
    cntx.font=canvasFont;
}

function writeOnCanvas(x,y,msg,canvasFontSize,font,align,color,borderColor,fillColor,shadowSwitch,rotationAngle){
    cntx.font=font;
    var txtWidth=cntx.measureText(msg).width;
    var x1=x;
    if(align=="center"){
        x1=x-txtWidth/2;
    }
    else if(align=="right"){
        x1=x-txtWidth;
    }
    else if(align=="left"){
        x1=x;
    }
    if(shadowSwitch){
        cntx.shadowBlur=20;
        cntx.shadowColor="black";
    }
    if(fillColor!=""){
        cntx.fillStyle=fillColor;
        cntx.fillRect(x1,y-canvasFontSize,txtWidth,1.5*canvasFontSize);
    }
    if(borderColor!=""){
        cntx.strokeStyle=borderColor;
        cntx.rect(x1,y-canvasFontSize,txtWidth,1.5*canvasFontSize);
    }
    //cntx.font=font;
    cntx.fillStyle=color;
    if(disableTextRotation || rotationAngle==0){
        cntx.textAlign=align;
        cntx.fillText(msg,x,y);
    }
    else{
        cntx.save();
        cntx.translate(x-graphFontSize/2,y);
        cntx.rotate(rotationAngle);
        cntx.textAlign="right";
        cntx.fillText(msg,0,0);
        cntx.restore();        
    }
    cntx.shadowBlur=0;
}

function clearCanvas(){
    cntx.clearRect(0,0,canvas.width,canvas.height);
}

function defineCanvasSize(){
    var w=window.outerWidth;
    var h=window.outerHeight;
    canvasWidth=Math.max(Math.min(w*0.5,originalCanvasWidth),minimumCanvasWidth);
    canvasHeight=Math.max(Math.min(h*0.4,originalCanvasHeight),minimumCanvasHeight);
    canvas.setAttribute("width",canvasWidth);
    canvas.setAttribute("height",canvasHeight);
}

function handleMouseMove(evt){
    calcMousePos(evt);
    var shiftKeyPressed=evt.shiftKey;
    var ctrlKeyPressed=evt.ctrlKey;
    var altKeyPressed=evt.altKey;
    mouseMoveDraw(shiftKeyPressed,ctrlKeyPressed,altKeyPressed);
    
    var message="Συντεταγμένες:"+mouseX+" , "+mouseY;
    if(debug)showDebugInfo(message);
    if($("#mouseCoords").length){
        showMouseCoords(message);
    }
}

function handleDoubleClick(evt){
    mouseButton=evt.button;
    calcMousePos(evt);
    //mouseDoubleClickDraw();
}

function handleMouseDown(evt){
    mouseButton=evt.button;
    calcMousePos(evt);
    mouseDownDraw();
}

function handleMouseUp(evt){
    //mouseButton=noMouseButton;
    calcMousePos(evt);
    mouseUpDraw();
}

function calcMousePos(evt){
    mousePos=getMousePos(evt);
    if($("#snapToGrid").is(":checked")){
        mouseX=parseInt(mousePos.x/gridStep)*gridStep;
        mouseY=parseInt(mousePos.y/gridStep)*gridStep;
    }
    else{
        mouseX=mousePos.x;
        mouseY=parseInt(mousePos.y);
    }
}

function getMousePos(evt) {
    var rect=canvas.getBoundingClientRect();
    return{
        x:evt.clientX-rect.left,
        y:evt.clientY-rect.top
    };
}