// JavaScript Document

var phrase_height = 0;
var phrase_y = 0;
	
$(document).ready(function(){
	setBackground(items[current_item]["file"]);	
	var max_items = items.length;
	$("#arrows a.previous").click(function(event) {
		event.preventDefault();
		current_item--;
		if(current_item < 0)
		{
			current_item = max_items - 1;
		}
		createCookie("current_item",current_item,1);
		swapItems(items[current_item]);
	});
	$("#arrows a.next").click(function(event) {
		event.preventDefault();
		current_item++;
		if(current_item >= max_items)
		{
			current_item = 0;
		}
		createCookie("current_item",current_item,1);
		swapItems(items[current_item]);
	});
	
	phrase_height = ($("#phrase span").height() + 4) * $("#phrase span").length;
    phrase_y = 50;
	$("#content").css("top", phrase_height + phrase_y + 50); 
	//window.console.log(phrase_height);
	
	$(".project").hover(function() {
		$(this).children(".cover").css("display", "none");
	}, function() {
		$(this).children(".cover").css("display", "block");
	});
	
	$('.target_blank').click(function(){
		this.target = "_blank";
	});
	
	$("#totop").click(function() {
		nowScrolling = true;
		$(document).scrollTo( $("#content"), 1500, {axis:'y'} , {easing:'quadInOut'});
		$(this).oneTime(1500, function() {
			nowScrolling = false;
		});
	});	
});

function swapItems(item)
{
	changeBackground(item.file);
	$("#phrase").animate({opacity: 0}, 700, "easeOutCubic",function(){replaceText(item.phrase)});
}
function replaceText(phrase)
{
	$("#phrase").empty();
	for(line in phrase)
	{
		$("#phrase").append('<span class="' + phrase[line].bg + ' ' + phrase[line].color + ' ' + phrase[line].font + '">' + phrase[line].text + '</span><br clear="both" />');
	}
	phrase_height = ($("#phrase span").height() + 4) * $("#phrase span").length;
	phrase_y = 50;
	if (document.getElementById("content"))
		$("#content").animate({top:phrase_height + phrase_y}, 500, "easeOutCubic", fadeIn);
	else
		fadeIn();

}
function fadeIn()
{
		$("#phrase").animate({opacity: 1}, 700, "easeOutCubic");
}

function createCookie(name, value, days)
{
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime() + (1 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else 
    {
        var expires = "";
    }
    
    document.cookie = name + "=" + escape(value) + expires + "; path=/";
}
