$(document).ready(function() {
	setUpClientList(); // for the clients page
	setUpPeople(); // for the people page
	$('.scroll-pane').jScrollPane();
	setUpClientShowcase();
	setUpCampaignPieceSwap();
	setUpPostSwap();
	slideSwitch();
	// initialize fancybox
	$('.fancybox-image').fancybox();
	$('.fancybox-video').fancybox({'frameWidth':500,'frameHeight':340});
	$('a[rel="external"]').attr('target', '_blank');
	// initalize easySlider for the campaigns
	$('#campaigns').easySlider({
		prevId: 'prevbtn',
		nextId: 'nextbtn',
		controlsShow: true,
		controlsFade: true,
		speed: 500
	});
	
	//homepage banner
    $('#homeBanner').cycle({ 
		fx:     'fade', 
		speed:  '300', 
		timeout: 5000, 
		pager:  '#homeBannerNav' 
	});
	// tweets
	$(".tweet").tweet({
        avatar_size: 48,
			count: 4,
			query: "from:wscottbrandon OR from:hjohnson843 OR from:andykovan OR from:heyerica OR from:julievonbokern OR from:mljordan #tba",
			loading_text: "searching twitter..."
    });
});

function setUpClientShowcase(){
	$("#showcase #controls a").click(function(){
		// clear the selcted state of all
		$("#showcase #controls a").each(function(){ $(this).removeClass('selected'); });
		$(this).addClass("selected");
		$("#client-image img").attr('src',$(this).attr("rel")).hide().fadeIn();
		$("#client-image").attr('href',$(this).attr("href"));
		if($(this).attr('title') == "broadcast"){
			$("#client-image").removeClass('fancybox-image');
			$("#client-image").addClass("fancybox-video");
			$('.fancybox-video').fancybox({'frameWidth':500,'frameHeight':340});
		} else {
			$("#client-image").removeClass('fancybox-video');
			$("#client-image").addClass("fancybox-image");
			$('.fancybox-image').fancybox();
		}
		return false;
	});
}

function setUpCampaignPieceSwap(){
	$("#campaign-viewer a").click(function(){
		// clear the selected state of all controls and highlight this one
		$("#campaign-controls a").each(function(){ $(this).removeClass('selected'); });
		$(this).addClass('selected');
		// clear the selected state of all pieces
		$("#campaign-viewer .piece").each(function(){ $(this).hide(); });
		var pieceToShow = $(this).attr("href");
		$('#'+pieceToShow).fadeIn('slow');
		return false;
	});
}

function setUpPostSwap(){
	$(".post-controls a").click(function(){
		// clear the selected state of all controls and highlight this one
		$(this).siblings().each(function(){ $(this).removeClass('selected'); });
		$(this).addClass('selected');
		// clear the selected state of all pieces
		$(this).parent().parent().find(".home-article").each(function(){ $(this).hide(); });
		var pieceToShow = $(this).attr("href");
		$('#'+pieceToShow).fadeIn('slow');
		return false;
	});
}

function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}


 
function setUpClientList(){
	// click handler when selecting categories
	$("#client-categories li a").click(function(){
		// clear the categories selected state
		$("#client-categories li a").each(function(){
			$(this).removeClass("selected");
		});
		// highlight the category
		$(this).addClass("selected");
		// grab the category title
		var $category = $(this).attr("title");
		// reset the list' text color
		$("#client-list ul li span").each(function(){
			$(this).css("color","#999");
		});
		// highlight the applicable clients
		$("#client-list ul li."+$category+" span").each(function(){
			$(this).hide().css("color","#fff").animate({ "opacity": "show" }, { "duration": 500 });
		});
		// swap the image in the header
		$("#header-pic").attr("src","/media/images/header-pic-clients-"+$category+".jpg").hide().fadeIn();
		return false; //  kill the click-through
	});
}

function setUpPeople(){
	// set the people grey on load
	$("#people-list ul li a").each(function(){
		$(this).css({"opacity": 0.3});
	});
	// click handler when selecting categories
	$("#people-categories li a").click(function(){
		// clear the categories selected state
		$("#people-categories li a").each(function(){
			$(this).removeClass("selected");
		});
		// highlight the category
		$(this).addClass("selected");
		// grab the category title
		var $category = $(this).attr("title");
		// reset the people thumbnails
		$("#people-list ul li a").each(function(){
			$(this).animate({ "opacity": 0.3 }, { "duration": 50 });
		});
		// highlight the applicable clients
		var timeDelay = 0;
		$("#people-list ul li."+$category+" a").each(function(){
			$(this).animate({ "opacity": 0.3 }, { "duration": timeDelay }).animate({ "opacity": 1 }, { "duration": 500 });
			timeDelay += 20;
		});
		// swap the image in the header
		//$("#header-pic").attr("src","/media/images/header-pic-clients-"+$category+".jpg");
		return false; //  kill the click-through
	});
	// hover behavior
	$("#people-list ul li a").hover(
		function(){
			if($(this).css("opacity") < 0.98){
				$(this).animate({ "opacity": 0.98 }, { "duration": 300 });
			}
		},
		function(){
			if($(this).css("opacity") < 0.99){
				$(this).animate({ "opacity": 0.3 }, { "duration": 50 });
			}
		}
	);
}
