var random_interval = 0;
var logo_interval = 0;
var what_we_do_interval = 0;
var press_interval = 0;

$(document).ready(function(){

	// Global - Last Items

    $('.slideshow-caption:last').addClass('slideshow-caption-last');
    $('.image-caption:last').addClass('image-caption-last');
    $('.work-info-item:last').addClass('work-info-item-last');
    $('.comments-link-wrap:last').addClass('comments-link-wrap-last');
    
    // People
	
	$('.profile-image-wrapper').mouseenter(function() {
    	$(this).children('img.radish').fadeIn('slow');
  	});
  	$('.profile-image-wrapper').mouseleave(function() {
    	$(this).children('img.radish').fadeOut('slow');
	});
	
	// Jobs
	$('.job-listing:last').addClass('job-listing-last');
	
	// Home
	
	$('#pixogram-button a').click(function(e) {
		e.preventDefault();
		if (!$('#pixogram-wrapper').is(':visible')) {
			$('.squigglies').toggleClass('squigglies-open');
			$('#pixogram-wrapper').slideDown('slow');
			$('#pixogram-button a').html('Close');
		}
		else {
			$('#pixogram-wrapper').slideUp('slow', function() {
				$('.squigglies').toggleClass('squigglies-open');
				$('#pixogram-button a').html('click!');
			});
		}
	});
	
	$('#randomate').hover(
		function() { $('.randomizer, #pixogram .two').mouseover(); },
		function() { $('.randomizer, #pixogram .two').mouseout();
	});
	$('#randomate').click(function(e) { e.preventDefault(); });
	
	$('.randomizer.one').randomize({ max_width: -422 });
	$('.randomizer.three').randomize({ max_width: -422 });
	$('.randomizer.four').randomize({ max_width: -246 });
	$('.randomizer.five').randomize({ max_width: -510 });
	$('.randomizer.eight').randomize({ max_width: -334 });
	$('.randomizer.nine').randomize({ max_width: -510 });
	$('.randomizer.ten').randomize({ max_width: -422 });
	$('.randomizer.eleven').randomize({ max_width: -422 });

	$('#pixogram .two').html(Math.floor(Math.random()*100));
	
	$('#pixogram .two').hover(
		function() {
			random_interval = setInterval(function() {
			$('#pixogram .two').html(Math.floor(Math.random()*100))
		}, 200);
		},
		function() { clearInterval(random_interval); }
  	);
  	
  	$('#what-we-do-wrap').animate({backgroundPosition: '-450px -150px'}, 5000);
  	setInterval(function() {
  		$('#what-we-do-wrap').animate({backgroundPosition: '-450px -150px'}, 5000);
  	}, 5000);
  	setInterval(function() {
  		$('#what-we-do-wrap').animate({backgroundPosition: '-20px -350px'}, 5000);
  	}, 10000);
  	
  	$('#what-we-do-wrap .double-slides .left img').eq(Math.floor(Math.random()*$('#what-we-do-wrap .double-slides .left img').length)).show().addClass('current');
  	$('#what-we-do-wrap .double-slides .right img').eq(Math.floor(Math.random()*$('#what-we-do-wrap .double-slides .right img').length)).show().addClass('current');
  	
  	$('#press-wrap .double-slides .left img').eq(Math.floor(Math.random()*$('#press-wrap .double-slides .left img').length)).show().addClass('current');
  	$('#press-wrap .double-slides .right img').eq(Math.floor(Math.random()*$('#press-wrap .double-slides .right img').length)).show().addClass('current');
  	
  	$('#what-we-do-wrap').hover(
		function() {
			what_we_do_interval = setInterval(function() {
				shuffle_slides('#what-we-do-wrap .double-slides');
			}, 100);
		},
		function() { clearInterval(what_we_do_interval); }
  	);
  	
  	$('#press-wrap .double-slides').hover(
		function() {
			press_interval = setInterval(function() {
				shuffle_slides('#press-wrap .double-slides');
			}, 100);
		},
		function() { clearInterval(press_interval); }
  	);

	randomize_first();
	
	$('#logo').hover(
		function () {
			logo_interval = setInterval('randomize_logo()', 200);
  		},
  		function () {
  			clearInterval(logo_interval);
  		}
	);

});

function shuffle_slides(element) {
	$('.left img.current', element).hide();
	$('.right img.current', element).hide();
	$('.left img', element).eq(Math.floor(Math.random()*$('.left img', element).length)).show().addClass('current');
  	$('.right img', element).eq(Math.floor(Math.random()*$('.right img', element).length)).show().addClass('current');
	}

(function($){
	$.fn.randomize = function(options) {
	
	var defaults = {
		initial_x: 10,
		width: 88,
		max_width: -300,
		interval_id: null
	};
  	
  	return this.each(function() {
  	
  		if (options) { 
        	$.extend(defaults, options);
      	}
      	
  		var obj = $(this);
  		$(obj).hover(
  			function() {
  				defaults.interval_id = setInterval(function() {
  				var position_x_y = $(obj).css('backgroundPosition').split(' ');
	  			var background_x = position_x_y[0].replace('px', '');
	  			var new_background_x = background_x - 88;
	  			
	  			if (new_background_x < options.max_width) {
	  				new_background_x = defaults.initial_x;
	  			}
	  			
	  			$(obj).css('backgroundPosition', new_background_x + 'px ' + position_x_y[1]);
	  		}, 200);
  			},
  			function() {
  				clearInterval(defaults.interval_id);
  			});
  	});


};  
})(jQuery); 

function randomize_logo() {
	var current_index = $('#logo img.current').index();
	$('#logo img.current').removeClass('current');
	if (current_index == ($('#logo img').length - 1)) {
		randomize_first();
	}
	else {
		$('#logo img').eq(current_index + 1).addClass('current');
	}
}

function randomize_first() {
	$('#logo img').eq(0).addClass('current');
}
























