﻿/**
 * Copyright 2007 Fox Interactive Media
 * <DISCLAIMER HERE>
 * @fileoverview Interface for container and associated classes; everything needed to query/update MySpace API data via OpenSocial interface.
 * 
 * @author mnewbould [_at_] myspace [_dot_] com (Max Newbould)
 * @author crussell [_at_] myspace [_dot_] com (Chad Russell)
 */
 
 /**
 * A class representing an album.
 * @constructor
 * @private
 */
MyOpenSpace.Album = function() {}
/**
 * The fields for MyOpenSpace.Album
 * @class
 * @name MyOpenSpace.Album.Field
 * @static
 */
MyOpenSpace.Album.Field = {
    /**
     * A number representing an Album's unique identifier.
     * @memberOf MyOpenSpace.Album.Field
     */
    ALBUM_ID:"ALBUM_ID",
    
    /**
     * The RESTFUL URI with which to access the album on the API.
     * @memberOf MyOpenSpace.Album.Field
     */
    ALBUM_URI:"ALBUM_URI",
    
    /**
     * The album's title.
     * @memberOf MyOpenSpace.Album.Field
     */
    TITLE:"TITLE",
    
    /**
     * The geographic location where the album's pictures were taken.
     * @memberOf MyOpenSpace.Album.Field
     */
    LOCATION:"LOCATION",
    
    /**
     * A URL for the album's default image.
     * @memberOf MyOpenSpace.Album.Field
     */
    DEFAULT_IMAGE:"DEFAULT_IMAGE",
    
    /**
     * A string representing the album's privacy setting, such as "Public" or "Private"
     * @memberOf MyOpenSpace.Album.Field
     */
    PRIVACY:"PRIVACY",
    
    /**
     * An integer representing the total number of photos in the album (not the number of photos actually contained within the current object).
     * @memberOf MyOpenSpace.Album.Field
     */
    PHOTO_COUNT:"PHOTO_COUNT",
    
    /**
     * A RESTFUL URI with which to access the photos contained in the album.
     * @memberOf MyOpenSpace.Album.Field
     */
    PHOTOS_URI:"PHOTOS_URI"
};

/**
 * Returns the field specified by key
 * @param {String} key The key to search by
 * @return {MyOpenSpace.Album || undefined} The album if found, nothing otherwise.
 */
MyOpenSpace.Album.prototype.getField = function(key) { return this[key]; };
/**
 * Writes a value to the field specified by the key
 * @param {String} key The key to search by.
 * @param {String} val The value to set.
 * @private
 * @internal
 */
MyOpenSpace.Album.prototype.setField_ = function(key,val) { this[key] = val; };

/**
 * A class representing a video.
 * @constructor
 * @private
 */
MyOpenSpace.Video = function() {}
/**
 * The fields for MyOpenSpace.Video
 * @class
 * @name MyOpenSpace.Video.Field
 * @static
 */
MyOpenSpace.Video.Field = {
    /**
     * A number representing a Video's unique identifier.
     * @memberOf MyOpenSpace.Video.Field
     */
    VIDEO_ID:"VIDEO_ID",
    
    /**
     * The RESTFUL URI with which to access the video on the API.
     * @memberOf MyOpenSpace.Video.Field
     */
    VIDEO_URI:"VIDEO_URI",
    
    /**
     * The video's title.
     * @memberOf MyOpenSpace.Video.Field
     */
    TITLE:"TITLE",
    
    /**
     * The date the video was created.
     * @memberOf MyOpenSpace.Video.Field
     */
    DATE_CREATED:"DATE_CREATED",
    
    /**
     * The date the video was last updated.
     * @memberOf MyOpenSpace.Video.Field
     */
    LAST_UPDATE:"LAST_UPDATE",
    
    /**
     * An integer representing the media type for this video.
     * @memberOf MyOpenSpace.Video.Field
     */
    MEDIA_TYPE:"MEDIA_TYPE",
    
    /**
     * A URL for the thumbnail image associated with this video.
     * @memberOf MyOpenSpace.Video.Field
     */
    THUMB_URI:"THUMB_URI",
    
    /**
     * A description of the video.
     * @memberOf MyOpenSpace.Video.Field
     */
    DESCRIPTION:"DESCRIPTION",
    
    /**
     * The current status of the video, such as "ProcessingFailed"
     * @memberOf MyOpenSpace.Video.Field
     */
    MEDIA_STATUS:"MEDIA_STATUS",
    
    /**
     * The length of the video.
     * @memberOf MyOpenSpace.Video.Field
     */
    RUN_TIME:"RUN_TIME",
    
    /**
     * An integer representing the number of views the video has currently received.
     * @memberOf MyOpenSpace.Video.Field
     */
    TOTAL_VIEWS:"TOTAL_VIEWS",
    
    /**
     * An integer representing the number of comments the video has currently received.
     * @memberOf MyOpenSpace.Video.Field
     */
    TOTAL_COMMENTS:"TOTAL_COMMENTS",
    
    /**
     * An integer representing the rating the video has currently received.
     * @memberOf MyOpenSpace.Video.Field
     */
    TOTAL_RATING:"TOTAL_RATING",
    
    /**
     * An integer representing the number of votes the video has currently received.
     * @memberOf MyOpenSpace.Video.Field
     */
    TOTAL_VOTES:"TOTAL_VOTES",
    
    /**
     * A string representing the video's country, in terms of identifying culture, not necessarily geographic location.
     * @memberOf MyOpenSpace.Video.Field
     */
    COUNTRY:"COUNTRY",
    
    /**
     * A string representing the video's language.
     * @memberOf MyOpenSpace.Video.Field
     */
    LANGUAGE:"LANGUAGE"
};
/**
 * Returns the field specified by key
 * @param {String} key The key to search by
 * @return {MyOpenSpace.Video || undefined} The video if found, nothing otherwise.
 */
