var homepage_video_width = "432";
var homepage_video_height = "259";

window.addEvent('domready',function()
{

	//slideshow js
	
	//set the bg image (first load)
	var firstActive = $('slideshow').getElement('.active');
	
	var firstId = firstActive.id.replace('text-', '');
	
	$('link-container').innerHTML = '<a href="'+linkArray[firstId]+'" alt="">View work</a>';
	
	changeBG(backgroundImages[firstId]);
	
	var startLeftPix = String(startLeft) + 'px';
	
	$('slideshow-wrapper').setStyle("left", startLeftPix);
	
	var call = setInterval('MoveSlide()', 5000);
	
	var tween = new Fx.Tween($('slideshow-wrapper'));
	
	var isTweening = false;
	
	$('slideshow-container').removeClass('noscript');
	$('title-container').removeClass('noscript');
	//$('sub-title-container').removeClass('noscript');
	
	//if they click an image (with a video in it)
	$('slideshow-wrapper').getElements('.video-home img').addEvent('click', function(){
	
		var currentActive = $('slideshow').getElement('.active');
		var currentID = currentActive.id.replace('text-', '');
		EmbedVideo(currentID);
		
		window.clearInterval(call);
		
	});

	if ($('slideshow')){
		$('slideshow').getElements('a').addEvent('click', function(e) {
			//stop the other videos;
			stopVideos();
		});
	}
		
	//if they click the nav icons
	if( $('slideshow') )
	{
		//tween.start('left', 0);
		$('slideshow').getElements('a').addEvent('click', function(event) {
			event = new Event(event).stop();

			var activeNavItem = $('slideshow').getElement('.active');
			var activeNavId = activeNavItem.id.replace('text-', '');
			
			var Linkid = this.id.replace('text-', '');
			
			if (isTweening == false)
			{

				isTweening = true;
				
				window.clearInterval(call);
				
				
				if (Linkid > activeNavItem.id)
				{
					//negative move amount multiplied by distance
					
					var distance = Linkid - activeNavId;
					var moveMultiplyer = parseInt(distance);
					var moveAmount = -428 * moveMultiplyer;
					activeNavItem.removeClass('active');
					var nextActiveNavItem = $('slideshow').getElementById(String('text-'+Linkid));
					var nextActiveId = nextActiveNavItem.id.replace('text-', '');
					nextActiveNavItem.addClass('active');

					var currentLeft = $('slideshow-wrapper').getStyle("left");
					currentLeft = parseInt(currentLeft.replace('px', ''));
					$('link-container').innerHTML = '<a href="'+linkArray[nextActiveId]+'" alt="">View work</a>';
					
					changeBG(backgroundImages[nextActiveId]);
					
					$('slideshow-wrapper').set('tween', {
					onComplete: function(e) {
							isTweening = false;
						}
					});
					
					$('slideshow-wrapper').tween('left', currentLeft + moveAmount);
					$('title-wrapper').tween('left', currentLeft + moveAmount);
					//$('sub-title-wrapper').tween('left', currentLeft + moveAmount);
					
				}
				else if (Linkid < activeNavItem.id)
				{
					//positive move amount multiplied by distance
					var distance = activeNavId - Linkid;
					var moveMultiplyer = parseInt(distance);
					
					var moveAmount = 428 * moveMultiplyer;
					
					activeNavItem.removeClass('active');
					var nextActiveNavItem = $('slideshow').getElementById(String('text-'+Linkid));
					nextActiveNavItem.addClass('active');
					var nextActiveId = nextActiveNavItem.id.replace('text-', '');

					var currentLeft = $('slideshow-wrapper').getStyle("left");
					currentLeft = parseInt(currentLeft.replace('px', ''));
					$('link-container').innerHTML = '<a href="'+linkArray[nextActiveId]+'" alt="">View work</a>';
					
					changeBG(backgroundImages[nextActiveId]);
					
					$('slideshow-wrapper').set('tween', {
					onComplete: function(e) {
							isTweening = false;
						}
					});
					
					
					//add a condition so that if they click the first nav item the tweening doesnt apply
					
					
					if (Linkid == 0)
					{
						$('slideshow-wrapper').setStyle('left', 0);
						$('title-wrapper').setStyle('left', 0);
						//$('sub-title-wrapper').setStyle('left', 0);
						isTweening = false;
					}
					else
					{
						$('slideshow-wrapper').tween('left', currentLeft + moveAmount);
						$('title-wrapper').tween('left', currentLeft + moveAmount);
						//$('sub-title-wrapper').tween('left', currentLeft + moveAmount);
					}
				}

			}
		});
	}
			
});




