	function mycarousel_initCallback(carousel) {
		jQuery('.jcarousel-control a').bind('click', function() {
			carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('rel')));
			//return false;
		});
		jQuery('#mycarousel-next').bind('click', function() {
			carousel.next();
		    //return false;
		});
		jQuery('#mycarousel-prev').bind('click', function() {
			carousel.prev();
			//return false;
		}); 
	};
	function trigger(carousel, state)
	{
		thisSlide=parseInt(carousel.first);
		prevSlide=parseInt(thisSlide-1);
		nextSlide=parseInt(thisSlide+1);
		n=$('#mycarousel li.slide_counter').length;
		$('#top_nav').find('a').removeClass('active');
		$('#top_nav').find('a.slide' + thisSlide).addClass('active');
		$('#prevSlideDisplay').val(prevSlide);		
		$('#nextSlideDisplay').val(nextSlide);		
		if (prevSlide==0) {
			if ($('#mycarousel-prev').attr('rel')=='hide') {
				$('#mycarousel-prev').addClass('nav_hide');
			}
			else {
				$('#mycarousel-prev').attr('href', $('#mycarousel-prev').attr('rel'));
				$('#mycarousel-prev').find('span').removeClass('nav_hide');
				$('#mycarousel-prev').removeClass('nav_hide');
			}
		}
		else {
			$('#mycarousel-prev').attr('href', '#' + prevSlide);
			$('#mycarousel-prev').find('span').not('nav_hide').addClass('nav_hide');
			$('#mycarousel-prev').removeClass('nav_hide');
		}
		if (nextSlide>n) {
			$('#mycarousel-next').attr('href', $('#mycarousel-next').attr('rel'));
			$('#mycarousel-next').find('span').removeClass('nav_hide');
		}
		else {
			$('#mycarousel-next').attr('href', '#' + nextSlide);	
			$('#mycarousel-next').find('span').not('nav_hide').addClass('nav_hide');
		}
	}
	
	function changeLinks(theSelector)
	{
		var myRegExp = /\?/;
		$('.worklink').each(function(linkName){

			var string1 = $(linkName).attr('href');
			var matchPos1 = string1.search(myRegExp);
			if(matchPos1 != -1)
				$(linkName).attr('href', string1.substr(0,matchPos1) + '?from=' + theSelector);
			else
				$(linkName).attr('href', string1 + '?from=' + theSelector);
		
		});		
	}

