appSV = Object.extend (new HPEItemApp, {

	opsSelect:"OtherOpsSource",
	pageFirst:0,
	pageItemsPerPage:0,
	pageItemCount:0,
	pageSort:"bydate",
	pageFilterPop:"0",
	pageFilterDate:"0",
	pageFilterSource:-1,
	pageCurrentSource:-1,

	InitApp : function() {
		var html = "<form id='AppSourceTools' style='display:none' class='nomargin'><table width='100%' height='27' border='0' cellspacing='0' cellpadding='0'><tr>\
			<td id='AppSourceToolSubscribe'>Add to page <select name=\"subscribe_page\" id=\"subscribe_page\" onChange=\"this.nextSibling.style.display=(this.value==-1)?\'\':\'none\'\">'";
			for (var i=0; i<pageList.length; i++)
				html += '<option value="' + pageList[i].index + '">' + pageList[i].title + '</option>';
			html += "<option value='-1'>[New page]</option></select><span style='display:none'>\
			Name: <input type='text' id='subscribe_page_name' name='subscribe_page_name' value='Page " +
			(pageList.length+1) + "' size='10'> </span>\
			<input type='button' value='Subscribe' onClick='app.subscribeToSource()'></td>\
			<td align='right' width=100%><input type='button' value='Reload' onClick='app.Reload()' /></td>\
			</tr></table></form>";

		var elt = $("tabbarmiddle");
		if(!elt) { hpe.error("Failed to find tabbarmiddle"); return }
		new Insertion.Bottom(elt, html);

		this.hideItemTools = 0;
		this.hideItemMetaData = 0;

		this.body = "<div id='infocol-l' style='padding:5px'>\
			<div class='box1' style='margin-top:5px'>\
				<div class='hdrbox1'>Source Details</div>\
				<div style='background-color:white; padding:2px 0 2px 0; font-size:100%'>\
				<div class='cpb'><a onClick='app.GenerateRSS()'><img src='/HPE/Images/feed-icon16x16.png' width=14 height=14 align='left' hspace=5 /> Generate RSS</a>\
					<div class='cpbdesc'>Get an RSS feed of current news, updated every hour</div></div>\
				</div>\
				<div class='cntbox1' id='PowerAppItemOptions'>\
					<form><table>\
					<tr><td><input type='checkbox' name='HPEToggleShortDesc' id='HPEToggleShortDesc' onClick='app.ToggleShortDesc()' /></td>\
						<td>Show short item description</td></tr>\
					</table></form>\
					<div style='padding:5px;font-size:90%'>You are using the <b>advanced</b> view, switch to <a href='#' onClick='app.SwitchToHTML()'>plain&nbsp;html</a> view</div>\
					</div>\
				<div class='cntbox1' id='ApplicationBackButton' style='display:none;text-align:center'></div>\
			</div><br />";
		this.body += $("GlobalNavigation").innerHTML;
		this.body += "</div><div id='content'><div id='contentbreakprotect'><div id='SRCMainContent' style='padding-top:5px'><p>Initializing...</p></div></div></div>";
	},

	StartApp : function(id) {
		var elt = $("AppSourceTools");
		if(elt) Element.show(elt);
		elt = $("MainBody"); if(elt) elt.innerHTML = this.body;

		this.pageCurrentSource = id;
		this.SetItemToolState();
		this.ResumeApp();
	},

	SuspendApp : function () {
		var elt = $("AppSourceTools");
		elt.style.display = "none";
	},

	ResumeApp : function () {
		var elt = $("AppSourceTools"); if(elt) elt.show();
		elt = $("PowerAppItemOptions"); if(elt) elt.show();
		elt = $("MainBody"); if(elt) elt.innerHTML = this.body;

		var id = this.pageCurrentSource;
		this.pageCurrentSource = -1;
		this.SetItemToolState();
		this.ShowSource(id);
		hpe.goToTop();
	},

	SwitchToHTML : function() {
		document.location.href = siteHome + "sources/info/" + pageCurrentSource + "/";
	},

	ShowBackMessage : function() {
		var elt = $("ApplicationBackButton");
		if(!elt) return;
		elt.innerHTML = "<a class=\"OB\" href=\"#\" onClick=\"HPEBackApp();return false;\">[back to source details]</a>";
		Element.show(elt);
	},

	SwitchToSource : function (id) {
		this.pageCurrentSource = id;
		HPEResumeApp(this);
	},

	ShowSource : function (id) {
		if(id == -1)
			id = this.pageCurrentSource;
		var url = siteHome + "ajaxAPI.php?api_call=Source::details&api_format=JSON&id=" + id + "&";
		if(id == this.pageCurrentSource && id != -1) {
			url += "sort=" + this.pageSort + "&";
			url += "filter_pop=" + this.pageFilterPop + "&";
			url += "filter_date=" + this.pageFilterDate + "&";
			if(this.pageFirst)
				url += "first=" + this.pageFirst + "&";
		} else {
			this.pageSort="date";
			this.pageFilterPop=0;
			this.pageFilterDate=0;
			this.pageFirst=0;
		}
		url += "seed=" + pageLoadSeed;

		if(id != -1)
			this.pageCurrentSource = id;
		var myAjax = new Ajax.Request(url, { method:'get', onComplete:this.ShowSourceProcess.bind(this) });
		var elt = $("SRCMainContent");
		if(elt)
			elt.innerHTML = "<p>Loading items...</p>";
		elt = $("SourceNavMenuList");
		if(elt)
			elt.innerHTML = "<p>Loading source details...</p>";
	},

	ShowSourceProcess : function (request) {
		var res = hpe.ProcessItemResponse(request);
		if(!res || res.response_code != 200) {
			$("SubscriptionList").style.display = "none";
			$("SRCMainContent").innerHTML = "<p>Failed to process data</p>"; 
			return;
		}

		this.pageFilterPop = res.pageFilterPop;
		this.pageFilterDate = res.pageFilterDate;
		this.pageFilterSource = res.pageFilterSource;
		this.pageFirst = res.pageFirst;
		this.pageItemsPerPage = res.pageItemsPerPage;
		this.pageItemCount = res.pageItemCount;

		var html = "<div class='PowerAppItemListTitle'>Items for source " + srcInfo[itemInfo[0].sourcekey].title;
		for(var sourcekey in srcInfo) if(typeof srcInfo[sourcekey] != 'function') {
			if(srcInfo[sourcekey].isxml && srcInfo[sourcekey].source)
				html += "<a href='" + srcInfo[sourcekey].source + "' style='padding:5px;float:right'><img src='/HPE/Images/feed-icon16x16.png' /></a>";
			html += "<div style='font-weight:normal'>";
			if(srcInfo[sourcekey].desc != "")
				html += ("<div><em>" + srcInfo[sourcekey].description + "</em></div>");
			if(srcInfo[sourcekey].category.length) {
				html += "<div style=\"padding:5px 0px 5px 0px\">In categorie(s):";
				for(var i=0; i<srcInfo[sourcekey].category.length; i++) if(catInfo[srcInfo[sourcekey].category[i]]) {
					html += "<div style='padding-top:3px'>&nbsp;&bull; ";
					html += Try.these(
				      function() {if(appCB) return "<a href=\"javascript:DisplayCatItems(" + srcInfo[sourcekey].category[i] + ", '" + srcInfo[sourcekey].language + "')\">"},
				      function() {if(appSP) return "<a href=\"javascript:DisplayCatSources(" + srcInfo[sourcekey].category[i] + ")\">"},
				      function() {return "< href=\"/pages/power/?cat=" + srcInfo[sourcekey].category[i] + "&lang=" + srcInfo[sourcekey].language + "\">"} );
					var catname = catInfo[srcInfo[sourcekey].category[i]].title;
					var id = catInfo[srcInfo[sourcekey].category[i]].parentid;
					while(id) {
						catname = catInfo[id].title + " / " + catname;
						id = catInfo[id].parentid;
					}
					html += catname + "</a></div>\n";
				}
				html += "</div>";
			}
			html += "<div style='padding:3px;text-align:right'>";
			if(srcInfo[sourcekey].lastupdStr != "")
				html += ("Last Update: " + srcInfo[sourcekey].lastupdStr + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
			if(srcInfo[sourcekey].lastmodStr != "")
				html += ("Last Change: " + srcInfo[sourcekey].lastmodStr + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
			html += "</div></div>";
			if(srcInfo[sourcekey].id) $("AppSourceToolSubscribe").show(); else $("AppSourceToolSubscribe").hide();
			break;
		}
		html += "</div>"; 
		var n = itemInfo.length;
		if(n == 0) {
			html += "<p>No items found matching filters</p><p><form>";
			html += "<input type='button' onClick='app.ShowSource(-1)' value='Show All Items for This Source' /> ";
			html += "</form></p>";
		} else {
			var q = this.pageFirst + this.pageItemsPerPage;
			if(q > this.pageItemCount)
				q = this.pageItemCount;
			var navpages = "<div class='CPBITEMTOOLS'><ul><li class='pl'>Showing items " + (this.pageFirst + 1) + " to " + q + " of " + this.pageItemCount;
			if(this.pageItemCount > this.pageItemsPerPage) {
				navpages += "<span style='margin-left:20px'>";
				var minItem = this.pageFirst - this.pageItemsPerPage * 5;
				if(minItem < 0)
					minItem = 0;
				var maxItem = this.pageFirst + this.pageItemsPerPage * 5;
				if(maxItem > this.pageItemCount)
					maxItem = this.pageItemCount;
				for(i=minItem; i<maxItem; i+=this.pageItemsPerPage) {
					if(i==minItem && minItem > 0)
						navpages += "<span id=\"pagination\"><a onclick='app.ShowNextItems(0)'>&lt;&lt;</a></span>&nbsp;";
					if(i == this.pageFirst)
						navpages += "<span id=\"pagination_a\">" + (i/this.pageItemsPerPage+1) + "</span>";
					else
						navpages += "<span id=\"pagination\"><a onclick='app.ShowNextItems(" + i + ")'>" + (i/this.pageItemsPerPage+1) + "</a></span>";
				}
				if(i>=maxItem && maxItem < this.pageItemCount)
					navpages += "&nbsp;<span id=\"pagination\"><a onclick='app.ShowNextItems(" + (this.pageItemCount - this.pageItemCount % this.pageItemsPerPage) + ")'>&gt;&gt;</a></span>";
				navpages += "</span>";
			}
			navpages += "</li>";
			navpages += "<li>Select: <a onClick='app.CheckAllItems()'>all</A> | <a onClick='app.UnCheckAllItems()'>none</a></li>";
			navpages += "<li>With selected: <a onClick=\"hpe.PostItems('ma', 0)\">mail</A> | <a onClick=\"hpe.PostItems('bl', 0)\">blog</a>";
			navpages += "</ul><br clear='both'></div>";
			html += navpages + hpe.ShowItemList() + navpages + "<br />";
		}

		$("SRCMainContent").innerHTML = html;

		hpe.MarkCurrentItem(false);
		savedElement = "";
		savedContent = "";
	},

	GenerateRSS : function() {
		if(isPremiumExport) {
			if(!window.open(siteHome + "sources/rss/" + this.pageCurrentSource + "/"))
				alert("A popup blocker seems to have prevented the opening of a new window");
		} else hpe.requirePremium("Custom RSS Feeds", "Export");
	},

	ShowNextItems : function (itemId) {
		this.pageFirst = itemId;
		this.ShowSource(this.pageCurrentSource);
	},

	Reload : function () {
		this.pageFirst = 0;
		this.ShowSource(this.pageCurrentSource);
	},

	subscribeToSource : function() {
		var url = siteHome + "ajaxAPI.php?api_call=Page::subscribe&api_format=JSON";
		var page = $F('subscribe_page');
		var post = "siteid=" + this.pageCurrentSource + "&page=" + page;
		if (page == -1) post += "&name=" + $F('subscribe_page_name');
		var myAjax = new Ajax.Request( url, {method: 'post', postBody: post, onSuccess: this.subscribeToSourceProcess.bind(this) } );
	},

	subscribeToSourceProcess : function(Request) {
		try { var res = eval('('+Request.responseText+')'); }
		catch(e) { return hpe.error(e); }
		if (res.response_code>=300)
			return false;
		if (res.page_id && res.page_id>0)
			// Add new page to custom pages list
			pageList[res.page_id] = {index:res.page_id, title:res.page_name};
		$("AppSourceToolSubscribe").hide();
	}

});

function HPESVShowSourceProcess()
{
	app.ShowSourceProcess();
}

