var _movieIconSource = (Browser.Engine.trident) ? 'http://www.clarkconstruction.com/images/sections/film_icon.gif' : 'http://www.clarkconstruction.com/images/sections/film_icon.png';
var _defaultPicSource;
var _atLeastOneMovie = false;
var _activeThumb;
var _picsToLoad = new Array();

function sectionSlideShow()
{
	if ( $('side_pics') )
	{
		/*iterate through all of the side pics to see if there are any videos*/
		$$('#side_pics .side_pic img').each(function(element, index)
		{
			if (element.hasClass('mov'))
			{
				/*there is at least one movie in the group*/
				_atLeastOneMovie = true;
			}
		});
		
		_defaultPicSource = $('primary_image').getFirst('img').getProperty('src');
		$('side_pics').set('morph', {duration: 100, transition: 'sine:out'});
		$('primary_image').set('morph', {duration: 100, transition: 'sine:out'});
		$$('#side_pics .side_pic img').each(function(element, index)
		{
			var source = element.getProperty('src');
			var pos = source.indexOf('_sm.jpg', 0);
			var imageName;
			/*the thumb links to a movie*/
			if (element.hasClass('mov'))
			{	
				imageName = source.substr(0, pos) + '.flv';
				element.getParent().store('type', 'mov');
				element.getParent().store('index', index);
				
				/*attach the movie icon */
				var movieIcon = new Element('img', {'src': _movieIconSource, 'class':'stop', 'morph':{'duration': 250, 'transition':'sine:out'}});
				
				element.getParent().adopt(movieIcon);
				element.getParent().setStyle('cursor', 'pointer');
				
				/*add click event*/
				element.getParent().addEvent('click', function() {
					if (swfobject.getFlashPlayerVersion().major > 6)
					{
						$('primary_image').set('html', '<div id="player"></div>');
						swfobject.embedSWF('http://www.clarkconstruction.com/assets/flash/player.swf', 'player', '524', '250', '9.0.45', 'http://www.clarkconstruction.com/assets/flash/expressInstall.swf', {'the_video': this.getFirst('img').retrieve('mainSource')}, {'menu': 'false', 'base': '.', 'align': 'top'});
					}
					else
					{
						$('primary_image').set('html', '<p id="no_flash">You do not have the required version of Flash Player needed to view the interactive portfolio pieces.  Please <a href="http://www.macromedia.com/go/getflash/">click here to update your Adobe Flash Player.</a></p>');
					}
					fadeSidePicsOut(this);
				});
				element.getParent().addEvent('mouseenter', function() 
				{
					this.morph({'opacity': 1});										
				});
				element.getParent().addEvent('mouseleave', function()
				{
					if (this != _activeThumb)
					{
						this.morph({'opacity': .6});
					}
				});
			}
			else  /*the thumb links to an image*/
			{
				imageName = source.substr(0, pos) + '_main.jpg';
				element.store('type', 'pic');
				
				/*preload the image*/
				_picsToLoad.push( imageName );
				
				/*fade out the images except for the one related to the main pic*/
				if ( $('primary_image').getFirst('img').getProperty('src') == imageName  )
				{fadeSidePicsOut(element.getParent());}

				/*the mouse over event*/
				element.getParent().addEvent('mouseenter', function()
				{
					/*if their is an image in the primary image div*/
					if ( $$('#primary_image img').length == 1)
					{
						var tempSrc = this.getFirst('img').retrieve('mainSource');
						/*if this image is not the same as the main image*/
						if ( $('primary_image').getFirst('img').getProperty('src') != tempSrc  ) 
						{
							/*replace the image*/
							$('primary_image').set('html', '<img src="' + tempSrc + '" alt="" />');
						}
						fadeSidePicsOut(this);
						this.setStyle('cursor', 'default');
						this.removeEvents('click');
						this.removeEvents('mouseleave');
					}
					else
					{
						this.addEvent('click', function()
						{
							//replace the image
							var tempSrc = this.getFirst('img').retrieve('mainSource');
							$('primary_image').set('html', '<img src="' + tempSrc + '" alt="" />');
							fadeSidePicsOut(this);
						});
						this.setStyle('cursor', 'pointer');
						this.morph({'opacity': 1});										
						this.addEvent('mouseleave', function()
						{
							if (this != _activeThumb)
							{
								this.morph({'opacity': .6});
							}
						});
					}
				});
			}
			
			element.store('mainSource', imageName);
		});
		var myImages = new Asset.images( _picsToLoad );
	}
}

/*fades all of the side pics out except the one past in*/
function fadeSidePicsOut(thisElement)
{
	_activeThumb = thisElement;
	
	$$('#side_pics .side_pic').each(function(element)
	{
		if ( thisElement != element )
		{
				element.morph({'opacity': .6});
		}
		else
		{
			element.morph({'opacity': 1});
		}
	});
}

window.addEvent('domready', function()
{sectionSlideShow();});
