/*
new 
<channel>
    <title>Rip Curl Video Channel</title>
    <description>Upload and share videos, find and watch funny clips, vote and comment on your favorites, subscribe to online video channels, add cool videos to your MySpace profile at MySpaceTV.com</description>
    <link>http://vids.myspace.com/index.cfm?fuseaction=vids.channel&amp;channelID=43032638&amp;RSSAction=VideoChannel</link>
    <language>en-AU</language>
 
    <copyright>MySpace, Inc. All Rights Reserved</copyright>
    <managingEditor>videos@myspace.com (myspacetv)</managingEditor>
    <webMaster>videos@myspace.com (myspacetv)</webMaster>
    <lastBuildDate>Sat, 28 Mar 2009 16:19:33 GMT</lastBuildDate>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <generator>myspacetv</generator>
 
    <myspace:totalResults>114</myspace:totalResults>
    <myspace:totalPages>5</myspace:totalPages>
    <myspace:itemsPerPage>20</myspace:itemsPerPage>
    <myspace:logo>http://x.myspacecdn.com/modules/videos/static/img/tiny-css/logo_myspacetv.gif</myspace:logo>
    <item>
      <title>Steph Gilmore</title>
 
      <description>MySpace interview with Steph Gilmore</description>
      <link>http://vids.myspace.com/index.cfm?fuseaction=vids.individual&amp;VideoID=51710757</link>
      <guid>http://vids.myspace.com/index.cfm?fuseaction=vids.individual&amp;VideoID=51710757</guid>
      <pubDate>Tue, 03 Feb 2009 22:42:47 GMT</pubDate>
      <myspace:videoIndex>1</myspace:videoIndex>
 
      <myspace:statistics  plays="61" comments="0" />
      <media:title>Steph Gilmore</media:title>
      <media:content url="http://mediaservices.myspace.com/services/media/embed.aspx/m=51710757,t=1,mt=video,ap=0" type="application/x-shockwave-flash" medium="video" isDefault="true" expression="full" duration="190" />
      <media:player url="http://mediaservices.myspace.com/services/media/embed.aspx/m=51710757,t=1,mt=video,ap=0" height="360" width="640" />
      <media:thumbnail url="http://d3.ac-videos.myspacecdn.com/videos02/72/thumb1_f7bb050b355c46d885beab07eaa87ba2.jpg" />
      <media:credit role="author" scheme="http://vids.myspace.com/index.cfm?fuseaction=vids.channel&amp;ChannelID=43032638" />
      <media:category scheme="http://vids.myspace.com/index.cfm?fuseaction=vids.charts&amp;category=14" label="Sports" />
      <media:category scheme="http://vids.myspace.com/index.cfm?fuseaction=vids.charts&amp;category=405" label="405" />
 
      <media:keywords>surf ripcurl interview steph gilmore </media:keywords>
    </item>
 
*/
 

