<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Designer</title>
  <meta name="description" 
          content="Designer Drawing Block.">
  <link rel="stylesheet" href="{{ static_url("style.css") }}">
  <link rel="stylesheet" href="{{ static_url("jquery-ui.min.css") }}">
  <!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
</head>

<body>
<div id="page_info">Designer Block. Generated by BORA 0.0.1</div>

{% if "background" in data %}
<img src="{{ static_url( data['background'] ) }}"></img>    
{% else %}
<img src="{{ static_url( 'background.png' ) }}"></img>
{% end %}

<button class="button showhide" onclick="showhide()">Show/Hide</button>

{% if data['style'] %}
{% for key in data['style'] %}
    
{% if data['style'][key]['type'] == "calc" %}
<div style='position: absolute; top: {{ data['style'][key]['top'] }}; left:{{
    data['style'][key]['left'] }}; width:{{ data['style'][key]['width'] }}px;
    height:{{ data['style'][key]['height'] }}px;' 
{% if "condition" in data['style'][key] %}
 data-cond="{{ data['style'][key]['condition'] }}"
{% else%}position
{% end%}
{% if "formula" in data['style'][key] %}
 data-formula="{{ data['style'][key]['formula'] }}"
{% else %}
{% end %}
{% if "lesser" in data['style'][key] %}
 data-lesser="{{ data['style'][key]['lesser'] }}"
{% else %}
{% end %}
{% if "larger" in data['style'][key] %}
 data-larger="{{ data['style'][key]['larger'] }}"
{% else %}
{% end %}
 data-type="{{ data['style'][key]['type'] }}" class='varbox' id="{{ key }}">
<p><span style="color: #000; font-size: {{ data['style'][key]['header']['size'] }}px; font-weight: {{ data['style'][key]['header']['weight'] }};" class="title">{{ data['style'][key]['header']['title'] }} </span><span style="color: #000; font-size: {{ data['style'][key]['unit']['size'] }}px; font-weight: {{ data['style'][key]['unit']['weight'] }};" class="varval"> XXX.XX <span class='unit_title' style="color: #000; font-size: {{ data['style'][key]['unit']['size'] }}px; font-weight: {{ data['style'][key]['unit']['weight'] }};" > {{ data['style'][key]['unit']['title'] }}</span></span></p>
</div>
{% else %}
{% end %}

    
{% if data['style'][key]['type'] == "data" %}
<div style='position: absolute; top: {{ data['style'][key]['top'] }}; left:{{
    data['style'][key]['left'] }}; width:{{ data['style'][key]['width'] }}px;
    height:{{ data['style'][key]['height'] }}px;' 
{% if "condition" in data['style'][key] %}
 data-cond="{{ data['style'][key]['condition'] }}"
{% else%}position
{% end%}
{% if "formula" in data['style'][key] %}
 data-formula="{{ data['style'][key]['formula'] }}"
{% else %}
{% end %}
{% if "lesser" in data['style'][key] %}
 data-lesser="{{ data['style'][key]['lesser'] }}"
{% else %}
{% end %}
{% if "larger" in data['style'][key] %}
 data-larger="{{ data['style'][key]['larger'] }}"
{% else %}
{% end %}
{% if "exponential" in data['style'][key] %}
 data-exponential="{{ data['style'][key]['exponential'] }}"
{% else %}
{% end %}
 data-type="{{ data['style'][key]['type'] }}" class='varbox' id="{{ key }}">
<p><span style="color: #000; font-size: {{ data['style'][key]['header']['size'] }}px; font-weight: {{ data['style'][key]['header']['weight'] }};" class="title">{{ data['style'][key]['header']['title'] }} </span><span style="color: #000; font-size: {{ data['style'][key]['unit']['size'] }}px; font-weight: {{ data['style'][key]['unit']['weight'] }};" class="varval"> XXX.XX <span class='unit_title' style="color: #000; font-size: {{ data['style'][key]['unit']['size'] }}px; font-weight: {{ data['style'][key]['unit']['weight'] }};" > {{ data['style'][key]['unit']['title'] }}</span></span></p>
</div>
{% else %}
{% end %}


{% if data['style'][key]['type'] == "valve" %}
<div style='position: absolute; top: {{ data['style'][key]['top'] }}; left:{{ data['style'][key]['left'] }}; width:{{ data['style'][key]['width'] }}px; height:{{ data['style'][key]['height'] }}px;' data-type="{{ data['style'][key]['type'] }}" data-off="{{ data['style'][key]['off'] }}" data-on="{{ data['style'][key]['on'] }}" class='varbox' id="{{ key }}">
<img width='100%' height='100%' src='{{ static_url("ventil_inactive.png") }}'></img>
</div>
{% else %}
{% end %}

{% if data['style'][key]['type'] == "integer-to-string" %}
<div style='position: absolute; top: {{ data['style'][key]['top'] }}; left:{{
    data['style'][key]['left'] }}; width:{{ data['style'][key]['width'] }}px;
    height:{{ data['style'][key]['height'] }}px;' 
{% if "cond" in data['style'][key] %}
 data-cond="{{ data['style'][key]['cond'] }}"
{% else%}
{% end%}
 data-dict="{{ data['style'][key]['dict'] }}" data-type="{{ data['style'][key]['type'] }}" class='varbox' id="{{ key }}">
