// window.onload = startMap;
// <div id="map"></div>

// Can this be moved inside StartMap()?
var map = new YMap(document.getElementById('map'));

var candidates = new Array(
[ "Timothy Hannan_",	"CA Assembly - 6",		"San Rafael, CA",	"smartvoter.org/2008/06/03/ca/state/race/caasm06l"],
[ "Brian Perry_",	"CA Assembly - 19",		"San Mateo, CA",	"BrianPerry.us"],
[ "Pamela Brown_",	"CA Assembly - 40",		"Northridge, CA",	"CAWantsFreedom.com"],
[ "Thomas Logan_",	"CA Assembly - 44",		"Pasadena, CA",		"smartvoter.org/2008/06/03/ca/state/race/caasm44l"],
[ "John Kling_",	"CA Assembly - 54",		"Rolling Hills, CA",	"smartvoter.org/2008/06/03/ca/state/race/caasm54l"],
[ "Maureen Keedy_",	"CA Assembly - 59",		"Apple Valley, CA",	"smartvoter.org/2008/06/03/ca/state/race/caasm59l"],
[ "Michael Mendez_",	"CA Assembly - 61",		"Montclair, CA",	"VoteSmart.org/bio.php?can_id=70950"],
[ "Daniel Baehr_",	"CA ASsembly - 76",		"San Diego, CA",	"smartvoter.org/2008/06/03/ca/state/race/caasm76l"],
[ "Richard Belitz_",	"CA Assembly - 77",		"La Mesa, CA",		"VoteSmart.org/bio.php?can_id=59955"],
[ "Philip Berg_",	"U.S. Congress - CA 8",		"San Francisco, CA",	"ChoosePeaceNow.us"],
[ "Kevin Peterson_",	"U.S. Congress - CA 12",	"San Mateo, CA",	"KDPeterson.net/congress2008"],
[ "Tim Denton_",	"U.S. Congress - CA 27",	"San Fernando, CA",	"smartvoter.org/2008/06/03/ca/state/race/usrep27l"],
[ "Alan Pyeatt_",	"U.S. Congress - CA 29",	"Glendale, CA",		"PyeattForCongress.com"],
[ "Chris Agrella_",	"U.S. Congress - CA 38",	"Industry, CA",		"smartvoter.org/2008/06/03/ca/state/race/usrep38l"],
[ "Ernst Gasteiger_",	"U.S. Congress - CA 46",	"San Pedro, CA",	"SmartVoter.org/2008/06/03/ca/state/vote/gasteiger_e"],
[ "Lars Grossmith_",	"U.S. Congress - CA 49",	"Oceanside, CA",	"SmartVoter.org/2008/06/03/ca/state/vote/grossmith_l"],
[ "Marty Swinney",	"CA Assembly - 51",		"Inglewood, CA",	"Vote4Marty.com"],
[ "Janice Bonser",	"CA Assembly - 10",		"Carmichael, CA",	"VoteSmart.org/bio.php?can_id=57770"],
[ "Mark Hinkle",	"CA Assembly - 27",		"Morgan Hill, CA",	"MarkHinkle.DefendsLiberty.com"],
[ "Marty Swinney",	"CA Assembly - 51",		"Inglewood, CA",	"Vote4Marty.com"],
[ "Andy Favor",		"CA Assembly - 73",		"Laguna Niguel, CA",	"AndyFavor.com"],
[ "Paul King",		"CA Assembly - 74",		"Carlsbad, CA",		"VoteSmart.org/bio.php?can_id=16614"],
[ "John Murphy",	"CA Assembly - 75",		"La Jolla, CA",		"VoteSmart.org/bio.php?can_id=16703"],
[ "Art Tuma"	,	"U.S. Congress - CA 3",		"Antelope, CA",		"ArtTuma.com"],
[ "Joel Smolen",	"U.S. Congress - CA 6",		"Petaluma, CA",		"SmolenForCongress.com"],
[ "Camden McConnell",	"U.S. Congress - CA 7",		"Pittsburg, CA",	"VoteSmart.org/bio.php?can_id=59809"],
[ "Jim Eyer",		"U.S. Congress - CA 9",		"Oakland, CA",		"JimEyer.org"],
[ "Steve Wells",	"U.S. Congress - CA 16",	"San Jose, CA",		"SmartVoter.org/2008/06/03/ca/state/vote/wells_s"],
[ "Herb Peters",	"U.S. Congress - CA 35",	"Carson, CA",		"HerbPeters.com"],
[ "Don Patterson",	"U.S. Congress - CA 48",	"Dana Point, CA",	"Vote-Don.com"],
[ "Wayne Dunlap",	"U.S. Congress - CA 50",	"Del Mar, CA",		"DunlapForCongress.com"],
[ "Dan Litwin",		"U.S. Congress - CA 51",	"San Diego, CA",	"FrodoForCongress.com/"],
[ "Michael Benoit",	"U.S. Congress - CA 52",	"Santee, CA",		"MichaelBenoit.org"],
[ "Edward Teyssier",	"U.S. Congress - CA 53",	"San Diego, CA",	"votesmart.org/bio.php?can_id=16797"],
[ "Brian Holtz",	"U.S. Congress - CA 14",	"Los Altos Hills, CA",	"BrianHoltz.DefendsLiberty.com"],
[ "John Webster",	"CA Senate - 13",		"Mountain View, CA",	"jwebster.com/senate13.htm"],
[ "Steve Myers",	"CA Senate - 21",		"Pasadena, CA",		"groups.google.com/group/myers4liberty"],
[ "Colin Goldman",	"CA Senate - 23",		"Sherman Oaks, CA",	"ColinGoldman.com"],
[ "Jill Stone",		"CA Senate - 29",		"Monrovia, CA",		"JillStoneForCASenate29.org"],
[ "Jesse Thomas",	"CA Senate - 39",		"San Diego, CA",	""],
[ "John Zukoski",	"City Council - Dublin CA",	"Dublin, CA",		"ElectJohnZukoski.com"],
[ "Norm Westwell",	"School Board - Ocean View, CA",	"Huntington Beach, CA",		"ReelectFirecracker.com"],
[ "Michael Munger",	"Governor - NC",		"Raleigh, NC",		"Munger4NCgov.com"],
[ "Pat Dixon",		"City Council - Lago Vista, TX",	"Lago Vista, TX",		"PatDixon.org"],
[ "Kent Marmon",	"U.S. Senate - ID",		"Caldwell, ID",		"MarmonForIdaho.com"],
[ "Scotty Boman",	"U.S. Senate - MI",		"Dearborn Heights, MI",	"ScottyBoman.org"],
[ "Mike Kien",		"Routt Co. Commission - CO",	"Phippsburg , CO",	""],
[ "Rex Bell",		"Indiana House - 54",		"Hagerstown, IN",	"ElectRexBell.com"],
[ "Sean Shepard",	"U.S. Congress - IN 7",		"Indianapolis , IN",	"shepard2008.com"],
[ "Ron Darby",		"TX House - 14",		"Bryan, TX",		""],
[ "Brendan Kelly",	"NH House - Rockingham",	"Seabrook, NH",		"BrendanKellyLPNH.com"],
[ "Allen Buckley",	"U.S. Senate - GA",		"Atlanta, GA",		"BuckleyForSenate.com"],
[ "Joseph Buchman",	"U.S. Congress - UT 14",	"Park City, UT",	"BuchmanForCongress.com"],
[ "Daniel Reale",	"U.S. Congress - CT 2",		"Plainfield , CT",	"RealeDealForCongress.com"],
[ "James Fryman",	"U.S. Congress - PA 5",		"Victory Township, PA",	""],
[ "Jeffrey Bouffard",	"U.S. Congress - FL 15",	"Satellite Beach, FL",	"ElectBouf.com"],
[ "James Frye",		"CO House - 42",		"Aurora, CO",		"FryeFor42.com"],
[ "Teddy Fleck",	"Lt. Governor - MO",		"Springfield , MO",	"TeddyFleck.com"],
[ "Jason Peck",		"KS House - 24",		"Overland Park, KS",	"boston-tea-party.info"],
[ "Mike Taylor",	"U.S. House - TX 24",		"Houston, TX",		"MikeTaylor08.us"],
[ "Daniel Lewis",	"U.S. Senate - TN",		"Nashville, TN",	"GetYourCountryBack.com"],
[ "Jack Woehr",		"CO House - 25",		"Golden, CO",		"well.com/~jax/2008"],
[ "Lisa Leeds",		"TN House - 58",		"Nashville , TN",	"LisaLeeds2008.com"],
[ "Coy Reynolds",	"TX House - 59",		"Glen Rose, TX",	"CoyReynolds.blogspot.com"],
[ "Rob Hodgkinson",	"KS Sentate - 37",		"Stillwell, KS",	"www.vote-hodgkinson.org"],
[ "",	"",	"",		""],
[ "Bill Redpath",	"U.S. Senate - VA",		"Leesburg, VA",		"redpath2008.com"]
);

