/**********************************************************/
/*        Thematic maps SVG fuctions                      */
/*        Requires: Yui librabries						  */
/*		  Wisdom 2008	                                  */
/**********************************************************/
var maxSVGInits = 5;
var numSVGInits = 0;
var initViews = [];
var initRanges = [];


YAHOO.bosatlas.SVG = {
	SVGDoc 		: null,
	SVGEl  		: null,
	SVGViewObj	: null,	
	
	init : function(id) {
		if(numSVGInits > maxSVGInits) {
			numSVGInits = 0;
			return false; /* abort SVG init*/
		}
		numSVGInits++;
		
		var navi = navigator.appVersion;
		if(navi.indexOf("MSIE")>-1){
			checkAndGetSVGViewer();
		}
				
		// re-initialize global objects
		
		_gwmCore = null;
		gObjMapView = null;
		//_gwmBasME = null;
		//_gwmAPI = null;
		
		_gwmCore = new _gwmCoreObject();
		//_gwmAPI = new _gwmAPIObject();
		//_gwmBas = new _gwmBasicFunctionsObject();
		//_gwmBasME = new _gwmBasicMouseEventsObject();
		
		this.SVGViewObj = Dom.get("objMapView_" + id);
		
		if(!this.SVGViewObj) {
			refreshView(id);
			return false;	
		}
		
		try {
			this.SVGDoc = this.SVGViewObj.getSVGDocument();
			this.SVGEl = new  SVGElement(this.SVGDoc, this.SVGDoc.firstChild);
		}
		catch(e) {
			setTimeout( function() {
    			YAHOO.bosatlas.SVG.init(id);
    			;
    		}
    		, 200 );
    		return false;
		}
		
		// activate SVG tooltips (old method)
		initializeGWMSVG(this.SVGViewObj, true, false);
		initializeView(id);
		numSVGInits = 0;
		
		// init slider
		YAHOO.bosatlas.initSlider(id);
	}
}

// SVGElement constructor
function SVGElement(svgDocument, rootNode)  {
	this.svgDocument = svgDocument;
    this.rootNode = rootNode;
	this.panFactor = 100;
	this.curPanX = 0;
	this.curPanY = 0;
	this.curZoomFactor = 1.0;
}

function initializeView(id) {
	if(YAHOO.bosatlas.SVG.SVGDoc.firstChild) {
		if (!initViews[id]) {
			initViews[id] = YAHOO.bosatlas.SVG.SVGDoc.firstChild.getAttribute('viewBox');
		}
		if (!initRanges[id]) {
			if(YAHOO.bosatlas.SVG.SVGViewObj.getViewRange) {
				initRanges[id] = YAHOO.bosatlas.SVG.SVGViewObj.getViewRange();
			}
		}

		refreshView(id);
	}
}

function refreshView(id) {
	if (initViews[id] && initRanges[id]) {
		YAHOO.bosatlas.SVG.SVGEl.initView = initViews[id];
		YAHOO.bosatlas.SVG.SVGEl.initRange = initRanges[id];
		
		// Calculate the transformation (scale) factors from the view box to the view range
		var coords = YAHOO.bosatlas.SVG.SVGEl.initView.split(' ');
		var viewWidth = parseFloat(coords[2]);
		var viewHeight = parseFloat(coords[3]);
	
		var range = YAHOO.bosatlas.SVG.SVGEl.initRange.split('|');
		var rangeWidth = parseFloat(range[2]) - parseFloat(range[0]);
		var rangeHeight = parseFloat(range[3]) - parseFloat(range[1]);
		
		YAHOO.bosatlas.SVG.SVGEl.panRangeFactorX = rangeWidth / viewWidth;
		YAHOO.bosatlas.SVG.SVGEl.panRangeFactorY = rangeHeight / viewHeight;

		YAHOO.bosatlas.SVG.SVGEl.curPanX = 0;
		YAHOO.bosatlas.SVG.SVGEl.curPanY = 0;
		YAHOO.bosatlas.SVG.SVGEl.curZoomFactor = 1.0;
	}
}

// pan
SVGElement.prototype.pan = function(direction, amount) {
	if(this.rootNode == null) {
  		return false; /* can't attach zoom function*/
  	}

	var viewBox = this.rootNode.getAttribute('viewBox');
    var viewVals = viewBox.split(' ');
    if (amount == null) {
        amount = SVGElement.panFactor;
    }
    switch (direction) {
        case 'left':
            amount = 0 - amount;
            // intentionally fall through
        case 'right':
            var currentPosition = parseFloat(viewVals[0]);
            currentPosition += amount;
            this.curPanX += amount;
            viewVals[0] = currentPosition;
            break;
        case 'up':
            amount = 0 - amount;
            // intentionally fall through
        case 'down':
            var currentPosition = parseFloat(viewVals[1]);
            currentPosition += amount;
            this.curPanY += amount;
            viewVals[1] = currentPosition;
            break;
        case 'origin':
            // reset everything to initial values
            viewVals[0] = 0;
            viewVals[1] = 0;
			this.curPanX = this.curPanY = 0;
			this.rootNode.currentScale = 1;
            this.rootNode.currentTranslate.x = 0;
            this.rootNode.currentTranslate.y = 0;
            break;
    }
    this.rootNode.setAttribute('viewBox', viewVals.join(' '));

};