<p><span style="color: #000; font-size: {{ data['style'][key]['header']['size'] }}px; font-weight: {{ data['style'][key]['header']['weight'] }};" class="title">{{ data['style'][key]['header']['title'] }} </span><br /><span style="color: #000; font-size: {{ data['style'][key]['unit']['size'] }}px; font-weight: {{ data['style'][key]['unit']['weight'] }};" class="varval">??? </span></p>
</div>
{% else %}
{% end %}

{% if data['style'][key]['type'] == "header" %}
<div style='position: absolute; top: {{ data['style'][key]['top'] }}; left:{{
    data['style'][key]['left'] }}; width:{{ data['style'][key]['width'] }}px;
    height:{{ data['style'][key]['height'] }}px;' data-type="{{ data['style'][key]['type'] }}" class='varbox' id="{{ key }}">
<p><span style="color: #000; font-size: {{ data['style'][key]['header']['size'] }}px; font-weight: {{ data['style'][key]['header']['weight'] }};" class="title">{{ data['style'][key]['header']['title'] }} </span></p>
</div>
{% else %}
{% end %}


{% end %}

{% else %}
{% end %}

    
    
<div id="tool-btn">
<span class="info">Variable</span>
<select id="varname" style="font-size:16pt;">
{% if data['index'] %}  
  {% for key in data['index'] %}
    {% if key != "time" %}  
    <option value={{ key }}>{{key}}</option>
    {% else %}
    {% end %}
  {% end %}
{% else %}
{% end %}
</select>
<span class="info">Type</span>
<select id="vartype" style="font-size:16pt;">
  <option value="data">data</option>
  <option value="valve">valve</option>
  <option value="integer-to-string">integer-to-string</option>
  <option value="header">header</option>
  <option value="calc">calc</option>
</select>
<span class="info">Attribute</span>
<select id="varattr" style="font-size:16pt;">
  <option value="normal">Normal</option>
  <option value="virtual">Virtual</option>
</select>
<span class="info">Virtual ID</span>
<input type="text" id="virtual_id_text" placeholder="text" style="font-size:16pt; width=100px;"/>
<br /><br />
<span class="info">Title</span>
<input type="text" id="elem_title_text" placeholder="text" style="font-size:16pt; width=100px;"/>
<input type="text" id="elem_title_size" placeholder="size" style="font-size:16pt; width=100px;"/>

<input type="text" id="elem_lesser_range" placeholder="smaller than (red)" style="font-size:16pt; width=100px;"/>
<input type="text" id="elem_larger_range" placeholder="larger than (red)" style="font-size:16pt; width=100px;"/>

<select id="elem_title_style" style="font-size:16pt;">
  <option value="400">normal</option>
  <option value="700">bold</option>
</select>
<br /><br />
<span class="info">Value</span>
<input type="text" id="elem_unit_text" placeholder="unit" style="font-size:16pt; width=100px;"/>
<input type="text" id="elem_unit_size" placeholder="size" style="font-size:16pt; width=100px;"/>

<input type="text" id="elem_condition_range" placeholder="condition (red)" style="font-size:16pt; width=100px;"/>
<input type="text" id="elem_formula" placeholder="Equation" style="font-size:16pt; width=100px;"/>
<select id="elem_unit_style" style="font-size:16pt;">
  <option value="400">normal</option>
  <option value="700">bold</option>
</select>
<span class="info">Exp</span><input type="checkbox" id="elem_exponential" style="font-size:16pt; width:20px; height:20px;"/>
<br /><br />
<button class="button save" onclick="backup()">Backup</button>
<button style="display:none;" id="buttonHighlight" class="button highlight" onclick="highlight({{ data['cache'] }})">Highlight</button>
<button class="button add" onclick="add()">Add</button>
<button class="button remove" onclick="myremove()">Remove</button>
<button class="button save" onclick="mysave({{ data['cache'] }})">Save</button>

</div>

    
<!-- java script -->
<script src="{{ static_url("jquery-1.12.3.min.js") }}"></script>
<script src="{{ static_url("jquery-ui.min.js") }}"></script>
<script>
function backup() {
    $.ajax({
        url: '/backup/',
        type: 'POST',
        success: function (response) {
            alert("Backup success!");
        },
        error: function () {
            console.log("Error.")
        }
    });
}    
    
function showhide() {
    console.log("Show or Hide");
    $("#tool-btn").toggle();
}
    
