var cur = 0;
$(window).load(function(){
	$("#wrap").css("visibility","visible").animate({"opacity":"1"});
	var wid = parseInt($("section").css("width"));
	$("section").scrollLeft(wid).animate({scrollLeft: 0}, 1000);
});

$("#dwn > a").click(function(){
	$("#popover").css({"margin-top":"-70px", "opacity":"0", "display":"block"}).animate({"margin-top":"-84px", "opacity":1},200).hover(function(){$(this).show();},function(){});;
	return false;
}).parent().hover(function(){},function(){
	$("#popover").fadeOut();
});

$("#popover a:last").attr("href","mailto:drew@drewwilson.com");

var delay = false;
function feat_delay(num){ 
	if(delay) { window.clearTimeout(delay); }
	delay = window.setTimeout(function() { $("#featured div.details").addClass("off"); window.clearTimeout(delay); }, num);
}
feat_delay(1000);

var date_elem = $("#date time"),
	featured_elem = $("#featured");

function click_item(that, d, featured_elem){
	var title = $("img", that).attr("alt"),
		link = $("img", that).attr("data-link"),
		url = $("img", that).attr("data-url"),
		img = $("img", that).attr("src"),
		date = $("img", that).attr("data-date");
	featured_elem.find("div.img img").attr("src",img).end().find("a.perma").attr("href","http://⚡.la/"+link).end().find("a.perma span").html(link).end().find("h3 a").attr("href",url).html(title);
	date_elem.html(date);
	if(d){
		featured_elem.find(".details").removeClass("off").end().mouseover();
		feat_delay(2000);
	}
}

$("#projects a").click(function(d){
	var ind = $("#projects a").index($(this)),
		math = parseInt($(this).position()["left"]) - (parseInt($(window).width())/2)+70,
		that = this;
	change_frames = false;
console.log(math);
	$("section").stop().animate({"scrollLeft":math},300,function(){
		click_item(that, true, featured_elem);
		change_frames = true;
	});
	return false;
});

$("#leftnav").click(function(){
	var e = $.Event("keydown");
	e.which = 40;
	e.keyCode = 40;
	$(document).trigger(e);	
});

$("#rightnav").click(function(){
	var e = $.Event("keydown");
	e.which = 38;
	e.keyCode = 38;
	$(document).trigger(e);
});

$("section").scroll(function(){
	var win = parseInt($(window).width()),
		total = $("#projects a").length,
		middle = Math.floor(total / 2),
		pos = $("section").scrollLeft()+69,
		math = Math.round(pos / 182) ,
		x = math;
	
	if(math > x) { 
		x++; 
	} else if(math < x) {
		x--;
	}
	cur = (math <= 0) ? 0 : math-1;
	var item = $("#projects a").eq(cur);
	if(change_frames && cur < total){
		click_item(item, true, featured_elem);
	}	
}); 

/* Observe keypresses */			
var curKey = false,
	total_items = $("#projects a").length,
	change_frames = true;
$(document).keydown(function(event){
	curKey = event.keyCode;
	if(event.keyCode == 38 || event.keyCode == 39) { //up OR right
		event.preventDefault();
		if(cur < (total_items-1)){
			cur = cur+1;
		}
		$("#projects a:eq("+cur+")").click();
	} else if(event.keyCode == 40 || event.keyCode == 37) { //down OR left
		event.preventDefault();
		if(cur > 0){
			cur = cur-1;
		}
		$("#projects a:eq("+cur+")").click();
	}
	return curKey;
}).keyup(function(event){
	curKey = false;
	return curKey;
});
