var Flckr =
{
  targId: "",
  maxImg: 3,
  scrollType: true,
  init: function(flckrUID, targId, maxImg, scrollType )
  {
    if( $(targId).length == 0 ){ return; }
    Flckr.targId = targId;
    Flckr.maxImg = maxImg;
    Flckr.scrollType = (typeof(scrollType)=="undefined"? (new LiteBox()).SCROLL_BRACKET : scrollType);
    $.getScript( "http://api.flickr.com/services/feeds/photos_public.gne?id="+flckrUID+"&format=json" );
  },
  create: function(o)
  {
    if( $(Flckr.targId).length == 0 )
    {
      return;
    }
    $(Flckr.targId)[0].flckrData = o;
    var e;
    var imgs = [];

    for( var i = 0; o.items != null && i < o.items.length; i++ )
    {
      imgs[imgs.length] = new Media(null, o.items[i].media.m, o.items[i].title, o.items[i].link );
    }
    new LiteBox( Flckr.targId, imgs, Flckr.maxImg, Flckr.scrollType );
  }
};

function jsonFlickrFeed(o)
{
  Flckr.create(o);
}

function LiteBox( targId, imgs, maxImg, scrollType )
{
  this.SCROLL_BRACKET = "b";
  this.SCROLL_UNDER   = "u";

  if( !targId )
  {
    return;
  }

  this.pos = 0;
  this.imgs = imgs;
  this.maxImg = maxImg;

  var d = $( (targId.indexOf("#")?"#":"") + targId );
  var e;
  var scrollLabel = "";
  if( imgs !== null && imgs.length > 0 )
  {
    this.maxImg    = Math.min(this.maxImg,imgs.length);
    this.scrollType= (typeof(scrollType)=="undefined"? "" : scrollType);

    if( this.scrollType == this.SCROLL_UNDER )
    {
      e = $("<div/>").addClass("liteBoxImgs").attr("id", $(d).attr("id")+"_imgs");
      d.append(e);
      this.draw(d);
    }

    if( this.scrollType != "" && imgs.length > this.maxImg )
    {
      scrollLabel = (this.scrollType == this.SCROLL_UNDER ? " Prev ": "");
      e = $("<div/>").addClass("liteBoxPrv").html("<a href=\"#\" onclick=\"this.parentNode.box.goPrev(this); return false;\"><span>&lt;"+scrollLabel+"</span></a>");
      e[0].box = this;
      d.append(e);
    }

    if( this.scrollType == this.SCROLL_BRACKET )
    {
      e = $("<div/>").addClass("liteBoxImgs").attr("id", $(d).attr("id")+"_imgs");
      d.append(e);
      this.draw(d);
    }


    if( this.scrollType != "" && imgs.length > this.maxImg )
    {
      scrollLabel = (this.scrollType == this.SCROLL_UNDER ? " Next ": "");
      e = $("<div/>").addClass("liteBoxNxt").html("<a href=\"#\" onclick=\"this.parentNode.box.goNext(this); return false;\"><span>"+scrollLabel+"&gt;</span></a>");
      e[0].box = this;
      d.append(e);
    }
    e = $("<div/>").attr("style","clear:left;");
    d.append(e);
  }
}
LiteBox.prototype.draw = function(d)
{
  var g = $("#"+ $(d).attr("id") +"_imgs");

  g.children().remove();

  if( this.pos < 0 )
  {
    this.pos += this.imgs.length;
  }
  if( this.pos >= this.imgs.length )
  {
    this.pos = 0;
  }
  var a,e;
  var curPos = this.pos;
  for( var i = 0; i < this.maxImg; i++ )
  {
    if(curPos >= this.imgs.length )
    {
      if( this.scrollType == this.SCROLL_UNDER )
      {
        return;
      }
      else
      {
        curPos = 0;
      }
    }
    e = $("<div/>").addClass("liteBoxImg")
        .css("background","transparent url(" + this.imgs[curPos].thumbSrc + ") 50% 50% no-repeat")
        .attr("title",this.imgs[curPos].title)
    a = $("<a/>").attr("href",this.imgs[curPos].link);
    if( this.imgs[curPos].link.indexOf("://") != -1 )
    {
      a.attr("target","orig");
    }
    a.append(e);
    g.append(a);
    curPos++;
  }
};
LiteBox.prototype.goTo = function( e, showNext )
{
  var step = (this.scrollType == this.SCROLL_UNDER) ? this.maxImg : 1;
  var d    = e.parentNode.parentNode;
  this.pos += showNext ? step : step*-1;
  this.draw(d);
  return false;
};
LiteBox.prototype.goNext = function(e){this.goTo(e,true)};
LiteBox.prototype.goPrev = function(e){this.goTo(e,false)};