function add() {
    console.log("add element");
    title_text = $("#elem_title_text").val();
    title_text = title_text.trim();
    title_color = "#000000";
    //title_color = $("#elem_title_color").val();
    title_size = $("#elem_title_size").val();
    title_style = $("#elem_title_style option:selected").val();
    
    unit_text = $("#elem_unit_text").val();
    unit_text = unit_text.trim();
    unit_color = "#000";
    //unit_color = $("#elem_unit_color").val();
    unit_size = $("#elem_unit_size").val();
    unit_style = $("#elem_unit_style option:selected").val();
    
    data_condition = $("#elem_condition_range").val();
    //data_max = $("#elem_max_range").val();
    
    data_formula = $("#elem_formula").val();
    
    virtual_id = $("#virtual_id_text").val();
    virtual_id = virtual_id.trim();
    if (virtual_id == "") {
        virtual_id = "0";
    }
    
    if ($('#elem_exponential').is(":checked")) {
        data_exponential = true;
        $("#"+key).attr("data-exponential", true);
    } else {
        data_exponential = false;
        $("#"+key).attr("data-exponential", false);
    }
    
    data_lesser = $("#elem_lesser_range").val();
    data_larger = $("#elem_larger_range").val();
    
    varval = $( "#varname option:selected").val();
    varname = $("#varname option:selected").text();
    vartype = $("#vartype option:selected").val();
    varattr = $("#varattr option:selected").val();
    console.log("Selected");
    console.log(varattr);
    console.log(virtual_id);
    console.log(vartype);

    console.log(title_text, unit_text, varname, varval, vartype);
    
        
    if(($("#" + varname).length == 0) || (vartype=="header") || (vartype=="calc") || (varattr=="virtual")) {
        console.log("Must enter here");
        console.log(vartype);
        if (vartype == "data") {
            
            if (varattr=="virtual") {
                virtual_name = "virtual_" + varname + "_" + virtual_id;
                if ( $("#" + virtual_name).length > 0 ) {
                    for (i = 0; i < 50; i++) { 
                        virtual_name = "virtual_" + varname + "_" + i.toString();
                        console.log($("#" + virtual_name).length);
                        if (!$("#" + virtual_name).length){
                            break;
                         }
                    }
                }
                varname = virtual_name;
            }
            console.log("Data");
            html_text = "<div " +
                    "style='position: absolute; " +
                           "top:0; " +
                           "left:0;' " +
                    "class='varbox box_highlight' " +
                    "id='" + varname +
                    "' data-type='" + vartype +
                    "' data-cond='" + data_condition +
                    "' data-formula='" + data_formula +
                    "' data-lesser='" + data_lesser + 
 		            "' data-larger='" + data_larger + 
                    "' data-exponential='" + data_exponential + 
                    "'>" +
                    "<p>" +
                    "<span style='color: " + title_color + ";" + 
                    "font-size:" + title_size + "px; " + 
                    "font-weight: " + title_style + ";' " + 
                    "class='title'>" + title_text +
                    " </span>" + 
                    "<span style='color: " + unit_color + "; " + 
                           "font-size:" + unit_size + "px; " +
                           "font-weight: " + unit_style + ";' " + 
                    "class='varval'>XXX.XX </span>" + 
                    "<span class='unit_title'>" + unit_text + "</span></p>" +
        	    "</div>";
        } else if (vartype == "header") {
            console.log("Header");
            if (title_text.length > 0) { 
                var clean_text = title_text.replace(/[^a-zA-Z0-9 ]/g, "")
                varname = "header_" + clean_text.replace(/\s+/g, '-').toLowerCase();
                html_text = "<div " +
                        "style='position: absolute; " +
                               "top:0; " +
                               "left:0;' " +
                        "class='varbox box_highlight' " + 
                        "id='" + varname +
                        "' data-type='" + vartype +
                        "' data-cond='" + data_condition +
                        "' data-lesser='" + data_lesser + 
 		                "' data-larger='" + data_larger + 
                        "'>" +
                        "<p>" +
                        "<span style='color: " + title_color + ";" + 
                        "font-size:" + title_size + "px; " + 
                        "font-weight: " + title_style + ";' " + 
                        "class='title'>" + title_text +
                        " </span></p>" + 
        	        "</div>";
            
                $('#varname').append($('<option>', {
                    value: varname,
                    text: varname
                }));
                $('#varname').val(varname);
                $('#elem_title_text').removeClass('glowing_border');
            } else {
                console.log("NoInput");
                html_text = "";
                $('#elem_title_text').addClass('glowing_border');
            }
            

        } else if (vartype == "calc") {
            console.log("Calc");
            if (data_formula.length > 0) {
                var clean_text = virtual_id.replace(/[^a-zA-Z0-9 ]/g, "")
                varname = "calc_" + clean_text.replace(/\s+/g, '-').toLowerCase();
                html_text = "<div " +
                        "style='position: absolute; " +
                               "top:0; " +
                               "left:0;' " +
                        "class='varbox box_highlight' " + 
                        "id='" + varname +
                        "' data-type='" + vartype +
                        "' data-cond='" + data_condition +
                        "' data-lesser='" + data_lesser + 
 		                "' data-larger='" + data_larger +
                        "' data-formula='" + data_formula +
                        "'>" +
                        "<p>" +
                        "<span style='color: " + title_color + ";" + 
                        "font-size:" + title_size + "px; " + 
                        "font-weight: " + title_style + ";' " + 
                        "class='title'>" + title_text +
                        " </span>" + 
                        "<span style='color: " + unit_color + "; " + 
                               "font-size:" + unit_size + "px; " +
                               "font-weight: " + unit_style + ";' " + 
                        "class='varval'>XXX.XX </span>" + 
                        "<span class='unit_title'>" + unit_text + "</span></p>" +
        	        "</div>";
            
                $('#varname').append($('<option>', {
                    value: varname,
                    text: varname
                }));
                $('#varname').val(varname);
                $('#elem_formula').removeClass('glowing_border');
            } else {
                console.log("NoInput");
                html_text = "";
                $('#elem_formula').addClass('glowing_border');
            }

            
        } else if (vartype == "valve") {
            console.log("Ventil");
            if (unit_text.length > 0) {
                var token = JSON.parse(unit_text);
                data_on = token["on"];
                data_off = token["off"];

                 html_text = "<div " +
                    "style='position: absolute; " +
                           "top:0; " +
                           "left:0;' " +
                    "class='varbox box_highlight' " + 
                    "id='" + varname +
                    "' data-type='" + vartype +
                    "' data-cond='" + data_condition +
                    "' data-lesser='" + data_lesser + 
 		            "' data-larger='" + data_larger + 
 		            "' data-on='" + data_on + 
 		            "' data-off='" + data_off + 
                    "'>" +
 		            "<img width='100%' height='100%' " + 
                    "src='{{ static_url("ventil_inactive.png") }}'></img>" +
        	        "</div>";
                 $('#elem_unit_text').removeClass('glowing_border');
            } else{
                console.log("NoInput");
                html_text = "";
                $('#elem_unit_text').addClass('glowing_border');
            }
   
	} else if (vartype == "integer-to-string") {
            console.log("Integer2String");
            
            if (unit_text.length > 0) {
                html_text = "<div " +
                        "style='position: absolute; " +
                               "top:0; " +
                               "left:0;' " +
                        "class='varbox box_highlight' " + 
                        "id='" + varname +
                        "' data-type='" + vartype +
                        "' data-dict='" + unit_text +
                        "' data-cond='" + data_condition +
                        "'>" +
                        "<p>" +
                        "<span style='color: " + title_color + ";" + 
                        "font-size:" + title_size + "px; " + 
                        "font-weight: " + title_style + ";' " + 
                        "class='title'>" + title_text +
                        " </span><br />" + 
                        "<span style='color: " + unit_color + "; " + 
                               "font-size:" + unit_size + "px; " +
                               "font-weight: " + unit_style + ";' " + 
                        "class='varval'>??? </span>" + 
                        "</p>" +
        	            "</div>";
                $('#elem_unit_text').removeClass('glowing_border');
            } else {
                console.log("NoInput");
                html_text = "";
                $('#elem_unit_text').addClass('glowing_border');
            }
        }
        
        console.log("Adding element");
        console.log(html_text);
        if (html_text.length > 0) {
            $( "body" ).append(html_text);
            $("#"+varname).draggable();
            $("#"+varname).resizable();
        }
    }
} 
function myremove() {
    varname = $("#varname option:selected").text();
    vartype = $("#vartype option:selected").val();
    if($("#" + varname).length > 0) {
        $("#"+varname).remove();
        console.log(varname +" removed.");
    }
}

