// JQuery Extensions:
jQuery.extend(jQuery.easing, {
  easeOutCubic: function (x, t, b, c, d) {
    return c*((t=t/d-1)*t*t + 1) + b;
  }
});

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function toggleWrapper(value) {
	if (value == "expand") {
		$("#wrapper").animate({"width": "60em"},300,"easeOutCubic",function(){toggleAside(value)}); //,{"complete":toggleAside(value)});
		$("#footshell").animate({"width": "60em"},300,"easeOutCubic");
		//$("aside").fadeIn("fast");
		
	} else if (value == "collapse") {
		$("#wrapper").animate({"width": "37.5em"},300,"easeOutCubic");
		$("#footshell").animate({"width": "37.5em"},300,"easeOutCubic");
		//$("aside").fadeOut("fast");
		$("#side-toggle a").removeClass("expanded");
		$("#side-toggle a").addClass("collapsed");
	}
}

function toggleAside(value) {
	if (value == "expand") {
		$("#side-toggle a").removeClass("collapsed");
		$("#side-toggle a").addClass("expanded");
		$("aside").fadeIn("fast");
	} else if (value == "collapse") {
		$("aside").fadeOut("fast",function(){
			toggleWrapper("collapse");
		});
	}
}

var textareaValue = "You may type your response here.";
var c = 0;
var limit = 0;
var isAlt = false;
//Initialization
$(document).ready(function()
{
	limit = $('article').size();
	$(window).keydown(function(e) {
		if (e.keyCode == 18) {
			isAlt = true;
		}
	});
	$(window).keydown(function(e) {
		if (e.keyCode == 39 && isAlt) {
			if ($('#side-toggle a').hasClass("collapsed")) {
				toggleWrapper("expand");
				$('#side-toggle a').text("Collapse Tools");
				$.cookie("ui", "expanded", {expires: 90});
			}
		}
	});
	$(window).keydown(function(e) {
		if (e.keyCode == 37 && isAlt) {
			if ($('#side-toggle a').hasClass("expanded")) {
				toggleAside("collapse");
				$('#side-toggle a').text("Expand Tools");
				$.cookie("ui", "collapsed", {expires: 90});
			}
		}
	});
	$(window).keyup(function(e) {
		if (e.keyCode == 18) {
			isAlt = false;
		}
	});
	
	$(window).keydown(function(e) {
		if(e.keyCode == 32 && isAlt) {
			if (c == limit) c = 0;
			$('html,body').animate({
							scrollTop: ($('#a-'+c++).offset().top - 10)+'px'
			}, 500, 'easeOutCubic');
		}
	})
	
	$('aside').mouseenter(function() {
		$(this).fadeTo('fast', 1.0);
	})
	$('aside').mouseleave(function() {
		$(this).fadeTo('fast', 0.65);
	});
	
	// When user clicks the expand arrow...
	$('#side-toggle a').click(function()
	{
		// ...expand the wrapper to 60em
		if ($(this).hasClass("collapsed")) {
			toggleWrapper("expand");
			$(this).text("Collapse Tools");
			$.cookie("ui", "expanded", {expires: 90});
		} else if ($(this).hasClass("expanded")) {
			toggleAside("collapse");
			$(this).text("Expand Tools");
			$.cookie("ui", "collapsed", {expires: 90});
		}
		// ...make the side nav visible
		//$("aside").fadeIn("fast");
		// ...change the link's class/text/arrow to collapse
	});
	
	$("a").focus(function() {
		$(this).blur();
	});
	$("#comment").val(textareaValue);
	$("#comment").focus(function() {
		if ($(this).val() == textareaValue) {
			$(this).val("");
		}
	})
	$("#comment").blur(function() {
		if (jQuery.trim($(this).val()) == "") {
			$(this).val(textareaValue);
		}
	});
	
	$("#submit").mousedown(function() {
		$(this).toggleClass("pressed");
	}).mouseup(function() {
		$(this).toggleClass("pressed");
	}).mouseout(function() {
		$(this).removeClass("pressed");
	});
	
	$('#side-toggle a.expanded').click(function()
	{
		// ...expand the wrapper to 60em
		// ...make the side nav visible
		//$("aside").fadeIn("fast");
		// ...change the link's class/text/arrow to collapse
	});
});