﻿var this_page = 1;
var pageCount = 1;
var pageCount_saved = 0;

function pagerUpdate() {

    if (!hasKey()) {
        document.getElementById("pagination").style.display = "none";       
    }
    else {
        document.getElementById("add_page").style.display = "block";      
        document.getElementById("LinkKeyTable").style.display = "block";
            
        pageCountUpdate();
    }
    
}

function pageCountUpdate() {
    if (pageCount_saved != pageCount) {
        pageCount_saved = pageCount;

        document.getElementById("pagination").style.display = "block";
        $("#pagination").pagination(pageCount, {
            callback: pageselectCallback,
            items_per_page: 1,
            next_text: '&gt;',
            prev_text: '&lt;',
            num_edge_entries: 2,
            num_display_entries: 7,
            current_page: this_page - 1
        });
   }

}

function pageselectCallback(page_index, jq) {
    this_page = page_index + 1;

    Set_Cookie('pomipage', this_page, 300, '/', '', '');
   
    pomi_clearCanvas();

    siteDB.drawing = new Array();

    lastSynx = 0;
    arrayKey = [];
    timeCount = 0;

    sid = Math.floor(Math.random() * 1000000001);

    return false;
}



function getQueryKey() {

    return location.search.replace('?', '');
}


function setLineWidth(num, reCook) {

    resetCanvasLineWidth(num, reCook);

    function resetCanvasLineWidth(lineWidth, reCook) {

        if (!hasKey()) {
            return;
        }

        if (reCook) {
            Set_Cookie('LineWidth', num, 300, '/', '', '');
        }
        context.lineWidth = canvasScale * lineWidth;
        contextI.lineWidth = canvasScale * lineWidth;
        canvasPainter.drawWidth = canvasScale * lineWidth;
    }



    fig = 'w1';

    switch (parseInt(num)) {
        case 1:
            fig = 'w1';
            break;
        case 2:
            fig = 'w3';
            break;
        case 4:
            fig = 'w5';
            break;
        case 6:
            fig = 'w7';
            break;
    }


    if (wfig_selected == fig) {
        return;
    }

    document.getElementById("Btn_w1").src = lng_subfolder + "img_t/" + getLinewidthByFig("w1") + "_1.PNG";
    document.getElementById("Btn_w3").src = lng_subfolder + "img_t/" + getLinewidthByFig("w3") + "_1.PNG";
    document.getElementById("Btn_w5").src = lng_subfolder + "img_t/" + getLinewidthByFig("w5") + "_1.PNG";
    document.getElementById("Btn_w7").src = lng_subfolder + "img_t/" + getLinewidthByFig("w7") + "_1.PNG";

    document.getElementById("Btn_" + fig).src = lng_subfolder + "img_t/" + getLinewidthByFig(fig) + "_3.PNG";

    wfig_selected = fig;
    unfocus();

}

function unfocus() {

    document.getElementById("linkBlank5").focus();
}


function getWidthFig() {

    var num = parseInt(canvasPainter.drawWidth) / canvasScale;

    fig = 'w3';

    switch (num) {
        case 1:
            fig = 'w1';
            break;
        case 2:
            fig = 'w3';
            break;
        case 4:
            fig = 'w5';
            break;
        case 6:
            fig = 'w7';
            break;
    }

    return fig;
}



function poFlood() {

    if (document.getElementById("chkBodyColor").checked) {
        if (hasKey()) {
            canvasPainter.isFilled = 1;
        }
        document.getElementById("spanFillColorPkr").style.visibility = "visible";
    }
    else {
        if (hasKey()) {
            canvasPainter.isFilled = 0;
        }
        document.getElementById("spanFillColorPkr").style.visibility = "hidden";
    }
}

