// ****
dojo.require("dojox.gfx");

var CircleID = new Object();
var container = null;
var container_position = null;
var surface = null;
var surface_size = null;

var APickList = ''; var AllOntario = 999999; var AttrIDGlobal;
var AttrNew = 1; var AttrNewFlag;
var OldCode = ''; var NewCode = '';
var AttrSuite = 'A,B,C'; var IMQSRebuild = '';
var MenuWidth = 740;
var ButtonWidth = (MenuWidth-24)/4;

// Init Rebuild the querystring excluding the current page
var URLKeys = AttrSuite.split(',');  var IMQSRebuild = '';

// load and decode the url querystring  
var IMQueryString = unescape(location.search);

// Choices cookie
var Choices = 'BaTChoices';
var mnth = new Date(); fixDate(mnth);
mnth.setTime(mnth.getTime() + 30 * 24 * 60 * 60 * 1000); // expires in 30 days

function ResultsButton() {
  document.getElementById("BaTResultsLink").href = 'AttrFill.html?' + getCookie(Choices);
}

function ResultsButtonOpen() {
  if (getCookie(Choices)) {
    document.getElementById("BaTResults").style.visibility = 'visible';
    ResultsButton();
  };
}

// Complete the TripWizard navigation suite

if (IMQueryString != '') { WizNavShow = 'visible' } else { WizNavShow = 'hidden' };

var TripWizard = '<div class=\"BuildaTrip Trans85\" style=\"position:relative;visibility:' + WizNavShow + ';height:1em;top:-1em;margin:0em 2px 1.5em 0em;padding:2px 2px 4px 12px;width:' + eval(MenuWidth-4) + 'px;\"><div style=\"float:left;font-size:9pt;font-weight:bolder;margin:0px 0px 0px 2px;width:' + ButtonWidth + 'px;\" id=\"BaT\">Build-a-trip</div><div class=\"clear\" style=\"float:right;text-align:center;padding-right:6px;\"><div style=\"float:left;font-size:8pt;margin:0px 0px 0px 2px;width:' + ButtonWidth + 'px;\" id=\"Wiz2\"><a id=\"WizL2\" href=\"buildatripA.html' + IMQueryString + '\">How to Get There</a></div><div style=\"float:left;font-size:8pt;margin:0px 0px 0px 2px;width:' + ButtonWidth + 'px;\" id=\"Wiz3\"><a id=\"WizL3\" href=\"buildatripB.html' + IMQueryString + '\">Where to Stay</a></div><div style=\"float:left;font-size:8pt;margin:0px 0px 0px 2px;width:' + ButtonWidth + 'px;\" id=\"Wiz4\"><a id=\"WizL4\" href=\"buildatrippC.html' + IMQueryString + '\">What to Do</a></div></div></div>';

// Complete rebuild of querystring

for (i = 0; i < URLKeys.length; i++) {
  if(queryString('a'+URLKeys[i])!='false') {
    if(queryString('cs'+URLKeys[i])=='false') {
      IMQSRebuild += '&cs' + URLKeys[i] + '=1';
    } else {
      IMQSRebuild += '&cs' + URLKeys[i] + '=' + queryString('cs'+URLKeys[i]);
    }
    IMQSRebuild += '&a' + URLKeys[i] + '=' + queryString('a'+URLKeys[i]);
  }
}

/*attach a handler to the onmousedown event that calls a function to store the values*/
document.onmousedown = getMouseXY;
//detect browser
var IE = document.all?true:false
var mapleft = maptop = 0;
var userX; var userY;

function getMouseXY(e) {
	findPos("OntarioMap");
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		userX = e.pageX;
		userY = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		userX = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft - 3;
		userY = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop - 3;
	}
	userX -= mapleft; userY -= maptop;
	return true;
}


function findPos(map2) {
	map = document.getElementById(map2);
	if (map.offsetParent) {
		mapleft = map.offsetLeft
		maptop = map.offsetTop
		while (map = map.offsetParent) {
			mapleft += map.offsetLeft
			maptop += map.offsetTop
		}
	}
	return [mapleft,maptop];
}

function initGfx() {
	container = dojo.byId("OntarioMap");
	container_position = dojo.coords(container, true);
	surface = dojox.gfx.createSurface(container, 750, 752);
	surface_size = surface.getDimensions();
	surface_size.width  = parseInt(surface_size.width);
	surface_size.height = parseInt(surface_size.height);
//	surface.createCircle({cx: 0, cy: 0, r: 0})
//		.setFill([0,0,0,0.0])
//		.setStroke({color: [0,0,0,0.0], width: 0})
//		;
	AttrPrepicked();
}

