﻿var shareAction = ""; //print/pdf/xls/ppt

function printMap() {
	
	//grab an image of the current map
	if (!_map) {
		showError("Unable to locate map element.");
		hideSpinner();
	}

	_map.getSnapshot("onFlashMapExport");				
}

function onFlashMapExport(base64Jpg) {
	//upload it to the server
	velir.atlas.uploadBase64Image(base64Jpg, "jpg", function(result) {
		var imageUrl = result.d;

		if ($.string(imageUrl).startsWith("error")) {
			showError("Unable to save an image of the map.");
			hideSpinner();
		}

		hideSpinner();
		
		//redirect with a ref to the share action and the chart image
		window.location = buildShareUrl(shareAction, imageUrl);
	});
}

function printFusionChart() {
	var chartToPrint = getChartFromId("fusion-chart");
	if (!chartToPrint) {
		//this can happen if there is no data for the currently selected options,
		//so the chart never loads. In that case, just redirect to the regular printer friendly page.
		hideSpinner();
		
		//redirect with a ref to the share action and the chart image
		window.location = buildShareUrl(shareAction);
		return;	
	}
	
	if(String.isNullOrEmpty(shareAction)) {
		showError("Unable to determine action.");
	}
	
	if(shareAction == "print") {
		chartToPrint.exportChart({ exportFormat: "png" });	
	}
	if(shareAction == "gpdf") {
		chartToPrint.exportChart({ exportFormat: "png" });	
	}
	//if(shareAction == "pdf") {
	//	chartToPrint.exportChart({ exportFormat: "png" });	
	//}
	if(shareAction == "ppt") {
		chartToPrint.exportChart({ exportFormat: "jpg" });
	}
}

function onFusionExportFinished(objRtn) {
	hideSpinner();
	
	if (objRtn.statusCode != "1") {
		showError("The chart could not be saved on server: " + objRtn.statusMessage);			
		return;
	}
	
	//redirect with a ref to the share action and the chart image
	window.location = buildShareUrl(shareAction, objRtn.fileName);
}

function buildShareUrl(shareAction, imageUrl) {
	var url = pathAndQuery();
	var separator = "?";
	if (url.indexOf("?") > -1) {
		separator = "&";
	}

	if(String.isNullOrEmpty(shareAction)) {
		showError("Unable to determine action.");
	}

	//add params to the print url
	if(shareAction == "print") {
		url +=  $.format("{0}{1}={2}", separator, urlParams.printerFriendlyParam, "1");
	}
	//add params to the pdf url
	if(shareAction == "gpdf") {
		url +=  $.format("{0}{1}={2}", separator, urlParams.pdfGenerateParam, "1");		
	}
	//add params to the pdf url
	//if(shareAction == "pdf") {
	//	url +=  $.format("{0}{1}={2}", separator, urlParams.pdfFriendlyParam, "1");
	//}
	//add params to the ppt url	
	if(shareAction == "ppt") {		
		url +=  $.format("{0}{1}={2}", separator, urlParams.powerpointParam, "1");
	}
	
	//finally, add the chart image param to the url
	if (typeof (imageUrl) != "undefined" && imageUrl != null) {
		url += $.format("&{0}={1}", urlParams.imageUrlParam, escape(imageUrl));
	}
			
	return url;
}

function pathAndQuery() {
	return $(document).url().attr("path") + $(document).url().attr("query");
}

function urlParamValue(paramName) {
	return $(document).url().param(paramName);
}

function showSpinner(_shareAction) {

	//set the shareAction
	shareAction = _shareAction;
	
	if(String.isNullOrEmpty(shareAction)) {
		showError("Unable to determine action.");
	}
	
	//display the appropriate message
	if(shareAction == "print") {	
		$("#preparing-share").html("Preparing page for Printing...");
	}
	if(shareAction == "gpdf") {
		$("#preparing-share").html("Preparing page for PDF...");
	}
	//if(shareAction == "pdf") {
	//	$("#preparing-share").html("Preparing page for PDF...");
	//}
	if(shareAction == "xls") {
		$("#preparing-share").html("Downloading data to Excel...");
	}
	if(shareAction == "ppt") {
		$("#preparing-share").html("Downloading chart to PowerPoint...");
	}
	
	$("#preparing-share").show();
}

function hideSpinner() {
	$("#preparing-share").hide();
}

function showError(msg) {
	alert("Oops, something has gone wrong:\n\n" + msg);
}