$(document).ready(function() {
	var $panels = $('#work-slider .scrollContainer-work > div');
	var $container = $('#work-slider .scrollContainer-work');
	var $scroll = $('#work-slider .scroll-work');
	$scroll.css('overflow', 'hidden');
	// if false, we'll float all the panels left and fix the width 
	// of the container
	var horizontal = true;
	
	// float the panels left if we're going horizontal
	if (horizontal) {
	  $panels.css({
	    'float' : 'left',
	    'position' : 'relative' // IE fix to ensure overflow is hidden
	  });
	  
	  // calculate a new width for the container (so it holds all panels)
	  $container.css('width', $panels[0].offsetWidth * $panels.length);
	}
	
	// handle nav selection
	function selectNav(){
	  $(this)
	    .parents('ul:first')
	      .find('li')
	        .removeClass('selected')
	      .end()
	    .end()
	    .parent('li')
			.addClass('selected');
	}
	
	$('#work-slider .slider-nav').find('a').click(selectNav);
	
	// go find the navigation link that has this target and select the nav
	function trigger(data) {
	  var el = $('#work-slider .slider-nav').find('a[href$="' + data.id + '"]').get(0);
	  selectNav.call(el);
		//Replace title with the company name
		$('#block-workshowcase-0 h2.comp-name').html($(el).siblings('.comp').html());
		$('#block-workshowcase-0 a.more').attr('href',$(el).siblings('.comp').attr('href'));
	}
		
	var scrollOptions = {
	  target: $scroll, // the element that has the overflow
	  //target: $container,
	  
	  // can be a selector which will be relative to the target
	  items: $panels,
	  force: true,
	  
		onAfter:function(elem){
			trigger(elem);
			var currentView = $(elem).attr("id");
			
			//Play the current flash panel and then once finished, goto the next slide
			$f($("#" + currentView + " a").attr('id')).play().onFinish(function(){this.unload();nextSlide();});
			
			var something; //break
		},
		onBefore:function(elem){var currentView = $(elem).attr("id");},

	  // allow the scroll effect to run both directions
	  axis: 'x',
	  
	  // duration of the sliding effect
	  duration: 1000,
	  
	  // easing - can be used with the easing plugin: 
	  // http://gsgd.co.uk/sandbox/jquery/easing/
	  easing: 'swing'
	};
	
	// apply serialScroll to the slider - we chose this plugin because it supports
	// the indexed next and previous scroll along with hooking in to our navigation.
	$('#work-slider').serialScroll(scrollOptions);
	
	// now apply localScroll to hook any other arbitrary links to trigger 
	// the effect
	
	//$.localScroll(scrollOptions);
	$('#work-slider').localScroll(scrollOptions);
	
	// select all links that has a .flv suffix and make them playable with flowplayer. 	
	$("a[href*=.flv]").flowplayer({src: "/sites/all/themes/vccp/flash/flowplayer-3.1.1.swf", wmode: 'opaque'}, {
		clip: {
			autoBuffering: true,
			autoPlay: false
		},
		plugins:{
			controls: { 
    		autoHide: "always" 
  		} 
		}
	});
	

	function nextSlide(){
		$scroll.trigger('next');
	}
});