function mysave(data) {
    console.log("saving...");
    varname = $("#varname option:selected").text();
    varattr = $("#varattr option:selected").val();
    
    console.log("debug");
    console.log(varattr);
    var position = {};
    for(key in data) {
        var tmp = {};
        //console.log(key);
        if($("#" + key).length > 0 ) {
            tmp["ref"] = key;
            tmp["left"] = $("#"+key).css("left");
            tmp["top"] = $("#"+key).css("top");
            tmp["width"] = $("#"+key).width();
            tmp["height"] = $("#"+key).height();
            tmp["type"] = $("#"+key).attr("data-type");

            if ( $("#"+key).attr("data-type") == "data" ) {
                if ( (varname == key) && (varattr == "normal") ) {
            	    tmp["condition"] = $("#elem_condtion_range").val();
            	    tmp["formula"] = $("#elem_formula").val();
             	    tmp["lesser"] = $("#elem_lesser_range").val();
            	    tmp["larger"] = $("#elem_larger_range").val();
                    if ($('#elem_exponential').is(":checked")) {
                        tmp["exponential"] = true;
                    } else {
                        tmp["exponential"] = false;
                    }
            
            	    header = {};
            	    header["title"] = $("#elem_title_text").val().trim();
            	    //header["color"] = $(".title", "#"+key).css("color");
            	    header["size"] = $("#elem_title_size").val();
            	    header["weight"] = $("#elem_title_style").val();
         
                    // update field
                    $(".title", "#"+key).text(header["title"]);
                    $(".title", "#"+key).css("font-size", header["size"] + "px");
                    $(".title", "#"+key).css("font-weight", header["weight"]);
   
            	    unit = {};
            	    unit["title"] = $("#elem_unit_text").val().trim();
            	    //unit["color"] = $(".varval", "#"+key).css("color");
            	    unit["size"] = $("#elem_unit_size").val();
            	    unit["weight"] = $("#elem_unit_style").val();
                    $(".varval", "#"+key).css("font-size", unit["size"] + "px");
                    $(".unit_title", "#"+key).css("font-size", unit["size"] + "px");
                    $(".unit_title", "#"+key).css("font-weight", unit["weight"]);                    
                    $(".varval", "#"+key).css("font-weight", unit["weight"]);
                    
                } else {
            	    tmp["condition"] = $("#"+key).attr("data-cond");
            	    tmp["formula"] = $("#"+key).attr("data-formula");
             	    tmp["lesser"] = $("#"+key).attr("data-lesser");
            	    tmp["larger"] = $("#"+key).attr("data-larger");
                    tmp["exponential"] = $("#"+key).attr("data-exponential");
             	    //tmp["min"] = $("#"+key).attr("min");
           	    //tmp["max"] = $("#"+key).attr("max");
            
            	    header = {};
            	    header["title"] = $(".title", "#"+key).text();
            	    //header["color"] = $(".title", "#"+key).css("color");
            	    header["size"] = parseInt($(".title", "#"+key).css("font-size")) || 28;

                    var fontWeight = $(".title", "#"+key).css("font-weight");
                    if (fontWeight == 'bold' || fontWeight == '700') {
            	        header["weight"] = "700";
                    } else {
            	        header["weight"] = "400";
                    }
                    

            
            	    unit = {};
            	    unit["title"] = $(".unit_title", "#"+key).text();
            	    //unit["color"] = $(".varval", "#"+key).css("color");
            	    unit["size"] = parseInt($(".varval", "#"+key).css("font-size")) || 28;
                    
                    fontWeight = $(".varval", "#"+key).css("font-weight");
                    if (fontWeight == 'bold' || fontWeight == '700') {
            	        unit["weight"] = "700";
                    } else {
            	        unit["weight"] = "400";
                    }
                }

            	tmp["header"] = header;
            	tmp["unit"] = unit;
            //} else if ( $("#"+key).attr("data-type") == "header" ) {
	    //	header = {};
	    //	header["title"] = $(".title", "#"+key).text();
	    //	header["size"] = $(".title", "#"+key).css("font-size");
	    //	header["weight"] = $(".title", "#"+key).css("font-weight");
            //	tmp["header"] = header;
            } else if ( $("#"+key).attr("data-type") == "valve" ) {
                if ((varname == key) && (varattr == "normal")) {
                    var unit_text = $("#elem_unit_text").val();
                    unit_text = unit_text.trim();
                    var dtoken = JSON.parse(unit_text);
                    tmp["on"] = dtoken["on"];
                    tmp["off"] = dtoken["off"];
                } else {
            	    tmp["on"] = $("#"+key).attr("data-on");
            	    tmp["off"] = $("#"+key).attr("data-off");
                }
            } else if ( $("#"+key).attr("data-type") == "integer-to-string" ) {
                if ((varname == key)&& (varattr == "normal")) {
                    var unit_text = $("#elem_unit_text").val();
                    unit_text = unit_text.trim();
                    var data_condition = $("#elem_condition_range").val();

            	    tmp["cond"] = data_condition;
            	    tmp["dict"] = unit_text;
             	    
            	    header = {};
            	    header["title"] = $("#elem_title_text").val().trim();
            	    header["size"] = $("#elem_title_size").val();
            	    header["weight"] = $("#elem_title_style").val();
            
            	    unit = {};
            	    //unit["color"] = $(".varval", "#"+key).css("color");
            	    unit["size"] = $("#elem_unit_size").val();
            	    unit["weight"] = $("#elem_unit_style option:selected").val();
                } else {
            	    tmp["dict"] = $("#"+key).attr("data-dict");
            	    tmp["cond"] = $("#"+key).attr("data-cond");
            	    header = {};
            	    header["title"] = $(".title", "#"+key).text();
            	    //header["color"] = $(".title", "#"+key).css("color");
            	    header["size"] = $(".title", "#"+key).css("font-size");
            	    header["weight"] = $(".title", "#"+key).css("font-weight");
            
            	    unit = {};
            	    //unit["title"] = $(".unit_title", "#"+key).text();
            	    //unit["color"] = $(".varval", "#"+key).css("color");
            	    unit["size"] = $(".varval", "#"+key).css("font-size");
            	    unit["weight"] = $(".varval", "#"+key).css("font-weight");
                }

            	tmp["header"] = header;
            	tmp["unit"] = unit;
            }

            position[key] = tmp;
        }
    }
    
    var title_text = $("#elem_title_text").val();
    title_text = title_text.trim();
    
    var clean_text = title_text.replace(/[^a-zA-Z0-9 ]/g, "")
    var token = "header_" + clean_text.replace(/\s+/g, '-').toLowerCase();

    $('[id^=virtual_]').each(function( index ) {
        var tmp = {};
        var key = this.id;
        var ref = key.split("_");
        ref = ref[1];
        if (key != "virtual_id_text") {

            
        console.log("Check this");
        console.log(key);
        if($("#" + key).length > 0 ) {
            tmp["ref"] = ref;
            tmp["left"] = $("#"+key).css("left");
            tmp["top"] = $("#"+key).css("top");
            tmp["width"] = $("#"+key).width();
            tmp["height"] = $("#"+key).height();
            tmp["type"] = $("#"+key).attr("data-type");

            

            if ( $("#"+key).attr("data-type") == "data" ) {
                if (varname == key) {
            	    tmp["condition"] = $("#elem_condtion_range").val();
            	    tmp["formula"] = $("#elem_formula").val();
             	    tmp["lesser"] = $("#elem_lesser_range").val();
            	    tmp["larger"] = $("#elem_larger_range").val();
            
            	    header = {};
            	    header["title"] = $("#elem_title_text").val().trim();
            	    //header["color"] = $(".title", "#"+key).css("color");
            	    header["size"] = $("#elem_title_size").val();
            	    header["weight"] = $("#elem_title_style option:selected").val();
            
                    // update field
                    $(".title", "#"+key).css("font-size", header["size"] + "px");
                    $(".title", "#"+key).css("font-weight", header["weight"] + "px");
            
            	    unit = {};
            	    unit["title"] = $("#elem_unit_text").val().trim();
            	    //unit["color"] = $(".varval", "#"+key).css("color");
            	    unit["size"] = $("#elem_unit_size").val();
            	    unit["weight"] = $("#elem_unit_style option:selected").val();
                    
                    // update field
                    $(".varval", "#"+key).css("font-size", unit["size"] + "px");
                    $(".unit_title", "#"+key).css("font-size", unit["size"] + "px");
                    $(".varval", "#"+key).css("font-weight", unit["weight"] + "px");
                    
                } else {
            	    tmp["condition"] = $("#"+key).attr("data-cond");
            	    tmp["formula"] = $("#"+key).attr("data-formula");
             	    tmp["lesser"] = $("#"+key).attr("data-lesser");
            	    tmp["larger"] = $("#"+key).attr("data-larger");
             	    //tmp["min"] = $("#"+key).attr("min");
           	    //tmp["max"] = $("#"+key).attr("max");
            
            	    header = {};
            	    header["title"] = $(".title", "#"+key).text();
            	    //header["color"] = $(".title", "#"+key).css("color");
            	    header["size"] = parseInt($(".title", "#"+key).css("font-size")) || 28;
            	    header["weight"] = parseInt($(".title", "#"+key).css("font-weight")) || 28;
            
            	    unit = {};
            	    unit["title"] = $(".unit_title", "#"+key).text();
            	    //unit["color"] = $(".varval", "#"+key).css("color");
            	    unit["size"] = parseInt($(".varval", "#"+key).css("font-size")) || 28;
            	    unit["weight"] = parseInt($(".varval", "#"+key).css("font-weight")) || 28;
                }

            	tmp["header"] = header;
            	tmp["unit"] = unit;
            //} else if ( $("#"+key).attr("data-type") == "header" ) {
	    //	        header = {};
	    //	        header["title"] = $(".title", "#"+key).text();
	    //	        header["size"] = $(".title", "#"+key).css("font-size");
	    //	        header["weight"] = $(".title", "#"+key).css("font-weight");
            //	tmp["header"] = header;
            } else if ( $("#"+key).attr("data-type") == "valve" ) {
                if (varname == key) {
                    var unit_text = $("#elem_unit_text").val();
                    unit_text = unit_text.trim();
                    var dtoken = JSON.parse(unit_text);
                    tmp["on"] = dtoken["on"];
                    tmp["off"] = dtoken["off"];
                } else {
            	    tmp["on"] = $("#"+key).attr("data-on");
            	    tmp["off"] = $("#"+key).attr("data-off");
                }
            } else if ( $("#"+key).attr("data-type") == "integer-to-string" ) {
                if (varname == key) {
                    var unit_text = $("#elem_unit_text").val();
                    unit_text = unit_text.trim();
                    var data_condition = $("#elem_condition_range").val();

            	    tmp["cond"] = data_condition;
            	    tmp["dict"] = unit_text;
             	    
            	    header = {};
            	    header["title"] = $("#elem_title_text").val().trim();
            	    header["size"] = $("#elem_title_size").val();
            	    header["weight"] = $("#elem_title_style option:selected").val();
            
            	    unit = {};
            	    //unit["color"] = $(".varval", "#"+key).css("color");
            	    unit["size"] = $("#elem_unit_size").val();
            	    unit["weight"] = $("#elem_unit_style option:selected").val();
                } else {
            	    tmp["dict"] = $("#"+key).attr("data-dict");
            	    tmp["cond"] = $("#"+key).attr("data-cond");
            	    header = {};
            	    header["title"] = $(".title", "#"+key).text();
            	    //header["color"] = $(".title", "#"+key).css("color");
            	    header["size"] = $(".title", "#"+key).css("font-size");
            	    header["weight"] = $(".title", "#"+key).css("font-weight");
            
            	    unit = {};
            	    //unit["title"] = $(".unit_title", "#"+key).text();
            	    //unit["color"] = $(".varval", "#"+key).css("color");
            	    unit["size"] = $(".varval", "#"+key).css("font-size");
            	    unit["weight"] = $(".varval", "#"+key).css("font-weight");
                }

            	tmp["header"] = header;
            	tmp["unit"] = unit;
            }

            position[key] = tmp;
        }
        }
    });

    $('[id^=header_]').each(function( index ) {
        tmp = {};
        var hkey = $( this ).attr('id');
        console.log(hkey);
        tmp["left"] = $("#"+hkey).css("left");
        tmp["top"] = $("#"+hkey).css("top");
        tmp["width"] = $("#"+hkey).width();
        tmp["height"] = $("#"+hkey).height();
        tmp["type"] = $("#"+hkey).attr("data-type");

        header = {};
        if (token == hkey) {
            header["title"] = $("#elem_title_text").val().trim();
            //header["color"] = $(".title", "#"+key).css("color");
            header["size"] = $("#elem_title_size").val();
            header["weight"] = $("#elem_title_style option:selected").val();

            // update field
            $(".title", "#"+hkey).css("font-size", header["size"] + "px");
            $(".title", "#"+hkey).css("font-weight", header["weight"]);
            

        } else {
            header["title"] = $(".title", "#"+hkey).text();
            //header["color"] = $(".title", "#"+hkey).css("color");
            header["size"] = parseInt($(".title", "#"+hkey).css("font-size")) || 28;
            header["weight"] = parseInt($(".title", "#"+hkey).css("font-weight")) || 28;
            
	    var fontWeight = $(".title", "#"+hkey).css("font-weight");
	    if (fontWeight == 'bold' || fontWeight == '700') {
		header["weight"] = "700";
	    } else {
		header["weight"] = "400";
	    }
        }

        tmp["header"] = header;
        position[hkey] = tmp;
        //console.log( index + ": " + $( this ).text() );
    });
    
    $('[id^=calc_]').each(function( index ) {
      var tmp = {};
        var key = this.id;
        if($("#" + key).length > 0 ) {
            tmp["ref"] = "";
            tmp["left"] = $("#"+key).css("left");
            tmp["top"] = $("#"+key).css("top");
            tmp["width"] = $("#"+key).width();
            tmp["height"] = $("#"+key).height();
            tmp["type"] = $("#"+key).attr("data-type");

            if ( $("#"+key).attr("data-type") == "calc" ) {
                if (varname == key) {
            	    tmp["condition"] = $("#elem_condtion_range").val();
            	    tmp["formula"] = $("#elem_formula").val();
             	    tmp["lesser"] = $("#elem_lesser_range").val();
            	    tmp["larger"] = $("#elem_larger_range").val();
            
            	    header = {};
            	    header["title"] = $("#elem_title_text").val().trim();
            	    //header["color"] = $(".title", "#"+key).css("color");
            	    header["size"] = $("#elem_title_size").val();
            	    header["weight"] = $("#elem_title_style option:selected").val();
            
            	    unit = {};
            	    unit["title"] = $("#elem_unit_text").val().trim();
            	    //unit["color"] = $(".varval", "#"+key).css("color");
            	    unit["size"] = $("#elem_unit_size").val();
            	    unit["weight"] = $("#elem_unit_style option:selected").val();
                    
                } else {
            	    tmp["condition"] = $("#"+key).attr("data-cond");
            	    tmp["formula"] = $("#"+key).attr("data-formula");
             	    tmp["lesser"] = $("#"+key).attr("data-lesser");
            	    tmp["larger"] = $("#"+key).attr("data-larger");
             	    //tmp["min"] = $("#"+key).attr("min");
           	        //tmp["max"] = $("#"+key).attr("max");
            
            	    header = {};
            	    header["title"] = $(".title", "#"+key).text();
            	    //header["color"] = $(".title", "#"+key).css("color");
            	    header["size"] = $(".title", "#"+key).css("font-size");
            	    header["weight"] = $(".title", "#"+key).css("font-weight");
            
            	    unit = {};
            	    unit["title"] = $(".unit_title", "#"+key).text();
            	    //unit["color"] = $(".varval", "#"+key).css("color");
            	    unit["size"] = $(".varval", "#"+key).css("font-size");
            	    unit["weight"] = $(".varval", "#"+key).css("font-weight");
                }

            	tmp["header"] = header;
            	tmp["unit"] = unit;
            }
            position[key] = tmp;
        }
    });
    
    console.log("Check here");
    //console.log(position);
    var dataToSend = JSON.stringify(position);
    console.log(dataToSend);
    $.ajax({
        url: '/save/',
        type: 'POST',
        data: dataToSend,
        success: function (response) {
            var objresponse = JSON.parse(response);
            console.log(objresponse);
        },
        error: function () {
            console.log("Error.")
        }
    });
}
    
