//nav
//pause
$.fn.pause = function(duration) {
	$(this).stop().animate({ dummy: 1 }, duration);
	return this;
};

function mouseleft() {
	$("#resourcesbar").triggerHandler("mouseleave");
}

//open nav
$(document).ready(function(){
	var selectedInput = null;
	var barHeight = $("#resources").height() + 10;
	if(barHeight <= 50) { barHeight = 40; }
	$("#resourcesbar").bind("mouseenter",
		function () {
			$(this).stop().pause(60).animate({ "height": barHeight }, 550, "easeOutQuart" );
		});
	$("#resourcesbar").bind("mouseleave",
		function () {
			if (selectedInput == null) {
				$(this).stop().pause(60).animate({ height:"40px" }, 550, "easeOutQuart" );
			}
		});
	
	$('#slider').nivoSlider({
		effect: 'sliceDown',
		directionNav: false,
		pauseTime:6000,
		captionOpacity:1,
		startSlide:0, //Set starting Slide (0 index)
		slideshowEnd: function(){$('#slider').data('nivo:vars').stop = true;}
	});
	
	/*if (($.cookie('resources-popup') !== null && $.cookie('resources-popup') == '') || $.cookie('resources-popup') === null){
		$('#header').append('<div id="tooltipbox"><a href="#"></a></div>');
		$('#tooltipbox').bind("click",
			function() {
				$('#tooltipbox').remove();
				$.cookie('resources-popup','true', {expires: 10000 });
			});
		$("#resourcesbar").stop().pause(60).animate({ "height": barHeight }, 550, "easeOutQuart" );
	}*/
	
	CU.init();
		
	$(".customizePopUp").fancybox({
				'autoScale'			: true,
				'scrolling'			: 'no',
				'transitionIn'		: 'elastic',
				'transitionOut'		: 'elastic',
				'height' : 210,
				'type'				: 'iframe'
			});
	
	ct=0;
	list = [
        {
          service: 'twitter',
          user: 'clemsonstudents'
        }
      ];

      $("#lifestream").lifestream({
        limit: 5,
        list: list,
        feedloaded: function(){
          ct++;
          // Check if all the feeds have been loaded
          if( ct === list.length ){
            $("#lifestream li").each(function(){
              var element = $(this),
                  date = new Date(element.data("time"));
              element.append(' <abbr class="timeago" title="' + date.toISO8601(date) + '">' + date + "</abbr>");
            })
            $("#lifestream .timeago").timeago();
          }
        }
      });
});

// Accordian
$(function() {
	var icons = {
		header: "accordion-header",
		headerSelected: "accordion-headerselected"
	};
	$( "#accordion" ).accordion({
		fillSpace: true,
		icons: icons
	});
});

Date.prototype.toISO8601 = function(date) {
          var pad = function (amount, width) {
              var padding = "";
              while (padding.length < width - 1 && amount < Math.pow(10, width - padding.length - 1))
                  padding += "0";
              return padding + amount.toString();
          }
          date = date ? date : new Date();
          var offset = date.getTimezoneOffset();
          return pad(date.getFullYear(), 4)
              + "-" + pad(date.getMonth() + 1, 2)
              + "-" + pad(date.getDate(), 2)
              + "T" + pad(date.getHours(), 2)
              + ":" + pad(date.getMinutes(), 2)
              + ":" + pad(date.getUTCSeconds(), 2)
              + (offset > 0 ? "-" : "+")
              + pad(Math.floor(Math.abs(offset) / 60), 2)
              + ":" + pad(Math.abs(offset) % 60, 2);
      };


//COOKIES
jQuery.cookie = function (key, value, options) {

    // key and value given, set cookie...
    if (arguments.length > 1 && (value === null || typeof value !== "object")) {
        options = jQuery.extend({}, options);

        if (value === null) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? String(value) : encodeURIComponent(String(value)),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