MyOpenSpace.Video.prototype.getField = function(key) { return this[key]; };
/**
 * Writes a value to the field specified by the key
 * @param {String} key The key to search by.
 * @param {String} val The value to set.
 * @private
 * @internal
 */
MyOpenSpace.Video.prototype.setField_ = function(key,val) { this[key] = val; };


/**
 * A class representing a photo.
 * @constructor
 * @private
 */
MyOpenSpace.Photo = function() {}
/**
 * The fields for MyOpenSpace.Photo
 * @class
 * @name MyOpenSpace.Photo.Field
 * @static
 */
MyOpenSpace.Photo.Field = {
    /**
     * A number representing a Photo's unique identifier.
     * @memberOf MyOpenSpace.Photo.Field
     */
    PHOTO_ID:"PHOTO_ID",
    
    /**
     * The RESTFUL URI with which to access the photo on the API.
     * @memberOf MyOpenSpace.Photo.Field
     */
    PHOTO_URI:"PHOTO_URI",
      
    /**
     * The URL of the photo.
     * @memberOf MyOpenSpace.Photo.Field
     */
    IMAGE_URI:"IMAGE_URI",
    
    /**
     * The photo's caption.
     * @memberOf MyOpenSpace.Photo.Field
     */
    CAPTION:"CAPTION"
};

/**
 * Returns the field specified by key
 * @param {String} key The key to search by
 * @return {MyOpenSpace.Photo || undefined} The photo if found, nothing otherwise.
 */
MyOpenSpace.Photo.prototype.getField = function(key) { return this[key]; };
/**
 * Writes a value to the field specified by the key
 * @param {String} key The key to search by.
 * @param {String} val The value to set.
 * @private
 * @internal
 */
MyOpenSpace.Photo.prototype.setField_ = function(key,val) { this[key] = val; };