var gShapes = new Object();
var gShapeCounter = 0;
var id; var id2; var id3; var id4;
var cx=0; var cy=0; var cx2; var cy2; var cx2b; var cy2b; var cx3; var cy3; var cx3b; var cy3b; var cy2delta; var cy2bdelta; var cx3delta;
var menublx = 552; var menubly = 478; // Bottom-left of menu
var AObjectb; var AObjectb_Pos; var AObjectb_Posx; var AObjectb_Posy; var AObjectb_Posbx; var AObjectb_Posby;

function makeCircle()
{
	var red   = RFillARed[AttrIDGlobal]; // regioncolours.js
	var green = RFillAGreen[AttrIDGlobal];
	var blue  = RFillABlue[AttrIDGlobal];
	var red2   = RFillBRed[AttrIDGlobal];
	var green2 = RFillBGreen[AttrIDGlobal];
	var blue2  = RFillBBlue[AttrIDGlobal];
	var spotr = "255"; var spotg = "255"; var spotb = "0";
	var opc = 0.6; 	var opc2 = 0.85; var opc3 = 1; var opc4 = opc/2; 
	var r = 15; var r2 = 3; var r3 = 20; var r4 = r + (r * 0.2);
	var offset = 1.5;
	if (userX != null) { cx = userX;  cy = userY;};
	if (userX > menublx & userY < menubly){ // menu click
		cx = RegionX[AttrIDGlobal]; cy = RegionY[AttrIDGlobal];
	}
	CircleID[AttrIDGlobal] = id = "Locale_" + AttrIDGlobal;
	id2 = id + "b"; id3 = id + "drpa"; id4 = id + "drpb";
	id5 = id + "tiea";
	AObjectb = document.getElementById("R"+AttrIDGlobal+"b");
	AObjectb_Pos = dojo.coords(AObjectb, true);
	container = dojo.byId("OntarioMap");
	container_position = dojo.coords(container, true);
	
	AObjectb_Posbx = AObjectb_Posx = AObjectb_Pos.x - container_position.x - 5;
	AObjectb_Posy = AObjectb_Pos.y - container_position.y - 5;
	AObjectb_Posby = AObjectb_Pos.y - container_position.y + 15;
	
	cy3 = AObjectb_Posy; cy3b = AObjectb_Posby; cx2 = cx; cy2 = cy;
		
	var cxi = cx+offset; var cxii = cx-offset
	var cx2i = cx2+offset; var cx2ii = cx2-offset;
	var cyi = cy+offset; var cyii = cy-offset;
	var cy2i = cy2+offset; var cy2ii = cy2-offset;
	
	if (cx > AObjectb_Posx) {
		if (cx-AObjectb_Posx > r3) {
			cx3 = AObjectb_Posx - r3;
			cx3b = cx3 + (cx-AObjectb_Posx)/r3;
		} else {
			cx3b = cx3 = AObjectb_Posx - (cx-AObjectb_Posx) - r3;
		}
		cx2 = cx - (cx-AObjectb_Posx) - r3; cx2i = cx2+(cx-AObjectb_Posx)/r2/2; cx2ii = cx2-(cx-AObjectb_Posx)/r2/2;
		cy2b = cy2 = cy; cyi = cy-offset; cyii = cy+offset;
		
	} else {
		cx3delta = AObjectb_Posx - cx;
		if (cy > AObjectb_Posy) {
			cy2delta = Math.abs(cy - AObjectb_Posy);
			cy2bdelta = Math.abs(cy - AObjectb_Posby);
			if (cy2delta > r3) {
				if (cy2delta/cx3delta < 1) {
					cy2 = cy-cy2delta+Math.abs(cy2delta/cx3delta*10);
					cy2b = cy-cy2bdelta-Math.abs(cy2bdelta/cx3delta*10);
				} else {
					cy2 = cy-cy2delta+Math.abs(cx3delta/cy2delta*10);
					cy2b = cy-cy2bdelta-Math.abs(cx3delta/cy2bdelta*10);
				}
			} else {
				cy2 = cy;
				cy2b = cy;	
			}
			
		} else {	
			cxi = cx-offset; cxii = cx+offset;
			cx2i = cx2-offset; cx2ii = cx2+offset;
			cyi = cy+offset; cyii = cy-offset;
			cy2delta = Math.abs(AObjectb_Posy - cy);
			cy2bdelta = Math.abs(AObjectb_Posby - cy);
			if (cy2delta > r2 * 2) {
				cy2 = cy+cy2delta+Math.abs(cy2delta/cx3delta);
				cy2b = cy+cy2bdelta-Math.abs(cy2bdelta/cx3delta);
			} else {
				cy2 = cy;
				cy2b = cy;				
			}
		};

		if (cx3delta < r3) {
			cx3 = AObjectb_Posx - r3 - (r3 - cx3delta);
			cx2 = cx - r3 - (r3 - cx3delta);
			cx3b = cx3 + r3 / 2;
		} else {
			cx3b = cx3 = AObjectb_Posx-r3;
		};
	};
	
	var lg = {
			type: "linear",
			x1: cx, y1: cy, x2: AObjectb_Posx, y2: AObjectb_Posy,
			colors: [
				{ offset: 0, color: [red2,green2,blue2,opc3] },
				{ offset: 1, color: [red,green,blue,opc3] }
			]
		};
	
	var aShape = surface.createCircle({cx: cx, cy: cy, r: r})
		.setFill(null)
		.setStroke({color: [red2,green2,blue2,opc2], width: 4})
		.setTransform({dx: 2, dy: 2})
		;
	aShape.getEventSource().setAttribute('Locale', id3);
	gShapes[id3] = aShape;
	
	aShape = surface.createCircle({cx: cx, cy: cy, r: r})
		.setFill([red2,green2,blue2,opc4])
		.setStroke({color: [red,green,blue,opc2], width: 4})
		;
	aShape.getEventSource().setAttribute('Locale', id);
	gShapes[id] = aShape;

	// Curve from button to Region
	aShape = surface.createPath()
		.moveTo(cxi, cyi).lineTo(cxii,cyii)
		.curveTo(cx2ii, cy2, cx3, cy3, AObjectb_Posx, AObjectb_Posy)
		.lineTo(AObjectb_Posx, AObjectb_Posby)
		.curveTo(cx3b, cy3b, cx2i, cy2b, cxi, cyi)
		.closePath()
		.setStroke({ color: [red,green,blue,opc4], width: 0})
		.setFill(lg)
	;
	aShape.getEventSource().setAttribute('Locale', id5);
//	alert( cx + " " + cy + " | " + cx2 + " " + cy2 + " | " + cx3 + " " + cy3 + " | " + AObjectb_Posx + " " + AObjectb_Posy);

//	alert( AObjectb_Posx + " " + AObjectb_Posby + " | " + cx3 + " " + cy3b + " | " + cx2 + " " + cy2 + " | " + cx + " " + cy);
	gShapes[id5] = aShape;
	
	aShape = surface.createCircle({cx: cx, cy: cy, r: r2})
		.setFill([red,green,blue,opc3])
		.setStroke({color: [red2,green2,blue2,opc2], width: 3})
		.setTransform({dx: 1.44, dy: 1.44})
		;
	aShape.getEventSource().setAttribute('Locale', id2);
	gShapes[id2] = aShape;
	
	aShape = surface.createCircle({cx: cx, cy: cy, r: r2})
		.setFill([red,green,blue,opc3])
		.setStroke({color: [red,green,blue,opc], width: 2})
		;
	aShape.getEventSource().setAttribute('Locale', id4);
	gShapes[id4] = aShape;
}