function RSSMediaItemXML() {
 
 this.rssItemXML = new RSSItemXML();
 
 //XML tags
 this.XML_NS = "http://search.yahoo.com/mrss";
 
 this.XML_ELEMENT_TITLE = "title";
 this.XML_ATTR_URL = "url";
 this.XML_ATTR_HEIGHT = "height";
 this.XML_ATTR_WIDTH = "width";
 this.XML_ATTR_SCHEME = "scheme";
 this.XML_ATTR_ROLE = "role";
 this.XML_ATTR_LABEL = "label";
 
 this.XML_ELEMENT_CONTENT = "content";
 this.XML_ATTR_CONTENT_TYPE = "type";
 this.XML_ATTR_CONTENT_MEDIUM = "medium";
 this.XML_ATTR_CONTENT_ISDEFAULT = "isDefault";
 this.XML_ATTR_CONTENT_EXPRESSION = "expression";
 this.XML_ATTR_CONTENT_DURATION = "duration";
 
 this.XML_ELEMENT_PLAYER = "player";
 this.XML_ELEMENT_THUMBNAIL = "thumbnail";
 this.XML_ELEMENT_CREDIT = "credit";
 
 this.XML_ELEMENT_CATEGORY = "category";
 
 this.XML_ELEMENT_KEYWORDS = "keywords";
 
 
 
 this.categories = new Array();
 
 this.title = "";
 this.content = "";
 this.player = "";
 this.thumbnail = "";
 this.thumbnailUri = "";
 this.credit = "";
 this.keywords = "";
 
 //getters + setters
 this.getTitle = getTitle;
 this.setTitle = setTitle;
 this.getContent = getContent;
 this.setContent = setContent;
 this.getPlayer = getPlayer;
 this.setPlayer = setPlayer;
 this.getThumbnail = getThumbnail;
 this.setThumbnail = setThumbnail;
 this.getCredit = getCredit;
 this.setCredit = setCredit;
 this.getKeywords = getKeywords;
 this.setKeywords = setKeywords;
 this.addCategory = addCategory;
 this.getCategories = getCategories;
 
 this.setRssItemXML = setRssItemXML;
 this.getRssItemXML = getRssItemXML;
 
 this.getRootElementName = getRootElementName;
 this.fromDOMNode = fromDOMNode;
 this.toString = toString;
 
 function getTitle() {
  return this.title;
 }
 function setTitle(title) {
  this.title = title;
 }
 function getContent() {
  return this.content;
 }
 function setContent(url, type, medium, isDefault, expression, duration) {
  this.content = { url : url, type : type, medium : medium, isDefault : isDefault, expression : expression, duration : duration};
 }
 function getPlayer() {
  return this.player;
 }
 function setPlayer(url, height, width) {
  this.player = { url: url, height : height, width : width };
 }
 
 function loadImage(imgUri) {
  thumbnail = new Image();
  thumbnail.src = imgUri; 
  
  return thumbnail;
 }
 function getThumbnail() {
  return this.thumbnail;
 }
 function setThumbnail(url) {
  this.thumbnail = new Image();
  this.thumbnail.src = url;
 }
 function getCredit() {
  return this.credit;
 }
 function setCredit(role, scheme) {
  this.credit = { role : role, scheme : scheme };
 }
 function addCategory(label, scheme) {
  this.categories[this.categories.length] = { label : label, scheme : scheme};
 }
 function getCategories() {
  return this.categories;
 }
 function getKeywords() {
  return this.keywords;
 }
 function setKeywords(keywords) {
  this.keywords = keywords;
 }
 function setRssItemXML(rssItemXML) {
  this.rssItemXML = rssItemXML;
 }
 function getRssItemXML() {
  return this.rssItemXML;
 }
 
 function serialise() {
 
 }
 
 function getRootElementName() {
  return this.getRssItemXML().XML_ELEMENT_ROOT;
 }
 
 function fromDOMNode(node) {
  this.categories = new Array();
  
  if(!node)
   return;
  
  this.getRssItemXML().fromDOMNode(node);
  
  this.setTitle(getElementsByTagNameNS(node, this.XML_NS, this.XML_ELEMENT_TITLE)[0].childNodes[0].nodeValue);
  
  //childNode = node.getElementsByTagNameNS(this.XML_NS, this.XML_ELEMENT_CONTENT)[0];
  childNode = getElementsByTagNameNS(node, this.XML_NS, this.XML_ELEMENT_CONTENT)[0];
  this.setContent(childNode.getAttribute(this.XML_ATTR_URL), childNode.getAttribute(this.XML_ATTR_CONTENT_TYPE), childNode.getAttribute(this.XML_ATTR_CONTENT_MEDIUM), childNode.getAttribute(this.XML_ATTR_CONTENT_ISDEFAULT), childNode.getAttribute(this.XML_ATTR_CONTENT_EXPRESSION), childNode.getAttribute(this.XML_ATTR_CONTENT_DURATION));
  
  //childNode = node.getElementsByTagNameNS(this.XML_NS, this.XML_ELEMENT_PLAYER)[0];
  childNode = getElementsByTagNameNS(node, this.XML_NS, this.XML_ELEMENT_PLAYER)[0];
  this.setPlayer(childNode.getAttribute(this.XML_ATTR_URL), childNode.getAttribute(this.XML_ATTR_HEIGHT), childNode.getAttribute(this.XML_ATTR_WIDTH)); 
  
  //childNode = node.getElementsByTagNameNS(this.XML_NS, this.XML_ELEMENT_THUMBNAIL)[0];
  childNode = getElementsByTagNameNS(node, this.XML_NS, this.XML_ELEMENT_THUMBNAIL)[0];
  this.setThumbnail(childNode.getAttribute(this.XML_ATTR_URL));  
  
  //childNode = node.getElementsByTagNameNS(this.XML_NS, this.XML_ELEMENT_CREDIT)[0];
  childNode = getElementsByTagNameNS(node, this.XML_NS, this.XML_ELEMENT_CREDIT)[0];
  this.setCredit(childNode.getAttribute(this.XML_ATTR_ROLE), childNode.getAttribute(this.XML_ATTR_SCHEME));
  
  //childNodes = node.getElementsByTagNameNS(this.XML_NS, this.XML_ELEMENT_CATEGORY);
  childNodes = getElementsByTagNameNS(node, this.XML_NS, this.XML_ELEMENT_CATEGORY)[0];
  for(var i = 0; i < childNodes.length; i++) {
   childNode = childNodes[i];
   this.addCategory(childNode.getAttribute(this.XML_ATTR_LABEL), childNode.getAttribute(this.XML_ATTR_SCHEME));
  }
  
  this.setKeywords(getElementsByTagNameNS(node, this.XML_NS, this.XML_ELEMENT_KEYWORDS)[0].childNodes[0].nodeValue);
 }
 
 function printChildren(node) {
  if(node.childNodes)
   printNodes(node.childNodes);
 }
 
 function printNodes(nodes) {
  if(nodes) {
   result = "count=" + nodes.length + "\n";
 
   for(var i = 0; i < nodes.length; i++) {
    aNode = nodes[i]; 
    result += "[node: name=" + aNode.nodeName + ", type=" + aNode.nodeType + ", data=" + aNode.nodeValue + "]\n";
   }
  
   alert(result);
  } else {
   alert('no nodes supplied.');
  }
 }
 
 function toString() {
  
  result = "RSSMediaItemXML[" + this.getRssItemXML() + ", ";
  
  result = result + ", title=" + this.title
      + ", content=[url=" + this.getContent().url + ", type=" + this.getContent().type + ", medium=" + this.getContent().medium + ", duration=" + this.getContent().expression + "]";
      
  return result;    
      
 }
}
 
