'use strict';
var txtBold = "normal", txtItaclic = "normal", txtUnderline = "none", txtAlign = "start", clicked = 0;
//var tm_size_spinner = $("#textmarkup_size_spinner").spinner();
//var tm_angle_spinner = $("#textmarkup_angle_spinner").spinner();

function TextMarkup(args) {
    /*jshint validthis: true */
    var options = {
        position: {
            //top: '100px',
            //left: '100px',
            //bottom: '100px',
            //right: '100px'
        },
        center: true,
        id: 0
    };
    $.extend(options, args);
    this.options = options;
};

TextMarkup.prototype.init = function() {
	$("#chkMarkup").on('change', function(){
		if (this.checked) {
			$('#markupBody').css('height', '305px');
			$("#markuppt_options").show();
		} else {
			$('#markupBody').css('height', '270px');
			$("#markuppt_options").hide();
		}
	});

	$("#chkHalo").on('change', function(){
		if (this.checked) {
			$("#halo_size").css('display', "inline-block");
			$("#halo_color").css('display', "inline-block");
		} else {
			$("#halo_size").css('display', "none");
			$("#halo_color").css('display', "none");
		}
	});
	
	$('#textmarkup_color_picker').colpick({
		layout:	'hex',
		submit:	0,
		colorScheme: 'dark',
		onChange: function(hsb,hex,rgb,el,bySetColor) {
			$(el).css('border-right-color','#'+hex);
			// Fill the text box just if the color was set using the picker, and not the colpickSetColor function.
			if(!bySetColor) {
				$(el).val(hex);
				$('#textmarkup_color_picker').val("#"+String(hex).toUpperCase())
			}
		}
	}).keyup(function(){
		$(this).colpickSetColor("#" + String(this.value).toUpperCase());
	}).mouseup(function(){
		$(this).colpickSetColor("#" + String(this.value).toUpperCase());
	});

	$('#markuppt_color_picker').colpick({
		layout:	'hex',
		submit:	0,
		colorScheme: 'dark',
		onChange: function(hsb,hex,rgb,el,bySetColor) {
			$(el).css('border-right-color','#'+hex);
			// Fill the text box just if the color was set using the picker, and not the colpickSetColor function.
			if(!bySetColor) {
				$(el).val(hex);
				$('#markuppt_color_picker').val("#"+String(hex).toUpperCase())
			}
		}
	}).keyup(function(){
		$(this).colpickSetColor("#" + String(this.value).toUpperCase());
	}).mouseup(function(){
		$(this).colpickSetColor("#" + String(this.value).toUpperCase());
	});		

	$('#halo_color_picker').colpick({
		layout:	'hex',
		submit:	0,
		colorScheme: 'dark',
		onChange: function(hsb,hex,rgb,el,bySetColor) {
			$(el).css('border-right-color','#'+hex);
			// Fill the text box just if the color was set using the picker, and not the colpickSetColor function.
			if(!bySetColor) {
				$(el).val(hex);
				$('#halo_color_picker').val("#"+String(hex).toUpperCase())
			}
		}
	}).keyup(function(){
		$(this).colpickSetColor("#" + String(this.value).toUpperCase());
	}).mouseup(function(){
		$(this).colpickSetColor("#" + String(this.value).toUpperCase());
	});
	
	$("#btnTextmarkup").click(function() {
		if (clicked == 0) {
			//DOT.measurement.setTool("location",false);
			//DOT.measurement.setTool("area",false);
			//DOT.measurement.setTool("distance",false);
			$("#btnTextmarkup").css("background-color","#e7e7e7");
			DOT.DOTClickHandler.remove();
			DOT.txtmarkClickHandler = DOT.map.on("click", DOT.txtmarkClick);
			DOT.toolActivated = true;
			clicked = 1;
		} else {
			$("#btnTextmarkup").css("background-color","white");
			DOT.txtmarkClickHandler.remove();
			DOT.DOTClickHandler = DOT.map.on("click", DOT.onPlanClick);
			DOT.toolActivated  = false;
			clicked = 0;
		}
	});
	
	$("#btnTextmarkupErase").click(function(e) {
		DOT.textmarkupGraphic.removeAll();
		if (clicked == 1) {
			DOT.txtmarkClickHandler.remove();
			DOT.DOTClickHandler = DOT.map.on("click", DOT.onPlanClick);
			DOT.toolActivated = false;
			//DOT.map.setInfoWindowOnClick(true);
			DOT.map.popup.autoOpenEnabled = true;
			$("#btnTextmarkup").css("background-color","white");
			clicked = 0;
		}
	});
	
	$("#btnTxtBold").click(function(e){
		if (txtBold == "normal") txtBold = "bold";
		else
			txtBold = "normal";
	});
	
	$("#btnTxtItalic").click(function(e){
		if (txtItaclic == "normal") txtItaclic = "italic";
		else 
			txtItaclic = "normal";
	});
	
	$("#btnTxtUnderline").click(function(e){
		if (txtUnderline == "none") txtUnderline = "underline";
		else 
			txtUnderline = "none";
	});

	$("#btnAlignLeft").click(function(e){
		txtAlign = "start";
	});
	
	$("#btnAlignCenter").click(function(e){
		txtAlign = "middle";
	});

	$("#btnAlignRight").click(function(e){
		txtAlign = "end";
	});
	$('#markupPanel').on('shown.bs.collapse', function () {
		//DOT.DOTClickHandler.remove();
		//DOT.map.setInfoWindowOnClick(false);
		//DOT.txtmarkClickHandler = DOT.map.on("click", DOT.txtmarkClick);
		DOT.map.popup.autoOpenEnabled = false;
	});

	$('#markupPanel').on('hidden.bs.collapse', function () {
		if (clicked == 1) {
			DOT.txtmarkClickHandler.remove();
			DOT.DOTClickHandler = DOT.map.on("click", DOT.onPlanClick);
			//DOT.map.setInfoWindowOnClick(true);
			DOT.map.popup.autoOpenEnabled = true;
			$("#btnTextmarkup").css("background-color","white");
			clicked = 0;
			DOT.toolActivated = false;
		}
	});
};