function reSize(reCook) {

    if (reCook) {
        if (canvasScale === 2) {
            Set_Cookie('zoom100', true, 300, '/', '', '');
        }
        else {
            Set_Cookie('zoom100', false, 300, '/', '', '');
        }
    }

    //wfig_selected = getWidthFig();
    document.getElementById("Btn_w1").src = lng_subfolder + "img_t/" + getLinewidthByFig("w1") + "_1.PNG";
    document.getElementById("Btn_w3").src = lng_subfolder + "img_t/" + getLinewidthByFig("w3") + "_1.PNG";
    document.getElementById("Btn_w5").src = lng_subfolder + "img_t/" + getLinewidthByFig("w5") + "_1.PNG";
    document.getElementById("Btn_w7").src = lng_subfolder + "img_t/" + getLinewidthByFig("w7") + "_1.PNG";
    document.getElementById("Btn_" + wfig_selected).src = lng_subfolder + "img_t/" + getLinewidthByFig(wfig_selected) + "_3.PNG";

    document.getElementById("canvasInterface").style.width = parseInt(canvasScale * canvasWith) + "px";
    document.getElementById("canvas").style.width = parseInt(canvasScale * canvasWith) + "px";
    document.getElementById("canvasInterface").style.height = parseInt(canvasScale * canvasHeight) + "px";
    document.getElementById("canvas").style.height = parseInt(canvasScale * canvasHeight) + "px";

    document.getElementById("canvasInterface").width = canvasScale * canvasWith;
    document.getElementById("canvas").width = canvasScale * canvasWith;
    document.getElementById("canvasInterface").height = canvasScale * canvasHeight;
    document.getElementById("canvas").height = canvasScale * canvasHeight;

    document.getElementById("canvasContainer").style.height = parseInt(canvasScale * canvasHeight) + "px";
    document.getElementById("canvasContainer").style.width = parseInt(canvasScale * canvasWith) + "px";


    if (canvasScale == 2) {
        canvasPainter.drawWidth = canvasPainter.drawWidth * 2;
        canvasPainter.erzWidth = canvasPainter.erzWidth * 2;
        document.getElementById("Pomicopyright").style.top = (textoutHeightDelts + 1580) + "px";
        document.getElementById("tableLng").style.top = (textoutHeightDelts + 1531) + "px";
          
    }
    else {
        canvasPainter.drawWidth = canvasPainter.drawWidth / 2;
        canvasPainter.erzWidth = canvasPainter.erzWidth / 2;
        document.getElementById("Pomicopyright").style.top = (textoutHeightDelts + 880) + "px";
        document.getElementById("tableLng").style.top = (textoutHeightDelts + 831) + "px";
        
    }
    
    contextI.lineWidth = canvasPainter.drawWidth;
    context.lineWidth = canvasPainter.drawWidth;

    color = 'rgb(' + canvasPainter.LineColorR + ',' + canvasPainter.LineColorG + ',' + canvasPainter.LineColorB + ')';
    context.strokeStyle = color;
    contextI.strokeStyle = color;

    color = 'rgb(' + canvasPainter.FillColorR + ',' + canvasPainter.FillColorG + ',' + canvasPainter.FillColorB + ')';
    context.fillStyle = color;
    contextI.fillStyle = color;

    context.lineJoin = "round";
    contextI.lineJoin = "round";
    context.lineCap = "round";
    contextI.lineCap = "round";

    siteDB.paintDrawing();
}

function showPanelFillColor(show) {

    if (show) {

        document.getElementById("chkBodyColor").style.display = "block";
        document.getElementById("SpanFiller").style.display = "block";
        if (document.getElementById("chkBodyColor").checked) {
            document.getElementById("spanFillColorPkr").style.visibility = "visible";
        }
        else {            
            document.getElementById("spanFillColorPkr").style.visibility = "hidden";
        }
    } else {
        document.getElementById("chkBodyColor").style.display = "none";
        document.getElementById("SpanFiller").style.display = "none";
        document.getElementById("spanFillColorPkr").style.visibility = "hidden";

    }



}

  fig_saved = '';
  wfig_selected = '';

  function poOver(fig) {

      if (fig == 'zoom' && canvasScale == 1) {
          document.getElementById("Btn_zoom").src = lng_subfolder + "img_t/zoom_2.PNG";
          return;
      }

      if (fig == 'zoom' && canvasScale == 2) {
          document.getElementById("Btn_zoom").src = lng_subfolder + "img_t/zoom_3.PNG";
          return;
      }

    if (fig_saved == fig || wfig_selected == fig) {
        return;
    }

    if ('flood' == fig && canvasPainter.isFilled == 1) {
        return;
    }

    if (fig == 'w1' || fig == 'w3' || fig == 'w5' || fig == 'w7') {
        document.getElementById("Btn_" + fig).src = lng_subfolder + "img_t/" + getLinewidthByFig(fig) + "_2.PNG";
    }
    else {
        document.getElementById("Btn_" + fig).src = lng_subfolder + "img_t/" + fig + "_2.PNG";
    }

}

