var markers = [];
var sel_marker_id;
var base_icon;
var new_id;

function createMarker(point,title,desc,id,icon) {

	var content = '<div class="infowindow"><div style="font-weight: bold; font-family: georgia, serif; color: #1879DC;">'+title+'</div><div style="font-size: 11px; font-weight: normal; line-height: 1.6em">'+desc+'</div></div>';
	
	var max_content_div = document.createElement('div');
	
	var eLat = point.lat();
	var eLng = point.lng();
	
	var map_image = '<img src="http://maps.google.com/staticmap?center='+eLat+','+eLng+'&zoom=12&size=420x240&markers='+eLat+','+eLng+',midgreen&key=ABQIAAAA5VQHkeKkTrdsrzJpfimgvRStRX5bweRSfEOVUvaytO9RyeGkExQLKsBRw7MmIC9hiAHqjzwQ8GZ9uA" style="padding: 5px 0px;" />';
	
	$(max_content_div).html('<div style="font-size: 11px; font-weight: normal; line-height: 1.6em">'+desc+'</div>'+map_image);
	
	var marker = new GMarker(point, {title: title, icon:icon});
	
	GEvent.addListener(marker, 'click', function() {
		
		marker.openInfoWindowHtml(content, {maxContent: max_content_div, maxTitle: title});
		
		var off_set = $('#headline-link-'+id).offset();
		
		var breaking_list = $('#breaking-news-list').offset();
		
		$('#headline-link-'+id).css({'background':'#DA7AFF','color':'#ffffff'});
		
		sel_marker_id = id;
		
		for (x in markers) {
			markers[x].hide();
		}
		
		marker.show();
	
		marker.setImage('images/library/icons/mapdango-hover.png');
		
		showExcerpt(id);
		
	});
	
	GEvent.addListener(marker, 'infowindowclose', function() {
	
		for (x in markers) {
			markers[x].show();
		}
		
		marker.setImage('images/library/icons/mapdango.png');
		
		$('#headline-link-'+id).css({'background':'#F3F9F6','color':'#1879DC'});
		$('#story-icons-'+id).css({'display':'none'});
		
		sel_marker_id = false;
		
		hideExcerpts();
		
	});
	
	return marker;

}

function loadData(paging) {

	if (!paging) {
		paging = 0;
	}

	map.clearOverlays();
	
	var c = 1;
		
	for (var i=paging; i < map_data.length; i++) {
	
		var id= map_data[i][0];
		var lat= map_data[i][1];
		var lng= map_data[i][2];
		
		var title = $('#title-'+id).html();
		var desc = $('#excerpt-'+id).html();

		var point = new GLatLng(lat,lng);
		
		base_icon = new GIcon();
		base_icon.image = "images/library/icons/mapdango.png";
		base_icon.iconSize            = new GSize(30,30);
		base_icon.iconAnchor          = new GPoint(15,15);
		base_icon.infoWindowAnchor    = new GPoint(15,15);
		
		var marker = createMarker(point,title,desc,id,base_icon);
		
		markers[id] = marker;
		
		map.addOverlay(marker);
		
		c++;
	
		if (c == 11) { break; }
		
	}

}

function showLocation(id) {

	if (markers[id]) {
		GEvent.trigger(markers[id], 'click');
	}
}


function markerHover(id,tp) {

	if (tp == 'over') {
	
		if (!sel_marker_id) {
	
			for (x in markers) {
				markers[x].hide();
			}
		
			markers[id].show();
		
			markers[id].setImage('images/library/icons/mapdango-hover.png');
		
		}
		
		$('#story-icons-'+id).css({'display':'inline'});
		$('#headline-link-'+id).css({'background':'#DA7AFF','color':'#ffffff'});
		
	}
	
	if (tp == 'out') {
	
		if (!sel_marker_id) {
			for (x in markers) {
				markers[x].show();
			}
			markers[id].setImage('images/library/icons/mapdango.png');
		}
		
		
		if (sel_marker_id != id) {
			$('#headline-link-'+id).css({'background':'#F3F9F6','color':'#1879DC'});
			$('#story-icons-'+id).css({'display':'none'});
		}
		
		
		
	}

}


function showExcerpt(id) {
	
	if (!$('#excerpt-'+id).is(':hidden')) {
	
		$('#excerpt-'+id).css("display","none");
	
	}
	
	else {
		
		hideExcerpts();
		
		$('#excerpt-'+id).css("display","block");
		
	}

}

function hideExcerpts() {

	$(".story-excerpt").css("display","none");

}


function clickStory(story_id) {

	//See if the ID has changed
	GDownloadUrl("_vote.php", function(data, responseCode) {
		 
	},"id="+story_id);

}