$(document).ready(function() {
	//Speed of the slideshow
	var speed = 5000;
	
	//You have to specify width and height in #slider CSS properties
	//After that, the following script will set the width and height accordingly
	$('#mask-gallery, #gallery li').width($('#slider').width());	
	$('#gallery').width($('#slider').width() * $('#gallery li').length);
	$('#mask-gallery, #gallery li, #mask-excerpt, #excerpt li').height($('#slider').height());
	
	//Assign a timer, so it will run periodically
	var run = setInterval('newsscoller(0)', speed);	
	
	$('#gallery li:first, #excerpt li:first').addClass('selected');

	//Pause the slidershow with clearInterval
	$('#btn-pause').click(function () {
		clearInterval(run);
		return false;
	});

	//Continue the slideshow with setInterval
	$('#btn-play').click(function () {
		run = setInterval('newsscoller(0)', speed);	
		return false;
	});
	
	//Next Slide by calling the function
	$('#btn-next').click(function () {
		newsscoller(0);	
		return false;
	});	

	//Previous slide by passing prev=1
	$('#btn-prev').click(function () {
		newsscoller(1);	
		return false;
	});	
	
	//Mouse over, pause it, on mouse out, resume the slider show
	$('#slider').hover(
	
		function() {
			clearInterval(run);
		}, 
		function() {
			run = setInterval('newsscoller(0)', speed);	
		}
	); 	  
});

function newsscoller(prev) {

	//Get the current selected item (with selected class), if none was found, get the first item
	var current_image = $('#gallery li.selected').length ? $('#gallery li.selected') : $('#gallery li:first');
	var current_excerpt = $('#excerpt li.selected').length ? $('#excerpt li.selected') : $('#excerpt li:first');

	//if prev is set to 1 (previous item)
	if (prev) {
		
		//Get previous sibling
		var next_image = (current_image.prev().length) ? current_image.prev() : $('#gallery li:last');
		var next_excerpt = (current_excerpt.prev().length) ? current_excerpt.prev() : $('#excerpt li:last');
	
	//if prev is set to 0 (next item)
	} else {
		
		//Get next sibling
		var next_image = (current_image.next().length) ? current_image.next() : $('#gallery li:first');
		var next_excerpt = (current_excerpt.next().length) ? current_excerpt.next() : $('#excerpt li:first');
	}

	//clear the selected class
	$('#excerpt li, #gallery li').removeClass('selected');
	
	//reassign the selected class to current items
	next_image.addClass('selected');
	next_excerpt.addClass('selected');

	//Scroll the items
	$('#mask-gallery').scrollTo(next_image, 800);		
	$('#mask-excerpt').scrollTo(next_excerpt, 800);					
	
}

Cufon.replace('h2, h3, h4, h5, h6, #navigation li', { hover:true, fontFamily: 'Lubalin' });
Cufon.replace('#excerpt li, .dinparagraph', { hover:true, fontFamily: 'trade-gothic' });
Cufon.replace('.box h4, .quotesource, .hometitle, .casetext h3, .missingheading h1', { hover:true, fontFamily: 'ziggurat' });