function poOut(fig) {

    if (fig == 'zoom' && canvasScale == 1) {
        document.getElementById("Btn_zoom").src = lng_subfolder + "img_t/zoom_1.PNG";
        return;
    }

    if (fig == 'zoom' && canvasScale == 2) {
        document.getElementById("Btn_zoom").src = lng_subfolder + "img_t/zoom_3.PNG";
        return;
    }

    if ('flood' == fig && canvasPainter.isFilled == 1) {
        return;
    }

    if (fig_saved == fig || wfig_selected == fig) {
        return;
    }

    if(fig == 'w1' || fig == 'w3' || fig == 'w5' || fig == 'w7')
    {
        document.getElementById("Btn_" + fig).src = lng_subfolder + "img_t/" + getLinewidthByFig(fig) + "_1.PNG";
    }
    else {
        document.getElementById("Btn_" + fig).src = lng_subfolder + "img_t/" + fig + "_1.PNG";
    }

}

function poClick(fig) {

    if (!hasKey()) {
        return;
    }

    if ("text" == fig) {
        OpenTextout(); 
        return;
    }

    if (fig_saved == fig) {
        if (fig != 'pencil') {
            poClick('pencil');
        }
        else {
            poClick('line');
        }
       return;
   }

   if (fig != 'lastik') {
       Set_Cookie('clickAction', fig, 300, '/', '', '');

       document.getElementById("Btn_w1").style.display = "block";
       document.getElementById("Btn_w3").style.display = "block";
       document.getElementById("Btn_w5").style.display = "block";
       document.getElementById("Btn_w7").style.display = "block";
   }
   else {
       document.getElementById("Btn_w1").style.display = "none";
       document.getElementById("Btn_w3").style.display = "none";
       document.getElementById("Btn_w5").style.display = "none";
       document.getElementById("Btn_w7").style.display = "none";
   }

   document.getElementById("Btn_line").src = lng_subfolder + "img_t/line_1.PNG";
   document.getElementById("Btn_pencil").src = lng_subfolder + "img_t/pencil_1.PNG";
   document.getElementById("Btn_circle").src = lng_subfolder + "img_t/circle_1.PNG";
   document.getElementById("Btn_rect").src = lng_subfolder + "img_t/rect_1.PNG";
   document.getElementById("Btn_oval").src = lng_subfolder + "img_t/oval_1.PNG";
   document.getElementById("Btn_polygon").src = lng_subfolder + "img_t/polygon_1.PNG";
   document.getElementById("Btn_arror").src = lng_subfolder + "img_t/arror_1.PNG";
   document.getElementById("Btn_lastik").src = lng_subfolder + "img_t/lastik_1.PNG";

  

    document.getElementById("Btn_" + fig).src = lng_subfolder + "img_t/" + fig + "_3.PNG";

    fig_saved = fig;
    //document.getElementById("linkSmallLogo").focus();
    unfocus();

    switch(fig)
    {
        case 'line':
            action = 3;
            break;
        case 'pencil':
            action = 1;
            break;
        case 'circle':
            action = 7;
            break;
        case 'rect':
            action = 5;
            break;
       case 'oval':
            action = 6;
            break;
       case 'polygon':
            action = 0;
            break;
       case 'arror':
            action = 4;
            break;
       case 'lastik':
            action = 2;
            break;
   }

    wfig_selected = getWidthFig(); 
    document.getElementById("Btn_w1").src = lng_subfolder + "img_t/" + getLinewidthByFig("w1") + "_1.PNG";
    document.getElementById("Btn_w3").src = lng_subfolder + "img_t/" + getLinewidthByFig("w3") + "_1.PNG";
    document.getElementById("Btn_w5").src = lng_subfolder + "img_t/" + getLinewidthByFig("w5") + "_1.PNG";
    document.getElementById("Btn_w7").src = lng_subfolder + "img_t/" + getLinewidthByFig("w7") + "_1.PNG";
    document.getElementById("Btn_" + wfig_selected).src = lng_subfolder + "img_t/" + getLinewidthByFig(wfig_selected) + "_3.PNG";

    if (action == canvasPainter.curDrawAction) {
        return;
    }

    poFlood();

    switch (action) {
        case 2:
            document.getElementById("toolPanelBorderColor").style.display = "none";
            showPanelFillColor(false);
            break;

        case 3:
        case 4:
            showPanelFillColor(false);
            document.getElementById("toolPanelBorderColor").style.display = "block";
            break;

        default:
            document.getElementById("toolPanelBorderColor").style.display = "block";
            showPanelFillColor(true);
            $(".ColorExt").css("display", "block");
            break;
    }

    canvasPainter.curDrawAction = action;
}