var _mSvgForced = true;
var _mSvgEnabled = true;


Date.prototype.getTimestamp = function()
{
  return this.getUTCFullYear() +","+ this.getUTCMonth() +","+ this.getUTCDate() +","+ this.getUTCHours() +","+ this.getUTCMinutes();
};


var Mappr =
{
  gmap           : null,
  shadowLayer    : null,
  layerTimestamp : "",
  markerOpt      : null,
  markerMgr      : null,
  markers        : [],

  iconImgNormal  : "http://labs.google.com/ridefinder/images/mm_20_red.png",
  iconImgHLight  : "http://labs.google.com/ridefinder/images/mm_20_blue.png",

  init : function( controls,lat,lng,zoom,initMarkers )
  {
    if( !(GBrowserIsCompatible()) )
    {
      return;
    }
    window.onunload = function(){GUnload();};
    lat = (typeof(lat)=="undefined"?20:lat);
    lng = (typeof(lng)=="undefined"?0:lng);
    zoom = (typeof(zoom)=="undefined"?0:zoom);
    Mappr.GMapExtend();

    Mappr.layerTimestamp = new Date().getTimestamp();

    Mappr.gmap = new GMap2($("#map")[0],{mapTypes:[G_NORMAL_MAP,G_HYBRID_MAP,G_SATELLITE_MAP]});
    if( typeof(controls) != "undefined" && controls )
    {
      Mappr.gmap.addControl(new GMapTypeControl());
      Mappr.gmap.addControl(new GLargeMapControl());
      Mappr.gmap.enableDoubleClickZoom();
      Mappr.gmap.enableContinuousZoom();
      Mappr.gmap.enableScrollWheelZoom();
    }

    Mappr.gmap.setCenter(new GLatLng(lat, lng), zoom);

    var icon = new GIcon();
    icon.image = Mappr.iconImgNormal;
    icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    icon.iconSize = new GSize(12, 20);
    icon.shadowSize = new GSize(22, 20);
    icon.iconAnchor = new GPoint(6, 20);
    icon.infoWindowAnchor = new GPoint(5, 1);
    Mappr.markerOpt = {icon:icon, draggable:false};
    for( var i = 0; typeof(initMarkers) != "undefined" && i < initMarkers.length; i++ )
    {
      Mappr.createMarker( initMarkers[i] );
    }
  },

  createMarker: function( mapMarker )
  {
    if( Mappr.gmap === null || !(GBrowserIsCompatible()) )
    {
      return;
    }

    var marker = new GMarker( new GLatLng(mapMarker.latitude, mapMarker.longitude),  Mappr.markerOpt);
    marker.info = mapMarker.info;
    marker.uri = mapMarker.uri;
    if( typeof(marker.info) != "undefined" && marker.info !== null && marker.info !== "")
    {
      GEvent.addDomListener(marker, "mouseover", marker.showFooter );
      GEvent.addDomListener(marker, "mouseout", marker.clearFooter );
    }
    if( typeof(marker.uri) != "undefined" && marker.uri !== null && marker.uri !== "")
    {
      GEvent.addDomListener(marker, "click", marker.goUri );
    }

    if( typeof(mapMarker.domEventBindings) != "undefined" && mapMarker.domEventBindings !== null)
    {
      GEvent.addDomListener( getElt(mapMarker.domEventBindings.source), "mouseover", function(){marker.hiLite()} );
      GEvent.addDomListener( getElt(mapMarker.domEventBindings.source), "mouseout",  function(){marker.loLite()} );
      GEvent.addDomListener( getElt(mapMarker.domEventBindings.source), mapMarker.domEventBindings.event, mapMarker.domEventBindings.action );
    }
    Mappr.markers[Mappr.markers.length] = marker;
    Mappr.gmap.addOverlay(marker);
  },

  clearMarkers: function()
  {
    for( var i = 0; Mappr.markers !== null && i < Mappr.markers.length; i++ )
    {
      Mappr.gmap.removeOverlay( Mappr.markers[i]  );
    }
    Mappr.markers = [];
  },

  Marker: function( latitude, longitude, info, uri, tzOffset, domEventBindings )
  {
    this.latitude   = latitude;
    this.longitude  = longitude;
    this.info       = info;
    this.uri        = uri;
    this.tzOffset   = isNaN(parseFloat(tzOffset))?(0):parseFloat(tzOffset);
    this.domEventBindings = domEventBindings;
  },

  DomEventBinding : function( source, event, action )
  {
    this.source  = source;
    this.event   = event;
    this.action  = action;
  },


  GMapExtend : function()
  {
    GMarker.prototype.showInfo = function(zoom)
    {
      this.openInfoWindowHtml( this.info );
      if(zoom && ! Mappr.gmap.getBounds().contains( this.getPoint() ) ){Mappr.gmap.setZoom(1);}
    };
    GMarker.prototype.showFooter = function()
    {
      $("#mapInfo").html(this.info);
    };
    GMarker.prototype.clearFooter = function()
    {
      $("#mapInfo").html("");
    };
    GMarker.prototype.goUri = function()
    {
      document.location.href = this.uri;
    };

    GMarker.prototype.hiLite = function()
    {
      if( Mappr.gmap.getBounds().contains( this.getPoint() ) )
      {
        this.setImage(Mappr.iconImgHLight);
      }
    };
    GMarker.prototype.loLite = function()
    {
      this.setImage(Mappr.iconImgNormal);
    };
  }
};

