/* Writes an e-mail link to the document by javascript to fool spam robots */
function spamProtectEmail(user, domain, text) {
	email = user + '@' + domain;
	if (text == undefined) document.write("<a href=\"mailto:" + email + "\">" + email + "<\/a>");
	else document.write("<a href=\"mailto:" + email + "\">" + text + "<\/a>");
}

/* Validates an e-mail address */
function validateEmail(address) {
	return address.toString().match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i);
}

// To be done when the page is loaded
$(function () {
	// Remove bottom margins from the empty advertisements
	var emptyAds = new Array(
		'http://adimages.sanomawsoy.fi/01/_taustat/1x1_140x350.jpg',
		'http://adimages.sanomawsoy.fi/blank.gif'
	);
	jQuery.each(emptyAds, function() {
		for (i = 3; i <= 9; i++) {
			$('#ad-space-' + i + ' img[src="' + this + '"]').css('margin-bottom','0px');
		}
	});

	//Blogs change hover
	$('#main-navigation .active').removeClass("active");

	$('#node_id_345').addClass("active");
	
	// Archive activate selected
	var url = document.location.href;

	$('#archive-dropdown option').each(function(){
		if(this.value == url) {
			$('#archive-dropdown').val(url);
		}
	});
	
	// Fetch sharing parameters
	var shareUrl = location.href;
	var shareTitle = document.title;
	var shareContent = '';
	// Test if summary exists to prevent null content
	if ($('#article .summary').length > 0) {
		shareContent = $('#article .summary').html();
		// A safe URL length is about 2000 bytes...
		if (shareContent.length > 1000) {
			shareContent = shareContent.substring(0, 997) + '...';
		}
	}
	
	/* Initialize sharing on Facebook (see: http://www.facebook.com/share_partners.php) */
	$('#social-toolbar .facebook').attr('href', 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(shareUrl) + '&t=' + encodeURIComponent(shareTitle));

	/* Initialize sharing on MySpace (see: http://x.myspace.com/download/posttomyspacedeveloperdocumentation001.pdf)
	MySpace sharing locations:
	1 = Blog
	2 = Bulletin
	3 = About Me
	4 = Who I'd Like to Meet
	5 = Interests
	6 = Music
	7 = Movies
	8 = Television
	9 = Books
	10 = Heroes
	*/
	var shareLocation = 2;
	$('#social-toolbar .mySpace').attr('href', 'http://www.myspace.com/index.cfm?fuseaction=postto&' + 't=' + encodeURIComponent(shareTitle) + '&c=' + encodeURIComponent(shareContent) + '&u=' + encodeURIComponent(shareUrl) + '&l=' + shareLocation);

	/* Initialize sharing on Twitter */
	$('#social-toolbar .twitter').attr('href', 'http://twitter.com/home?status=' + encodeURIComponent(shareUrl));

	/* Initialize sharing on Delicious */
	$('#social-toolbar .delicious').attr('href', 'http://www.delicious.com/post?url=' + encodeURIComponent(shareUrl) + '&title=' + encodeURIComponent(shareTitle));
});
