$(document).ready(function() { 
	
	//Jesse's code.
	$(".clickable").click(function() {
		if($(this).hasClass(".expanded")) {
			$(this).removeClass("expanded");
		} else {
			$(this).addClass("expanded");
		}
	});
	
	// disable the tab key navigation - Taryn
   	if ($.browser.mozilla) {
		$(this).keypress(function(event) {
			if(event.keyCode == 9) {
	        	return false;
	      	}
		});
   	} else {
      	$(this).keydown(function(event) {
			if(event.keyCode == 9) {
	        	return false;
	      	}
		});
   	}
	
	/*----------------------------------------	
	|	SCROLL PANE
	------------------------------------------*/
	//Jesse installed this plugin.
	$(function() {
		$('.scroller').jScrollPane({
			showArrows: true
		});
	});
	
	
	function scroll_to_include(target) {
		// IE sucks.
		// height = $(window).height();
		// //top = $(window).scrollTop();
		// top = document.documentElement.scrollTop();
		// bottom = top + height;
		// if(target < top) {
		// 	//$('html,body').animate({scrollTop: target+'px'}, 'slow');
		// 	document.documentElement.animate({scrollTop: target+'px'}, 'slow');
		// 	// console.log('scrolled up');
		// }
		// if(target > bottom) {
		// 	//$('html,body').animate({scrollTop: target-height}, 'slow');
		// 	document.documentElement.animate({scrollTop: target-height}, 'slow');
		// 	// console.log('scrolled down');
		// }
	}
	
	
		
	/*----------------------------------------	
	|	PEOPLE
	------------------------------------------*/
	//Jesse and Will wrote this bit.
	$("#bio_thumbs.cycler").cycle({ 
		fx:     'fade', 
		speed:   1200, 
		timeout: 5618,
		next: 	'#nextBio',
		prev: 	'#prevBio'
	});
	$("#prevBio").attr('style', 'display: none');
	$("#nextBio").attr('style', 'display: none');
	$("#people img, #people h4").click(function() {
		if(!$('#people').hasClass("expanded")) {
			$("#people").addClass("expanded")
			$("#bio_thumbs.cycler").cycle('pause');
			$("#people").find(".h1").animate({height: "270px"}, "slow");
			$("#people").animate({height:"280px"}, "slow", function(){
				$("#people .people_collapse").css({'z-index': 50});
				$("#people .show_all").css({'z-index': 50});
				$("#prevBio").show("slow");
				$("#nextBio").show("slow");
				$("#people").animate({height: "302px"}, "slow", function(){
					scroll_to_include($('#people').offset().top + 332);
				});
			});
			return false;
		}
	});
	$('.people_collapse').click( function() {
		$("#people .people_collapse").css({'z-index': 0});
		$("#people .show_all").css({'z-index': 0});
		$("#bio_thumbs.cycler").cycle('resume');;
		$("#prevBio").hide("slow");
		$("#nextBio").hide("slow", function() {
			$("#people").removeClass('expanded');
		});
		$("#people").animate({height:"125px"}, "slow");
		$("#people").find(".h1").animate({height: "122px"}, "slow");
	})
	$("#people").mouseover(function() {
		$("#bio_thumbs.cycler").cycle('pause');
	});
	$("#people").mouseout(function() {
		if(!$(this).is(".expanded")) {
			$("#bio_thumbs.cycler").cycle('resume');
		}
	});
	
	/*----------------------------------------
	|	end PEOPLE
	*/

	/*----------------------------------------	
	|	TWITTER
	------------------------------------------*/
	//Will wrote this bit.
	//$(".tweets div:first-child").css("height", "95px");
	
	$("#scc_tweets").click(function() {
		var tweet = $(this);
		if(!tweet.is(":animated")) {
			if(tweet.hasClass("tweetexpanded")) {
				tweet.animate({height : "125px"}, 900);
				tweet.find(".h1").animate({height: "98px"}, 900);
				tweet.removeClass("tweetexpanded");
				//tweet.find("div:first-child").css("height", "95px");
				$(".tweets").animate({height: "95px"}, 900);
			} else {
				tweet.find("div:first-child").css("height", "auto");
	 			var tweetheight = $(".tweets").height();
				tweet.animate({height: tweetheight + 27 + "px"}, 900);
				tweet.find(".h1").animate({height: tweetheight + "px"}, 900, '', function () {
					scroll_to_include(tweet.offset().top + tweetheight + 30);
				});
				tweet.addClass("tweetexpanded");
				return false;
			}
		}
	});
	
	/*----------------------------------------
	|	end TWITTER
	*/
	
	/*----------------------------------------	
	|	CLIENTS - New, using cycler.
	------------------------------------------*/
	$(".clients_logos").cycle({ 
		fx:     'fade', 
		speed:   1000, 
		timeout: 1963,
		next: '#nextLogo',
		prev: '#previousLogo'
	});
	function show_client_arrows () {
		$("#logoControls").animate({opacity: 1});
		$(".clients_logos").cycle('pause');
	}
	function hide_client_arrows () {
		$("#logoControls").animate({opacity: 0});
		$(".clients_logos").cycle('resume');
	}
	var config = {    
	     sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
	     interval: 200, // number = milliseconds for onMouseOver polling interval    
	     over: show_client_arrows, // function = onMouseOver callback (REQUIRED)    
	     timeout: 500, // number = milliseconds delay before onMouseOut    
	     out: hide_client_arrows // function = onMouseOut callback (REQUIRED)    
	};
	
	$('#clients').hoverIntent(config);
	
	$(".client_logo").click(function() {
		window.location.href = base_url+'clients';
	});
	
	
	/*----------------------------------------	
	|	CLIENTS - Old, using scroller.
	------------------------------------------*/
	// var clients = $('#clients');
	// var client = clients.find('client');
	// var clients_scroller = $('#clients .scroller')
	// //Jesse wrote this bit.
	// function getClientPositions() {
	// 	var client_positions = new Array();
	// 	clients.find('.client').each(function(intIndex) {
	// 		var client_id = $(this).attr("id");
	// 		var container_height = parseInt($(".client").css("height"));
	// 		
	// 		distance = intIndex * container_height;
	// 	
	// 		client_positions[intIndex] = [];
	// 		client_positions[intIndex]['client_id'] = client_id;
	// 		client_positions[intIndex]['distance'] = distance;
	// 	});
	// 	return client_positions;
	// }
	// 
	// $("#clients .scroller").scroll(function () {
	// 	var top_position = parseInt($(this).css("top"));
	// 	client_positions = getClientPositions();
	// 	container_height = -parseInt($(".client").css("height"));
	// 	offset = 50;
	// 	
	// 	$.each(client_positions, function(intIndex, ClientId) {
	// 		this_position = -client_positions[intIndex]['distance'] + offset;
	// 		previous_position = this_position + container_height; 
	// 		
	// 		if(top_position < this_position) {
	// 			if(previous_position > top_position || top_position == 0) {
	// 				$(".client").removeClass("active");
	// 				$("#" + client_positions[0]['client_id']).addClass("active");
	// 			} else {
	// 				$("#" + client_positions[intIndex]['client_id']).addClass("active");
	// 			}
	// 		}
	// 	});
	// });
	// 
	// //Will wrote this bit.
	// 
	// var num_clients = $("#clients .client").get();
	// var i = 0;
	// $(num_clients[0]).addClass('active');
	// function scroll_clients(){
	// 	setTimeout(function() {
	// 		if(!clients.hasClass('hovering') && !clients.hasClass('clientsexpanded')){
	// 			clients_scroller.fadeOut('fast', function() {
	// 				clients_scroller[0].scrollTo($(num_clients[i]).position().top);
	// 			});
	// 			clients_scroller.fadeIn('fast');
	// 			
	// 			if(i < num_clients.length-1) {
	// 				i = i+1;
	// 			} else {
	// 				i = 0;
	// 			}
	// 		}
	// 		scroll_clients();
	// 	}, 1935);
	// }
	// scroll_clients();
	// 
	// clients.mouseover(function() {
	// 	clients.addClass('hovering');
	// });
	// clients.mouseout(function() {
	// 	clients.removeClass('hovering');
	// });
	// 
	// 
	// // Clients Expansion using Scroller
	// // $(".client").click(function(){
	// // 	var client = $(this);
	// // 	var client_content = client.find(".client_content")
	// // 	if(!client_content.is(":animated")){
	// // 		var position = parseInt(client.position().top);
	// // 		var relative = clients_scroller.css("top");
	// // 		clients_scroller.jScrollPaneRemove();
	// // 		clients_scroller.css("position", "relative");
	// // 		clients_scroller.css("top", relative);
	// // 		clients_scroller.css("height", "auto");
	// // 		
	// // 		if(client_content.is(":hidden")) {
	// // 			client_content.slideDown("slow");
	// // 			if(!clients.hasClass('clientexpanded')) {
	// // 				clients.addClass('clientexpanded');
	// // 				clients.animate({height : "370px"}, "slow", '', function () {
	// // 					scroll_to_include(clients.offset().top + 375);
	// // 				});
	// // 				clients.find('.h2').animate({height: "313px"});
	// // 				$('#clients_collapse').show('slow');
	// // 			}
	// // 		}else{
	// // 			client_content.slideUp("slow");
	// // 		}
	// // 		
	// // 		setTimeout(function() {
	// // 			var clients_scroller = $("#clients .scroller");
	// // 			clients_scroller.css("height", $('#clients .h2').css('height'));
	// // 			clients_scroller.jScrollPane({showArrows: true});
	// // 			clients_scroller[0].scrollTo(position);
	// // 		}, 800);
	// // 	}
	// // });
	// // End Clients Expansion Using Scroller.
	// 
	// $(".client_logo").click(function() {
	// 	window.location.href = base_url+'clients';
	// });
	// 
	// $("#clients_collapse").click(function() {
	// 	if(!$('.clients_content').is(':animated')){
	// 		var relative = clients_scroller.css("top");
	// 		clients_scroller.jScrollPaneRemove();
	// 		clients_scroller.css("position", "relative");
	// 		clients_scroller.css("top", relative);
	// 		clients_scroller.css("height", 'auto');
	// 		$('.client_content').hide();
	// 		clients.animate({height: "240px"}, "slow");
	// 		clients.removeClass("clientexpanded");
	// 		clients.find('.h2').animate({height:'185px'}, "slow");
	// 		$('#clients_collapse').hide('slow');
	// 		setTimeout(function() {
	// 			var position = parseInt($(".client.active:last").position().top);
	// 			var clients_scroller = $('#clients .scroller');
	// 			clients_scroller.css("height", $('#clients .h2').css('height'));
	// 			clients_scroller.jScrollPane({showArrows: true});
	// 			clients_scroller[0].scrollTo(position);
	// 		}, 800);
	// 	}
	// });
	/*----------------------------------------
	|	end CLIENTS
	*/
	
	/*----------------------------------------	
	|	NEWS
	------------------------------------------*/
	//Will wrote this bit.
	
	$(".colorbox").colorbox({
		opacity: .7
	});
	
	$("#news_collapse").click(function () {
		var news = $('#news');
		news.find(".scroller").jScrollPaneRemove();
		news.find(".h3").animate({height: "342px"}, "slow");
		news.animate({height: "370px"}, "slow");
		news.find(".news_container").animate({height: "305px"}, "slow");
		news.find(".news_item .news_content").slideUp(900);
		news.find(".news_item .news_description").slideDown(900);
		$("#news_collapse").hide("slow", function(){
			$("#news .scroller").jScrollPane({showArrows: true});
		});
	});
	$(".news_item").click(function () {
		var myid = $(this).attr("id");
		myid = '#' + myid;
		if(!$(myid).find("div").is(":animated")){
			var news = $('#news');
			scroll_to_include(news.offset().top);
			var top = news.find(".scroller").css("top");
			news.find(".scroller").jScrollPaneRemove();
			news.find(".scroller").css({ "position" : "relative", "top" : top, "height" : "auto"});
			news.find(".h3").animate({height: "472px"}, "slow");
			news.animate({height: "500px"}, "slow");
			setTimeout(function() {
				$("#news .news_container").animate({height: "436px"}, 3);
			}, 900);
			$("#news_collapse").show("slow");
			$(myid).find(".news_description:visible, .news_content:visible").slideUp(1000, function() {
				$("#news .scroller").jScrollPane({showArrows: true});
			});
			$(myid).find("div:hidden").slideDown(1000, function() {
				$("#news .scroller").jScrollPane({showArrows: true});
			});
		}
	});
	
	/*----------------------------------------
	|	end NEWS
	*/
	
	/*----------------------------------------	
	|	SCC RADIO
	------------------------------------------*/
	//Taryn wrote this bit.
	
	// update the stream to keep track of air status and playing track
	$("#player").everyTime("12s", "update", function() {
		$.ajax({
			url: base_url + "scc_radio/stream_info",
			type: "POST",
			success: function(html) {
				$("#stream_info").html(html);
			}
		});
	});
	
	// display and animate the equalizer based on stream and playing status
	$("#stream").click(function() {
		var stream = $(this);
		var equalizer = $("div#equalizer");
		if($(this).hasClass("paused")) {;
			stream.removeClass("paused");
			stream.addClass("playing");
			
			equalizer.attr("style", "visibility: visible");
			equalizer.everyTime(0, "play", function() {
				var width = Math.floor(Math.random() * 61);
				
				stream.animate({width: width + "px"}, 130);
			});
		} else {
			// stop and hide the equalizer
			equalizer.stopTime("play");
			equalizer.stop();
			equalizer.attr("style", "visibility: hidden");
			
			stream.removeClass("playing");
			stream.addClass("paused");
		}
	});
	
	/*----------------------------------------
	|	end RADIO
	*/
	
	/*----------------------------------------	
	|	THE WORK
	------------------------------------------*/
	//Jesse wrote this bit.
	$("#work_items")
		.before('<div id="work_nav">')
		.cycle({ 
		fx:     'fade', 
		speed:   1200, 
		timeout: 12641,
		next: '#work_next',
		prev: '#work_prev'
	});
	
	var work_item = $(".work_item");
	var work_items = $("#work_items");
	var work = $("#work");
	var work_collapse = $('#work_collapse');
	var work_h2 = $("#work .h2");
	var work_nav2 = $("#work_nav2");
	var work_item_anchor = $(".work_item a");
	var work_item_title = $("span.work_title");
	
	work_item_anchor.click(function(e) {
		if(!work.is('.expanded')) {
			e.preventDefault();
		}
	});
	
	work_item.click(function() {
		expand_work();
	});
	
	work_item_title.click(function() {
		expand_work();
	});
	
	work_collapse.click(function() {
		work_nav2.animate({height: "0px"}, "normal");
		work_h2.animate({height: "213px"}, "slow");
		work_items.animate({height: "185px"}, "slow");
		work.animate({height: "240px"}, "slow");
		work_collapse.hide('slow');
		work.removeClass('expanded');
	});
	
	function expand_work() {
		if(!work.is('.expanded')){
			work_h2.animate({height: "343px"}, "slow");
			work_items.animate({height: "314px"}, "slow", function() {
				work_nav2.animate({height: "20px"}, "normal");
			});
			work.animate({height: "370px"}, "slow");
			work_collapse.show('fast');
			scroll_to_include(work_h2.offset().top + 370);
			work.addClass('expanded');
		}
	}
	
	/*----------------------------------------
	|	end THE WORK
	*/
	
	/*----------------------------------------	
	|	CASE STUDIES
	------------------------------------------*/
	//Will wrote this bit.
	var case_studies = $('#case_studies');
	var case_names = $('#case_names');
	var case_container = case_names.find('.case_container');
	var video = case_studies.find('#video');
	var thumb_blurb = case_studies.find('#thumb_blurb');
	var case_expand_link = $('#case_expand');
	var case_collapse_link = $('#case_collapse');
	
	//functions to set the stage
	function reset_case_names () {
		case_names.find('h4').each(function() {
			$(this).removeAttr("class");
		})
	}
	function switch_to_case (id, reset) {
		reset_case_names();
		$('#case_'+id).addClass('activeCase');
		thumb_blurb.find('.activeCase').each(function() {
			var activeCase = $(this);
			activeCase.removeClass('activeCase');
			activeCase.fadeOut('fast', function(){
				if (case_studies.hasClass('expanded')) {
					video.fadeOut('fast', function(){
						var flv = $('#case_flv_'+id).html();
						// the embed only works if the whole thing is written with the html method
						video.html("<embed src=\"" + base_url + "public/player.swf\" allowscriptaccess=\"always\" allowfullscreen=\"true\" flashvars=\"file=" + base_url + "public/case_studies/" + flv + "&skin=" + base_url + "public/scc_skin.swf&autostart=true\" />");
						video.fadeIn("slow");

						var edit = $('#case_blurb_'+id);
						edit.slideDown('slow', function(){
							scroll_to_include(case_collapse_link.offset().top+50);
						});
						edit.addClass('activeCase');

					})
				}
				else {
					$('#case_thumb_'+id).each(function(){
						var edit = $(this);
						edit.fadeIn('', function () {
						});
						edit.addClass('activeCase');
					});
					if(reset){
						case_container.removeAttr('style');
					}
					arrange_titles(id);
				}
			});
		});
	}
	function arrange_titles(id) {
		first_cs_element = case_names.find('h4:first');
		last_cs_element = case_names.find('h4:last')
		
		// last_title_id = case_names.find('h4:last').attr("id");
		// last_title_value = case_names.find('h4:last').text();
		first_title_id = case_names.find('h4:first').attr("id");
		first_title_value = case_names.find('h4:first').text();
		
		//first_cs_element.remove();
		
		first_cs_element.slideUp("normal", function() { first_cs_element.remove(); });
		last_cs_element.after('<h4 id="'+first_title_id+'">'+first_title_value+'</h4>');	
		
		//position = 16-$('#case_'+id).position().top;
		//case_container.animate({top: position+'px'}, "slow");
	}
	function case_expand (id) {
		case_container.removeAttr('style');
		case_studies.addClass('expanded');
		var activeCaseClass = $('#case_'+id).attr("class");
		var thumb_blurb_fade = '';
		if(activeCaseClass == '') {
			switch_to_case(id);
			thumb_blurb_fade = thumb_blurb.find('.thumb');
		} else {
			thumb_blurb_fade = thumb_blurb.find('.thumb.activeCase');
		}
		thumb_blurb_fade.fadeOut('', function(){
			var flv = $('#case_flv_'+id).html();
			video.show("slow", function() {
					var edit = $('#case_blurb_'+id);
					edit.slideDown('slow', function(){
						scroll_to_include(case_container.offset().top+case_container.height()+30);
					});
					edit.addClass('activeCase');
				// the embed only works if the whole thing is written with the html method
				video.html("<embed src=\"" + base_url + "public/player.swf\" allowscriptaccess=\"always\" allowfullscreen=\"true\" flashvars=\"file=" + base_url + "public/case_studies/" + flv + "&skin=" + base_url + "public/scc_skin.swf&autostart=true\" />");
				case_expand_link.hide();
				case_collapse_link.show();
			});
		});
	}
	function case_collapse (id) {
		case_studies.removeClass('expanded');
		video.find('embed').fadeOut('', function() {
			video.hide('slow', function() {
				case_collapse_link.hide();
				case_expand_link.show()
				switch_to_case(id);
			});
		});
	}
	function case_cycle (element) {
		setTimeout(function () {
			if(!element.hasClass('expanded') && !element.hasClass('hover')) {
				var id = /\d+/.exec($('#case_names .activeCase').next().attr('id'));
				if(id != null) {
					switch_to_case(id, false);
				}
				else {
					var id = /\d+/.exec($('#case_names h4:first').attr('id'));
					switch_to_case(id, true);
				}
			}
			case_cycle(element);
		}, 10283);
	}
	
	//code to do the deed.
	case_cycle(case_studies);
	case_studies.mouseover(function () {
		case_studies.addClass('hover');
	});
	case_studies.mouseout(function () {
		case_studies.removeClass('hover');
	});
	case_names.find('h4').live("click", function () {
		//switch_to_case(/\d+/.exec($(this).attr("id"))[0]);
		case_expand(/\d+/.exec($(this).attr("id"))[0]);
	});
	$('#thumb_blurb .thumb, #case_expand').click(function(){
		case_expand(/\d+/.exec(case_names.find('.activeCase').attr("id"))[0]);
	});
	$('#case_studies_link').click(function(){
		case_expand(/\d+/.exec(case_names.find('.activeCase').attr("id"))[0]);
	});
	$('#case_collapse').click(function(){
		case_collapse(/\d+/.exec(case_names.find('.activeCase').attr("id"))[0]);
	});
	/*----------------------------------------
	|	end CASE STUDIES
	*/

	/*----------------------------------------	
	|	THINK AGAIN
	------------------------------------------*/
	//Will wrote this bit.
	var currentMargin;
	var mantrasHeight;
	function animateMantras(){
		setTimeout(function () {
			var mantrasUL = $('#mantrasUL');
			mantrasHeight = parseInt(mantrasUL.css('height'));
			currentMargin = parseInt(mantrasUL.css('marginTop'));
			mantrasUL.animate({"marginTop": currentMargin - 250 + "px"}, "slow", function () {
				if(mantrasHeight + currentMargin < 500) {
					mantrasUL.removeAttr('style');
					mantrasUL.css({'marginTop': '290px'});
				}
			});
			animateMantras();
		}, 2165);
	}
	animateMantras();
	$('#think_again').click(function () {
		window.location=base_url+"thinkagain";
	})
	/*----------------------------------------
	|	end THINK AGAIN
	*/
});