// setScale

SVGElement.prototype.setScale = function(factor) {
	this.curZoomFactor = factor;

	// Retrieve the initial view box
    var viewVals = this.initView.split(' ');

	// Translate the origin using the current pan position
	var x = parseFloat(viewVals[0]) + parseFloat(this.curPanX);
	var y = parseFloat(viewVals[1]) + parseFloat(this.curPanY);

	var width = parseFloat(viewVals[2]);
	var height = parseFloat(viewVals[3]);

	// Calculate the center
	var cx = x + width / 2;
	var cy = y + height / 2;

	// Scale to the new position
	var newX = cx - width / 2 * factor;
	var newY = cy - height / 2 * factor;

	// Scale to the new width and height
	var newWidth = width * factor;
	var newHeight = height * factor;

	// Set the new view box
	viewVals[0] = newX;
	viewVals[1] = newY;
	viewVals[2] = newWidth;
	viewVals[3] = newHeight;

    this.rootNode.setAttribute('viewBox', viewVals.join(' '));
}

function getCurrentRangeBounds() {
	var coords = YAHOO.bosatlas.SVG.SVGEl.initRange.split('|');

	// Translate the original range...
	var panX = parseFloat(YAHOO.bosatlas.SVG.SVGEl.curPanX) * YAHOO.bosatlas.SVG.SVGEl.panRangeFactorX;
	var panY = parseFloat(YAHOO.bosatlas.SVG.SVGEl.curPanY) * YAHOO.bosatlas.SVG.SVGEl.panRangeFactorY;
	
	var x1 = parseFloat(coords[0]) + panX;
	var y1 = parseFloat(coords[1]) + panY;
	var x2 = parseFloat(coords[2]) + panX;
	var y2 = parseFloat(coords[3]) + panY;

	// And scale using the current zoom factor...

	// Calculate half of the width and height
	var width = (x2 - x1);
	var height = (y2 - y1);

	// Calculate the center of the view
	var cx = x1 + width / 2.0;
	var cy = y1 + height / 2.0;

	// Scale the view range
	x1 = cx - (width * YAHOO.bosatlas.SVG.SVGEl.curZoomFactor) / 2.0;
	y1 = cy - (height * YAHOO.bosatlas.SVG.SVGEl.curZoomFactor) / 2.0;
	x2 = cx + (width * YAHOO.bosatlas.SVG.SVGEl.curZoomFactor) / 2.0;
	y2 = cy + (height * YAHOO.bosatlas.SVG.SVGEl.curZoomFactor) / 2.0;

	return x1 + '|' + y1 + '|' + x2 + '|' + y2;
}

// zoom

SVGElement.prototype.zoom = function(direction, factor) {
	if(this.rootNode == null) {
		return false; /* can't attach zoom function*/
	}

	factor = parseFloat(factor);

	switch (direction) {
	    case 'in':
	        if (factor == null) {
	            factor = 1.5;
	        }
	        break;
	    case 'out':
	        if (factor == null) {
	            factor = 0.67;
	        }
	        break;
	    case 'to':
	        break;
	}

	// Apply the scaling factor
    if (factor != 0.0) {
		this.setScale(1 / factor);
    } else {
		this.setScale(1.0);
    }
}; 

SVGElement.prototype.resetValues = function() {
	this.zoom('to', '1.0');
	this.pan('origin', 0);
}

function setRangeBounds(idInput) {
	try {
		Dom.get(idInput).value = getCurrentRangeBounds();
	}
	catch(e) {
		
	}	
}

/* overruling some tooltip functions that fail */

function _gwmClearTooltip()
{
  try {
	  _gwmCore.tooltipRect.style.setProperty('display', 'none');
	  _gwmCore.tooltipText.style.setProperty('display', 'none');

  } catch(e) {
  	
  }
}
/* custom simple tooltip function*/
function _gwmShowTooltip(evt, pt, tt) {
	
	var ttArray = tt.split("GWMNL");
	
	var selectedTab = YAHOO.bosatlas.tabHandler.getActiveTab();
	var tabId = selectedTab.getAttributeConfig('element').value.id.split("_")[1];
	var toolTipNode = Dom.getElementsByClassName("svgTTNode","div","tabContent_" + tabId)[0];
	if(toolTipNode) {
		toolTipNode.innerHTML = ttArray[0];
	}
}

function showTooltipInLegend(tooltipText,tooltipValue) {
	return false; /* feature not supported */	
}

function getIdElementSVG(idElement,svgObj) {
	return false;
}

function activateActiveXControl(elementId)  {
	object = Dom.get(elementId);
	if(object) {
		object.outerHTML = object.outerHTML;
	}
}
