$(document).ready(function() {
	if($('#feed-content').length > 0) {
		Feed.init();
	}
});

var Feed = {
	jsonURI: 'offset={offset}',
	moreBtn: null,
	offset: {},
	init: function() {
		$("#feed-content .more").show();
		$("#feed-content .more a").each(function(index) {
			$(this).click(Feed.showMore);
		});
	},
	showMore: function(e,elemObj) {
		if($(this).parent().hasClass('more')) {
			var el = $(this).parent().parent();
		} else {
			var el = $('#'+elemObj);
		}
		el.find('p.displayNone').before('<div class="preloader"></div>');
		var uri = el.attr('rel');
		var fulluri = uri;		
		if(fulluri[fulluri.length - 1] != '&' && fulluri[fulluri.length - 1] != '?') {
			if(fulluri.indexOf('?') == -1) {
				fulluri += '?';
			} else {
				fulluri += '&';
			}
		}

		if( typeof(Feed.offset[el.attr('rel')]) == 'undefined' ) {
/*			
			if(fulluri.indexOf('searchPageType') != -1) {
				//This needs to be refactored. Should not be done like this.
				var pageType = fulluri.substring(fulluri.indexOf("searchPageType"));
				var pageTypeTempVal = pageType.split("=")[1];
				var pageTypeVal = pageTypeTempVal.split("&")[0];
			}
			if(pageTypeVal == "RBSECTION" || pageTypeVal == "ALL"){
				if($(this).parent().hasClass('more') && $(this).parents('#related-content-all').length>0) {
					Feed.offset[el.attr('rel')] = 11;
				} else {
					Feed.offset[el.attr('rel')] = 1;
				}
			}
			else{
				if($(this).parent().hasClass('more') && $(this).parents('#related-content-all').length>0) {
					Feed.offset[el.attr('rel')] = 11;
				} else {
					Feed.offset[el.attr('rel')] = 1;
				}
			}
*/
			if(fulluri.indexOf('maxRows') != -1) {
				//This needs to be refactored. Should not be done like this.
				var maxRows = fulluri.substring(fulluri.indexOf("maxRows"));
				var maxRowsTempVal = maxRows.split("=")[1];
				var maxRowsVal = maxRowsTempVal.split("&")[0];
			
				if($(this).parent().hasClass('more') && $(this).parents('#related-content-all').length>0) {
					Feed.offset[el.attr('rel')] = parseInt(maxRowsVal)+1;
				} else {
					Feed.offset[el.attr('rel')] = 1;
				}
			} else {
				Feed.offset[el.attr('rel')] = 11;
			}
		}

		fulluri += Feed.jsonURI.replace('{offset}', Feed.offset[el.attr('rel')]);
		$.get(fulluri, null, function(data, status){ Feed.onLoad(uri, data, el); }, 'json');
		return false;
	},
	onLoad: function(uri, data, el) {
		var type = Feed.getType(el.get(0).id);
		Feed.offset[uri] = Feed.offset[uri] + data.length;
		for(var i = 0; i < data.length; i++) {
			if(typeof(data[i]['isLast']) != 'undefined' && data[i]['isLast'] == true) {
				el.find('p.more').hide();
				break;
			}
			
			switch (data[i]['type']) {
				case 'article':
					el.find('p.displayNone').before(Feed.createArticle(data[i]));
				break;
				case 'video':
					el.find('p.displayNone').before(Feed.createVideo(data[i]));
				break;
				case 'photo':
					el.find('p.displayNone').before(Feed.createPhoto(data[i]));
				break;
				case 'event':
					el.find('p.displayNone').before(Feed.createEvent(data[i]));
				break;
			}
			el.find('div.preloader').remove();
			if(Feed.offset[el.attr('rel')] <= 11 && el.find('h4').length<2) {
				el.find('div').eq(0).addClass('first');
			}
		}
	},
	createVideo: function(video) {
  		var out = '<div class="related-video">\
			<h4 class="clearfix" ><a href="'+video.url+'"><span class="contentheading">'+video.title+'</span></a> <span class="date">'+video.date+'</span>';
		if (video.comments){
			out = out.concat('<span class="comments-views">' + video.comments + '</span>');
		}
		out = out.concat('</h4>\
			<a class="photo" href="'+video.url+'"><img alt="" src="'+video.image_url+'"/></a>\
			<div class="description">\
				<p>'+video.snippet+'</p>\
				<a href="'+video.url+'" class="icon-link">'+video.label+'</a>\
			</div>\
		</div>');
		return out;
	},
	createEvent: function(event) {
		return '\
    	<div class="related-event">\
			<h4 class="clearfix" ><a href="'+event.url+'"><span class="contentheading">'+event.title+'</span></a> <span class="date">'+event.date+'</span></h4>\
			<a href="'+event.url+'" class="photo"><img src="'+event.image_url+'" alt="" /></a>\
			<div class="description">\
				<p>'+event.snippet+'</p>\
				<a href="'+event.url+'" class="icon-link">'+event.label+'</a>\
			</div>\
		</div>\
		'
	},
	createPhoto: function(image) {
		var out = '<div class="related-gallery">\
			<h4 class="clearfix"><a href="'+image.url+'"><span class="contentheading">'+image.title+'</span></a> <span class="date">'+image.date+'</span>';
		if (image.comments){
			out = out.concat('<span class="comments-views">' + image.comments + '</span>');
		}
		out = out.concat('</h4>');
		out = out.concat('<ul class="gallery-thumbs">');

		for(var i=0; i < image.featured_images.length; i++){
			if(image.featured_images[i])
				out = out.concat('<li class="big"><a href="'+image.featured_images[i].url+'"><img src="'+image.featured_images[i].image+'" alt="" /></a></li>');
		}
		
		for(var i=0; i < image.thumbnails.length; i++){
			if (image.thumbnails[i])
				out = out.concat('<li><a href="'+image.thumbnails[i].url+'"><img src="'+image.thumbnails[i].image+'" alt="" /></a></li>');
		}
		
		out = out.concat('</ul>\
			<a href="'+image.url+'" class="icon-link">'+image.label+'</a>\
				</div>');
		return out;
  	},
		createArticle: function(article) {
		var out = '<div class="related-article">\
			<h4 class="clearfix" >\
				<a href="'+article.url+'"><span class="contentheading">'+article.title+'</span></a> <span class="date">'+article.date+'</span>';
		if (article.comments){
			out = out.concat('<span class="comments-views">' + article.comments + '</span>');
		}
		out = out.concat('</h4>');
		if (article.image_url && article.image_url != ""){
			out = out.concat('<a class="photo" href="'+article.url+'"><img alt="" src="'+article.image_url+'"/></a>');
		}
				

		if (article.author && article.author != ""){
			out = out.concat('<h5>' + translate('by') + ' ' + article.author + '</h5>');
		}
		out = out.concat('<p>'+article.snippet+'</p>\
			<a href="'+article.url+'" class="icon-link">'+article.label+'</a>\
				</div>');
		return out;
	},
	getType: function(id) {
		return id.replace('related-content-','');
	}
}