var CHANNEL = "channel";

//Common Vars
var TITLE = "title";
var DESCRIPTION = "description";
var PUBDATE = "pubDate";
var CATEGORY = "category";
var LINK = "link";

//Channel Specific Vars
var LANGUAGE = "language";
var COPYRIGHT = "copyright";
var MANAGINGEDITOR = "managingEditor";
var WEBMASTER = "webMaster";
var LASTBUILDDATE = "lastBuildDate";
var GENERATOR = "generator";
var DOCS = "docs";
var TTL = "ttl";
var RATING = "rating";
var IMAGE = "image";
var ITEM = "item";

//Image Specific Vars
var URL="url";
var WIDTH="width";
var HEIGHT="height";

//Item Specific Vars
var AUTHOR="author";
var COMMENTS="comments";
var ENCLOSURE="enclosure";
var GUID="guid";
var SOURCE="source";

//Enclosure Specific Vars
var LENGTH="length";
var TYPE="type";

function Channel()
{
    this.title = null;
    this.link = null;
    this.description = null;
    this.language = null;
    this.copyright = null;
    this.managingEditor = null;
    this.webMaster = null;
    this.pubDate = null;
    this.lastBuildDate = null;
    this.category = null;
    this.generator = null;
    this.docs = null;
    this.ttl = null;
    this.rating = null;
    this.images = null;
    this.items = null;

    this.populate = populate;
    
    function populate(feedXML)
    {
        var channel = getNthOccurance(feedXML, CHANNEL, 0);
        this.title = getData(channel, TITLE);
        this.link = getData(channel, LINK);
        this.description = getData(channel, DESCRIPTION);
        this.language = getData(channel, LANGUAGE);
        this.copyright = getData(channel, COPYRIGHT);
        this.managingEditor = getData(channel, MANAGINGEDITOR);
        this.webMaster = getData(channel, WEBMASTER);
        this.pubDate = getData(channel, PUBDATE);
        this.lastBuildDate = getData(channel, LASTBUILDDATE);
        this.category = getData(channel, CATEGORY);
        this.generator = getData(channel, GENERATOR);
        this.docs = getData(channel, DOCS);
        this.ttl = getData(channel, TTL);
        this.rating = getData(channel, RATING);
        
        var images = getAllOccurances(channel, IMAGE);
        var imagesLen = images.length;
        var imageArray = new Array();
        for(i=0;i<imagesLen;i++)
        {
            imageArray[i] = new Image();
            imageArray[i].populate(getNthOccurance(channel, IMAGE, i));
        }
        this.images = imageArray;
                
        var items = getAllOccurances(channel, ITEM);        
        var itemsLen = items.length;
        var itemArray = new Array();
        for(i=0;i<itemsLen;i++)
        {
            itemArray[i] = new Item();
            itemArray[i].populate(getNthOccurance(channel, ITEM, i));
        }
        this.items = itemArray;
    }
    
}

function Image()
{
    this.url = null;
    this.title = null;
    this.link = null;
    this.width = null;
    this.height = null;
    this.description = null;
    
    this.populate = populate;
    
    function populate(imageSubTree)
    {
        if(imageSubTree)
        {
            this.url = getData(imageSubTree, URL);
            this.title = getData(imageSubTree, TITLE);
            this.link = getData(imageSubTree, LINK);
            this.width = getData(imageSubTree, WIDTH);
            this.height = getData(imageSubTree, HEIGHT);
            this.description = getData(imageSubTree, DESCRIPTION);
        }
    }
}

function Item()
{
    this.title = null;
    this.link = null;
    this.description = null;
    this.author = null;
    this.category = null;
    this.comments = null;
    this.enclosure = null;
    this.guid = null;
    this.pubDate = null;
    this.source = null;
    
    this.populate = populate;
    
    function populate(itemSubTree)
    {
        if(itemSubTree)
        {
            this.title = getData(itemSubTree, TITLE);
            this.description = getData(itemSubTree, DESCRIPTION);
            this.link = getData(itemSubTree, LINK);
            this.author = getData(itemSubTree, AUTHOR);
            this.category = getData(itemSubTree, CATEGORY);
            this.comments = getData(itemSubTree, COMMENTS);
            var tempEncl = new Enclosure();
            tempEncl.populate(getNthOccurance(itemSubTree, ENCLOSURE, 0));
            this.guid = getData(itemSubTree, GUID);
            this.pubDate = getData(itemSubTree, PUBDATE);
            this.source = getData(itemSubTree, SOURCE);
        }
    }
}

function Enclosure()
{
    this.url = null;
    this.length = null;
    this.type = null;
    
    this.populate = populate;
    
    function populate(enclosureSubTree)
    {
        if(enclosureSubTree)
        {
            this.url = getData(enclosureSubTree, URL);
            this.length = getData(enclosureSubTree, LENGTH);
            this.type = getData(enclosureSubTree, TYPE);
        }
    }
}

function getAllOccurances(xmlText, tagName)
{
    return xmlText.getElementsByTagName(tagName);
}

function getNthOccurance(xmlText, tagName, n)
{
    return getAllOccurances(xmlText, tagName)[n];
}

function getData(xmlText, tagName)
{
    var currTag = getNthOccurance(xmlText, tagName, 0);
    if(currTag) 
    {
        if(currTag.firstChild)
            return currTag.firstChild.nodeValue;
        else
            return null;
    }
    else
    {
        return "No Data";
    }
}