function getLinewidthByFig(fig) {

    var new_fig = '';

    switch (fig) {
        case 'w1':
            new_fig = 'w3';
            break;
        case 'w3':
            new_fig = 'w5';
            break;
        case 'w5':
            new_fig = 'w7';
            break;
        case 'w7':
            new_fig = 'w11';
            break;
    }

    if (canvasScale == 1) {
        new_fig = fig;
    }

    return new_fig;
}

function makeNewKey() {
    document.getElementById("ideaBtn").style.display = "none";
    document.getElementById("imgWait").style.display = "block";

    $.ajax({
        type: 'POST',
        dataType: 'text',
        url: lng_subfolder + 'draw.asmx/getK',
        data: {
            solt: solt,
            lng: lng
        },
        success: function (response) {

            window.location.href = '?' + response;

        },
        error: function (response) {

        }
    });

}

//function mouseDownBody(e) {

//    if (isInCanvase(e)) {
//        canvasPainter.mouseDownActionPerformed(e);
//    }
//}

function mouseUpBody(e) {

    if (isInCanvase(e)) {
        canvasPainter.mouseUpActionPerformed(e);
    }
    else if (canvasPainter.curDrawAction == 0) {
        completePolygon();
    }
    else if (canvasPainter.curDrawAction == 1) {
        if (canvasPainter.mouseMoveTrigger) {
            contextI.clearRect(0, 0, canvasScale * canvasWith, canvasScale * canvasHeight);
            siteDB.recordAction();

            SendDrawL(siteDB.drawing[canvasPainter.currentNodeIndex]);
            siteDB.paintDrawing();

            canvasPainter.mouseMoveTrigger = false;
        }
    }
    else
    {
        canvasPainter.currentNodeIndex = -1;
        canvasPainter.fatNode = false;
        canvasPainter.mouseMoveTrigger = false;  
    }

}

function isInCanvase(e) {


    //canvasContainerLeft = $('#canvasContainer').position().left;
    canvasContainerLeft = $('#canvasContainer').offset().left;
    canvasContainerTop = $('#canvasContainer').offset().top + textoutHeightDelts;


    //document.title = canvasContainerLeft; 
    //document.title = " YYYrandom ::: " + Math.floor(Math.random() * 1000000001);


    var posx = 0;
    var posy = 0;

    if (!e) var e = window.event;

    if (e.pageX || e.pageY) {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY) {
        posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }

    if (canvasContainerTop > posy ||
        canvasScale * canvasHeight + canvasContainerTop < posy ||
        canvasContainerLeft > posx ||
        canvasContainerLeft + canvasScale * canvasWith < posx) {
            return false;
        }

    return true;

}

var isInCanvaseFlag = false;
function mouseMoveBody(e) {
    

    if (isInCanvase(e)) {
        canvasPainter.mouseMoveActionPerformed(e);
        isInCanvaseFlag = true;
    }
    else if (isInCanvaseFlag) {
        contextI.clearRect(0, 0, canvasScale * canvasWith, canvasScale * canvasHeight);
        isInCanvaseFlag = false;
    }
}

var DebugCount = 0;

function addPage() {

    document.getElementById("add_page").style.display = "none";
    document.getElementById("idRemoveLastNodes").style.display = "none";

    $.ajax({
        type: 'POST',
        dataType: 'text',
        url: lng_subfolder + 'draw.asmx/addPage',
        data: {
            solt: solt
        },
        success: function (response) {
            pageCount = parseInt(response);
            this_page = pageCount;
            pageCountUpdate();
            pageselectCallback(this_page - 1, "jq");

            document.getElementById("add_page").style.display = "block";
        }
    });

    

}
