/*<Video 
	MediaID="42869320" 
	UserID="393250426" 
	MediaSourceID="0" 
	MediaType="" 
	
	Thumbnail="http://cache01-videos02.myspacecdn.com/18/thumb1_228a43a97d90f823553b12beb8a581e8.jpg" 
	LargeThumbURI="http://cache01-videos02.myspacecdn.com/18/thumb1_228a43a97d90f823553b12beb8a581e8.jpg" 
	FirstThumbURI="http://cache01-videos02.myspacecdn.com/18/thumb1_228a43a97d90f823553b12beb8a581e8.jpg" 
	SecondThumbURI="http://cache01-videos02.myspacecdn.com/18/thumb1_228a43a97d90f823553b12beb8a581e8.jpg" 
	ThirdThumbURI="http://cache01-videos02.myspacecdn.com/18/thumb1_228a43a97d90f823553b12beb8a581e8.jpg" 
	ThumbUri="http://cache01-videos02.myspacecdn.com/18/thumb1_228a43a97d90f823553b12beb8a581e8.jpg" 
	
	FileUri="/index.cfm?fuseaction=vids.individual&VideoID=42869320" 
	
	LastUpdate="2008-09-15T06:25:52.303" 
	Genre="Entertainment," 
	Runtime="183" 
	TotalSeconds="183" 
	
	TotalViews="6189" 
	AverageRating="90" 
	TotalComments="0" 
	
	IsFavorite="false" 
	PId="">
<Title>Episode 10 Highlight 1</Title>
<UserDisplayName>prauhighlights</UserDisplayName>
<Description>Lee&#39;s Final Runway</Description>
</Video>
*/
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();
		
		//alert(this.toString());
		
		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 MSVideoFeedXMLParser(id) {
		
	this.XML_ELEMENT_ROOT = "Videos";
	this.XML_ELEMENT_VIDEO = "Video";
	
	this.videos = new Array();
	this.onLoadCallback = function() { return true; };
	this.id = id;
	
	this.getVideos = getVideos;
	this.setVideos = setVideos;
	this.addVideo = addVideo;
	this.getVideo = getVideo;
	this.setVideo = setVideo;
	this.setId = setId;
	this.getId = getId;
	this.parse = parse;
	this.getVideoCount = getVideoCount;
	this.parseDocument = parseDocument;
	this.handleRequestStateChange = handleRequestStateChange;
	this.downloadDocument = downloadDocument;
	
	function getId() {
		return this.id;
	}
	function setId(id) {
		this.id = id;
	}

	function getVideos() {
		return this.videos;
	}
	function setVideos(videos) {
		this.videos = videos;
	}
	
	function getVideo(index) {
		if(index < this.videos.length)
			return this.videos[index];
		
		return new VideoXML();
	}
	function getVideoCount() {
		return this.videos.length;
	}
	function addVideo(video) {
		this.videos[this.videos.length] = video;
	}
	function setVideo(index, video) {
		this.videos[index] = video;
	}
	
	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(httpRequest, callback, url) {	
		this.onLoadCallback = callback;
		
		
		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('text/xml');
		}

		downloadDocument(httpRequest, url, this);
		
	}

	function parseDocument(xmlDoc) {
  		if(!xmlDoc.hasChildNodes())
  			return;
  		
		var videoNodes = xmlDoc.getElementsByTagName("Video");
		
		for(i = 0; i < videoNodes.length; i++) {
			video = new VideoXML();
			video.fromDOMNode(videoNodes[i]);
			this.addVideo(video);
		}
		
		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 {
		    }
		}	
	}

}