/**
* Assign the view handler
*/

viewHandler = Blog;

/**
* Creates a new object with methods used by the Blog page
*
* @author				Matt Gifford
* @copyright			2008 Timeshifting Interactive Limited
*/
function Blog()
	{
	// Step 1. Define Properties

	var _instance = this;

	this.shareUrl = '';
	this.shareTitle = '';


	// Step 2. Define Public Methods

	/**
	* Sets up the initial page state and event handlers
	*/
	this.init = function()
		{
		// Call generic page init method
		this.base.init.call(this);
		}


	/**
	* Displays the enlargement share
	*
	* @param		obj		The anchor clicked on
	*/
	this.shareOpen = function(obj)
		{
		// Save the url and title
		if (obj.getElementsByTagName('h2')[0].getElementsByTagName('a').length)
			{
			this.shareUrl = obj.getElementsByTagName('h2')[0].getElementsByTagName('a')[0].href;
			this.shareTitle = 'TRU Blog: ' + obj.getElementsByTagName('h2')[0].getElementsByTagName('a')[0].innerHTML.replace(/<([^>]+)>/ig, '');
			}
		else
			{
			this.shareUrl = window.location.href;
			this.shareTitle = 'TRU Blog: ' + obj.getElementsByTagName('h2')[0].innerHTML.replace(/<([^>]+)>/ig, '');
			}

		// Display the share dialog
		this.shareUpdateOverlay();
		document.getElementById('share').className = '';
		document.getElementById('shareBackground').className = '';
		document.getElementById('shareDialog').className = '';

		// Center the dialog
		this.centerDiv( document.getElementById('shareDialog') );
		}


	/**
	* Closes the enlargement share
	*/
	this.shareClose = function()
		{
		// Reset the email form
		document.getElementById('formShareEmail').getElementsByTagName('label')[0].className = 'hidden';
		document.getElementById('formShareEmail').getElementsByTagName('label')[1].className = 'hidden';
		document.getElementById('formShareEmail').getElementsByTagName('input')[0].value = '';
		document.getElementById('formShareEmail').getElementsByTagName('fieldset')[0].className = 'hidden';
		document.getElementById('formShareEmail').getElementsByTagName('fieldset')[1].className = '';

		// Hide the share
		document.getElementById('share').className = 'invisible';
		document.getElementById('shareBackground').className = 'invisible';
		document.getElementById('shareDialog').className = 'invisible';
		}


	/**
	* Shares the current page on facebook
	*
	* @param		obj		The anchor clicked on
	*/
	this.shareFacebook = function(obj)
		{
		// Save the url and title
		if (obj.getElementsByTagName('h2')[0].getElementsByTagName('a').length)
			{
			this.shareUrl = obj.getElementsByTagName('h2')[0].getElementsByTagName('a')[0].href;
			this.shareTitle = 'TRU Blog: ' + obj.getElementsByTagName('h2')[0].getElementsByTagName('a')[0].innerHTML.replace(/<([^>]+)>/ig, '');
			}
		else
			{
			this.shareUrl = window.location.href;
			this.shareTitle = 'TRU Blog: ' + obj.getElementsByTagName('h2')[0].innerHTML.replace(/<([^>]+)>/ig, '');
			}

		if (confirm('Would you like to post this item to your Facebook account?') == true)
			{
			var url = 'http://www.facebook.com/sharer.php?u=%url%&t=%title%';
			url = url.replace('%url%', encodeURIComponent(this.shareUrl));
			url = url.replace('%title%', encodeURIComponent(this.shareTitle));

			// Throw the url
			window.open(url, '_blank');
			}
		}


	/**
	* Shares the current page via social networking
	*
	* @param		network		The social media network
	*/
	this.shareSocial = function(network)
		{
		// Select the correct url to throw
		switch (network)
			{
			case 'bebo':
				var url = 'http://www.bebo.com/c/share?Url=%url%&t=%title%';
				break;

			case 'delicious':
				var url = 'http://del.icio.us/post?url=%url%&title=%title%';
				break;

			case 'digg':
				var url = 'http://digg.com/submit?phase=2&url=%url%&title=%title%&bodytext=%desc%';
				break;

			case 'facebook':
				var url = 'http://www.facebook.com/sharer.php?u=%url%&t=%title%';
				break;

			case 'google':
				var url = 'http://www.google.com/bookmarks/mark?op=edit&bkmk=%url%&title=%title%';
				break;

			case 'myspace':
				var url = 'http://www.myspace.com/Modules/PostTo/Pages/?u=%url%&t=%title%';
				break;

			case 'technorati':
				var url = 'http://www.technorati.com/faves?add=%url%';
				break;

			case 'windowslive':
				var url = 'https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url=%url%&title=%title%&top=1';
				break;

			case 'yahoomyweb':
				var url = 'http://myweb2.search.yahoo.com/myresults/bookmarklet?u=%url%&t=%title%';
				break;

			default:
				this.shareClose();
				return;
			}

		// Configure the url
		url = url.replace('%url%', encodeURIComponent(this.shareUrl));
		url = url.replace('%title%', encodeURIComponent(this.shareTitle));
		url = url.replace('%desc%', '');

		// Throw the url
		window.open(url, '_blank');

		// Close the share box
		this.shareClose();
		}


	/**
	* Shares the current page via email
	*/
	this.shareEmail = function()
		{
		// Check for blank input
		var obj = document.getElementById('formShareEmail');
		var inputs = obj.getElementsByTagName('input');
		for (var x = 0; x < 3; x++)
			{
			if (inputs[x].value == '')
				{
				obj.getElementsByTagName('fieldset')[0].className = '';
				obj.getElementsByTagName('label')[0].className = 'missing';
				return;
				}
			}

		// Check for valid email addresses
		var regex = /^[a-z0-9_+-.]+@[a-z0-9_-]*\.?[a-z0-9_-]*\.?[a-z0-9_-]*\.[a-z]+$/i;
		if (regex.test(inputs[0].value) == false || regex.test(inputs[2].value) == false)
			{
			obj.getElementsByTagName('fieldset')[0].className = '';
			obj.getElementsByTagName('label')[0].className = 'missing';
			return;
			}

		// Inject the page details
		document.getElementById('formShareEmailUrl').value = this.shareUrl;
		document.getElementById('formShareEmailTitle').value = this.shareTitle;

		// Send the request
		YAHOO.util.Connect.setForm('formShareEmail');
		YAHOO.util.Connect.asyncRequest("POST", "ajax-share.php", { success: function() { xhtml.shareEmailSent(); }, failure: function() { alert('There was a problem sending your message, please try again.'); } } );

		// Update the share box
		obj.getElementsByTagName('fieldset')[0].className = '';
		obj.getElementsByTagName('label')[0].className = 'hidden';
		obj.getElementsByTagName('label')[1].className = 'sending';
		}


	/**
	* Displays the message send message, and closes the share dialog
	*/
	this.shareEmailSent = function()
		{
		// Update the share box message
		document.getElementById('formShareEmail').getElementsByTagName('fieldset')[0].className = '';
		document.getElementById('formShareEmail').getElementsByTagName('label')[0].className = 'hidden';
		document.getElementById('formShareEmail').getElementsByTagName('label')[1].className = 'sending';
		document.getElementById('formShareEmail').getElementsByTagName('label')[1].innerHTML = '<b>Message Sent!</b>';
		document.getElementById('formShareEmail').getElementsByTagName('fieldset')[1].className = 'hidden';

		// Close the share box
		setTimeout("xhtml.shareClose();", 1750);
		}


	/**
	* Updates the display of the background overlay
	*/
	this.shareUpdateOverlay = function()
		{
		var overlay = document.getElementById('shareBackground');
		overlay.style.position = 'absolute';
		overlay.style.left = '0px';
		overlay.style.top = '0px';
		overlay.style.background = '#000';
		overlay.style.opacity = 0.6;
		overlay.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=60)';
		overlay.style.width = document.body.offsetWidth + 'px';

		var height = (document.getElementById('page').offsetHeight < document.documentElement.clientHeight ? document.documentElement.clientHeight : document.getElementById('page').offsetHeight);
		var shareDialogHeight = 280;
		height = ((shareDialogHeight + 40) < height ? height : (shareDialogHeight + 40));
		overlay.style.height = height + 'px';
		overlay.className = '';
		}
	}
