
    (function($){
    
        // Case studies sliders
        $(document).ready(function($){
            
            // Set the category select to activate the slider
            var selector = '.embedded_media .categoryselector select';
            selector += ',.case_studies .categoryselector select';
            $(selector).change(function(event){
                event.preventDefault();
                var category_id = $(this).val();
                var category = category_id ? 'c'+category_id : false;
                
                var selector = '.embedded_media .items';
                selector += ',.case_studies .items';
                $(selector).listingsSlider(category);
            });
            
            // If the URL contains a category set the category select's value
            // and activate the slider 
            var array = window.location.href.split('#c');
            if (array[1]) { 
                $('.categoryselector select').val(array[1]);
                $('.items').listingsSlider('c' + array[1]);
            }
            
        });
        
        $.fn.listingsSlider = function(category) {
            
            return this.each(function() {
                
                var container = $(this);
                var items = container.children();
                
                items.each(function(){
                    
                    // Append all hrefs with the category. I.e. #c6
                    $('a', $(this)).each(function(){
                        $(this).unbind('click').click(function(){
                            if (category){
                                var href = $(this).attr('href');
                                //href = href.trim(container.attr('data-category')); BUG IN IE
                                $(this).attr('href', href += '#' + category);
                            }
                        });
                    });
                    
                    var element = $(this);
                    var show_item = element.hasClass(category);
                    
                    // Stop the current animation
                    element.stop(true);
                    
                    // Open items
                    if (
                        show_item
                        && element.css('display') == 'none'
                        || !category
                    ){
                        element.show('blind', { direction: "left" }, 'slow').dequeue().fadeTo('slow', 1);
                    
                    // Close items
                    } else if (
                        !show_item 
                        && element.css('display') == 'block'
                    ) {
                        element.hide('blind', { direction: "left" }, 'slow').dequeue().fadeTo('slow', 0);
                    }
                });
                
                // Retain the selected category
                container.attr('data-category', category);
                
            });
        };
        
        
    })(jQuery);
      




    // Snippets hover effects
    jQuery(document).ready(function($){
        $('.snippet').hover(function(){
            switch (true) {
                case $(this).hasClass('snippet2'):
                    var container_height = $(this).innerHeight();
                    var inner_div = $(this).children('div');
                    // The item's height must be calculated before it's placed out of view
                    // otherwise its height will be 0
                    inner_div.hide().css('top', 'auto');
                    var height = inner_div.height();
                    // Now move out of view and slide back into view
                    inner_div.css('top', container_height).show().animate({
                        top: container_height-height
                    }, 'fast');
                    break;
                    
                case $(this).hasClass('snippet3'):
                    break;
                
                default:
                    $(this).children('div').fadeIn();
                    break;
            }
        }, function(){
            switch (true) {
                case $(this).hasClass('snippet2'):
                case $(this).hasClass('snippet2'):
                    $(this).children('div').animate({
                        top: $(this).innerHeight()
                    }, 'fast');
                    break;
                
                case $(this).hasClass('snippet3'):
                    break;
               
                default:
                    $(this).children('div').fadeOut();
                    break;
            }
        });
        /*
        $('.snippet').live('mouseout', function(){
            if (
                !$(this).hasClass('snippet2')
                && !$(this).hasClass('snippet3')
            ) {
                $(this).children('div').hide();
            }
        });
        */
    });
	
	
	document.observe('dom:loaded', function(){
		var container = $$('div.dynamicheader')[0];
		if (container){
			
			container.left = 0;
			
			// Pause animation on mouseover, prevents a bug where the slider gets stuck halfway
			container.paused = false;
			container.observe('mouseover', function(){ container.paused = true; });
			container.observe('mouseout', function(){ container.paused = false; });
			
			container.select('ul li').each(function(li, key){
				
				li.observe('click', function(event){
					event.stop();
					if (container.locked) return false;
					container.locked = true;
					move_dynamic_header(container, li, key);
				}.bindAsEventListener());
			});
			
			setInterval(function(){
				if (container.paused) return false;
				
				container.select('li').each(function(li, key){
					if (li.hasClassName('active')){
						var next_li = li.next();
						if (next_li){
							move_dynamic_header(container, next_li, key+1); 
						} else {
							next_li = li.adjacent('li').first();
							move_dynamic_header(container, next_li, 0); 
						}
						throw $break;
					}
				});
			}, 7000);
		}
	});

	function move_dynamic_header(container, li, key)
	{
		var items = container.select('div.item');
		var item_width = items[0].getWidth();
		var inner = container.down('div.inner');
		
		var distance = key * item_width;
		var x = container.left - distance;
		
		// Store the current position
		container.left = (key * item_width);
		
		li.adjacent('li.active').invoke('removeClassName', 'active');
		li.addClassName('active');
		new Effect.Move(inner, {
			x: x,
			y: 0,
			mode: 'relative',
			duration: 0.5,
			afterFinish: function(){
				container.locked = false;
			}
		});
	}

