'use strict';
/*dojo.require("esri.dijit.Print");
dojo.require("esri.tasks.PrintTask"); 
dojo.require("esri.tasks.PrintTemplate");
dojo.require("esri.tasks.LegendLayer");
dojo.require("esri.request");
dojo.require("dojo._base.array");
require([
	"esri/tasks/PrintTask", 
	"esri/tasks/support/PrintTemplate",
	"esri/tasks/support/PrintParameters", 
	"esri/tasks/support/LegendLayer",
	"esri/request",
	"dojo/_base/array"
], function (PrintTask, PrintTemplate, PrintParameters, LegendLayer, esriRequest, arrayUtils) 
{*/
function Printing(args) {
	var options = {
		position: {
			//top: '100px',
			//left: '100px',
			//bottom: '100px',
			//right: '100px'
		},
		center: true,
		id: 0
	};
	$.extend(options, args);
	this.options = options;
}

Printing.prototype.init = function() {
		
		$("#printButton").click(function() {
			$("#printButton").attr('disabled','disabled');
			$("#Printing").css("height","250px");
			//var printTask 		= new esri.tasks.PrintTask(services_application.printTask);
			//var printTemplate	= new esri.tasks.PrintTemplate();
			//DOT.gPrintTask.url = services_application.printTask;
			DOT.gPrintTemplate.format	= "PDF";
			DOT.gPrintTemplate.layout 	= "Letter ANSI A Portrait";
			DOT.gPrintTemplate.layoutOptions = {
				"authorText": $("#myNote").val().replace(/\n/g, "\r\n"),
				"titleText": $("#mapTitle").val(), 
				/*"customTextElements": [
					{"ParcelOwner"		: $("#parcelOwnerText").text()},
					{"ParcelAddress"	: $("#parcelAddrText").text()},
					{"ParcelType"		: $("#parcelClassText").text()},
					{"ParcelLegal"		: $("#parcelLegalText").text()},
				],*/
				"legendLayers": [DOT.lgdP]
			};
			
			//var params		= new esri.tasks.PrintParameters();
			DOT.gPrintParameters.view 		= DOT.map;
			DOT.gPrintParameters.template	= DOT.gPrintTemplate;
			
			$('#printProgress').css("display","block");
			$('#printLink').css("display","none");
			$("#printError").css("display", "none");
			$('#printLink').empty();
			
			DOT.objPrint.execute(services_application.printTask,DOT.gPrintParameters).then(onPrintComplete, onPrintError);
		});
		
		function onPrintComplete(r){
			$("#printButton").removeAttr('disabled');
			$("#Printing").css("height","265px");
			var d = new Date();
			
			$('#printProgress').css("display","none");
			$("#printError").css("display", "none");
			//$('#printLink').append("Map is ready. Click <u><a href='" + r.url + "' target='_blank'> this link </a></u> to download.<br>(<i>The map, created at " + d.getHours() + ":" + d.getMinutes() + "." + d.getSeconds() +   ", will not be available after 15 minutes.</i>)");
			$('#printLink').append("Your map, created at <b>" + d.getHours() + ":" + d.getMinutes() + "." + d.getSeconds() + "</b> and based on <b>view of the time</b>, is ready for <u><a href='" + r.url + "' target='_blank'>download</a></u> for <b>10 min.</b> afterwards.");
			$('#printLink').css("display","block");
			$('.dijit .dijitReset .dijitInline .dijitLeft .esriPrintButton .dijitComboButton').css("display", "block");
		}
		
		function onPrintError(err){
			$("#printButton").removeAttr('disabled');
			$("#Printing").css("height","265px");
			
			console.log("Something broke: ", err);
			$("#printError").css("display", "block");
			if (err.message=="Timeout exceeded") {
				$("#printError").html("Error: " + err.message + ". Some reference layers, e.g., contour, roads, can't be printed at this moment. Try again later.");
				//$("#printError").html("Error in handling printing service request.<br> Try it again.");
			}
			else 
				$("#printError").html("Error in handling printing service request.<br> Try it again.");
				
			$('#printProgress').css("display","none");
		}
		
		function handleError(err) {
			console.log("Something broke: ", err);
		}
};
//})