$(function(){

	//SCROLLERS
	
	$('#nav-departmental a').click(function() {
		//event.preventDefault();
		$.scrollTo($(this).attr('href'), 1500, {easing:'easeOutQuad'});
	});
	
	//NEWS 
	  $('#news .items').slides({
		preload:false,
		preloadImage: 'img/loading.gif',		
		generatePagination: true,
		play: 10000,
		pause: 2500,
		hoverPause: false,
		animationComplete: function(current){
			
		},
		slidesLoaded: function() {
			$('#news .poster').fadeIn('medium');
			$('#news .caption').slideDown();
		}
	});  

	//JUST DELIVERED
	var optionsWidth = $('.options').outerWidth();	
	$('.options div').css('margin-right',-optionsWidth);
	
	$('.prev, .next').show();
	
	//Prevents glitchyness while content is loaded
	
	$('.scrollable').scrollable();
	scrollableapi = $('.scrollable').data("scrollable");	
		
	var currentFilter = $.cookie("justdelivered") ? $.cookie("justdelivered").split("/",1)[0] : 0;
	
	function openOptions(){
		$('.options').show();
		$('.options div').stop().animate({
			'margin-right':0
		})
	}

	function closeOptions() {
		$('.options div').stop().animate({'margin-right':-optionsWidth},400, killOptions);
	}

	function killOptions() {
		$('.options').hide();
	}	
	
	$('.options a').click(function(event) {
		event.preventDefault();	
		setJDState($(this).index(), 0);
		loadJDContent();
	});	
	
	//The blue options bar

	$('.options').mouseleave(closeOptions);
	
	//Set a cookie, and record the current filter/ item offset of history use
	function setJDState(filter, offset) {
		currentFilter = filter; 
		state = currentFilter + "/" + offset;
		$.cookie("justdelivered",state);
	}
	
	function getCurrentFilterLabel() {
		return $('.options a').eq(currentFilter).html();
	}	
	
	function loadJDContent(){
		$.ajax({
			  url: "home/justdelivered/" + getCurrentFilterLabel().toLowerCase(),
			  success: function(result){
				setJDContent(result);
			}
		});		
	}
	
	function setJDContent($html) {
		
		//Hiding the scrollable prevents glitchyness while we set it's content
		$('.scrollable .items').hide();
		
		//Having pulled in the content of the scrollable...
		scrollableapi.begin(0);
		$('.scrollable .items').html($html);
		
		//...We set the top of the previews below the scrollable, in order to hide them.
		$('.study-preview').css('top',$('.study-preview').height());
		
		//All set, we show again.
		$('.scrollable .items').show();

		initJDContent();
	}	
	
	//When all the images are preloaded, we're ready to finishing preparing the content
	function initJDContent() {
	
		$('.options a').removeClass('active');
		$('.options a').eq(currentFilter).addClass('active');	
	
		//Filter labeling
		$('.filter .current').html('<a href="#"><span class="legend">Filter:</span>' + getCurrentFilterLabel() + '</a>');
		Cufon.replace('.filter .current', {fontFamily:'font1'});

		$('.filter .current a').click(function(event) {
			event.preventDefault();
		});
		
		$('.filter .current a').mouseenter(function() {
			openOptions();
		});

		$('.study-preview img.thumbnail').fadeTo(0,.5);
	
		$('.study-preview a').click(function(event) {
			event.preventDefault();
			setJDState(currentFilter, $(this).parent().index());
			document.location.href=$(this).attr('href');
		});	
	
		$('.study-preview').mouseenter(function() {
			$(this).children('img.thumbnail').fadeTo(200,1);
			link = $(this).children('a');
			$(link).prev().animate({'margin-top': $(link).prev().height()/1.1},200);	
		});
		
		$('.study-preview').mouseleave(function() {
			$(this).children('img.thumbnail').fadeTo(200,.5);
			link = $(this).children('a');
			$(link).prev().animate({'margin-top': 0},200);
		});	
	
		$('img.thumbnail').imgpreload({
			all: function()
			{
				showJDContent();
			}
		});
	}
	
	function showJDContent() {

		$('.study-preview').each(function(idx) {
			$(this).delay( idx * 100).animate({ top: 0 },200);	
		});
		
	}	
	
	loadJDContent();
	
	//CONTACTS
	
	var totalTweets = 3;
	var currentTweet = 1;

	function showTweet() {
		$('.tweet ul li:nth-child(' + currentTweet + ')').fadeIn('slow').siblings().hide();
		currentTweet ++;
		if (currentTweet > totalTweets) {
			currentTweet = 1;
		}
	}	
	
	$(".tweet").tweet({
		username: "brandanddeliver",
		join_text: "auto",
		avatar_size: 32,
		count: totalTweets,
		template: "<p class=\"tweet-header\">{user} - {time}</p><p>{text}</p>",
		loading_text: "loading tweets..."
	});	
	
	$('.tweet').bind('loaded', function() {
		showTweet();
		tweetShowID = setInterval(showTweet, 10000);
    });
	
	//TESTIMONIALS
	
	function showTestimonial() {
		$("#testimonials-blurb ul li").hide();
		var current = "#testimonials-blurb ul li:nth-child(" + currentTestimonial + ")"
		$(current).show();	

		$(current + " q").css('right', -$(current + " q").outerWidth());
		$(current + " q").animate({
		  right: 0
		});	
		
		$(current + " p").hide();
		$(current + " p").fadeIn('fast');

		currentTestimonial++;
		if (currentTestimonial > totalTestimonials) {
			currentTestimonial = 1;
		}
	}	
	
	var totalTestimonials = $("#testimonials-blurb ul > li").size();
	var currentTestimonial = 1;
	
	showTestimonial();
	testimonialsShowID = setInterval(showTestimonial, 5000);
	
	//PRETTY PHOTO
	$(".image-gallery a").css({ opacity: 0.50 });
		
	$(".image-gallery a").mouseenter(function() {
	  $(this).css({ opacity: 1 });
	});
	
	$(".image-gallery a").mouseleave(function() {
	  $(this).animate({opacity:.50},250);
	});		
	
	$(".gallery a[rel^='prettyPhoto']").prettyPhoto({animation_speed:'normal', theme:'dark_square', overlay_gallery:false,  slideshow:false,  show_title: false, social_tools:'', deeplinking:false});

	//SERVICE LISTS

	$(".services .group").hide();	
	$(".services").show();

	$(".services .tab").click(function(event) {
		event.preventDefault();
		$(this).toggleClass('active');
		$($(this).attr('href')).slideToggle('300');
	});
	
	//SIGNIFICANT EVENT
	$('#significant-event').show();
	
	$('#significant-event').slides({
		preload:false,		
		preloadImage: 'img/loading.gif',		
		generatePagination: false,
		play: 5000,
		pause: 2500,
		hoverPause: false,
		next: 'slides-next',
		prev: 'slides-prev'
	}); 
	
});
