var map;
var rss;
var currentPinIdx = 0;
var allPins = [];
var icon = new FE.Icon('http://freeearth.poly9.com/_news/010.png');
function onMapLoad()
{

FE.GeoRSS.prototype._processRSS = function(r) {
this.allPins = [];
 var feed = this._importXML(r);
 var items = feed.getElementsByTagName('item');
 this.icon = new FE.Icon('http://freeearth.poly9.com/_news/010.png');
 this.icon.d = true;
 for(var i=0; i<items.length; i++)
  this.itemCallback(items[i]);
 if(this.onLoad) this.onLoad();
}

FE.GeoRSS.prototype.itemCallback = function(item) {
 var title = this._getTag(item, 'title');
 var desc = this._getTag(item, 'description');
 var point = this._getTag(item, 'point');
 if (!point) {
  point = this._getTag(item, 'georss:point');
  if (!point)
   return;
 }
 var thumb = item.getElementsByTagName('thumbnail');
 var icon;
 if (thumb.length > 0 && (typeof this.icon.d != 'undefined')) {
  icon = new FE.Icon(thumb[0].getAttribute('url'));
 } else
  icon = this.icon;
 var location = FE.LatLng.parse(point);
 var content = '<b>' + title + '</b><br/>';
 content += desc;
 var pin = new FE.Pushpin(location, icon);
 this.allPins.push(pin);
 FE.Event.addListener(pin, 'click', this._onPointClick.bind(this, content));
 this._map.addOverlay(pin);

}

FE.GeoRSS.prototype._onPointClick = function(content, pin)
{
	pin.openInfoWindowHtml(content, 240, 120);
}

/*	rss = new FE.GeoRSS('http://mapufacture.com/map/rss/431');
	rss.onLoad = rssOnLoad;
	this.addOverlay(rss);*/
	this.panTo(new FE.LatLng(40, -100));
	this.zoomTo(13000000);

	window.onresize();
	var s = document.createElement('script');
 	s.src = 'http://newsmaps.www.metacarta.com/reuters/newsmap-mccom.js'
 	document.getElementsByTagName('head')[0].appendChild(s)


}

function onNewsClick(placename, articles) {
	return function(p) {
		content = '<p style="font-weight:bold; color: black;">' + placename + '</p>';
		for(var i=0; i<articles.length; i++) {
			content += '<a style="color:black;" href="'+articles[i].Href+'">' + articles[i].Headline + '</a>';
			content += '<br/>';
		}
		p.openInfoWindowHtml(content);
	}
}

function processEntries(r) {
 for(var i=0; i<r.length; i++) {
	var art = r[i];
	var ll = new FE.LatLng(art['Latitude'], art['Longitude']);
	var p = new FE.Pushpin(ll, icon);
	FE.Event.addListener(p, 'click', onNewsClick(art['Placename'], art['Articles']));
	map.addOverlay(p);
	allPins.push(p);
 }
 cycleThroughPins();
}

function fisherYates ( myArray ) {
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
   }
}


function rssOnLoad()
{
	fisherYates(rss.allPins);

	cycleThroughPins();
}
function cycleThroughPins()
{
	currentPinIdx = currentPinIdx+1 % allPins.length;
	var thisPin = allPins[currentPinIdx];

	map.closeInfoWindow();
	thisPin.dispatch('click');
	map.panTo(new FE.LatLng(thisPin._location.lat, thisPin._location.lng), 2, 'easeincubic');


	setTimeout('cycleThroughPins()', 8000);
}

function GetWindowWidthHeight()
{
        var myWidth = 0, myHeight = 0;
        if(typeof(window.innerWidth) == 'number')
        {
                myWidth = window.innerWidth;
                myHeight = window.innerHeight;
        }
        else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
        {
                myWidth = document.documentElement.clientWidth;
                myHeight = document.documentElement.clientHeight;
        }
        else if(document.body && (document.body.clientWidth || document.body.clientHeight))
        {
                myWidth = document.body.clientWidth;
                myHeight = document.body.clientHeight;
        }

        return {'width' : myWidth, 'height' : myHeight};
}

function loadMap()
{
        window.onresize = function()
        {
                document.getElementById("map").style.width = (GetWindowWidthHeight().width-128)+"px";
                document.getElementById("map").style.height = (GetWindowWidthHeight().height-150)+"px";

                if(typeof map != "undefined" && map && typeof map.resize != "undefined" && typeof map._flash != "undefined" && typeof map._flash.resize != "undefined")
                       map.resize(GetWindowWidthHeight().width - 128, GetWindowWidthHeight().height - 150)
        }

        window.onresize();

        map = new FE.Map(document.getElementById("map"));
        map.onLoad = onMapLoad;
        map.load();
}
