/*
JS file for Nyhus
*/

$(document).ready(function() {
	var url = 'http://twitter.com/status/user_timeline/nyhus.json?include_rts=true&include_entities=true&callback=twitterCallback2&count=3';

	var script = document.createElement('script');
	script.setAttribute('src', url);
	document.body.appendChild(script);

  $('#home #carousel').jcarousel({scroll: 3, auto: 8, wrap: 'circular', initCallback: mycarousel_initCallback});
  $('#our-work #carousel').jcarousel({scroll: 4, auto: 8, wrap: 'circular', initCallback: mycarousel_initCallback});
  
  if ($("#whats-new #carousel li").length > 3) {
    $('#whats-new #carousel').jcarousel({scroll: 4, auto: 8, wrap: 'circular', initCallback: mycarousel_initCallback});
  }
  
  $(".jcarousel-next").css("display","");
  $(".jcarousel-prev").css("display","");
  
  // pause carousel autoscrolling on user interaction
  function mycarousel_initCallback(carousel) {
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
  };
  
  $(function(){
    $("#slides").slides({
      effect: 'fade',
  	  play: 8000,
  	  pauseBtn: true,
  	  crossfade: true,
  	  fadeSpeed: 500,
  	  generateNextPrev: true,
  	  paginationClass: 'nav-pagination'
    });
  });
  
  if ($(".section-page-banner h1").length > 0) {
    var html = $(".section-page-banner h1").html();
    var split = html.split(" ");
    if (split.length > 1) {
        split[split.length - 1] = "<b>" + split[split.length - 1] + "</b>"
        $(".section-page-banner h1").html(split.join(" "));
    }
  }

  var split_at = '.grid.bordered blockquote';
  $(split_at).each(function() {
    $(this).add($(this).nextUntil(split_at)).wrapAll("<article/>");
  });
  
  if ($(".nav-page").length > 0) {
    $(".current-page-ancestor > ul:eq(0)").clone().appendTo($(".nav-page"));
    
    if ($("#case-study-nav").length > 0) {
      $(".nav-page li:eq(0) a").after($("#case-study-nav"));
      $("#case-study-nav").css("display", "");
      $("#case-study-nav").css("width", $("#case-study-nav .column").size() * 140);
      
      var currentPage = $("body").attr("id").replace(/-/g,' ');
      if (currentPage == 'case studies') {
        currentPage = $("#case-study-nav a:first-child").attr("title").toLowerCase();
      }
      var prevLink = null;
      var nextLink = null;
 
      $("#case-study-nav a").each(function(index) {
        if ($(this).attr("title").toLowerCase() == currentPage) {
          if (index > 0) {
            prevLink = $("#case-study-nav a").eq(index-1).attr("href");
            $(".nav-pagination .alignleft").html("<a href='" + prevLink + "'>« Previous</a>");
          }
          if ($("#case-study-nav a").eq(index+1).length > 0) {
            nextLink = $("#case-study-nav a").eq(index+1).attr("href");
            $(".nav-pagination .alignright").html("<a href='" + nextLink + "'>Next »</a>");
          }
        }
      });
    }
    
    if (($("body#our-team").length > 0) || ($(".section-left.team").length > 0)) {
      var currentPage = $("body").attr("id").replace(/-/g,' ');
      if (currentPage == 'our team') {
        currentPage = $(".section-team a:first-child").attr("title").toLowerCase();
      }
      var prevLink = null;
      var nextLink = null;
      
      $(".section-team a").each(function(index) {
        if ($(this).attr("title").toLowerCase() == currentPage) {
          if (index > 0) {
            prevLink = $(".section-team a").eq(index-1).attr("href");
            $(".nav-pagination .alignleft").html("<a href='" + prevLink + "'>« Previous</a>");
          }
          if ($(".section-team a").eq(index+1).length > 0) {
            nextLink = $(".section-team a").eq(index+1).attr("href");
            $(".nav-pagination .alignright").html("<a href='" + nextLink + "'>Next »</a>");
          }
        }
      });
    }
  }
  
  
  if ($(".team").length > 0) {
    if ($("blockquote").length > 0) {   
      $(".copy header").insertBefore("blockquote");
    }
    else if ($(".copy img").length > 0) {
      $(".copy header").insertBefore("p:eq(1)");
    }
    else {
      $(".copy header").insertBefore("p:eq(0)");
    }
  }
  
});


function clearField(element, val) {
	if (element.value == val) {
		element.value = "";
	}
}

function setField(element, val) {
	if ((element.value == null) || (element.value == "")) {
		element.value = val;
	}
}

function twitterCallback2(twitters){
	if ($('#twitter-update-list')) {
		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 target="_blank" href="http://twitter.com/' + reply.substring(1) + '">' + reply.substring(1) + '</a>';
			});
			statusHTML.push('<li><a target="_blank" href="http://twitter.com/' + username + '/statuses/' + twitters[i].id + '">' + username + ': </a><span>' + status + '</span></li>');
		}
		$('#twitter-update-list').html(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';
	}
}


