/**
* Assign the view handler
*/

viewHandler = Portfolio;

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

	var _instance = this;



	// 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);

		// Add event handlers to portfolio navigation links
		this.addEventHandlers();
		}


	/**
	* Adds the animation events handlers for portfolio item
	* (unless it's an older version of IE less than 8.0)
	*/
	this.addEventHandlers = function()
		{
		// Check if we're using Internet Explorer prior to version 8.0
		// (the screen redraw on page change looks terrible in the older versions)
		var matches = navigator.userAgent.toLowerCase().match(/msie (\d+\.\d+)/i);
		if (!!(window.attachEvent && !window.opera) && matches && 1 < matches.length && parseFloat(matches[1]) < 8.0)
			{
			return;
			}

		// Add event handlers to portfolio navigation links
		var nodes = dojo.query('#portfolioIntro p.pagination a, #portfolioImages a');
		for (var x = 0; x < 4; x++)
			{
			nodes[x].onclick = function()
				{
				xhtml.pendingUrl = this.href;
				xhtml.changePage(this);
				return false;
				}
			}
		}


	/**
	* Animates the page change
	*
	* @param		obj			The anchor clicked on
	*/
	this.changePage = function(obj)
		{
		// The animation speed in msec
		var speed = 750;

		// The margin to apply (shifts the images either left or right)
		var margin = obj.getAttribute('rel') == 'prev' ? 660 : -660;

		// The final opacity for the previous and next project image at the end of the animation
		var opacity = {
			prev: obj.getAttribute('rel') == 'prev' ? 0.6 : 0.2,
			next: obj.getAttribute('rel') == 'next' ? 0.6 : 0.2
			};

		// The final colour for the previous and next project image borders
		dojo.query('#portfolioImages div').style.borderColor = '#000000';

		// Queue the animation
		window.scrollTo(0,0);
		dojo.animateProperty( {duration: speed, node: dojo.query('#portfolioImages div.prevImage')[0], properties: { marginLeft: margin, opacity: opacity.prev } } ).play();
		dojo.animateProperty( {duration: speed, node: dojo.query('#portfolioImages div.mainImage')[0], properties: { marginLeft: margin, opacity: 0.2 },
			onEnd: function()
				{
				window.location = xhtml.pendingUrl;
				}
			} ).play();
		dojo.animateProperty( {duration: speed, node: dojo.query('#portfolioImages div.nextImage')[0], properties: { marginLeft: margin, opacity: opacity.next } } ).play();
		dojo.animateProperty( {duration: speed, node: document.getElementById('portfolioStory'), properties: { opacity: 0.0 } } ).play();
		}
	}