/**
 * RSS Support methods
 * @internal
 */
 if (typeof(MyOpenSpace.Feed) == "undefined") MyOpenSpace.Feed = { "Supported" : "RSS2.0", RSS2 : {} };
 MyOpenSpace.Feed.RSS2 = {
    Channel : function (xmlFeed, summaryOnly, numEntries) {
        this.title;
        this.link;
        this.description;
          this.language;
            this.copyright;
            this.managingEditor;
            this.webMaster;
            this.pubDate;
            this.lastBuildDate;
            this.generator;
            this.docs;
            this.ttl;
            this.rating;
        
            var chanElement = xmlFeed.getElementsByTagName("channel")[0];
            var properties = new Array("title", "link", "description");
            var tmpElement = null;
            for (var i=0; i<properties.length; i++)
            {
                tmpElement = chanElement.getElementsByTagName(properties[i])[0];
                if (tmpElement!= null){
	                 var child = tmpElement.firstChild;
						while (child) {
							if(child.nodeValue.replace(/\s*\n*/g,"")!="")this[properties[i]] = child.nodeValue;
							child = child.nextSibling;
					}
				}
            }
    
            numEntries = numEntries || 3; //default as in opensocialreference
         

            /*optional object properties*/
            this.category;
            this.image;
            if (!summaryOnly) {
                this.items = new Array();
//                var chanElement = xmlFeed.getElementsByTagName("channel")[0];
                var itemElements = xmlFeed.getElementsByTagName("item");
                numEntries = (numEntries < itemElements.length) ? numEntries : itemElements.length;
                for (var i=0; i<numEntries; i++)
                {
                    Item = new MyOpenSpace.Feed.RSS2.Item(itemElements[i]);
                    this.items.push(Item);
                }
            }
            var properties = ["title", "link", "description", "language", "copyright", "managingEditor", "webMaster", "pubDate", "lastBuildDate", "generator", "docs", "ttl", "rating"];
            var tmpElement = null;
            for (var i=0; i<properties.length; i++)
            {
                tmpElement = chanElement.getElementsByTagName(properties[i])[0];
                if (tmpElement!= null){
                 var child = tmpElement.firstChild;
					while (child) {
						if(child.nodeValue.replace(/\s*\n*/g,"")!="")this[properties[i]] = child.nodeValue;
						child = child.nextSibling;
					}
				}
            }

            this.category = new MyOpenSpace.Feed.RSS2.Category(chanElement.getElementsByTagName("category")[0]);
            this.image = new MyOpenSpace.Feed.RSS2.Image(chanElement.getElementsByTagName("image")[0]);
        
    },
    Category : function(categoryNode)
    {
        if (categoryNode == null) {
            this.domain = null;
            this.value = null;
        } else {
            this.domain = categoryNode.getAttribute("domain");
            this.value = categoryNode.childNodes[0].nodeValue;
        }
    },
    Image : function(imgElement)
    {
        if (imgElement == null) {
            this.url = null;
            this.link = null;
            this.width = null;
            this.height = null;
            this.description = null;
        } else {
            imgAttribs = ["url","title","link","width","height","description"];
            for (var i=0; i<imgAttribs.length; i++)
                if (imgElement.getAttribute(imgAttribs[i]) != null)
                    this[imgAttribs[i]] = imgElement.getAttribute(imgAttribs[i]);
        }
    },
    Item : function(itemxml)
    {
        /*required*/
        this.title;
        this.link;
        this.description;

        /*optional*/
        this.author;
        this.comments;
        this.pubDate;
        this.category;
        this.enclosure;
        this.guid;
        this.source;

        var properties = ["title", "link", "description", "author", "comments", "pubDate"];
        var tmpElement = null;
        for (var i=0; i<properties.length; i++)
        {	
			this[properties[i]] = ""; //Resolve "<item></item>" issue
			try{
                tmpElement = itemxml.getElementsByTagName(properties[i])[0];
				if (tmpElement != null){
					var child = tmpElement.firstChild;
					while (child) {
						if(child.nodeValue.replace(/\s*\n*/g,"")!="")this[properties[i]] = child.nodeValue;
						child = child.nextSibling;
					}
				}//this[properties[i]] = tmpElement.childNodes[0].nodeValue;
			}catch(e){} 
        }

        this.category = new MyOpenSpace.Feed.RSS2.Category(itemxml.getElementsByTagName("category")[0]);
        this.enclosure = new MyOpenSpace.Feed.RSS2.Enclosure(itemxml.getElementsByTagName("enclosure")[0]);
        this.guid = new MyOpenSpace.Feed.RSS2.Guid(itemxml.getElementsByTagName("guid")[0]);
        this.source = new MyOpenSpace.Feed.RSS2.Source(itemxml.getElementsByTagName("source")[0]);
    },
    Enclosure : function(encElement)
    {
        if (encElement == null) {
            this.url = null;
            this.length = null;
            this.type = null;
        } else {
            this.url = encElement.getAttribute("url");
            this.length = encElement.getAttribute("length");
            this.type = encElement.getAttribute("type");
        }
    },
    Guid : function(guidElement)
    {
        if (guidElement == null) {
            this.isPermaLink = null;
            this.value = null;
        } else {
            this.isPermaLink = guidElement.getAttribute("isPermaLink");
            this.value = guidElement.childNodes[0].nodeValue;
        }
    },
    Source : function(srcElement)
    {
        if (srcElement == null) {
            this.url = null;
            this.value = null;
        } else {
            this.url = srcElement.getAttribute("url");
            this.value = srcElement.childNodes[0].nodeValue;
        }
    }
};
 
/*MyOpenSpace.Details = function() {}
MyOpenSpace.Details.Field = {
    ID:"ID",
    STATUS:"STATUS",
    ORIENTATION:"ORIENTATION",
    HOMETOWN:"HOMETOWN",
    BODY_TYPE:"BODY_TYPE",
    EDUCATION:"EDUCATION",
    RELIGION:"RELIGION",
    SMOKE:"SMOKE",
    DRINK:"DRINK",
    ETHNICITY:"ETHNICITY",
    CHILDREN:"CHILDREN",
    INCOME:"INCOME",
    HERE_FOR:"HERE_FOR"
};
MyOpenSpace.Details.prototype = {
	getField: function(key) { return this[key]; },
	setField_: function(key,val) { this[key] = val; }
};

MyOpenSpace.Interests = function() {}
MyOpenSpace.Interests.Field = {
    ID:"ID",
    GENERAL:"GENERAL",
    MUSIC:"MUSIC",
    MOVIES:"MOVIES",
    TELEVISION:"TELEVISION",
    BOOKS:"BOOKS",
    HEROES:"HEROES"
};
MyOpenSpace.Interests.prototype = {
	getField: function(key) { return this[key]; },
	setField_: function(key,val) { this[key] = val; }
};

MyOpenSpace.Groups = function() {}
MyOpenSpace.Groups.Field = {
};
MyOpenSpace.Groups.prototype = {
	getField: function(key) { return this[key]; },
	setField_: function(key,val) { this[key] = val; }
};*/
