/*
dragzoom_Obfs.js
-----------------------------------------------------------------------------------------------------
* DragZoomControl Class v1.2
*  Copyright (c) 2005-2007, Andre Lewis, andre@earthcode.com
*
* Back Button functionality
*  Copyright (c)  2007, Richard Garland, papabear.newyork@gmail.com
*
* This class lets you add a control to the map which will let the user
*  zoom by dragging a rectangle.
*  More info on original GZoom at http://earthcode.com

pdmarker_Obfs.js
-----------------------------------------------------------------------------------------------------
 PdMarker
 Purpose: extends Google Map API GMap and GMarker (hover effects, image swapping, moving)
 Details: http://www.pixeldevelopment.com/pdmarker.asp
 Updated: [see getPdMarkerRevisionInfo]
 Author:  Peter Jones
 Notes:   Relies on undocumented features of the Google Map API which may change.
          Based on my own PJToolTip and ideas from GxMarker, TLabel and the Google Maps API forum.

 Contact http://www.pixeldevelopment.com for your custom Google Map needs

mgeorss2_Obfs.js
-----------------------------------------------------------------------------------------------------
// MGeoRSS: GMaps API extension
// copyright 2006 Mikel Maron (email: mikel_maron yahoo com)
// http://brainoff.com/gmaps/mgeorss.html
// This work is public domain
*/

function MGeoRSS()
{
};
MGeoRSS.prototype.initialize=function(map)
{
    this.map=map;
    this.rssurl=false;
    this.request=false;
    this.visible=true;
    this.markers=new Array();
    o_map=this.map;
    o_markers=this.markers;
};
MGeoRSS.prototype.setIcon=function(img)
{

//    this.icon=new GIcon();
//    this.icon.image=img;
//    this.icon.shadow=i18n.img_georss_shadow;
//    this.icon.iconSize=new GSize(26,31);
//    this.icon.shadowSize=new GSize(38,31);
//    this.icon.iconAnchor=new GPoint(0,0);
//    this.icon.infoWindowAnchor=new GPoint(13,5);

      this.iconOption = {icon: img, width: 26, height: 31};
      this.icon_image=this.iconOption.icon;

	o_iconOption = {icon: img, width: 26, height: 31};
	o_img = img;


};
MGeoRSS.prototype.showHide=function()
{
    if(this.visible==true)
    {
        for(var i=0;i<this.markers.length;i++)
        {
//            this.map.removeOverlay(this.markers[i]);
			this.map.DeleteShape(this.markers[i])
        }
        this.visible=false;
    }
    else
    {
        for(var i=0;i<this.markers.length;i++)
        {
//            this.map.addOverlay(this.markers[i]);
//			var pin = new GShape(GShapeType.Pushpin, data._LatLong);
			this.map.AddShape(this.markers[i]);
        }
        this.visible=true;
    }
};
MGeoRSS.prototype.load=function(url,proxyurl,autozoom,otimeout)
{
    if(this.request!=false)
    {
        return;
    }
    this.rssurl=url;

    if(autozoom)
    {
        this.autozoom=autozoom;
        this.bounds=new Array();
        o_autozoom=this.autozoom;
        o_bounds=this.bounds;
    }

    var d=new Date();
    var get_url=(proxyurl!=undefined)?proxyurl+this.rssurl+"&amp;t="+d.getTime():this.rssurl+"&amp;t="+d.getTime();
    $.ajax(
    {
		url:get_url,
		type:'GET',
		dataType:'xml',
		timeout:otimeout,
		error:this.showerr,
		success:this.callback
    }
    );
};

MGeoRSS.prototype.showerr=function()
{
    $('#loading-indicator').html(i18n.txt_load_error).show(i18n.init_load_showtime);
};

