/* This class interfaces with the proxies to the MediaSite in order to ease with integrating image galleries into webpages 
author: cls
date: 02212011
*/

/* Main class - MediaSite
  provides methods to access MediaSite web services
*/
function MediaSite(base_url) {

	// some properties
	this.base_url = base_url;
}

setBase_url = function(url) {
	this.base_url = url;
}

MediaSite.prototype.setBase_url = setBase_url;

// takes XML for an image gallery and builds an array of image galleries
buildImageGalleryList = function(data) {
	var galleries = [];
	$(data).find("imageGallery").each(function() {
		var gallery = new ImageGallery();
		var imageGalleryId = $(this).find("imageGalleryId:first").text();
                if ( imageGalleryId == null || imageGalleryId == "" ) {
			return true;
		}
               	var galleryName = $(this).find("imageGalleryName:first").text();
		var createTime = $(this).find("createTimestamp:first").text();

		gallery.id = imageGalleryId;
		gallery.name = galleryName;
		gallery.createTime = createTime;

		// do we need images containing node? just pick up each image
		var images = [];
		$(this).find('image').each(function() { // need to change this at some point - should have images, then image tags
			var image = new Image();
			image.id = $(this).find("imageId:first").text();
			image.keywords = jQuery.trim($(this).find("keywords:first").text());
			image.name = $(this).find("imageTitle:first").text();
			image.onHold = $(this).find("onHold:first").text();
			image.pubDate = $(this).find("publishDate:first").text();
			image.shortCaption = $(this).find("shortCaption:first").text();
			image.status = $(this).find("status:first").text();
			image.caption = $(this).find("caption:first").text();
			image.lowres = $(this).find("imageThumb:first").text();
			image.midres = $(this).find("imageBrowse:first").text();
			image.highres = $(this).find("imageDownload:first").text();
			image.size = $(this).find("imageSize:first").text();
			images.push(image);
		});


		gallery.images = images;

		var relgalleries = [];
		$(this).find('relatedgallery').each(function() {
			var relgal = new ImageGallery();
			relgal.name = $(this).find("name:first").text();
			relgal.id = $(this).find("id:first").text();
			relgalleries.push(relgal);
		});

		gallery.relatedGalleries = relgalleries;

		galleries.push(gallery);

	});

	return galleries;
}

MediaSite.prototype.buildImageGalleryList = buildImageGalleryList;

// TODO: fetch multiple image galleries with one call, both here and in Jersey
// get channels (handler method that takes an array of gallery ids, optional comma-separated list of ids as a string)
MediaSite.prototype.processImageGalleries = function(handler,idList) {
	if ( idList == null ) {
		idList = "";
	}
	// we need to loop here since the api doesn't support fetching multiple - when that's added/figured out we can make a single call
	$.ajaxSetup();
	$.get(this.base_url + "imagegallery/" + idList,{}, function(data) {
//	$.get(this.base_url,{'':idList}, function(data) {
		var galleries = buildImageGalleryList(data);
		handler(galleries);
	});
		
}

buildNewsroomList = function(data,numdocs) {
        var newsrooms = [];
        $(data).find("newsroom").each(function() {
		var newsroom = new Newsroom();
//		var newsroomId = $(this).find("newsroomId:first").text();
                newsroom.name = $(this).find("newsroomName:first").text();
		var documents = [];
		var doccount = 0;
		$(this).find('document').each(function() {
			// first pass - jquery probably has numeric selectors...
			if ( numdocs == null || doccount < numdocs ) {
				var pressRelease = new PressRelease();
				pressRelease.docid = $(this).find("docid:first").text();
				pressRelease.publishDate = $(this).find("publishDate:first").text();
				pressRelease.headline = $(this).find("teaserHead:first").text();
				pressRelease.teaserText = $(this).find("teaserText:first").text();
				pressRelease.teaserImage = $(this).find("teaserImage:first").text();
				documents.push(pressRelease);
			}
			++doccount;
		});
		newsroom.pressReleases = documents;
		newsrooms.push(newsroom);
	});
	return newsrooms;
}

MediaSite.prototype.buildNewsroomList = buildNewsroomList;

MediaSite.prototype.processNewsrooms = function(handler,idList,numdocs) {
	if ( idList == null ) {
		idList = "";
	}
	// we need to loop here since the api doesn't support fetching multiple
	$.ajaxSetup();
	$.get(this.base_url + "newsroom/" + idList,{},function(data) {
		var newsrooms = buildNewsroomList(data,numdocs);
		handler(newsrooms);
	});
}

buildPages = function(data) {
	var pages = [];
	$(data).find("page").each(function() {
		var page = new WebPage();
		page.title = $(this).find("title").text();
		page.url = $(this).find("url").text();
		page.hits = $(this).find("hits").text();
		pages.push(page);
	});
	return pages;
}

MediaSite.prototype.buildPages = buildPages;

MediaSite.prototype.getTopPages = function(handler,keyword,days,start,end,results) {
	// doesn't use normal API - probably should?
	var args = "";
	if ( !results ) {
		var results = 10;
	}
	if ( keyword ) {
		args = args + "&keyword=" + keyword;
	}
	if ( days ) {
		args = args + "&days=" + days;
	}
	if ( start ) {
		args = args + "&startDate=" + start;
	}
	if ( end ) {
		args = args + "&endDate=" + end;
	}
	args = args + "&numresults=" + results;

	$.get("/ajax/service?action=mostPopular" + args,{},function(data) {
		var pages = buildPages(data);
		handler(pages);
	});
}

// used for mostPopular
function WebPage() {
	this.title = "";
	this.url = "";
	this.hits = 0;
}

function Newsroom() {
	this.id = "";
	this.name = "";
	this.parentnr = "";
	this.attachedChannels = [];
	this.pressReleases = [];
}

function PressRelease() {
	this.docid = "";
	this.publishDate = "";
	this.headline = "";
	this.teaserText = "";
	this.teaserImage = "";
}


function ImageGallery() {
	this.id = "";
	this.name = "";
	this.createTime = "";
	this.images = [];
	this.relatedGalleries = [];
}

function Image() {
	this.id = "";
	this.keywords = "";
	this.name =  "";
	this.onHold = "";
	this.pubDate = "";
	this.shortCaption = "";
	this.status = "";
	this.caption = "";
	this.lowres = "";
	this.midres = "";
	this.highres = "";
	this.size = "";
}