function getNSElementName(namespace, elementName) {
 return namespace + ':' + elementName;
}
 
function getElementsByTagNameNS(node, namespace, elementName) {
 return google.feeds.getElementsByTagNameNS(node, namespace, elementName);
}
 
function RSSItemXML(title, description, link, guid, pubDate) {
 
 //XML tags
 this.XML_ELEMENT_ROOT = "item";
 
 this.XML_ELEMENT_TITLE = "title";
 this.XML_ELEMENT_DESC = "description";
 this.XML_ELEMENT_LINK = "link";
 this.XML_ELEMENT_GUID = "guid";
 this.XML_ELEMENT_PUB_DATE = "pubDate";
  
 //attributes
 this.title = title;
 this.description = description; 
 this.link = link;
 this.guid = guid
 this.pubDate = pubDate;
 
 //getters + setters
 this.getTitle = getTitle;
 this.setTitle = setTitle;
 this.getDescription = getDescription;
 this.setDescription = setDescription;
 this.getLink = getLink;
 this.setLink = setLink;
 this.getGuid = getGuid;
 this.setGuid = setGuid;
 this.getPubDate = getPubDate;
 this.setPubDate = setPubDate;
 
 //methods
 this.getRootElementName = getRootElementName;
 this.fromDOMNode = fromDOMNode;
 this.toString = toString;
 
 function getLink() {
  return this.link
 }
 function setLink(link) {
  this.link = link;
 }
 function getGuid() {
  return this.guid
 }
 function setGuid(guid) {
  this.guid = guid;
 }
 function getTitle() {
  return this.title
 }
 function setTitle(title) {
  this.title = title
 }
 function getPubDate() {
  return this.pubDate
 }
 function setPubDate(pubDate) {
  this.pubDate = pubDate;
 }
 function getDescription() {
  return this.description
 }
 function setDescription(description) {
  this.description = description;
 }
 
 function serialise() {
 
 }
 
 function getRootElementName() {
  return this.XML_ELEMENT_ROOT;
 }
 
 function fromDOMNode(node) {
  this.setTitle(node.getElementsByTagName(this.XML_ELEMENT_TITLE)[0].childNodes[0].nodeValue);
  this.setDescription(node.getElementsByTagName(this.XML_ELEMENT_DESC)[0].childNodes[0].nodeValue);
  this.setGuid(node.getElementsByTagName(this.XML_ELEMENT_GUID)[0].childNodes[0].nodeValue);
  this.setLink(node.getElementsByTagName(this.XML_ELEMENT_LINK)[0].childNodes[0].nodeValue);
  this.setPubDate(node.getElementsByTagName(this.XML_ELEMENT_PUB_DATE)[0].childNodes[0].nodeValue);  
 }
 
 function toString() {
  return "RSSItemXML[title=" + this.title + ", guid=" + this.guid + ", link=" + this.link + ", pubDate=" + this.pubDate + ", description=" + this.description + "]";
 }
}
 
 
 

