'use strict';

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

ShareLink.prototype.init = function() {

	$("input[name='linkType']").on('change', function(){
		if (!$("input[name='linkType']:checked")) {
			$("#btnShareLink").prop("disabled", true);
		} else {
			$("#btnShareLink").prop("disabled", false);
		}
	});

	$("#btnShareLink").click(function() {
		$("#urlShareLink").text(generateShareLink());
		var textbox = document.getElementById("urlShareLink");
		textbox.select();
		textbox.onfocus = function() {
			textbox.select();
			
			// Work around Chrome's little problem
			textBox.onmouseup = function() {
				// Prevent further mouseup intervention
				textbox.onmouseup = null;
				return false;
			};	
		}
	});  
};

function generateShareLink (){
	/* items needed to be recorded:
	 * view; center; zoom level;  
	 */ 
	var str;
	if ( document.URL.indexOf("?")>0 )
		str = (document.URL).split("?")[0] + "?";
	else
		str = document.URL + "?";
		
	var m = DOT.map;
	var wkid = DOT.map.spatialReference.wkid;
	
	// check the link is for mouse click or map center
	var o = null;
	if (($('#linkType_extent').is(':checked')) || ($('#linkType_click').is(':checked'))){
		if ($('#linkType_extent').is(':checked')){
			o = DOT.map.extent.center;
		} else {
			if (DOT.mappoint) o = DOT.mappoint;
			else
				o = DOT.map.extent.center;
		}
		
		str += ( "wkid=" + wkid + "&x=" + o.x.toFixed(0) + "&y=" + o.y.toFixed(0) + "&l=" + m.zoom);
	} else if (($('#linkType_plan').is(':checked'))){
		//planSelected = $('#prjKeySelected').text();
		if (DOT.planSelected !== "") str += ("name=" + DOT.planSelected);
	}
	return str;
}