
	$.fn.changeClient = function(nohide)
	{
		var cl_num = $('.client').length;
		var current = $(this).index();
		$('#cur').text(current+1);
		var prev = (current == 0) ? $(this).siblings(':last') : $(this).prev();
		var next = (current == cl_num-1) ? $(this).siblings(':first') : $(this).next();
		$('#prev').attr('href',prev.attr('id'));
		$('#next').attr('href',next.attr('id'));

		$('#img_link').attr('href',$(this).find('a').attr('href'));
		var src = $(this).find('img.lg').attr('src');
		if (nohide)
		{
			$('img_link img').attr('src',src); 
		}
		else
		{	
			$('#img_link img').fadeOut(function() 
			{ 
				  $(this).load(function() { $(this).fadeIn(); }); 
				  $(this).attr('src', src); 
			});
		}

		$('#text h2').text($(this).find('h2').text());
		$('#text p').html($(this).find('p').html());
		var sel = $('#thumbs img:eq('+current+')');
		var pos = sel.position().left+20;
		$('#arrow').animate({left : pos });
		$('#thumbs img:eq('+current+')').addClass('selected').siblings().removeClass('selected');
	}

	function nextClient()
	{
		var sel = $('.selected');
		var next = (sel.is(':last-child')) ? sel.siblings().first() : sel.next();
		var cl = next.attr('alt');
		$cl = '#'+cl;
		$($cl).changeClient();
	}

	function userChange(myid,interval)
	{
		var cl = '#'+myid;
		clearInterval(interval.id);
		$(cl).changeClient();
		interval.id = setInterval (nextClient, 5000);
	}	
	
	function nextQuote()
	{
		var sel = $('.quote:visible');
		var next = (sel.is(':last-child')) ? sel.siblings().first() : sel.next();
		next.fadeIn('slow').siblings().hide();
	}

$(document).ready(function()
{
	if ($('#work').length)
	{	
		$('#count').text( $('.client').length);

		$('.client:first').changeClient(true);

		$('.thumbs img').click(function()
		{
			userChange($(this).attr('alt'),interval);

		});
		$('#prev').click(function(e)
		{
			e.preventDefault();
			userChange($(this).attr('href'),interval);
		});

		$('#next').click(function(e)
		{
			e.preventDefault();
			userChange($(this).attr('href'),interval);
		});

		var interval = {};
		interval.id = setInterval (nextClient, 7000);
	}	
	if ($('#goodworks').length)
	{
		$('.quote:first').fadeIn();

		var gwi = {};
		gwi.id = setInterval (nextQuote, 12000);
	}
	if ($('#about').length)
	{
		$('#photos').cycle(
		{ 
			prev:   '#prev', 
			next:   '#next', 
			before: onBefore
		});
		function onBefore() { 
			$('#title').html(this.alt); 
		}; 
	}
	if ($('#index').length)
	{
		$('#show_video').click(function(e)
		{
			e.preventDefault();
			$('#xxdemo_wrapper').show();
			$('#demo_wrapper').modal();
			$('#demo_wrapper video').height(480);
			$('#video_demo').width(752);
			$('#vjb').width(752);
			_V_("mwdemo").ready(function(){
			  var myPlayer = this;

			  // EXAMPLE: Start playing the video.
			  myPlayer.play();

			});
		});
	
	}

});