function VideoXML(fileUri, title, userDisplayName, description, mediaId, lazy) {
 
 //XML tags
 this.XML_ELEMENT_ROOT = "Video";
 
 this.XML_ATTR_THUMB_URI = "ThumbUri";
 this.XML_ATTR_THUMB_NAIL = "Thumbnail";
 this.XML_ATTR_THUMB_LARGE = "LargeThumbURI";
 this.XML_ATTR_THUMB_FIRST = "FirstThumbURI";
 this.XML_ATTR_THUMB_SECOND = "SecondThumbURI";
 this.XML_ATTR_THUMB_THIRD = "ThirdThumbURI";
 
 this.XML_ATTR_FILE_URI = "FileUri";
 this.XML_ATTR_MEDIA_ID = "MediaID";
 
 this.XML_THUMB_ATTRIBS = new Array(this.XML_ATTR_THUMB_URI, this.XML_ATTR_THUMB_NAIL, this.XML_ATTR_THUMB_LARGE, this.XML_ATTR_THUMB_FIRST, this.XML_ATTR_THUMB_SECOND, this.XML_ATTR_THUMB_THIRD);
 
 this.XML_ELEMENT_TITLE = "Title";
 this.XML_ELEMENT_USER_DISPLAY_NAME = "UserDisplayName";
 this.XML_ELEMENT_DESC = "Description";
 
 //Array to store loaded thumbnail images
 this.images = new Array();
 this.imageUris = new Array();
 
 //attributes
 this.mediaId = mediaId
 this.fileUri = fileUri;
 this.title = title;
 this.userDisplayName = userDisplayName;
 this.description = description; 
 this.lazy = lazy;
 
 //getters + setters
 this.getMediaId = getMediaId;
 this.setMediaId = setMediaId;
 this.getFileUri = getFileUri;
 this.setFileUri = setFileUri;
 this.getTitle = getTitle;
 this.setTitle = setTitle;
 this.getThumbnail = getThumbnail;
 this.getUserDisplayName = getUserDisplayName;
 this.setUserDisplayName = setUserDisplayName;
 this.getDescription = getDescription;
 this.setDescription = setDescription;
 
 //this.getThumbnail = getThumbnail
 //this.getLargeThumbnail = getLargeThumbnail;
 
 //methods
 this.loadImage = loadImage;
 this.getRootElementName = getRootElementName;
 this.fromDOMNode = fromDOMNode;
 this.toString = toString;
 
 function getThumbnail(indx) {
  if(indx >= this.XML_THUMB_ATTRIBS.length)
   return '';
  
  if(!this.images[this.XML_THUMB_ATTRIBS[indx]]) {
   this.images[this.XML_THUMB_ATTRIBS[indx]] = this.loadImage(this.imageUris[this.XML_THUMB_ATTRIBS[indx]]);
  }
  
  return this.images[this.XML_THUMB_ATTRIBS[indx]];
 }
 
 function getMediaId() {
  return this.mediaId
 }
 function setMediaId(mediaId) {
  this.mediaId = mediaId;
 }
 function getFileUri() {
  return this.fileUri
 }
 function setFileUri(fileUri) {
  this.fileUri = fileUri;
 }
 function getTitle() {
  return this.title
 }
 function setTitle(title) {
  this.title = title
 }
 function getUserDisplayName() {
  return this.userDisplayName
 }
 function setUserDisplayName(userDisplayName) {
  this.userDisplayName = userDisplayName;
 }
 function getDescription() {
  return this.description
 }
 function setDescription(description) {
  this.description = description;
 }
 
 function loadImage(imgUri) {
  thumbnail = new Image();
  thumbnail.src = imgUri; 
  
  return thumbnail;
 }
 
 function serialise() {
 
 }
 
 function getRootElementName() {
  return this.XML_ELEMENT_ROOT;
 }
 
 function fromDOMNode(node) {
  this.setMediaId(node.getAttribute(this.XML_ATTR_MEDIA_ID));
  this.setFileUri(node.getAttribute(this.XML_ATTR_FILE_URI));
  this.setTitle(node.getElementsByTagName(this.XML_ELEMENT_TITLE)[0].childNodes[0].nodeValue);
  this.setUserDisplayName(node.getElementsByTagName(this.XML_ELEMENT_USER_DISPLAY_NAME)[0].childNodes[0].nodeValue);
  this.setDescription(node.getElementsByTagName(this.XML_ELEMENT_DESC)[0].childNodes[0].nodeValue);
  
  this.images = new Array();
  this.imageUris = new Array();
    
  for(thumbAttribName in this.XML_THUMB_ATTRIBS) {
   thumbUri = node.getAttribute(this.XML_THUMB_ATTRIBS[thumbAttribName]);
   
   if(!lazy)
    this.images[this.XML_THUMB_ATTRIBS[thumbAttribName]] = this.loadImage(thumbUri);
   
   this.imageUris[thumbAttribName] = thumbUri;
  }
   
 }
 
 function toString() {
  return "VideoXML[title=" + this.title + ", mediaId=" + this.mediaId + ", fileUri=" + this.fileUri + ", userDisplayName=" + this.userDisplayName + ", description=" + this.description + "]";
 }
}
 
 
 