function getPageEventCoords(evt) {
	var coords = {left:0, top:0};
	if (evt.pageX) {
		coords.left = evt.pageX;
		coords.top = evt.pageY;
	} else if (evt.clientX) {
		coords.left =
			evt.clientX + document.body.scrollLeft - document.body.clientLeft;
		coords.top =
			evt.clientY + document.body.scrollTop - document.body.clientTop;
		// include html element space, if applicable
		if (document.body.parentElement && document.body.parentElement.clientLeft) {
			var bodParent = document.body.parentElement;
			coords.left += bodParent.scrollLeft - bodParent.clientLeft;
			coords.top += bodParent.scrollTop - bodParent.clientTop;
		}
	}
	return coords;
}
function getMousePos(e) {
        var posx = 0;
        var posy = 0;
        if(!e){ var e = window.event }
 
        posx = (e.pageX) ? e.pageX : window.event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        posy = (e.pageY) ? e.pageY : window.event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
 
	var coords = {y:0, x:0};
	coords.y = poxy;
	coords.x = posx;
        return coords;
}
function captureMousePosition(e) {
    var coords = {x:0, y:0};
    if (document.layers) {
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    } else if (document.all) {
        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
    } else if (document.getElementById) {
        xMousePos = e.pageX;
        yMousePos = e.pageY;
    }
    coords.x = xMousePos;
    coords.y = yMousePos;
    return coords;
}

