$(document).ready(function() {

	$('.fancybox').fancybox({ titleShow:false });

	$('.cycle').cycle({ pager:'.cycle_nav', timeout:5000 });

	$('.client-link').hover(function() {
		$(this).find('img').stop().animate({ opacity:0 }, 200);
	}, function() {
		$(this).find('img').stop().animate({ opacity:1 }, 200);	
	});

	/* Enews Form */
	$('.input').bind('focusin', function() { if($(this).val() == $(this).attr('data-default')) { $(this).val(''); } });
	$('.input').bind('focusout', function() { if($(this).val() == '') { $(this).val( $(this).attr('data-default') ); } });

	$('#enews_signup').submit(function() {
		$self = $(this);
	
		// Check for valid email
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})/;
		if(reg.test($self.find('.email').val()) == false)
		{
			alert('Please enter a valid e-mail address.');
			return false;
		}
	});


	/* Form Menus */
	$('.selectmenu').selectmenu({maxHeight: 150});

	/* Tweet Side */
	$('#social_media_side_right').click(function() {
		if($(this).hasClass('closed')) {
			$(this).animate({width: '300px'}, 400).removeClass('closed').addClass('open');
		} else if($(this).hasClass('open')) {
			$(this).animate({width: '52px'}, 400).removeClass('open').addClass('closed');
		}
	});

	/* Link Fades */
	$('a.button_fade').hover(function() {
		$(this).children('img').fadeOut('fast');
	}, function() {
		$(this).children('img').fadeIn('fast');
	});

	$('#social_media_inside a').css('opacity', '0.75');

	$('#social_media_inside a').hover(function() {
		$(this).animate({opacity:1}, 'fast');
	}, function() {
		$(this).animate({opacity:0.75}, 'fast');
	});
	
	
	/* Small Project Preview Links */
	$('.work-link').click(function(){ location.href = '' + $(this).attr('rel'); });

	$('.video img.captify').captify({ speedOut:100, speedOver:100, className:'caption video' });
	$('.web img.captify').captify({ speedOut:100, speedOver:100, className:'caption web' });	


	//SCROLLER
	var scrolling = false;
	var end_pos = 0;
	$('#Scroller').children('.img_holder').each(function(index) {
		var this_width = $(this).width();
		end_pos = end_pos+this_width+1;
	});

	jQuery(function($){
		$("#ScrollRight").mousedown(function(){
			var pos = $("#Scroller").position();

			if (pos.left <= -(end_pos-1358) || (pos.left % 679)) { return false; }
			else { scrolling = false; startScrolling($("#Scroller"), "-=679"); };
		}).mouseup(function(){
			scrolling = false;
		});
	});

	jQuery(function($){
		$("#ScrollLeft").mousedown(function(){
			var pos = $("#Scroller").position();
			if (pos.left >= 0 || (pos.left % 679)) { return false; }
			else { scrolling = false; startScrolling($("#Scroller"), "+=679"); };
		}).mouseup(function(){
			scrolling = false;
		});
	});

	function startScrolling(obj, param)
	{
		obj.animate({"left": param}, "fast", function(){
			if (scrolling) { startScrolling(obj, param); }
		});
	}
	
});