function RSSFeedXMLParser(id) {
  
 this.XML_ELEMENT_ROOT = "channel";
 this.XML_ELEMENT_ITEM = "item";
 
 this.items = new Array();
 this.onLoadCallback = function() { return true; };
 this.id = id;
 
 this.getItems = getItems;
 this.setItems = setItems;
 this.addItem = addItem;
 this.getItem = getItem;
 this.setItem = setItem;
 this.setId = setId;
 this.getId = getId;
 this.parse = parse;
 this.getItemCount = getItemCount;
 this.parseDocument = parseDocument;
 this.parseResult = parseResult;
 this.handleRequestStateChange = handleRequestStateChange;
 this.downloadDocument = downloadDocument;
 
 function getId() {
  return this.id;
 }
 function setId(id) {
  this.id = id;
 }
 
 function getItems() {
  return this.items;
 }
 function setItems(items) {
  this.items = items;
 }
 
 function getItem(index) {
  if(index < this.items.length)
   return this.items[index];
  
  return new RSSMediaItemXML();
 }
 function getItemCount() {
  return this.items.length;
 }
 function addItem(item) {
  this.items[this.items.length] = item;
 }
 function setItem(index, item) {
  this.items[index] = item;
 }
 
 function serialise() {
  /*response = "<" + this.XML_ELEMENT_ROOT + ">"
  for(i = 0; i < Videos.length; i++)
   response += this.Videos.serialise();
  response += "</" + this.XML_ELEMENT_ROOT + ">";
  
  return response;*/
 }
 
 function parse(feed, callback) { 
  var parser = this;
  this.onLoadCallback = callback;
  
  feed.setResultFormat(google.feeds.Feed.XML_FORMAT);
  
  feed.load( function(result){ parser.parseDocument(result.xmlDocument); } );
 }
 
 function parseResult(feed, parser) {
  parser.parseDocument(feed.xmlDocument);
 }
 
 function parseDocument(xmlDoc) {
    if(!xmlDoc.hasChildNodes())
     return;
    
  var itemNodes = xmlDoc.getElementsByTagName(this.XML_ELEMENT_ITEM);
    
  for(var i = 0; i < itemNodes.length; i++) {
   var rssItem = new RSSMediaItemXML();
   rssItem.fromDOMNode(itemNodes[i]);
   this.addItem(rssItem);
  }
  
  this.onLoadCallback(this);
 }
 
 function downloadDocument(httpRequest, url, dest) {
   httpRequest.onreadystatechange = function() { handleRequestStateChange(httpRequest, dest); };   
         httpRequest.open('GET', url, true);
         httpRequest.send(''); 
 }
 
 function handleRequestStateChange(httpRequest, parser) {
  if (httpRequest.readyState == 4) {
      if (httpRequest.status == 200) {
    parser.parseDocument(httpRequest.responseXML);
      } else {
      }
  } 
 }
 
}

