$(document).ready(	
function()
{
	/* PopUp */
	$('#popup-mask').click(function () { closePopUp(); });
	$('#popup #chiudi').click(function () { closePopUp(); });

	$("#logo A").attr("go", $("#logo A").attr("href")).attr("href", "javascript:;");
	$("#logo A").click( function(event) {
		event.preventDefault();
		openPage($(this));
	});
    
	$(".nav A").each( function() {
		$(this).attr("go", $(this).attr("href")).attr("href", "javascript:;").addClass("goto");
	})
    
    $(".goto").live("click", function() { openPage($(this)); });
    
	$("#social A").fadeTo("fast", 0.6);
	$("#social A").mouseenter( function() { $(this).fadeTo("fast", 1); } ).mouseleave( function() { $(this).fadeTo("fast", 0.6); } );
	
	$("#inner").animate( {'height': $("#inside").outerHeight()}, 'fast', function() { $("#inner").css("overflow", "hidden"); $("#inside").css("position", "relative"); });
	loadTwitter();
	loadPage();
});

function openPage(el, moving)
{
	if ($(el).parent().parent().hasClass("topnav"))
    {
        $("#menu A").parent().removeClass("active");
        $(el).parent().addClass("active");
        $(".footernav").hide();
        if ($(el).attr("rel")!="") $("#"+$(el).attr("rel")).show();
    }
    if (moving!="horiz")
    $.get($(el).attr("go")+"?a=1&"+$(el).attr("param"), function(data) {
    	$("#temp").html(data);
	    height = -$("#inner").outerHeight();
	    $("#inside").append($("#temp").html());
	    $("#inner").animate( {'height': $("#temp").outerHeight()}, 700, 'easeOutCirc');
        $("#inside").animate( {'top': height }, 700, 'easeOutBounce', function() {
	    
	    	$("#inside").css("top", 0).html($("#temp").html());
	    	loadPage();
	    
	    });
    });
    else
    $.get($(el).attr("go")+"?a=1&"+$(el).attr("param"), function(data) {
    	$("#temp").html(data);
    	$("#inside").css("width", "1240px");
    	$("#inner .clear").hide();
	    width = -$("#inner").outerWidth();
	    $("#inside").append($("#temp").html());
	    $("#inside").animate( {'left': width }, 700, 'easeOutBounce', function() {
	    
	    	$("#inside").css("left", 0).html($("#temp").html());
	    	loadPage();
    		$("#inside").css("width", "620px");
    		$("#inner .clear").show();
	    });
    });
    
    $.get($(el).attr("go"), function(data) {
    	title = data.match(/<(\S+).*>(.*)<\/\1>/);
    	title = title[2];
    	//title
    	document.title = title;
    });
    
    return false;
}

function loadPage()
{
	$("#lastprj A").each( function() {
		if($(this).attr("go")=="") $(this).attr("go", $(this).attr("href")).attr("href", "javascript:;").addClass("goto");
	});
    
	$.colorbox.init();
	$("a.colorbox").colorbox({ current:"{current} di {total}" });
	
	$(".portfolio DIV.desc").click( function (event) {
		if (event.target.className != "what-i-like")
		{
			if (!$("A", $(this).parent()).hasClass("colorbox"))
			{
		        event.preventDefault();
				window.open($("A", $(this).parent()).attr("href"));
			}
			else { $("A", $(this).parent()).click(); }
		}
	});
	
	$(".portfolio DIV.desc A.what-i-like").click( function (event) {
		event.preventDefault();
		loadPopUp("", "", "<strong>Cosa mi &egrave; piaciuto?</strong><br /><br />"+$(this).next().html())
	});
    
    $(".portfolio DIV.hover").mouseenter( function (event) {
        $(".desc", $(this)).fadeTo('fast', 0.9);
	}).mouseleave( function (event) {
        $(".desc", $(this)).fadeTo('fast', 0);
	});
    
	$(".prev A, .next A").each( function() {
        to = $(this).attr("href");
        to = to.split("?");
		$(this).attr("go", to[0]).attr("href", "javascript:;");
        if (to.length>1) $(this).attr("param", to[1])
	}).click( function() { openPage($(this), "horiz"); });
}

function loadTwitter()
{
	$.getJSON("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=anija&count=1&callback=?", function(data) {
	    var tweets = $.map(data.results || data, function(item)
		{
			var retweet = (typeof(item.retweeted_status) != 'undefined');
            var retweeted_screen_name = retweet ? item.retweeted_status.user.screen_name : null;
            var tweet_raw_text = retweet ? ('RT @'+retweeted_screen_name+' '+item.retweeted_status.text) : item.text; // avoid '...' in long retweets
            
    	    var regexp = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?гхрсту]))/gi;
    	    tweet_raw_text = tweet_raw_text.replace(regexp,
    	                                      function(match) {
    	                                        var url = (/^[a-z]+:/i).test(match) ? match : "http://"+match;
    	                                        return "<a href=\""+url+"\" target=\"_blank\">"+match+"</a>";
    	                                      });

	        var regexp = /[\@]+(\w+)/gi;
	        tweet_raw_text = tweet_raw_text.replace(regexp,"@<a href=\"http://www.twitter.com/$1\">$1</a>");
	    	$("#content_twitter").html(tweet_raw_text);
		});
	});
}

/* PopUp */
function loadPopUp(title, url, data)
{	
	//Get the screen height and width  
	var maskHeight = $(document).height();  
	var maskWidth = $(window).width();  

	//Set height and width to mask to fill up the whole screen  
	$('#popup-mask').css({'width':maskWidth,'height':maskHeight});  
	  
	//transition effect   
	$('#popup-mask').fadeTo("fast",0.8);    

	//Get the window height and width  
	var winH = $(window).height();  
	var winW = $(window).width();  
	        
	//Set the popup window to center  
	$('#popup').css('top',  winH/2-100);
	$('#popup').css('left', winW/2-$('#popup').width()/2);
	
	$("#popup").show();
	$('#popup H3').html(title);
	if (url == '')	$('#popup #contentAjax').html(data);
	else			$('#popup #contentAjax').load(url+"/?a", function() {  });
}
function closePopUp()
{
	$('#popup #contentAjax').html("");
	$('#popup-mask').fadeOut("fast");
	$('#popup').hide();  
}

