$(window).bind("load", function() {

var example = document.getElementById('example');

var context = example.getContext('2d');
var mapReady = false;
var Examplex;
var Exampley;

var sources = {
	"Northumberland": {image : "/img/map/Northumberland.png", link : "http://www.inorthumberland.co.uk", name : "iNorthumberland"},
	"Somerset": {image : "/img/map/Somerset.png", link : "http://www.isomerset.co.uk", name : "iSomerset"},
	"Wiltshire": {image : "/img/map/Wiltshire.png", link : "http://www.iwiltshire.co.uk", name : "iWiltshire"},
	"Scotland": {image : "/img/map/Scotland.png", link : "http://www.iscotland.co.uk", name : "iScotland"},
	"Northern Ireland": {image : "/img/map/NorthernIreland.png", link : "http://www.inorthernireland.co.uk", name : "iNorthern Ireland"},
	"Wales": {image : "/img/map/Wales.png", link : "http://www.iwales.co.uk", name : "Wales"},
	"Berskshire": {image : "/img/map/Berkshire.png", link : "http://www.iberkshire.co.uk", name : "iBerskshire"},
	"Bedfordshire": {image : "/img/map/Bedfordshire.png", link : "http://www.ibedfordshire.co.uk", name : "iBedfordshire"},
	"Birmingham": {image : "/img/map/Birmingham.png", link : "http://www.ibirmingham.co.uk", name : "iBirmingham"},
	"Bristol": {image : "/img/map/Bristol.png", link : "http://www.ibristol.co.uk", name : "iBristol"},
	"Buckinghamshire": {image : "/img/map/Buckinghamshire.png", link : "http://www.ibuckinghamshire.co.uk", name : "iBuckinghamshire"},
	"Cambridge": {image : "/img/map/Cambridge.png", link : "http://www.icambridge.co.uk", name : "iCambridge"},
	"Cheshire": {image : "/img/map/Cheshire.png", link : "http://www.icheshire.co.uk", name : "iCheshire"},
	"CountyDurham": {image : "/img/map/CountyDurham.png", link : "http://http://www.icountydurham.co.uk/", name : "iCounty Durham"},
	"Cornwall": {image : "/img/map/Cornwall.png", link : "http://www.icornwall.co.uk", name : "iCornwall"},
	"Cumbria": {image : "/img/map/Cumbria.png", link : "http://www.icumbria.info", name : "iCumbria"},
	"CountyDurham": {image : "/img/map/CountyDurham.png", link : "http://www.icountydurham.co.uk", name : "iCounty Durham"},
	"Derbyshire": {image : "/img/map/Derbyshire.png", link : "http://www.iderbyshire.co.uk", name : "iDerbyshire"},
	"Devon": {image : "/img/map/Devon.png", link : "http://www.idevon.co.uk", name : "iDevon"},
	"Dorset": {image : "/img/map/Dorset.png", link : "http://www.idorset.co.uk", name : "iDorset"},
	"EastYorkshire": {image : "/img/map/EastYorkshire.png", link : "http://www.ieastyorkshire.co.uk", name : "iEast Yorkshire"},
	"EastSussex": {image : "/img/map/EastSussex.png", link : "http://www.ieastsussex.co.uk", name : "iEast Sussex"},
	"Edinburgh": {image : "/img/map/Edinburgh.png", link : "http://www.iedinburgh.co.uk", name : "iEdinburgh"},
	"Essex": {image : "/img/map/Essex.png", link : "http://www.iessex.co.uk", name : "iEssex"},
	"Glasgow": {image : "/img/map/Glasgow.png", link : "http://www.iglasgow.co.uk", name : "iGlasgow"},
	"Gloucestershire": {image : "/img/map/Gloucestershire.png", link : "http://www.igloucestershire.co.uk", name : "iGloucestershire"},
	"Hampshire": {image : "/img/map/Hampshire.png", link : "http://www.ihampshire.co.uk", name : "iHampshire"},
	"Herefordshire": {image : "/img/map/Herefordshire.png", link : "http://www.iherefordshire.co.uk", name : "iHerefordshire"},
	"Hertfordshire": {image : "/img/map/Hertfordshire.png", link : "http://www.ihertfordshire.co.uk", name : "iHertfordshire"},
	"Kent": {image : "/img/map/Kent.png", link : "http://www.ikent.co.uk", name : "iKent"},
	"Lancashire": {image : "/img/map/Lancashire.png", link : "http://www.ilancashire.co.uk", name : "iLancashire"},
	"Leeds": {image : "/img/map/Leeds.png", link : "http://www.ileeds.co.uk", name : "iLeeds"},
	"Leicestershire": {image : "/img/map/Leicestershire.png", link : "http://www.ileicestershire.com", name : "iLeicestershire"},
	"Lincolnshire": {image : "/img/map/Lincolnshire.png", link : "http://www.ilincolnshire.co.uk", name : "iLincolnshire"},
	"Liverpool": {image : "/img/map/Liverpool.png", link : "http://www.iliverpool.co.uk", name : "iLiverpool"},
	"London": {image : "/img/map/London.png", link : "http://www.ilondon.co.uk", name : "iLondon"},
	"Manchester": {image : "/img/map/Manchester.png", link : "http://www.imanchester.co.uk", name : "iManchester"},
	"Merseyside": {image : "/img/map/Merseyside.png", link : "http://www.imerseyside.co.uk", name : "iMerseyside"},
	"Norfolk": {image : "/img/map/Norfolk.png", link : "http://www.inorfolk.co.uk", name : "iNorfolk"},
	"Northampton": {image : "/img/map/Northampton.png", link : "http://www.inorthampton.co.uk", name : "iNorthampton"},
	"NorthYorkshire": {image : "/img/map/NorthYorkshire.png", link : "http://www.inorthyorkshire.co.uk", name : "iNorth Yorkshire"},
	"Nottinghamshire": {image : "/img/map/Nottinghamshire.png", link : "http://www.inottinghamshire.co.uk", name : "iNottinghamshire"},
	"Oxfordshire": {image : "/img/map/Oxfordshire.png", link : "http://www.ioxfordshire.co.uk", name : "iOxfordshire"},
	"Sheffield": {image : "/img/map/Sheffield.png", link : "http://www.ishefffield.co.uk", name : "iSheffield"},
	"Shropshire": {image : "/img/map/Shropshire.png", link : "http://www.ishropshire.co.uk", name : "iShropshire"},
	"SouthYorkshire": {image : "/img/map/SouthYorkshire.png", link : "http://www.isouthyorkshire.com", name : "iSouthyorkshire"},
	"Staffordshire": {image : "/img/map/Staffordshire.png", link : "http://www.istaffordshire.co.uk", name : "iStaffordshire"},
	"Suffolk": {image : "/img/map/Suffolk.png", link : "http://www.isuffolk.co.uk", name : "iSuffolk"},
	"Surrey": {image : "/img/map/Surrey.png", link : "http://www.isurrey.co.uk", name : "iSurrey"},
	"TyneAndWear": {image : "/img/map/TyneAndWear.png", link : "http://www.ityneandwear.co.uk", name : "iTyne and Wear"},
	"Warwickshire": {image : "/img/map/Warwickshire.png", link : "http://www.iwarwickshire.co.uk", name : "iWarwickshire"},
	"WestMidlands": {image : "/img/map/WestMidlands.png", link : "http://www.iwestmidlands.co.uk", name : "iWest Midlands"},
	"WestSussex": {image : "/img/map/WestSussex.png", link : "http://www.iwestsussex.co.uk", name : "iWest Sussex"},
	"WestYorkshire": {image : "/img/map/WestYorkshire.png", link : "http://www.iwestyorkshire.co.uk", name : "iWest Yorkshire"},
	"Worcestershire": {image : "/img/map/Worcestershire.png", link : "http://www.iworcestershire.co.uk", name : "iWorcestershire"}
};

var imgCanvas = {};

var totalImages = 45;
var i=0;

function addCanvas(img,key) { 
		
		context.drawImage(img,0,0);
		imgCanvas[key] = document.createElement("canvas");		
		imgCanvas[key].width = img.width;
		imgCanvas[key].height = img.height;
		imgCanvas[key].getContext('2d').drawImage(img, 0, 0);
		
};

function goToLink(link) {
	$('#placeholder').click(function() {
		window.location = link;
	});
}

Examplex = $("#example").offset().left;
Exampley = $("#example").offset().top;
ExampleWidth = $("#example").width();
ExampleHeight = $("#example").height();

for (var key in sources) {
  if (sources.hasOwnProperty(key)) {
	var img=new Image();
	img.src = sources[key].image;
	img.onload = addCanvas(img,key);

  }						   
}	

mapReady = true;

$(document).mousemove(function(e) {
		$('#mapArea').mouseout(function() {
			$('#tooltip').css({
				display : "none"
			});
			$("#placeholder").attr("src",'/img/map/background.png');
		});
		
		var x = e.pageX - Examplex;
		var y = e.pageY - Exampley;

		if(x < 0 || y < 0 || x > ExampleWidth || y > ExampleHeight) return false;
				
		var coord = "x=" + x + ", y=" + y;
		var c = example.getContext('2d');
		var p = c.getImageData(x, y, 1, 1).data; 
		if(p[0] != 255) {
			
			for (var key in sources) {
			  if (sources.hasOwnProperty(key)) {
				
				var imgContext = imgCanvas[key].getContext('2d');
				var imgData = imgContext.getImageData(x, y, 1, 1).data;
				if (imgData[0]) {
					$("#placeholder").attr("src",sources[key].image);			
					$("#placeholder").click(goToLink(sources[key].link));

					$("#tooltip").html(sources[key].name);
					var mouseX = e.pageX; 
   					var mouseY = e.pageY;
					
					mouseX = mouseX - 120;
					mouseY = mouseY - 20;
					
					$('#tooltip').css({
   						top : mouseY,
   						left : mouseX,
						display : "block",
					});
				} else {

				}
			  }
			}
			
		} else {
			$("#placeholder").attr("src",'');
		}												
});

function findPos(obj) {
var curleft = 0, curtop = 0;
if (obj.offsetParent) {
    do {
		curleft += obj.offsetLeft;
        curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
    	return { x: curleft, y: curtop };
    }
    return undefined;
}

});
