/**
 * @author Vincent Helwig
 */
 
var current_section;
var support_canvas;
var incrementialNumber = 0;
 
$( document ).ready( function() {
	support_canvas = !!document.createElement('canvas').getContext;
	
	init();
	
	
 	if ( location.hash.length > 1 && $('#carroussel').find( location.hash ).attr('id') ) {
 		makeTransition( $('#carroussel').find( location.hash ).attr('id'), true );
 	} else {
 		makeTransition( $('#carroussel').find('section:last').attr('id'), true );
 	}
 	
 	$( window ).resize( function() {
		$('section.home_carroussel').each( function() {
		 	if ( current_section == $(this).attr('id') ) {
		 		var __textPosition = Math.max( -document.documentElement.clientWidth/2 - 60, -512 );
	 			$(this).find('.texte').attr('style', "opacity:1;margin-left: " + __textPosition + "px;" );
		  	}
		} );
	} );

	$('#carroussel .image').bind('swiperight',function( event, ui ) {
		getPreviousCarrousselItem();
	} );
	
	$('#carroussel .image').bind('swipeleft',function( event, ui ) {
		getNextCarrousselItem();
	} );

} );

function init() {
 	$('section.home_carroussel').each( function() {
 		$(this).attr('style', 'display: none' );
 		$('#carroussel nav').append("<div id='nav_" + $(this).attr('id') + "'><a onclick='makeTransition(\"" + $(this).attr('id') + "\");' href='#home_page'></a></div>");	
 	});
 	

 	/*
 	if ( support_canvas ) {
 		$('#carroussel').append("<canvas id='canvas_carroussel'></canvas>")
 		$('section.home_carroussel').each( function() {
 			$(this).remove();
 		});
 	}
 	*/
} 
 
function makeTransition( section_name, toRight ) {
	$(document).stopTime("carrousselTimer");
 	if ( current_section == section_name ) return;
	if ( toRight == undefined ) { toRight = true; }
 	current_section = section_name;
 	incrementialNumber++;
 	
	$('section.home_carroussel').each( function() {
	 	if ( section_name == $(this).attr('id') ) {
	 		
	 		// $('#carroussel nav').append( section_name );
	 		var __textPosition = Math.max( -document.documentElement.clientWidth/2 - 60, -512 );
	 		$('#nav_' + $(this).attr('id') ).addClass("selected");
	 		if ( toRight ) {
	 			$(this).attr('style', 'left:-' + Math.max( document.documentElement.clientWidth, $(this).find('img').width() ) + 'px; z-index:' + incrementialNumber );
	 		} else {
	 			$(this).attr('style', 'left: ' + document.documentElement.clientWidth + 'px; z-index:' + incrementialNumber );
	 		}
	 		$(this).find('.texte').attr('style', "opacity:0;margin-left: " + Number( __textPosition + 20 ) + "px;" );
	 		$(this).find('.descriptionContainer').attr('style', 'top:380px;height:0px;' );
	 		$(this).delay( 200 ).animate( { left : 0 }, 500, "linear", function() {
	 			$(this).find('.descriptionContainer').animate( { top: 322, height: 58 }, 100, "linear", function() {
	 				$(this).find('.texte').animate( { opacity: 1, "margin-left": + __textPosition }, 100 );
	 			} );
	 		} );
	 	} else if ( $(this).css( 'left' ) != 0 ) {
	 		$('#nav_' + $(this).attr('id') ).removeClass("selected");
	 		$(this).find('.texte').animate( { opacity: 0, "margin-left": -482 }, 100 );
	 		$(this).find('.descriptionContainer').animate( { top: 380, height: 0 }, 300 );
	 		if ( toRight ) {
	 			$(this).delay( 200 ).animate( { "left": document.documentElement.clientWidth }, 600 );
	 		} else {
	 			$(this).delay( 200 ).animate( { "left": - Math.max( document.documentElement.clientWidth, $(this).find('img').width() ) }, 600 );
	 		}
		}
	});
	
	// $(document).oneTime( 5000, "carrousselTimer", function(i) { getNextCarrousselItem() } );

}

function getNextCarrousselItem() {
	var goNext = false;
 	$('section.home_carroussel').each( function() {
 		if ( goNext == true ) {
 			 makeTransition( $(this).attr('id'), false );
 			 goNext = false;
 			return;
 		}
		if ( $(this).attr('id') == current_section ) {
			goNext = true;
		} 		
 	});
 	
 	if ( goNext == true ) {
 		makeTransition( $('#carroussel').find('section:first').attr('id'), false );
 	}
}

function getPreviousCarrousselItem() {
	var goPrevious = false;
	var previousId = undefined;
 	$('section.home_carroussel').each( function() {
		if ( $(this).attr('id') == current_section ) {
			if ( previousId == undefined ) {
				goPrevious = true;
				return;
			}  else {
				makeTransition( previousId, true );
			}
		}  else {
			previousId = $(this).attr('id');
		}
 	});
 	
 	if ( goPrevious == true ) {
 		makeTransition( $('#carroussel').find('section:last').attr('id'), true );
 	}
}

/* ---------- TWITTER ---------- */
$(document).ready(function(){

	$('#twitter-ticker').slideDown('slow');
	
	var fileref = document.createElement('script');
	
	fileref.setAttribute("type","text/javascript");
	fileref.setAttribute("src", "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=VincentHelwig&callback=TweetTick&count=3" );
	
	document.getElementsByTagName("head")[0].appendChild(fileref);
	
});

function TweetTick(ob) {
	var container=$('#tweet-container');
	container.html('');
	
	$(ob).each(function(el){
	
		var str = '	<div class="tweet">\
					<div class="time">' + relativeTime( this.created_at ) + '</div>\
					<div class="txt">' + formatTwitString( this.text ) + '</div>\
					</div>';
		
		container.append(str);
	
	});
}

function formatTwitString(str) {
	str=' '+str;
	str = str.replace(/((ftp|https?):\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?)/gm,'<a href="$1" target="_blank">$1</a>');
	str = str.replace(/([^\w])\@([\w\-]+)/gm,'$1@<a href="http://twitter.com/$2" target="_blank">$2</a>');
	str = str.replace(/([^\w])\#([\w\-]+)/gm,'$1<a href="http://twitter.com/search?q=%23$2" target="_blank">#$2</a>');
	return str;
}

function relativeTime( pastTime ) {
	var date = new Date( Date.parse(pastTime) );
	if ( isNaN( date ) ) {
		return pastTime;		
	} else {
		return formatOnTwoChars( date.getDate() ) + "/" + formatOnTwoChars( date.getMonth() + 1 ) + "/" + date.getFullYear() + " - " + formatOnTwoChars( date.getHours() ) + ":" + formatOnTwoChars( date.getMinutes() );
	}
}

function formatOnTwoChars( pString ) {
	if ( String( pString ).length == 1 ) {
		return "0" + pString;
	} else if ( String( pString ).length == 2 ) {
		return pString;
	}
}