var UTube =
{
  cur: "",
  vids: [],
  playerElt:null,
  listId:null,
  init: function( userId, playListId, playerId, listId )
  {
    UTube.playerElt = $(playerId);
    UTube.listId = listId;
    $.get( "/pages/collector.jsp?r="+userId+"&p="+playListId+"&t=yt", UTube.parser, "xml" );
  },

  parser: function( data )
  {
    var media;
    $("entry",data).each(function(i)
    {
      media = $.nsTag("group", "media", this);
      UTube.vids[UTube.vids.length] = new Media(
                                        $.nsTag("content", "media", media).attr("url"), // $("media\\:content",media).attr("url"),
                                        $.nsTag("thumbnail", "media", media).eq(1).attr("url"), //$("media\\:thumbnail",media).eq(0).attr("url"),
                                        $.nsTag("title", "media", media).text(),
                                        "javascript:UTube.show("+i+")"
                                       );
    })

    UTube.show(0);

    if( typeof(UTube.listId) != "undefined" && UTube.listId != null )
    {
      $(UTube.listId)[0].liteBox = new LiteBox( UTube.listId, UTube.vids, 3, (new LiteBox()).SCROLL_BRACKET );
    }
  },

  show: function( vidId )
  {
    var params = { allowScriptAccess: "always" };
    var atts = { id: "myytplayer" };

    UTube.playerElt.html( "<object><param name=\"movie\" data=\""+UTube.vids[vidId].origSrc+"\" value=\""+UTube.vids[vidId].origSrc+"\" /><param name=\"allowFullScreen\" value=\"true\" /><param name=\"FlashVars\" value=\"playerMode=embedded\" /><embed src=\""+UTube.vids[vidId].origSrc+"\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" FlashVar=\"playerMode=embedded\"></embed></object>" );
  }
}

function Media(origSrc,thumbSrc,title,link,width,height,length)
{
  this.origSrc = origSrc;
  this.thumbSrc = thumbSrc;
  this.title = title;
  this.link = link;
  this.width = width;
  this.height = height;
  this.length = length;
};

jQuery.extend(
{
  nsTag:function( tagName, ns, context )
  {
    if( (typeof(ns) != "string" || ns == "") )
    {
      return $(tagName, context)
    }

    if( typeof(context) != "object" )
    {
      context = document;
    }

    context = $(context)[0];

    var htmlColl = context.getElementsByTagName(ns+":"+tagName);

    if( htmlColl.length == 0 )
    {
      htmlColl = context.getElementsByTagName(tagName);
      if( htmlColl.length == 0 )
      {
        htmlColl = context.getElementsByTagName("ns:"+tagName);
        if( htmlColl.length == 0 && document.getElementsByTagNameNS )
        {
          htmlColl = context.getElementsByTagNameNS(ns, tagName);
        }
      }
    }
    return $(htmlColl);
  }
});

var mapTags = [];
$(document).ready(function()
{
  UTube.init("EdBallsMP","A193579F62843246","#vidPlyr","#vidList");
  Flckr.init("9012231@N08", "#flckrAll", 9, (new LiteBox()).SCROLL_UNDER);

  Mappr.init(false,53.700365,-1.505127,10, mapTags);
});