MGeoRSS.prototype.callback=function(xml)
{

    var items=xml.documentElement.getElementsByTagName("item");
    $('#stats_info').html(i18n.txt_stats_info_s+items.length+i18n.txt_stats_info_e);


    if(items.length<=0)
    {
        $('#loading-indicator').html(i18n.txt_info_nodata).hide(i18n.init_load_hidetime_fast);
        alert(i18n.txt_info_nodata);
    }
    else
    {
        for(var i=0;i<items.length;i++)
        {
            try
            {
                var title=$("title",items[i]).text();
                var desc=$("description",items[i]).text();
                var link=$("link",items[i]).text();
                var latlng="";
                var lat;
                var lng;
                var box="";
                var img="";
                if(navigator.userAgent.toLowerCase().indexOf("msie")<0)
                {
                    try
                    {
                        latlng=items[i].getElementsByTagNameNS("http://www.georss.org/georss","point")[0].childNodes[0].nodeValue;
                    }
                    catch(e)
                    {
                    }
                    try
                    {
                        lat=items[i].getElementsByTagNameNS("http://www.w3.org/2003/01/geo/wgs84_pos#","lat")[0].childNodes[0].nodeValue;
                        lng=items[i].getElementsByTagNameNS("http://www.w3.org/2003/01/geo/wgs84_pos#","long")[0].childNodes[0].nodeValue;
                    }
                    catch(e)
                    {
                    }
                    try
                    {
                        img=items[i].getElementsByTagNameNS("http://search.yahoo.com/mrss/","content")[0].getAttribute("url");
                    }
                    catch(e)
                    {
                    }
                    try
                    {
                        var rel=items[i].getElementsByTagNameNS("http://www.georss.org/georss","relationshiptag")[0].childNodes[0].nodeValue;
                        if(rel=="image-extent")
                        {
                            box=items[i].getElementsByTagNameNS("http://www.georss.org/georss","box")[0].childNodes[0].nodeValue;
                            img=items[i].getElementsByTagNameNS("http://search.yahoo.com/mrss","content")[0].getAttribute("url");
                        }
                    }
                    catch(e)
                    {
                    }
                }
                else
                {
                    try
                    {
                        var lat_=$(items[i]).text();
                        latlng=items[i].getElementsByTagName("georss:point")[0].childNodes[0].nodeValue;
                    }
                    catch(e)
                    {
                    }
                    try
                    {
                        lat=items[i].getElementsByTagName("geo:lat")[0].childNodes[0].nodeValue;
                        lng=items[i].getElementsByTagName("geo:long")[0].childNodes[0].nodeValue;
                    }
                    catch(e)
                    {
                    }
                    try
                    {
                        box=items[i].getElementsByTagName("georss:box")[0].childNodes[0].nodeValue;
                    }
                    catch(e)
                    {
                    }
                    try
                    {
                        img=items[i].getElementsByTagName("media:content")[0].getAttribute("url");
                    }
                    catch(e)
                    {
                    }
                    try
                    {
                        var rel=items[i].getElementsByTagName("georss:box")[0].getAttribute("relationshiptag");
                        if(rel=="image-extent")
                        {
                            box=items[i].getElementsByTagName("georss:box")[0].childNodes[0].nodeValue;
                            img=items[i].getElementsByTagName("media:content")[0].getAttribute("url");
                        }
                    }
                    catch(e)
                    {
                    }
                }

                if(latlng.length>0)
                {
                    lat=latlng.split(" ")[0];
                    lng=latlng.split(" ")[1];
                }

                if(box.length >0 && img.length > 0)
                {
                    var photo=new TPhoto();
                    photo.id=img;
                    photo.src=img;
                    photo.percentOpacity=80;
                    b=box.split(' ');
//                    photo.anchorTopLeft=new GLatLng(b[3],b[0]);
//                    photo.anchorBottomRight=new GLatLng(b[1],b[2]);

                    photo.anchorTopLeft=new GLatLon(b[3],b[0]);
                    photo.anchorBottomRight=new GLatLon(b[1],b[2]);

                    o_map.addTPhoto(photo);
                    if(o_autozoom)
                    {
//                        var nw=new GLatLng(parseFloat(b[3]),parseFloat(b[0]));
//                        var se=new GLatLng(parseFloat(b[1]),parseFloat(b[2]));

                        var nw=new GLatLon(parseFloat(b[3]),parseFloat(b[0]));
                        var se=new GLatLon(parseFloat(b[1]),parseFloat(b[2]));

//                        o_bounds.extend(nw);
//                        o_bounds.extend(se);

						o_bounds.push(nw);
						o_bounds.push(se);

                    }
                }


                if(lat.length >0 && lng.length > 0)
                {
//                    this.markers[i] = MGeoRSS.prototype.createMarker(i,lng,lat,title,desc,link,img);
//                    o_map.addOverlay(o_markers[i]);
					//this.map.AddShape(this.markers[i]);

					o_markers[i] = MGeoRSS.prototype.createMarker(i,lng,lat,title,desc,link,img);
					g_map.AddShape(o_markers[i]);

                }
                if(o_autozoom)
                {
					var bound = new GLatLon(lat,lng);
//                    o_bounds.extend(o_markers[i].getPoint());
					o_bounds.push(bound);
                }
            }
            catch(e)
            {
            }
        }
        try
        {
            if(o_autozoom)
            {

               // g_map.zoomToMarkers();
			   var bound = GLatLonRectangle.FromLocations(o_bounds);
			   g_map.SetOptions({bounds: bound});
			   zoom_ = g_map.GetZoomLevel();

            }

//			$('#movies_tips').slideUp(i18n.indicator_slide_time);
//			$('#movies_tips').slideUp('slow', function() { });
        }
        catch(e)
        {
        }
        $('#loading-indicator').html(i18n.txt_load_success).hide(i18n.init_load_hidetime);
    }
};

MGeoRSS.prototype.createMarker = function(i,lng,lat,title,desc,link,img)

