var resizeTimer;

function centerWork() {
	var viewportW = $(window).width();
	var scrollerW = $('#work-inner').width();
	var centerMarg = (((scrollerW-viewportW)/2)+84);
	//$("#work-sec").scrollLeft(centerMarg);
	$('#work-sec').animate({scrollLeft: centerMarg}, 0, function() {
		$('#work-inner').fadeTo('fast', 1);
	});
}

function slideWork(direction) {
	if ($(':animated').length) {
		return false;
	}
	switch (direction) {
		case 'left':
			$('#work-inner article').filter(':first').toggle('fast',function() {
				$($('#work-inner article').filter(':last')).after(this);
				$(this).toggle(0);
			});
		break;
		case 'right':
			$('#work-inner article').filter(':last').toggle(0,function() {
				$($('#work-inner article').filter(':first')).before(this);
				$(this).toggle('fast');
			});
		break;
	}
}

var articlesWidth;
$(document).ready(function() {
	$('.l-arrow').hover(
	  function () {
	    $(this).fadeTo("fast",0.9);
	  },
	  function () {
	     $(this).fadeTo("fast",1);
	  });
	var numArticles = $('#work-inner article').length;
	articlesWidth = (numArticles * 530);
	$('#work-inner').width(articlesWidth);
	
	 $('<img />').attr('src','http://sammcqueen.com/images/page/dividers/bulb-on.png').appendTo('body').css('display','none');
	$('#profile-photo').click(function() {
		$(this).animate({marginTop: '0'},50,function() {
			$(this).animate({marginTop: '-3'},50,function() {
				if ($('.bulb').hasClass('bulb-on')){
					$('.bulb').removeClass('bulb-on');
				} else {
					$('.bulb').addClass('bulb-on');
				}
			});
		});	
	});
	
	$('.work-slide img').hover(function() {
		$(this).fadeTo('fast',0.8);
	},function() {
		$(this).fadeTo('fast',1);
	});
	
	$('.work-slide a').colorbox({
		onOpen:function(){ $('body').addClass('hide-overflow'); },
		onCleanup:function(){ $('body').removeClass('hide-overflow'); },
		transition: "fade",
		speed: 0,
		height: "100%",
		width: "100%",
		returnFocus: false,
		opacity: 1
		});
	$('#cboxLoadedContent').live("click", function(){ $.colorbox.close(); }); 
});

$(function(){
	$.extend($.fn.disableTextSelect = function() {
		return this.each(function(){
			if($.browser.mozilla){//Firefox
				$(this).css('MozUserSelect','none');
			}else if($.browser.msie){//IE
				$(this).bind('selectstart',function(){return false;});
			}else{//Opera, etc.
				$(this).mousedown(function(){return false;});
			}
		});
	});
	$('.noSelect').disableTextSelect();//No text selection on elements with a class of 'noSelect'
	
	centerWork();
	
	$(document).keyup(function(event) {
		if (event.keyCode == '39') {
			slideWork('left');
		}
		if (event.keyCode == '37') {
			slideWork('right');
		}
	});
	
	$('#large-right').click(function() {
		slideWork('left');
	});
	$('#large-left').click(function() {
		slideWork('right');
	});
	
	var throttleTimeout;
	$(window).bind('resize',function(){
		if ($.browser.msie) {
			if (!throttleTimeout) {
				throttleTimeout = setTimeout(function() {
					centerWork();
					throttleTimeout = null;
				},50);
			}
		} else {
			centerWork();
		}
	});
});
