/**************************************************************************
 * VPRO - In Europa Weblog - Recent added News, Episodes, Geopoints
 * 
 * @author frenkie
 *************************************************************************/

/**************************************************************************
 * Add Recent items to the weblog footer onload
 * 
 * @author frenkie
 *************************************************************************/
 
var YEAR = function(){
	var ret = "";
	/* look for a category tag containing a year */
	$($$('#breadcrumbs a[rel~=category]')).each(function(current){
		var ch = current.innerHTML;
		if(/^\d{4}$/.test(ch)){		
			ret = "&year="+ ch;
			throw $break;
		}
	});	
	return ret;
}

var CONTAINER_GEO = "div.ieuatlas";
var CONTAINER_NEWS = "div.ieuweblog";
var CONTAINER_EPISODES = "div.ieutelevisie";
var RECENT_GEO;
var RECENT_NEWS;
var RECENT_EPISODES;

/** CHANGE FOR LIVE ENVIRONMENT */
var LINK_HOST = "http://www.ineuropa.nl";
var LINK_IMAGEROOT = LINK_HOST +"/themasites/images/ineuropa";

var LINK_GEO = LINK_HOST + "/ajax/ineuropa/recentatlas.jsp?&eventlocations=4&callback=updateRecentAtlas"+ YEAR();
var LINK_NEWS = LINK_HOST + "/ajax/ineuropa/recentweblog.jsp?&weblogposts=4&callback=updateRecentWeblog"+ YEAR();
var LINK_EPISODES = LINK_HOST + "/ajax/ineuropa/recenttelevisie.jsp?callback=updateRecentTelevisie"+ YEAR();

Event.observe(window, 'load', function(){
		
		RECENT_GEO = new vpro.ineuropa.Recent(LINK_IMAGEROOT, vpro.ineuropa.Recent_TYPE_GEO, {			
			list: '<ul class="tabs_top">'+
				'<li class="tab selected"><h3><a name="ieuatlas">{title}</a></h3></li>'+
				'</ul><div>{description}<ul class="links">{items}</ul></div>',
			item: '<li><a href="{link}">{image}<h3>{title}</h3><p>{content}</p></a></li>'
		});
		RECENT_GEO.fetch(LINK_GEO);


		RECENT_NEWS = new vpro.ineuropa.Recent(LINK_IMAGEROOT, vpro.ineuropa.Recent_TYPE_NEWS, {			
			list: '<ul class="tabs_top">'+
				'<li class="tab selected"><h3><a name="ieuweblog">{title}</a></h3></li>'+
				'</ul><div>{description}<ul class="links">{items}</ul></div>',
			item: '<li><a href="{link}">{image}<h3>{title}</h3><p>{content}</p></a></li>'
		});
		RECENT_NEWS.fetch(LINK_NEWS);


		RECENT_EPISODES = new vpro.ineuropa.Recent(LINK_IMAGEROOT, vpro.ineuropa.Recent_TYPE_EPISODE, {			
			list: '<ul class="tabs_top">'+
				'<li class="tab selected"><h3><a name="ieutelevisie">{title}</a></h3></li>'+
				'</ul><div>{description}<ul class="links">{items}</ul></div>',
			item: '<li><a href="{link}">{image}<h3>{title}</h3><p>{content}</p></a></li>'
		});
		RECENT_EPISODES.fetch(LINK_EPISODES);	
});


function updateRecentWeblog(data){
	if(typeof data != "undefined"){   	
		RECENT_NEWS.draw($$(CONTAINER_NEWS).first(), data);
	}
	var t = setTimeout(function(){		
		RECENT_NEWS.fetch(LINK_NEWS);	
	}, 60 * 1000 * 5);
}

function updateRecentAtlas(data){
		
	if(typeof data != "undefined"){   	
		RECENT_GEO.draw($$(CONTAINER_GEO).first(), data);
	}
	var t = setTimeout(function(){		
		RECENT_GEO.fetch(LINK_GEO);		
	}, 60 * 1000 * 4);
}

function updateRecentTelevisie(data){
	if(typeof data != "undefined"){
		RECENT_EPISODES.draw($$(CONTAINER_EPISODES).first(), data);
	}
	var t = setTimeout(function(){		
		RECENT_EPISODES.fetch(LINK_EPISODES);	
	}, 60 * 1000 * 5);
}        

 

/**************************************************************************
 * vpro package tailored to In Europa.
 *
 *************************************************************************/
if(typeof 	vpro == "undefined") {
	var 	vpro = new Object();
			vpro.ineuropa = new Object();
			
}else if(typeof vpro.ineuropa == "undefined"){
	vpro.ineuropa = new Object();
}