//function createMarker(i,lng,lat,title,desc,link,img)
{
    var no=i;
    var l_g=lng;
    var l_t=lat;
    var t=title;
    var d=desc;
    var l=link;
    var ig=img;
    var marker;
    var t_l="";
    var ig_l="";
    var html='<div class="card-l" style="height:'+ (InfoWindowH - 57)+'px; width:100%; overflow: auto;">';
//    var point=new GPoint(parseFloat(l_g),parseFloat(l_t));
//    var point=new GPixel(parseFloat(l_g),parseFloat(l_t));
    var point=new GLatLon(parseFloat(l_t), parseFloat(l_g));

    if(o_img)
    {
//        marker=new GMarker(point,o_icon);
       this.marker = new GShape(GShapeType.Pushpin, point, o_iconOption);

    }
    else
    {
        //marker=new GMarker(point);
        this.marker = new GShape(GShapeType.Pushpin, point);
    }

    t_l=(l)?'<h2 class="card-title"><a href="'+l+'" target="_blank">'+t+'</a></h2>':'<h2 class="card-title">'+t+'</h2>';
    ig_l=(l&&ig!="")?'<span class="card-photo"><a href="'+l+'" target="_blank"><img src="'+ig+'" title="'+t+i18n.txt_pop_info_copyright+'"></a></span>':'';
    if(d)
    {
        html+=t_l+'<p class="card-desc">'+ig_l+d+'</p>';
    }
    html+='<p class="card-info">'+i18n.txt_pop_info_s+l_t+' , '+l_g+i18n.txt_pop_info_e+'</p>';
    html+='</div>';

//    this.marker.setTooltip(t);

    var hover_icon=(o_img==i18n.img_georss_movie_icon)?i18n.img_georss_movie_icon_on:i18n.img_georss_news_icon_on;
//    marker.setHoverImage(hover_icon);
	this.marker.SetHoverImage(hover_icon);

//	this.marker.SetCustomIcon("http://garmapbeta.appspot.com/img/mj/green.png");
//	this.marker.SetHoverImage("http://garmapbeta.appspot.com/img/mj/red_1.png");
//	this.marker.SetDisplayType(Garmin.GMap.DisplayType.Click);
    this.marker.SetMouseEventType(Garmin.GMap.MouseEvent.MouseClick);
	this.marker.SetTitle(t);
	this.marker.SetDescription(html);
//	this.marker.SetIconOffset(0,-32); //y,x
	this.marker.SetInfoWindowWidth(InfoWindowW);
	this.marker.SetInfoWindowHeight(InfoWindowH);
//alert(this.marker.GetZIndex())

     this.marker.AttachEvent('onclick', function (pin) {
//            marker.openInfoWindowHtml(html);
//		pin.OpenInfoWin();
//		pin.SetCustomIcon(hover_icon);
//		pin.AnimateTo(38, 44, hover_icon, new PushPinOffset(0, -32))

        //    tipMeFunction(this, t);
        });

     this.marker.AttachEvent('onmouseover', function (pin) {
			pin.SetZIndex(1010);
        });

     this.marker.AttachEvent('onmouseout', function (pin) {
			pin.SetZIndex(900);
        });


      this.marker.addInfoWindowListener('open', function(pin){
		pin.SetCustomIcon(hover_icon);
		pin.SetZIndex(1010);

        var point=new GLatLon(pin.Latitude, pin.Longitude);
        var loc = g_map.LatLongToPixel(point);
        var pos_x = (g_map.GetWidth() - InfoWindowW) / 2,
            pos_y = (g_map.GetHeight() - InfoWindowH) / 2 + InfoWindowH - 20;

        var m_x = Math.round(loc.x - pos_x); //pos_x ;
        var m_y = Math.round(loc.y - pos_y); //pos_y ;
        g_map.PanByPixelFast(new GPixel(m_x, m_y)); //move

	  });


      this.marker.addInfoWindowListener('close', function(pin){
	  		pin.SetCustomIcon(o_img);
			pin.SetZIndex(900);

		  if (g_map.GetZoomLevel() == zoom_) {
              var bound = GLatLonRectangle.FromLocations(o_bounds);
			   g_map.SetOptions({bounds: bound});
			  }
	  });
/*

	  $("<li />").html((i+1) + ":" +t).click(function(){
		displayPoint(lat, lng , (i+1));
	})
	.appendTo("#list");
*/

    return this.marker;
};


function displayPoint(lat, lng , index){

$("#message").hide();

var getLatLng = new GLatLon(lat, lng);
var markerOffset = g_map.LatLongToPixel(getLatLng);
//alert(markerOffset.y + " - "+ markerOffset.x)

$("#message").fadeIn().css({ top:markerOffset.y, left:markerOffset.x });

g_map.PanToLatLong(getLatLng);
}

GarMap.prototype.addMGeoRSS=function(a)
{
    a.initialize(this);
};