function MoveSlide()
{
	
	var activeNavItem = $('slideshow').getElement('.active');
	
	var Linkid = activeNavItem.id.replace('text-', '');
	
	var tween = new Fx.Tween($('slideshow-wrapper'));
	var tweenTitle = new Fx.Tween($('title-wrapper'));
	//var tweenSubTitle = new Fx.Tween($('sub-title-wrapper'));
	
	if (Linkid != (Maximun -1))
	{
		var moveMultiplyer = parseInt(Linkid) + 1;
		var moveAmount = -428 * moveMultiplyer;
		activeNavItem.removeClass('active');
		var i = Linkid;
		i ++;
		var nextActiveNavItem = $('slideshow').getElementById(String('text-'+i));
		var nextActiveId = nextActiveNavItem.id.replace('text-', '');
		nextActiveNavItem.addClass('active');
		$('link-container').innerHTML = '<a href="'+linkArray[nextActiveId]+'" alt="">View work</a>';
		
		changeBG(backgroundImages[nextActiveId]);
		
		tween.start('left', moveAmount);
		tweenTitle.start('left', moveAmount);
		//tweenSubTitle.start('left', moveAmount);
	}
	else
	{
		
		var moveAmount = 0;
		activeNavItem.removeClass('active');
		var nextActiveNavItem = $('slideshow').getElementById(String('text-'+0));
		var nextActiveId = nextActiveNavItem.id.replace('text-', '');
		nextActiveNavItem.addClass('active');
		$('link-container').innerHTML = '<a href="'+linkArray[nextActiveId]+'" alt="">View work</a>';
		
		changeBG(backgroundImages[nextActiveId]);
		/* 
		REMOVE TWEENING
		
		tween.start('left', moveAmount);
		tweenTitle.start('left', moveAmount);
		tweenSubTitle.start('left', moveAmount);
		
		*/
		$('slideshow-wrapper').setStyle('left', 0);
		$('title-wrapper').setStyle('left', 0);
		//$('sub-title-wrapper').setStyle('left', 0);
	}
}

function EmbedVideo( index )
{
	if (videoArray[index].video)
	{
		var flashvars = {
			file: videoArray[index].video,
			image: videoArray[index].image,
			width: homepage_video_width,
			height: homepage_video_height,
			searchbar: "false",
			shownavigation: "false",
			allowfullscreen: "true",
			allowscriptaccess: "always",
			autostart: "true"
		};
		var params = { wmode: "transparent", allowscriptaccess: "always" };
		var attributes = { id: "media_"+index+"_home", name: "media"+index+"_home" };

		swfobject.embedSWF( swf_player, videoArray[index].targetDiv, homepage_video_width, homepage_video_height, "9.0.0", swf_install, flashvars, params, attributes );
	}

}

function stopVideos()
{	
	var home_videos = $('slideshow-wrapper').getElements('.video-home');
	for( var i=0, j=home_videos.length; i < j;  i++  )
	{
		if ( $('media_'+i+'_home') ) {
			$('media_'+i+'_home').sendEvent('STOP');
		}
	}
}

function changeBG( newImgSrc )
{
	// get current background container
	var imgContainer = $$('#bg .img-container')[0];
	// get current background
	var currImg = imgContainer.getElements('img.bg-img')[0];

	// if no current BG image
	if(currImg.src == '')
	{
		currImg.setProperty('src',newImgSrc);
	}
	else
	{
		var newImg = new Element('img', {
			'class':'bg-img',
			'alt':'',
			'events':
			{
				'load':function()
				{
					// fade out old background
					tweenBG(currImg);
				}
			}
		});
		// inject new background into the DOM before current background
		// hidden by z-index
		newImg.setProperty('src', newImgSrc);
		newImg.inject(currImg, 'before');
	}
}

function tweenBG(currImg)
{
	currImg.fade('out');
	window.setTimeout(function() {currImg.dispose(); }, 1000);
}