/* Author: Stephen Fairbanks

*/

// resize bg-wrap when the window is moved
$(document).ready( function() {
	stretch_portal_content();
	$(window).resize( stretch_portal_content );
});

function stretch_portal_content() {
	$("#bg-wrap").width($(window).width()); // resize
	$("nav ul").width($(window).width()); // resize
}

// addClass
$(document).ready(function(){
	$("#home-news-articles article:nth-child(3)").addClass('last-home-article'); // add this class to 3rd article
});


// Resize .bg-wrap-changes height on document load
$(document).ready(function(){
	$(".bg-wrap-changes").height($(document).height()); // animate to opaque
	
});



// Slide in objects
$(document).ready(function(){
	//$("#get-in-touch").fadeIn(3000);
	$("#get-in-touch").animate({'margin-top': '0'}, 1000 );
	$("#background-changer").animate({'top': '0'}, 1000 );
});




// Hover Fade function (global)
$(document).ready(function(){
	$(".hover-fade img").hover(function(){
		$(this).stop().fadeTo(300, 0); // animate to transparent
		},function(){
		$(this).stop().fadeTo(300, 1); // animate to opaque
	});
});




// Nav Animation
$(document).ready(function(){
	$("nav ul li a").hover(function(){
		$("span", this).stop(false, true).animate({left: '5px'}, 200 );},function(){
		$("span", this).stop(false, true).animate({left: '-25px'}, 100 ); // animate to opaque
	});
});




// Home News Img Animation
$(document).ready(function(){
	$(".shift-left").hover(function(){
		$("img", this).animate({left: '-25px'}, 200 );},function(){
		$("img", this).animate({left: '0px'}, 100 ); // animate to opaque
	});
});

// Speech Bubble Animation
$(document).ready(function(){
	$(".home-box-cta").hover(function(){
		$(".home-speech-bubble").stop().fadeTo(300, 1).animate({top: '-80px'}, 200 );},function(){
		$(".home-speech-bubble").stop().fadeTo(300, 0).animate({top: '-70px'}, 100 ); // animate to opaque
	});
});



// Home Banner 'View More' Animation
$(document).ready(function(){
	$("#home-banner-wrap").hover(function(){
		$("#view-gallery").stop().animate({top: '0px'}, 200 );},function(){
		$("#view-gallery").stop().animate({top: '-350px'}, 100 ); // animate to opaque
	});
});


//Portfolio Arrow Animation
$(document).ready(function(){
	$(".portfolio-list-entry").hover(function(){
		$(".portfolio-arrow", this).stop().animate({bottom: '0px'}, 200 );},function(){
		$(".portfolio-arrow", this).stop().animate({bottom: '-50px'}, 100 ); // animate to opaque
	});
});

//Portfolio Article Animation
$(document).ready(function(){
	$(".portfolio-list-entry-img").hover(function(){
		$(".portfolio-list-article", this).stop().animate({top: '0px'}, 200 );},function(){
		$(".portfolio-list-article", this).stop().animate({top: '-230px'}, 100 ); // animate to opaque
	});
});



// Scroll the window
$(document).ready(function(){
	$('#social-links a').click(function(){
	$.scrollTo( '#main-footer', 2000, {ease: 'easeOutQuint'});
	}); 
});



// Top Sliding text
$(function() {
	$('#sliding-text').cycle({
		fx: 'scrollLeft',
		speed: 200,
		timeout: 4000,
		random: true,
		slideExpr: 'strong'
	});
});



// Main banner
$(function() {
	$('#banner-images').cycle({
		fx: 'scrollVert',
		speed: 300,
		timeout: 7000,
		pager: '#banner-nav',
		random: true
	});
});



// Portfolio images
$(function() {
	$('.large-image').cycle({
		fx: 'scrollLeft',
		speed: 300,
		timeout: 7000,
		slideExpr: 'img'
	});
});


// News slide
$(function() {
	$('.large-image-news').cycle({
		fx: 'scrollLeft',
		speed: 300,
		timeout: 5000,
		slideExpr: 'img'
	});
});


// Bg Wrap Changer
$(function() {
	$('#bg-wrap').cycle({
		fx: 'fade',
		slideExpr: '.bg-wrap-changes',
		speed: 1000,
		timeout: 16000,
		pager: '#background-options'
	});
});

// Get Twitter
function twitterCallback2(twitters) {
  var statusHTML = [];
  for (var i=0; i<twitters.length; i++){
    var username = twitters[i].user.screen_name;
    var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      return '<a href="'+url+'">'+url+'</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
    });
    statusHTML.push('<li><span>'+status+'</span> <a href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'" target="_blank">'+relative_time(twitters[i].created_at)+'</a></li>');
  }
  document.getElementById('twitter-loading').style.display = 'none';
  document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
}

function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}


$(document).ready(function(){
	$('#lastfm-list').lastFM({
		username: 'thathurtabit',
		apikey: '9bb1bbd8e10026d59b3caa9f4f16b12c',
		number: 4,
		//artSize: 'small',
		//noart: 'images/noartwork.gif',
		onComplete: function(){
			// on complete, hide loader, display list
			document.getElementById('lastfm-loading').style.display = 'none';
			document.getElementById('lastfm-list').style.display = 'block';
		}
	});
});



$(function(){
	// adding class to all even gallery-box 
	$('.portfolio-list-entry:odd').addClass('box-margin');
});

//Fade in Gallery in sequence
$(".portfolio-list-entry-img img").fadeInSequence(500, 100);


//Find first letter, apply span.
$(".blue-intro").each(function () {
      var el = $(this),
            text = el.html(),
            first = text.slice(0, 1),
            rest = text.slice(1);
      el.html("<span class='first-letter'>" + first + "</span>" + rest);
});

//Find first letter, apply span.
$(".news-intro").each(function () {
      var el = $(this),
            text = el.html(),
            first = text.slice(0, 1),
            rest = text.slice(1);
      el.html("<span class='news-first-letter'>" + first + "</span>" + rest);
});



