//cufon replaces
Cufon.replace('.inside > h1');
Cufon.replace('.inside > h3');
Cufon.replace('#footer ul li span');
Cufon.replace('#portfolio ul li span');

//resize
var resizeTimeout;
var pageSize=746;
var active_count=0;

$(window).bind('resize',function(){
    if (resizeTimeout) {
        clearTimeout(resizeTimeout);
    }
    resizeTimeout = setTimeout(reset_size,250);
});
function reset_size()
{
    pageSize=$(window).width()+746;
    $('#pages').css('width',(pageSize*12)+'px')
    $('#pages > div').css('width',pageSize+'px');
    navigate_to($.address.value())

    //update overflows
    var footer_overflow=$('#footer').offset().top-135-20;//20 for some padding at the bottom
    if (footer_overflow>0) {
        $('#content .inside').height(footer_overflow);
    }
}
function scroll_page(skip_animation)
{
    var page=$.address.value();
    var npage=page.toString().replace('/','');

    go_to_page(npage,skip_animation);
        
}
function go_to_page(npage,skip_animation)
{
	$('html, body').animate({scrollTop:0}, 'fast'); 
    if (!npage) {
        return;
    }
    active_count=0;
    var found_index;
    //find the page
    $('#pages .inside').removeClass('current_page');

    $('#pages > div').each(function(index) {
        if ($(this).attr('id')==npage) {
            $(this).find('.inside').addClass('current_page');
            var go_to_pixel=-pageSize*index;
            found_index=index;
            if (!skip_animation) {
                $('#pages').stop().animate({left:go_to_pixel+'px'});
            }else{
                $('#pages').css({left:go_to_pixel+'px'});
            }
         }
    });

    //update bottom current page
    var position_array=[30,105,180,260,375,447,468,495,518,543,567,592];
    $('#footer ul').stop().animate({backgroundPosition:position_array[found_index]+'px 0px'});
}
//bind address change
$.address.init(function() {
    reset_size();
    scroll_page(true);
}).change(function(event) {    
    scroll_page(false);
});

//ie6 fixes
var oldScrollY=0;
if ($.browser.msie && $.browser.version == 6){

    $(function() {
        $('#footer ul li a').bind('click',function(e) {
            e.preventDefault();
            go_to_page($(this).attr('href').replace('#/',''),false);
            return false;
        });
        checkIeSixScroll();
    });
    
}
function checkIeSixScroll()
{
    if ($('#wrapper') && $('#wrapper').scrollTop()!=oldScrollY) {
        if (typeof(IE7)!='undefined') {
            IE7.recalc();
            oldScrollY=$('#wrapper').scrollTop();
        }
    }
    
        setTimeout(checkIeSixScroll,1);

}

function navigate_to(page)
{
    
    //save the address
    $.address.value(page);
    scroll_page(false);
}

function retweet()
{
    active_count++;
    if (active_count<30) {
        $.getJSON('http://twitter.com/status/user_timeline/DanAtPage7.json?count=3&callback=?',function(data) {
           
           if (data.length>0) {
                //update main tweet
                //$('#twitter h3').html(data[0].text);
                Cufon.replace('h3');
                //update other tweets
                for (var i=0;i<3;i++) {
                    //console.log(data[i]);
                    var made_date=data[i].created_at.split(' ');
                    made_date=made_date[0] + " " + made_date[1] + " " + made_date[2];
					
					var twitter_text=data[i].text;					
					
					//from twitter's blogger.js
					twitter_text = twitter_text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
					  return '<a href="'+url+'" target="_blank">'+url+'</a>';
					});

                    $('#tweets li:eq('+i+') dt').html(twitter_text);
                    $('#tweets li:eq('+i+') dd').html(made_date);
                }
           }
       });
    }
   setTimeout(retweet,60000);
}
$(function(){
   retweet();

   $('a[rel="blank"]').attr('target','_blank');
});