$(document).ready(function(){
	//Switch the "Open" and "Close" state per click
	$("#trigger").toggle(function(){
		$(this).addClass("active");
		}, function () {
		$(this).removeClass("active");
	});

	//Slide up and down on click
	$("#trigger").click(function(){
		$(this).next("#footer").slideToggle("slow");
	});
	
	//Full Caption Sliding (Hidden to Visible)
	$('.boxgrid.captionfull').hover(function(){
		$(".cover", this).stop().animate({top:'88px'},{queue:false,duration:600});
		$("img", this).fadeTo("slow", .70);			
	}, function() {
		$(".cover", this).stop().animate({top:'188px'},{queue:false,duration:600});
		$("img", this).fadeTo("slow", 1);
	});
	
  //Testimonial Slideshow
  var currentPosition = 0;
  var slideWidth = 578;
  var slides = $('.slide');
  var numberOfSlides = slides.length;

  // Remove scrollbar in JS
  $('#slidesContainer').css('overflow', 'hidden');

  // Wrap all .slides with #slideInner div
  slides
    .wrapAll('<div id="slideInner"></div>')
    // Float left to display horizontally, readjust .slides width
	.css({
      'float' : 'left',
      'width' : slideWidth
    });

  // Set #slideInner width equal to total width of all slides
  $('#slideInner').css('width', slideWidth * numberOfSlides);

  // Insert controls in the DOM
  $('#slideshow')
    .prepend('<div class="control" id="leftControl"><span href="#">Clicking moves left</span></div><div id="hiddenLeftControl"><span href="#">Clicking moves left</span></div>')
    .append('<div class="control" id="rightControl"><span href="#">Clicking moves right</span></div><div id="hiddenRightControl"><span href="#">Clicking moves right</span></div>');

  // Hide left arrow control on first load
  manageControls(currentPosition);

  // Create event listeners for .controls clicks
  $('.control')
    .bind('click', function(){
    // Determine new position
	currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
  /*
  if ($(this).attr('class')=='control rightControl') {
    currentPosition = currentPosition+1;    
  } else if ($(this).attr('class')=='control leftControl') {
    currentPosition = currentPosition-1; 
  }
  */
    
	// Hide / show controls
    manageControls(currentPosition);
    // Move slideInner using margin-left
    $('#slideInner').animate({
      'marginLeft' : slideWidth*(-currentPosition)
    });
  });

  // manageControls: Hides and Shows controls depending on currentPosition
  function manageControls(position){
    // Hide left arrow if position is first slide
	if(position==0){ $('#leftControl').animate({'opacity': 0}, 650); $('#hiddenLeftControl').show(); $('#leftControl').hide(); } else{ $('#leftControl').animate({'opacity': 1}, 650); $('#leftControl').show(); $('#hiddenLeftControl').hide(); }
	// Hide right arrow if position is last slide
    if(position==numberOfSlides-1){ $('#rightControl').animate({'opacity': 0}, 650); $('#hiddenRightControl').show();  $('#rightControl').hide(); } else{ $('#rightControl').animate({'opacity': 1}, 650); $('#hiddenRightControl').hide();  $('#rightControl').show(); }
  }	

	$(".port").fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'			
	});  	

  /*
  $('p.cta-button input')
    .css({ 'backgroundPosition': '0 0' })
	.hover(
      function(){
        $(this).stop()
          .animate({
            'opacity': 0
          }, 650);
	  },
	  function(){
        $(this).stop()
          .animate({
            'opacity': 1
          }, 650);
	  }
	);
	*/
	
  $('.contact_btn a')
    .css({ 'backgroundPosition': '0 0' })
	.hover(
      function(){
        $(this).stop()
          .animate({
            'opacity': 0
          }, 650);
	  },
	  function(){
        $(this).stop()
          .animate({
            'opacity': 1
          }, 650);
	  }
	);
	
  $('.twittertab a')
    .css({ 'backgroundPosition': '0 0px' })
	.hover(
      function(){
        $(this).stop()
          .animate({
            'opacity': 0
          }, 650);
	  },
	  function(){
        $(this).stop()
          .animate({
            'opacity': 1
          }, 650);
	  }
	);	
	
  $('#leftControl span')
    .css({ 'backgroundPosition': '0 0' })
	.hover(
      function(){
        $(this).stop()
          .animate({
            'opacity': 0
          }, 650);
	  },
	  function(){
        $(this).stop()
          .animate({
            'opacity': 1
          }, 650);
	  }
	);	
	
  $('#rightControl span')
    .css({ 'backgroundPosition': '0 0' })
	.hover(
      function(){
        $(this).stop()
          .animate({
            'opacity': 0
          }, 650);
	  },
	  function(){
        $(this).stop()
          .animate({
            'opacity': 1
          }, 650);
	  }
	);	
	
  $('.logo a')
    .css({ 'backgroundPosition': '0 0' })
	.hover(
      function(){
        $(this).stop()
          .animate({
            'opacity': 0
          }, 650);
	  },
	  function(){
        $(this).stop()
          .animate({
            'opacity': 1
          }, 650);
	  }
	);		
			
});	