google.setOnLoadCallback(function() {


		if(location.hash!=''){
			slideStart = location.hash.substr(1);
		}
		else {
			slideStart = 1;
		}
		slideStart=parseInt(slideStart);
		jQuery("#mycarousel").jcarousel({
			scroll: 1,
			initCallback: mycarousel_initCallback,
			itemLoadCallback: trigger,
			// This tells jCarousel NOT to autobuild prev/next buttons
			buttonNextHTML: null,
			buttonPrevHTML: null,
			easing: "swing",
			animation: "slow",
			start: slideStart
		});
		

		$('.button').not('#ie-buttons .button').click(function(e) {
			var thisSelector = $(this).attr('rel');
			var toTurnOff = new Array(); 
			var toKeep = new Array(); 
			$('.filter_nav li').removeClass('current_page_item_two');
			$(this).parent().addClass('current_page_item_two');
			$('.worknav').fadeTo(150, 0.1);
			$('.ul_container li div span').each(function(){
				if ( $(this).hasClass(thisSelector) ) {
					toKeep.push($(this).attr('id'));
				}
				else {
					toTurnOff.push($(this).attr('id'));					
				}
			});
			//changeLinks(thisSelector);
			shuffleArray(toTurnOff);
			fadeListItem(toTurnOff, 0, thisSelector, toKeep);
			//e.preventDefault(); 
		});

		$('#ie-buttons .button').click(function(e) {
			var thisSelector = $(this).attr('rel');
			$('.filter_nav li').removeClass('current_page_item_two');
			$(this).parent().addClass('current_page_item_two');
			$('.ul_container li div span.' + thisSelector).stop().fadeTo(200, 1);
			$('.ul_container li div span').not('.' + thisSelector).stop().fadeTo(200, 0.3);
			e.preventDefault(); 
		});

		$('.work_item span a').livequery(function(){ 
			$(this).hover(function() { 
				var htmlStr = '<img src="' + $(this).find("img").attr('src').replace("-off", "-on") + '" alt="" class="fade" style="opacity: 0;" />';
				$(this).append(htmlStr);
				$(this).find("img.fade").stop().animate({opacity: 1}, 1000);
			}, function() { 
				$(this).find("img.fade").stop().animate({opacity: 0}, 250, function(){
					$(this).remove();
				});
			}); 
		});

		$("#sub_nav li a").click(function(){
			var thisLink=$(this);
			$("#sub_nav li a").removeClass('active');
			$(thisLink).addClass('active');
		});

		$('#talk_form').slidinglabels({
			/* these are all optional */
			topPosition  : '1px',  // how far down you want each label to start
			leftPosition : '5px',  // how far left you want each label to start
			axis         : 'x',    // can take 'x' or 'y' for slide direction
			speed        : 'fast'  // can take 'fast', 'slow', or a numeric value
		});

		$('#awards li').not('#awards_container #awards li').hover(function () {
			$(this).stop().animate({backgroundColor:'#d0d2d3'}, 1000);
		}, function () {
			$(this).stop().animate({backgroundColor:'#e6e6e6'}, 500);
		});

		$('#js-container').not('#ie-squares #js-container').jsquares({ 
			fade_on_hover: true,
			caption_slide_down: false,
			caption_width: 430,
			caption_height: 220,
			fade_to: 0.3
		});

		// homepage and work
		$('.closing_navigation').localScroll();
		collectWork()

		$( "#ie-squares #js-container .cw" ).hover(
		function(){
			$('#js-container .cw').not($(this)).stop().fadeTo(200, 0.4);
			$('#ie-content').text('');
			$('#ie-content').append($(this).find('.js-overlay-caption-content').clone());
			var position = $(this).position();
			if ($(this).width() > 232) {
				$('#ie-content').css('top', (position.top + $(this).height()));
				$('#ie-content').css('left', (position.left+14));
			}
			else{
				if (position.left > 700) {
					$('#ie-content').css('top', (position.top+14));
					$('#ie-content').css('left', ((position.left-28)-$('#ie-content').width()));					
				}
				else {
					$('#ie-content').css('top', (position.top+14));
					$('#ie-content').css('left', (position.left + $(this).width()));					
				}
			}
			$('#ie-content').removeClass('hideme');
		}, function(){
			$('#ie-content').addClass('hideme');
			$('#js-container .cw').not($(this)).stop().fadeTo(200, 1);
		});		
		
		$('#talk_form').submit(function(e){
			if (checkField($('#name'))!=true) {
				e.preventDefault();
			}
			if (checkEmail($('#email'))!=true) {
				e.preventDefault();
			}			
		});
		
		$(function() {
			$('#name').blur(function(){
				checkField($('#name'));
			});
			$('#email').blur(function(){
				checkEmail($('#email'));
			});			
		});		

		$("a[rel=external]").attr('target', '_blank');

	});
	
	
	
	function collectWork() {
		var obj = $('.ul_container li');
		var arr = $.makeArray(obj);
		shuffleArray(arr);
		fadeUpWork(arr, 0);
		
	}
	function fadeUpWork(theArray, i){
		$(theArray[i]).fadeTo(150, 1, function(){
			i++;
			if( i >= $(theArray).length ){
				$('.worknav').fadeTo(150, 1);
			} else {
				fadeUpWork(theArray, i);
			}
		});
	}
	
	function shuffleArray(theArray) {
		theArray.sort(function(){return 0.5 - Math.random();})
		myNewArray = jQuery.map(theArray, function (i) { return i; });
		return myNewArray;
	}	
	
	function fadeListItem(thisArray, i, thisSelector, toKeep) {
		if (thisArray.length==0) {
			getNewItems(thisSelector, toKeep);
		}
		else {
			var thisContent = '#' + thisArray[i];
			$(thisContent).fadeTo(300, 0, function(){
				$(thisContent).parent().hide('slow', function(){
					$(thisContent).remove();
				});
				i++;
				if(i >= thisArray.length){
					getNewItems(thisSelector, toKeep);
				}
				else {
					fadeListItem(thisArray, i, thisSelector, toKeep);
				}
			});
		}
	}
	function getNewItems(thisSelector, toKeep) {
		var newItems = new Array();
		$.get( '/html/work_all.php', function(data) {
			$(data).find('span.' + thisSelector).each(function(thisKey, thisData){
				if (jQuery.inArray($(thisData).attr('id'), toKeep)==-1) {
					newItems.push(thisData);
				}
			});
			shuffleArray(newItems);
			appendItems(newItems);
		});  
	}
	function appendItems(newItems) {
		var newIDs = new Array();
		$(newItems).each(function(thisKey, thisData){
			var containerID = 'container-' + $(thisData).attr('id').substr(8);
			$('#' + containerID).find('div').prepend(thisData);
			$('#' + containerID).show();
			newIDs.push($(thisData).attr('id'));
		});
		fadeItemsUp(newIDs,0);
	}
	function fadeItemsUp(newIDs, i) {
		//thisArray = $.shuffle(thisArray);
		if (typeof newIDs[i]!="undefined") {
			//alert(newIDs[i].substr(8));
			//var containerID = 'container-' + $(newIDs[i]).attr('id').substr(8);
			$('#container-' + newIDs[i].substr(8) + ' div.work_item').show('slow');
			$('#' + newIDs[i]).fadeTo(200, 1, function(){
				i++;
				if(i >= newIDs.length){
					$('.worknav').fadeTo(150, 1);
				}
				else {
					fadeItemsUp(newIDs, i);
				}
			});		
		}
		else {
			$('.worknav').fadeTo(150, 1);
			return false;	
		}
	}
	function checkField(theField){
		if (theField.val()=='') {
			theField.parent().addClass('required_data');
			return false;
		}
		else {
			if (theField.parent().hasClass('required_data')) {
				theField.parent().removeClass('required_data');
			}
			return true;			
		}		
	}
	function checkEmail(theField){
		if (theField.val()=='') {
			theField.parent().addClass('required_data');
			return false;
		}
		else {
			if (echeck(theField.val())!=true) {
				theField.parent().addClass('required_data');
				return false;				
			}
			else {
				if (theField.parent().hasClass('required_data')) {
					theField.parent().removeClass('required_data');
				}
				return true;				
			}
		}		
	}
	function echeck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
			return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
			return false
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			return false
		}
		if (str.indexOf(at,(lat+1))!=-1){
			return false
		}
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			return false
		}
		if (str.indexOf(dot,(lat+2))==-1){
			return false
		}
		if (str.indexOf(" ")!=-1){
			return false
		}
		return true					
	}