/**************************************************************************
 * For a list of recent In Europa objects.
 * @param type {Number} TYPE_ constant
 * @param templates {Object} Hash of templates:
 * 		@option list {String} HTML templatestring of the list,
 * 								should contain '{items}', which will be
 * 								replaced by all items
 * 		@option item {String} HTML templatestring of the item
 *************************************************************************/
vpro.ineuropa.Recent = function(contextPath, type, templates){
	
	this.type = type;
	this.templates = templates;
	this.thumb = "<img class='marker' src='"+ contextPath  +"/ieu_marker_right.png' alt='' />";
};
/** I know, these are lame, but for now, I'm tired :) */
vpro.ineuropa.Recent_TYPE_NEWS = 0;
vpro.ineuropa.Recent_TYPE_EPISODE = 1;
vpro.ineuropa.Recent_TYPE_GEO = 2;

vpro.ineuropa.Recent.prototype = {	

	TYPE_NEWS : vpro.ineuropa.Recent_TYPE_NEWS,
	TYPE_EPISODE : vpro.ineuropa.Recent_TYPE_EPISODE,
	TYPE_GEO : vpro.ineuropa.Recent_TYPE_GEO,
	labels : {0:"Nieuws", 1:"Televisie", 2:"Atlas"},

	/**
	 * Draw the recent data in the given container.
	 * @param {Object} container Prototype list container element
	 * @param {Object} data Recent data in JSON format:
	 * 				{
	 * 					title: 'Recent object',
	 * 					description: 'These are Recent Objects',
	 * 					items:	[
	 * 						{
	 * 							title: 'Item 1',
	 * 							content: 'Item 1 content',
	 * 							link: 'http://hyper1.link'
	 * 							[image: 'image url']
	 * 						}
	 * 					]
	 * 				}
	 */
	draw : function(container, data){
		
		/* list */
		var list = (typeof this.templates.list == "string")? this.templates.list : "";
			list = list.replace(/\{title\}/ig, (typeof data.title == "string")? data.title : '');
			list = list.replace(/\{description\}/ig, (typeof data.description == "string")? data.description : '');
		
		/* items */
		var ref = this;
		var items = "";
		if(typeof data.items != "undefined" && data.items.constructor == Array){
				$(data.items).each(function(current){
				
					var item = (typeof ref.templates.item == "string")? ref.templates.item : ""; 
						item = item.replace(/\{title\}/ig, 	(typeof current.title == "string")? current.title : '');
						item = item.replace(/\{content\}/ig,(typeof current.content == "string")? current.content : '');
						item = item.replace(/\{link\}/ig, 	(typeof current.link == "string")? (ref.hasProtocol(current.link))? current.link : LINK_HOST+current.link : '');

					
					/** IMAGE */	
					var img = "<img class='illustration' src='{image}' width='50' alt='' />";
					if(ref.type == ref.TYPE_NEWS){						
						
						
						if(typeof current.contentOrg == "string" && typeof current.image == "undefined"){
							/** look for an image in the content */
							if (/<img[^<>]+>/.test(current.contentOrg)) {																
	
								var imgFound = current.contentOrg.match(/<img[^<>]+>/)[0];
								var imgSrc = imgFound.match(/src=\"([^\"]+)\"/);
								if(typeof imgSrc != "undefined"){
								}
								item = item.replace(/\{image\}/ig, 	img.replace(/\{image\}/ig, imgSrc[1]));
							}else{
								item = item.replace(/\{image\}/ig, 	ref.thumb);	
							}		
						}else{
							item = item.replace(/\{image\}/ig, 	(typeof current.image == "string")? img.replace(/\{image\}/ig, current.image) : ref.thumb);	
						}		
					}else{
						/** image is linked from the Editors, add an image profile to exclude overhead in size */
						item = item.replace(/\{image\}/ig, 	(typeof current.image == "string")? img.replace(/\{image\}/ig, current.image) : ref.thumb);
					}
					
					var hpReg = new RegExp(escape('{') +"link"+ escape('}'), "ig");
						item = item.replace(hpReg, (typeof current.link == "string")? (ref.hasProtocol(current.link))? current.link : LINK_HOST+current.link : '');
						
					items	+= item;
				});
		}
		list = list.replace(/\{items\}/ig, (typeof items != "undefined")? items : '');
		
		/** container can be an array */
		if(container.constructor == Array){
			$(container).each(function(){
				this.update(list);
			});
		}else{
			container.update(list);
		}
	},
	
	/**********************************************************************
	 * just a fast "Prototype's lack of a jQuery getScript" equivalent
	 *********************************************************************/
	fetch : function(jsonpUrl){   	
		var headTag = document.getElementsByTagName('head')[0];
		var script = document.createElement("script");
		script.src = jsonpUrl;
		script.language = "JavaScript";
		headTag.appendChild(script);
	},
	
	hasProtocol : function(linkString){
		return(/^https?:\/\//ig.test(linkString));
	}
};