function removeCircle()
{
	surface.remove(gShapes[CircleID[AttrIDGlobal]],false);
	surface.remove(gShapes[CircleID[AttrIDGlobal]+'b'],false);
	surface.remove(gShapes[CircleID[AttrIDGlobal]+'drpa'],false);
	surface.remove(gShapes[CircleID[AttrIDGlobal]+'drpb'],false);
	surface.remove(gShapes[CircleID[AttrIDGlobal]+'tiea'],false);
}

// gsprepAreaCoords unused
var AMapArea = new Object();var AMArea = new Array();

function gsprepAreaCoords(AttrID) {
	AMapArea[AttrID] = document.getElementById("RPoly" + AttrID);
	AMArea = AMapArea[AttrID].coords.replace(' ','').split(',');
	return;
}

var aCPicked;

function AttrPrepicked() {	
	if (queryString('R') != 'false') { 
		aCPicked = queryString('R').split('_');
		for (j = 0; j < aCPicked.length; j++) {
			if (aCPicked[j] != AllOntario) {
				cx = RegionX[aCPicked[j]]; cy = RegionY[aCPicked[j]];
				PLPicked(aCPicked[j]);
			};
		}
	}
}

var AObject; var AObject999999; // var AObjectb; now preface to makeCircle

function PLPicked(AttrID){
AttrIDGlobal = AttrID;
if (AttrID == AllOntario) {
	if (APickList == "") {
		document.location = "buildatripA.html?R=" + AllOntario + IMQSRebuild; return;
	} else {
		document.location = "buildatripA.html?R=" + APickList + IMQSRebuild; return;
	}
}
AObject = document.getElementById("R"+AttrID);
AObjectb = document.getElementById("R"+AttrID+"b");
AObject999999 = document.getElementById("R" + AllOntario);
AWizL2 = document.getElementById("WizL2");
AWizL3 = document.getElementById("WizL3");
AWizL4 = document.getElementById("WizL4");

/* <div id=\"R999999b\" style=\"line-height:.8em;width:.9em;height:.9em;padding:0px;margin:.15em;border:1px groove;float:left;\">&nbsp;</div> */
if (OldCode == "") {
	OldCode = AObject999999.innerHTML;
	NewCode = "<a href=\"javascript:PLPicked('999999');\" class=\"altlinks3\">Next Step</a>";
}

if (APickList != null) {	
	APick = APickList.split("_");
	AttrNewFlag = -1;
	for (i = 0; i < APick.length; i++) {
		if (APick[i] == AttrID) {
			AttrNewFlag = i;
		} 
	}
	if (AttrNewFlag == -1) // not already picked so add to list and change class to show this
	{
		APick.push(AttrID); 
		AttrNew = 2;
		makeCircle();
		if (AObject999999.innerHTML != NewCode) {
			Newclass2 = AObject999999.className.slice(0,AObject999999.className.length-1) + AttrNew;
			AObject999999.className = Newclass2;
			AObject999999.innerHTML = NewCode;
		}
	} else if (AttrNewFlag != -1){ // already picked but clicked on again so deselect
		removeCircle();
		APick.splice(AttrNewFlag,"1");
		AttrNew = 1;
		if (APick.length == 0) { 
			Newclass2 = AObject999999.className.slice(0,AObject999999.className.length-1) + AttrNew;
			AObject999999.className = Newclass2;
			AObject999999.innerHTML = OldCode;

		}		
	}
	Newclass = AObject.className.slice(0,AObject.className.length-1) + AttrNew;

	if (AttrNew == 2) {
//		AObjectb.innerHTML = '<div class=\"' + AObject.className.slice(0,AObject.className.length-1) + AttrNew + '\" style=\"line-height:0.4em;height:0.4em;width:0.4em;border:0px none;\">&nbsp;</div>';
	} else {
		AObjectb.innerHTML = '&nbsp;';
	}
	AObject.className = Newclass;
					
	APick.sort();
	APickList = APick.join("_");
	if (APickList.search("_") == 0) {
		APickList = APickList.slice(1,APickList.length);
	}
	if (APickList != '') {
		AObject999999.href = 'buildatripA.html?R=' + APickList + IMQSRebuild;
		AWizL2.href = 'buildatripA.html?R=' + APickList + IMQSRebuild;
		AWizL3.href = 'buildatripB.html?R=' + APickList + IMQSRebuild;
		AWizL4.href = 'buildatripC.html?R=' + APickList + IMQSRebuild;
		setCookie (Choices, 'R=' + APickList + IMQSRebuild, mnth);
		ResultsButton();
	}
	else {
		AObject999999.href = "buildatripA.html?R=" + AllOntario + IMQSRebuild;
		AWizL2.href = 'buildatripA.html?R=' + AllOntario + IMQSRebuild;
		AWizL3.href = 'buildatripB.html?R=' + AllOntario + IMQSRebuild;
		AWizL4.href = 'buildatripC.html?R=' + AllOntario + IMQSRebuild;
		setCookie (Choices, 'R=' + AllOntario + IMQSRebuild, mnth);
		ResultsButton();
	}
} else {
	APickList = AttrID; AttrNew = 2;
	Newclass = AObject.className.slice(0,AObject.className.length-1) + AttrNew;
	Newclass2 = AObject999999.className.slice(0,AObject999999.className.length-1) + AttrNew;
	AObject.className = Newclass;
	AObject999999.className = Newclass2;
	AObject999999.innerHTML = NewCode;
}

}

dojo.addOnLoad(initGfx);