/**
 * Copyright Nick Obrien, all written code!
 * Please ask/notice for using..
 */
$(document).ready(function(){
	$.ajaxSetup ({   
		cache: false  
	});
	
    // Page init
    // Vertical align wrap
	alignWrap();

	$(window).resize(function() {
		alignWrap();
	});

	// Cufon replace
	Cufon.replace('h1, h2', {
		fontFamily: 'TheSans'
	});
    Cufon.replace('h1 em, h2 em', {
        fontFamily: 'TheSansI'
    });

	// Focus field that is requesting focus
	$('.need-focus').focus();
	
	// Focus field that is requesting focus
	$('div.project').backOpacity({opacity: '0.1'});
	
	// Tiktak the clock
	setInterval('tiktakTheClock()', 1000);

	// Close elements
	setInterval('$(".close-after-showed").hide(500)', 5000);

	// Buttons hover
	$('.button').hover(
		function () {
			$(this).animate({ opacity: 0.8 }, 200);
		}, 
		function () {
			$(this).animate({ opacity: 1 }, 200);
		}
	);
});

function tiktakTheClock()
{
	var tik_el = $('#tik');
	var tak_el = $('#tak');

	if (tik_el.css('display') != 'block')
	{
		tik_el.css('display', 'block');
		tak_el.fadeOut(500);
	}
	else
	{
		tak_el.css('display', 'block');
		tik_el.fadeOut(500);
	}

	return true;
}

function scrollToElement (element)
{
	var el_offset = $(element).offset();
	var el_offsetTop = el_offset.top; 
	
	$('html, body').animate({scrollTop:el_offsetTop}, 500);

	return false;
}

function alignWrap ()
{
    var wrap_el = $('#wrap');

    if (wrap_el)
    {
        var wrap_height = wrap_el.height();
        var window_height = $(window).height();
        var margin = 0;

        if (wrap_height < window_height)
            margin = parseInt((window_height-wrap_height)/2);

        // Set attributes and show wrap element
        wrap_el.css('marginTop', margin + 'px');
        wrap_el.show();

		return true;
    }

	return false;
}