jQuery.fn.center = function(loaded) {
    var obj = this;
    body_width = parseInt($(window).width());
    body_height = parseInt($(window).height());
    block_width = parseInt(obj.width());
    block_height = parseInt(obj.height());
    
    left_position = parseInt((body_width/2) - (block_width/2)  + $(window).scrollLeft());
    if (body_width < block_width) { left_position = 0 };
    top_position = parseInt((body_height/2) - (block_height/2) + $(window).scrollTop());
    if (body_height < block_height) { top_position = 0 };
    
    if(!loaded) {
        
        obj.css({'position': 'absolute'});
        obj.css({'left': left_position, 'top': top_position});
        $(window).bind('resize', function() { 
            obj.center(!loaded);
        });
        $(window).bind('scroll', function() { 
            obj.center(!loaded);
        });
        
    } else {
        obj.stop();
        obj.css({'position': 'absolute'});
        obj.animate({'left': left_position, 'top': top_position}, 200, 'linear');
    }
}

Cufon.replace('#navigation > ul > li > a, .home .body p.register a, .inner .title h3, .inner .nav ul li, #sliders .nav p, .names ul li, .btn-learn, .btn-learn-cookie, .btn-register, .popup h2, .btn-orange .t, .form h2, .form label, .btn-back, #sliders .caption .prev, #sliders .caption .next, #sliders .caption p', { fontFamily : 'DINSchrift', hover : true });

var images = null;
$(function () {
	// nav dropdown
	$("#navigation ul li").hover(function() {
		$(this).css({ 'z-index' : 100 });
		$(this).find("> .dd").show();
		$(this).find("> a:eq(0)").addClass('hover');
	}, function() {
		$(this).css({ 'z-index' : 1 });
		$(this).find("> .dd").hide();
		$(this).find("> a:eq(0)").removeClass('hover');
		Cufon.replace('#navigation > ul > li > a', { fontFamily : 'DINSchrift', hover : true });
	});
	
	// inner tabs
	$('.inner .nav ul li a').removeClass('active').eq(0).addClass('active');
	$('.inner .body .holder').hide().eq(0).show();
	
	$('.inner .nav ul li a').click(function () {
		var index = $('.inner .nav ul li a').index(this);
		$('.inner .nav ul li a').removeClass('active').eq(index).addClass('active');
		$('.inner .body .holder').hide().eq(index).show();
		Cufon.replace('.inner .nav ul li', { fontFamily : 'DINSchrift', hover : true });
		return false;
	});
	
	// learn popup
	// $('.btn-learn').click(function () {
	// 	$('.overlay').fadeIn();
	// 	$('.popup').fadeIn().center();
	// 	return false;
	// });
	$('.overlay').click(function () {
		$('.overlay').fadeOut();
		$('.popup').fadeOut();
		return false;
	});
	$('.endoverlay').click(function () {
		$('.overlay').fadeOut();
		$('.popup').fadeOut();
		return false;
	});	

	$('.front-page-button').click(function () {
		$('.overlay').fadeIn();
		$('.popup').fadeIn().center();
		return false;
	});
	
	// test-tabs
	$('.testi-nav .names a').click(function () {
		var fileName = '';
		switch ($('.names ul li a').index(this)) {
			case 0: fileName = 'testi1.html'; break;
			case 1: fileName = 'testi2.html'; break;
			case 2: fileName = 'testi3.html'; break;
			case 3: fileName = 'testi4.html'; break;
			default: alert('Wrong Index!');
		}
		$('.testimonials').empty().load('ajax/' + fileName);
		return false;
	});
	
	// sliders
	$('.logos').jcarousel( {
		scroll: 1,
		auto: 5,
		wrap: 'both',
		vertical: true,
		// This tells jCarousel NOT to autobuild prev/next buttons
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
	$('.slider').jcarousel( {
		scroll: 1,
		auto: 0,
		wrap: 'both',
		// This tells jCarousel NOT to autobuild prev/next buttons
		buttonNextHTML: null,
		buttonPrevHTML: null,
	});
	$('.images').jcarousel( {
		initCallback: slider_initCallback,
		itemFirstInCallback: slider_firstInCallback,
		scroll: 1,
		auto: 2,
		wrap: 'both',
		// This tells jCarousel NOT to autobuild prev/next buttons
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
});



// $('#yourContainer').jcarousel({
// visible: 1,
// scroll: 1, 
// itemLoadCallback: {
//     onBeforeAnimation: mycarousel_fadeOut,
//     onAfterAnimation: mycarousel_fadeIn
// }
// });
// function mycarousel_fadeOut(carousel) {
// var JCcontainerID = carousel.clip.context.id;
// $('#' + JCcontainerID).fadeOut();
// }
// 
// function mycarousel_fadeIn(carousel) {
//     var JCcontainerID = carousel.clip.context.id;
//     $('#' + JCcontainerID).fadeIn();
// }
// 


// To end js errors and allow index page to call tweet js without problem.
function slider_initCallback (carousel) {
}

function slider_firstInCallback(carousel, item, idx, state) {
	$('.dots ul li a').removeClass('active');
	$('.dots ul li a').eq(idx-1).addClass('active');
	$('#sliders .caption p').html($('.images img').eq(idx-1).attr('alt'));
	Cufon.replace('#sliders .caption p', { fontFamily : 'DINSchrift', hover : true });
}