function highlight(mydata) {
    console.log("highlight");

    varname = $("#varname option:selected").text();
    vartype = $("#"+varname).attr("data-type");
    $("#vartype").val(vartype);
    //console.log(varname, vartype);

    // clear values
    $("#elem_title_text").val("");
    $("#elem_title_size").val("");
    $("#elem_unit_text").val("");
    $("#elem_unit_size").val("");
    $("#elem_lesser_range").val("");
    $("#elem_larger_range").val("");
    $("#elem_condition_range").val("");
    $("#elem_formula").val("");
    $("#virtual_id_text").val("");
    $('#elem_exponential').prop('checked', false); 
    $('#elem_unit_text').removeClass('glowing_border');  
    $('#elem_title_text').removeClass('glowing_border');
    $('#elem_formula').removeClass('glowing_border');
 
    if (vartype == "header") {
        $("#elem_title_text").val($("#"+varname + " .title").text());
        $("#elem_title_size").val(parseInt($("#"+varname + " .title").css("font-size")));
        
        $("#elem_title_style").val("400");
        var fontWeight = $("#"+varname + " .title").css("font-weight");
        if (fontWeight == 'bold' || fontWeight == '700') {
            $("#elem_title_style").val("700"); 
        }
        
        $("#elem_unit_style").val("400");
        var fontWeight = $("#"+varname + " .varval").css("font-weight");
        if (fontWeight == 'bold' || fontWeight == '700') {
            $("#elem_unit_style").val("700"); 
        }
    } else if (vartype == "data") {
        
        if ($("#"+varname).attr("data-exponential")) {
            $('#elem_exponential').prop('checked', true); 
        } else {
            $('#elem_exponential').prop('checked', false); 
        }
        
        $("#elem_title_text").val($("#"+varname + " .title").text().trim());
        $("#elem_title_size").val("");
        if ($("#"+varname + " .title").css("font-size")) {
            $("#elem_title_size").val(parseFloat($("#"+varname + " .title").css("font-size")));
        }
        $("#elem_unit_text").val($("#"+varname + " .unit_title").text().trim());
        $("#elem_unit_size").val("");
        if ($("#"+varname + " .varval").css("font-size")) {
            $("#elem_unit_size").val(parseFloat($("#"+varname + " .varval").css("font-size")));
        }
        $("#elem_lesser_range").val("");
        if ($("#"+varname).attr("data-lesser") ) {
            $("#elem_lesser_range").val($("#"+varname).attr("data-lesser"));
        }
        $("#elem_larger_range").val("");
        if ($("#"+varname).attr("data-larger") ) {
            $("#elem_larger_range").val($("#"+varname).attr("data-larger"));
        }
        $("#elem_condition_range").val("");
        if ($("#"+varname).attr("data-cond") ) {
            $("#elem_condition_range").val($("#"+varname).attr("data-cond"));
        }
        $("#elem_formula").val("");
        if ($("#"+varname).attr("data-formula") ) {
            $("#elem_formula").val($("#"+varname).attr("data-formula"));
        }
        
        $("#elem_title_style").val("400");
        var fontWeight = $("#"+varname + " .title").css("font-weight");
        if (fontWeight == 'bold' || fontWeight == '700') {
            $("#elem_title_style").val("700"); 
        }
        
        $("#elem_unit_style").val("400");
        var fontWeight = $("#"+varname + " .varval").css("font-weight");
        if (fontWeight == 'bold' || fontWeight == '700') {
            $("#elem_unit_style").val("700"); 
        }
   

    } else if (vartype == "calc") {
        var vid = varname.split("_");
        vid = vid[1];
        $("#virtual_id_text").val(vid);
        
        
        $("#elem_title_text").val($("#"+varname + " .title").text().trim());
        $("#elem_title_size").val("");
        if ($("#"+varname + " .title").css("font-size")) {
            $("#elem_title_size").val(parseFloat($("#"+varname + " .title").css("font-size")));
        }
        $("#elem_unit_text").val($("#"+varname + " .unit_title").text().trim());
        $("#elem_unit_size").val("");
        if ($("#"+varname + " .varval").css("font-size")) {
            $("#elem_unit_size").val(parseFloat($("#"+varname + " .varval").css("font-size")));
        }
        $("#elem_lesser_range").val("");
        if ($("#"+varname).attr("data-lesser") ) {
            $("#elem_lesser_range").val($("#"+varname).attr("data-lesser"));
        }
        $("#elem_larger_range").val("");
        if ($("#"+varname).attr("data-larger") ) {
            $("#elem_larger_range").val($("#"+varname).attr("data-larger"));
        }
        $("#elem_condition_range").val("");
        if ($("#"+varname).attr("data-cond") ) {
            $("#elem_condition_range").val($("#"+varname).attr("data-cond"));
        }
        $("#elem_formula").val("");
        if ($("#"+varname).attr("data-formula") ) {
            $("#elem_formula").val($("#"+varname).attr("data-formula"));
        }
    } else if (vartype == "valve") {
        $("#elem_unit_text").val("{'on':"+$("#"+varname).attr("data-on") + ",'off':"+$("#"+varname).attr("data-off")+"}" );
    } else if (vartype == "integer-to-string") {
        $("#elem_title_text").val($("#"+varname + " .title").text().trim());
        $("#elem_title_size").val("");
        if ($("#"+varname + " .title").css("font-size")) {
            $("#elem_title_size").val(parseFloat($("#"+varname + " .title").css("font-size")));
        }
        $("#elem_unit_text").val($("#"+varname).attr("data-dict"));
        $("#elem_condition_range").val("");
        if ($("#"+varname).attr("data-cond") ) {
            $("#elem_condition_range").val($("#"+varname).attr("data-cond"));
        }
        
    }

    for(key in mydata) {
        //console.log(key, varname);
        if(($("#" + key).length > 0) && (key == varname) ) {
            //if (!$("#"+varname).hasClass("box_highlight")) {
            $("#"+key).addClass("box_highlight");
            //}
        } else {
            $("#"+key).removeClass("box_highlight");
        }
    }
}


