// Utilities that add functionality to news articles. 
// Scans the page for all class=article and ads functionality to the hidden similarStories and related_btn

// Initialize article events
function init_articles() {
	$('div.article').unbind();
	$('div.article').mouseover(function() {
		this.className = 'article_hover';
		$('div.share', this).css('visibility', 'visible');
	});
	$('div.article').mouseout(function() {
		this.className = 'article';
		$('div.share', this).css('visibility', 'hidden');
	});
}

// Scan News divs and add functionality for similar articles
function scan_news() {
	init_articles();
	$(".related_stories_btn").css("cursor", "pointer");
	$(".related_stories_btn").unbind();
	$(".related_stories_btn").mouseover(function() {
		$(this).addClass('related_stories_btn_hover');
	});
	$(".related_stories_btn").mouseout(function(e) {
		$(this).removeClass('related_stories_btn_hover');
		// e.stopPropagation();
	});
	$(".related_stories_btn").click(function() {
		var article_ct = this.parentNode.parentNode;
		if ($.browser.msie)
			$(" > div.relatedStories", article_ct).slideToggle("fast");
		else 
			$(" > div.relatedStories", article_ct).slideToggle("fast");
		if (this.style.fontWeight == 'bold') {
			$(this).css("font-weight", "normal");
			$("img", this).attr({src: "images/button_down_arrow.gif"});
		} else {
			$(this).css("font-weight", "bold");
			$("img", this).attr({src: "images/button_up_arrow.gif"});
		}
	});
	$('span.email_btn').css("cursor", "pointer");
	$('span.email_btn').unbind();
	$('span.email_btn').click(function() {
		var article = $(this).parents('div.article_hover');
		var content = {
			title: 'Send this article',
			subject: $('a.news_title', article).text(),
			body: $('div.content', article)[0].innerHTML
		};
		show_email_form(content);
	});
}

function render_news(news) {
	var html = '';
	var imagetag = '';
	if (news.image) {
		if (!news.image.url.match(/www\.apogevmatini\.gr/))
			imagetag = '<a class="lightbox" href="'+news.image.url+'"><img src="'+news.image.url+'" width=80 class="image"></a>'+
				'<div align=center class=details><a href="'+news.image.originalContextUrl+'" title="'+news.image.titleNoFormatting+'">'+news.image.publisher+'</a></div>';
	}
	var relatedStoriesTag = '';
	if (news.relatedStories) {
		$.each(news.relatedStories, function() {
			relatedStoriesTag += '<div class=related>&raquo; <a href="'+this.unescapedUrl+'">'+this.titleNoFormatting+'</a> - <span class=source>'+this.publisher+'</span></div>';
		});
		if (relatedStoriesTag) {
			news.relatedStoriesTag = '<div class=details>'+relatedStoriesTag+'</div>'
			news.relatedTotal = news.relatedStories.length;
		}
	}
	news.relatedCap = '';
	if (new Date(news.publishedDate).format('j/n') == new Date().format('j/n'))
		news.publishedDate = new Date(news.publishedDate).format('H:i');
	else 
		news.publishedDate = new Date(news.publishedDate).format('j/n H:i');
	if (news.relatedTotal > 1) {
		news.relatedCap = 'Show '+news.relatedTotal+' related stories';
	} else {
		news.relatedCap = 'Show '+news.relatedTotal+' related story';
	}
	if (robby.lang == 'greek') {
		if (news.relatedTotal > 1) {
			news.relatedCap = news.relatedTotal+' Σχετικά άρθρα'
		} else {
			news.relatedCap = news.relatedTotal+' Σχετικό άρθρο'
		}
	}

	news.content = news.content.replace(/<.+?>/g, '');
	html = '<div class=article><div class=content>'+
		'<table><tr><td valign=top>'+
			imagetag+
		'</td><td valign=top>'+
			'<a class=news_title href="'+news.unescapedUrl+'">'+news.titleNoFormatting+'</a> '+
			'<a href="'+news.unescapedUrl+'" target=_blank><img src="images/newwind.gif" border=0 width=12 height=10></a>'+
			'<div class=descr>'+news.content+'</div>'+
			'<div class=details><span class=source><b>'+news.publisher+'</b></span> - <span class=url>'+news.publishedDate+'</span></div>'+
		'</td></tr></table>'+
		'</div>'; // content
		if (news.relatedStories) {
			html += '<div align=left><span class="related_stories_btn"><img src="images/button_down_arrow.gif" border=0 align=top width=12> '+news.relatedCap+'</span></div>'+
				'<div class="relatedStories" style="display: none; margin-top:4px"><div class=details>'+relatedStoriesTag+'</div></div>';
		}
		html += '</div>'; // article
	return html;
}