//var mapCenter = "Pahrump, NV";
var zoomLevel = 13;
// whether to omit candidates without photos
var skipPhotoless = false;

function startMap(){

	// Add the ability to change between Sat, Hybrid, and Regular Maps
	map.addTypeControl(); 	
	// Add the zoom control. Long specifies a Slider versus a "+" and "-" zoom control
	map.addZoomLong();    		
	// Add the Pan control to have North, South, East and West directional control
	map.addPanControl();  
	// Specifying the Map starting location and zoom level
	map.drawZoomAndCenter( mapCenter, zoomLevel );

	function placeMarker( name, office, location, url ){
		//YLog.print("Adding marker at....");
		//YLog.print("\nLatitude:" + geoPoint.Lat + "\nLongitude:" + geoPoint.Lon);
		var filename = name.replace( /_/g, '' );
		if (filename == name) {
			filename = name.replace( / /g, '' );
		} else {
			filename = name.replace( /(.).* (.).*/, '$1$2' );
		}
		name = name.replace( /_/g, '' );
		//office = office.replace( / /g, '&nbsp;' );
		var newMarker= new YMarker(location, createCustomMarkerImage( filename ));
		var bloon = '';
		bloon += '<center>';
		if (filename.length > 2) {
			if (url.length) { bloon += '<a href="http://' + url + '" target="_blank">'; }
			bloon += '<img src="http://marketliberal.org/DefendsLiberty/';
			bloon += filename + '.jpg"';
			bloon += 'height="80"><br>';
			if (url.length) { bloon += '</a>'; }
		}
		bloon += '<small>';
		bloon += name;
		bloon += '<br>' + office + '<br>';
		bloon += location + '<br>';
		var link = '';
		if (url.length) {
			link += '<a href="http://' + url + '" target="_blank">'; // or _top
			link += '<span class="Snap_Shot_Preview">';
			link += url;
			link += '</span>';
			link += '</a>';
			bloon += link;
		}
		bloon += '</small></center>';
		newMarker.addAutoExpand(bloon);
		YEvent.Capture(newMarker, EventsList.MouseClick, 
				function(event){
					newMarker.openSmartWindow(bloon);
					SNAP_COM.shot.Rescan();
				});
		YEvent.Capture(newMarker, EventsList.MouseOver, 
				function(event){
					SNAP_COM.shot.Rescan();
				});
		map.addOverlay(newMarker);
	}
	function createCustomMarkerImage( filename ){
	 	var myImage = new YImage();
		if (filename.length == 2) {
			//myImage.src = 'http://text2image.ning.com/TextToImage.php?text='
			//	+ filename + '&FontSize=9&depth=1&FontColor=FF0000';
			myImage.src = 'http://marketliberal.org/DefendsLiberty/StatueOfLiberty.png';
		} else if (navigator.appName == "Microsoft Internet Explorer") {
			// IE does not scale images
	  		myImage.src = 'http://marketliberal.org/DefendsLiberty/Thumbs/' + filename + '.jpg';
	 		myImage.size = new YSize(28,39);
		} else {
	  		myImage.src = 'http://marketliberal.org/DefendsLiberty/' + filename + '.jpg';
	 		myImage.size = new YSize(28,39);
		}
	  	myImage.offsetSmartWindow = new YCoordPoint(13,10);
		myImage.offset = new YCoordPoint(-14,10);
		return myImage;
	}
	while (candidates.length > 0) {
		if (candidates[0][0] == candidates[0][0].replace( /_/g, '' )) break;
		var candidate = candidates.splice( 0, 1 );
		if (skipPhotoless) { alert('skipping photoless'); break; }
		placeMarker( candidate[0][0], candidate[0][1], candidate[0][2], candidate[0][3] );
	}
	while (candidates.length > 0) {
		var randomIndex = Math.floor(Math.random() * candidates.length)
		var candidate = candidates.splice( randomIndex, 1 );
		if (candidate[0][0] == "") continue;
		placeMarker( candidate[0][0], candidate[0][1], candidate[0][2], candidate[0][3] );
	}
}