jQuery(window).load(function () {
    $("#varname > option").each(function() {
        if ($('#'+this.text).length){
            $('#'+this.text).draggable();
            $('#'+this.text).resizable(); 
            $('#'+this.text).click(function() {
                //console.log($(this).attr("id")); 
                $("#varname").val($(this).attr("id"));
                $("#buttonHighlight").trigger("click"); 
            });
        }
        //console.log(this.text + ' ' + this.value);
    });
    
    $("#buttonHighlight").trigger("click"); 

    $("#varname").change(function() {
        varval = $( "#varname option:selected").val();
        varname = $("#varname option:selected").text();
        console.log(varval, varname);
        $("#buttonHighlight").trigger("click"); 
    });
    
    $("#vartype").change(function() {
        vartype = $("#vartype option:selected").val();
        if ((vartype == "header") || (vartype == "calc")) {
            $("#varname").val("");
            // clear values
	        $("#elem_title_text").val("");
	        $("#elem_title_size").val("");
	        $("#elem_unit_text").val("");
	        $("#elem_unit_size").val("");
	        $("#elem_lesser_range").val("");
	        $("#elem_larger_range").val("");
	        $("#elem_condition_range").val("");
	        $("#elem_formula").val("");
        }       
    });


});
</script>
</body>
</html>
