var revisionview=null;


function PackageMercurialUrl () {
	if (!Packageid) {return;}
	MCLrepos = new Ajax();
	MCLrepos.onreadystatechange = PackageMercurialUrlCompleted;
	MCLrepos.open('GET', nocache(mercurialUrl), true);
	debuglog.debug ("accessing mercurial url: "+ mercurialUrl);
	MCLrepos.send(null);	
		
}

function PackageMercurialUrlCompleted () {
	 
	 if (MCLrepos.readyState == 4) {
	 	var pagetext = MCLrepos.responseText;
		
		var ind = pagetext.indexOf(Packageid +"/");
		pagetext = pagetext.substr(1,ind+Packageid.length);
		debuglog.debug("result of regexp search for url is " + ind +  " Package name is "+Packageid);
		
		if (ind==-1){
			uiManager.showNotification(1000, "Warning", "Mercurial - Package Not Found");
			debuglog.debug ("Error - Package not found in the FCL page");
			return;
		} 
		ind=pagetext.lastIndexOf("href");
		
		mercurialPackageUrl = symbianOrgBaseUrl + pagetext.substr(ind+6,pagetext.length-ind-6) + mercurialrss;
	 	debuglog.debug("mercurial FCL repo URL is:" + mercurialPackageUrl);
		//save settings
		    if (window.widget) {
				widget.setPreferenceForKey(mercurialPackageUrl, "mercurialPackageUrl");
			} else {
				debuglog.debug("mercurial - widget not found");
			}
	 }
}


function MercurialFeedPresenter(rssreader) {
	var myreader = rssreader;
	
	ButtonFeedPresenter.prototype.init.call(this, rssreader);
}

 MercurialFeedPresenter.prototype = new ButtonFeedPresenter(null);

// Handle the button-press
 MercurialFeedPresenter.prototype.feedClicked = function(event){
	var clickedButton = event.source;
	var id = clickedButton.id;
	var myitem = this.items[id]
	revisionpresenter(myitem,this.rssreader);
}

function revisionpresenter(aItem, aParent) {
	revisionview = new ListView(null, createCaption(aItem.title));
	revisionview.previousView = aParent;
	
	var mytext = new ContentPanel(null, null, null, true);
	mytext.setExpanded(true);
	
	revisionview.addControl(mytext);
	 var mybuf = "<br><div class=\"FeedItemLink\">";
	     mybuf += "<a href=\"JavaScript:void(0)\" onclick=\"openURL('" + symbianOrgBaseUrl + aItem.url + "'); return false;\">";
    	 mybuf += "Open in browser..";
         mybuf += "</a>";
         mybuf += "</div><br>";
			   
	var description = "<table style="+bugzillaTableStyle+">";
	description += "<tr><td>Author:" + "</td><td>" + aItem.author + "</td></tr>";
	description += "<tr><td>Date:" + "</td><td>" + aItem.date + "</td></tr>";
	description += "</table>";
	description += "<table style="+bugzillaTableStyle+"><tr><td>";
	description += mybuf;
	description += "<div class=\"FeedItemDescription\">" 
	description += "<b>Description:</b><br>" +  aItem.description ;
	description += "</div>";
	description += "</td></tr></table>";
	
	mytext.setContent(description);
	revisionview.show();
	 
	
}

function parseRepoFeed (broker, responseStatus, xmlDoc) {
	
	if (responseStatus == 200 && xmlDoc != null) {
        // node ref for iterating
        var node;

		// for compatibility with rss
		var lastModified = new Date();
		
        // init result items array
        var items = [];

		var itemElements = xmlDoc.getElementsByTagName("item");
		
		for (var i = 0; i < itemElements.length; i++) {
            // iterate through child nodes of this item and gather
            // all the data we need for a feed item
            var title = null;
            var date = null;
            var description = null;
            var url = null;
            var author = null;
			var changeset = null;
            node = itemElements[i].firstChild;
            while (node != null) {
                if (node.nodeType == Node.ELEMENT_NODE) {
                    if (node.nodeName == "title") {
                        // item title
                        title = getTextOfNode(node);
						if ( title.length > 48) {
							title = title.substring(0,45) + "...";
						}
                    } else if (node.nodeName == "pubDate" ) {
                        // item publishing date
                        date = getTextOfNode(node);
                    } else if (node.nodeName == "description" && !this.ignoreContent ) {
                        // item description
                        description = getTextOfNode(node);
                    } else if (node.nodeName == "guid") {
                        // link URL
                        url = getTextOfNode(node);
						// extract bug revision
						ind=url.indexOf("rev/");
						rev = url.substr(ind,url.length-ind);
						
                    } else if (node.nodeName == "author" ) {
						author = getTextOfNode(node);
					}
                }
                node = node.nextSibling;
            }
            items.push({ title: title, date: date, description: description, url: url, author: author, rev: rev });
		}

        // update was completed successfully
        return { status: "ok", lastModified: lastModified, items: items };
    } else {
        // update failed
        return { status: "error" };
    }
}


