/* Ce fichier est volontairement non-packé */

// On est prêts !
$(document).ready(function() {
	
	//Veuillez vous identifier?
	var $sociallink = $("#social a");
	var $contactmail = $("#contactmail");
	var $slideshow = $(".slideshow");
	var $slide = $(".slides");
	var $slidefolio = $(".slidefolio");
	
	// Swap social
	if($sociallink.length) $sociallink.hover(function() {
		var $this = $('img', this);
		var src = $this.attr('src');
		var pos = src.lastIndexOf('.');
		var postsrc = src.substring(0, pos);
		var srcover = postsrc+'-over.png';
		$this.attr('src', srcover);

	}, function() {
		$("#twitter img").attr("src","img/social/twitter.png");
		$("#facebook img").attr("src","img/social/facebook.png");
		$("#google img").attr("src","img/social/googleplus.png");
		$("#viadeo img").attr("src","img/social/viadeo.png");
		$("#behance img").attr("src","img/social/behance.png");
		$("#forrst img").attr("src","img/social/forrst.png");
		$("#flickr img").attr("src","img/social/flickr.png");
		$("#formspring img").attr("src","img/social/formspring.png");
	});
	
	
	// Antispam
	if($contactmail.length) {
		var nospam;
		nospam = 'contact';
		nospam += '@';
		nospam += 'maximesimeon.com';
		$contactmail.text(nospam);
		$contactmail.wrapInner("<a></a>");
		$contactmail.find("a").attr("href","mailto:"+nospam);
	}
	
	// scroll contact
    function smoothscroll(element){  
        $(element).click(function(){  
            var goscroll = false;  
            var the_hash = $(this).attr("href");  
            var regex = new RegExp("\#(.*)","gi");  
            if(the_hash.match("\#")) {  
                the_hash = the_hash.replace(regex,"$1");  
                if($("#"+the_hash).length>0) {  
                    the_hash = "#" + the_hash;  
                    goscroll = true;  
                }  
                else if($("a[name=" + hash + "]").length>0) {  
                    the_hash = "a[name=" + the_hash + "]";  
                    goscroll = true;  
                }  
                if(goscroll) {  
                    $('html, body').animate({  
                        scrollTop:$(the_hash).offset().top  
                    }, 'slow');  
                    return false;  
                }  
            }  
        });  
    };  
    smoothscroll('a[href^="#"]');  
	
	
	/* ----------------------------------------- */
	/* BackTop                                   */
	/* ----------------------------------------- */
	
	function backtop_go() {
		var taille = $(window).width();
		if (taille>1024) {
			
			//Style
			$('#backtop').css({
				'position'	: 	'fixed',
				'right'		:	'30px',
				'bottom'	:	'20px',
				'opacity'   :   '0.3',  
		    	'z-index'   :   '2000'
			});
			
			// hover
			$('#backtop').hover(function() {
				$('#backtop').css('opacity','0.7');
			}, function() {
				$('#backtop').css('opacity','0.3');
			});
			
			//In/Out
			$(window).scroll(function(){  
			    posScroll = $(document).scrollTop();  
			    if(posScroll >=550)  
			        $('#backtop').fadeIn(600);  
			    else  
			        $('#backtop').fadeOut(600);  
			});  		
			
		}
		
		if (taille<640){
			//Style
			$('#backtop').css({
				'display'	: 	'block',
				'margin-top':	'1em',
				'text-align':	'center'
			});
		} 
	}
	
	backtop_go();
	
	/* ----------------------------------------- */
	/* Tweets                                    */
	/* ----------------------------------------- */
	
	// Declare variables to hold twitter API url and user name
	var twitter_api_url = 'http://search.twitter.com/search.json';
	var twitter_user    = 'maximesimeon';
	var count = 3;
	
	// Enable caching
	$.ajaxSetup({ cache: true });
	
	// Send JSON request
	// The returned JSON object will have a property called "results" where we find
	// a list of the tweets matching our request query
	$.getJSON(twitter_api_url + '?callback=?&rpp='+count+'&q=from:' + twitter_user,	function(data) {
		$.each(data.results, function(i, tweet) {
			// Before we continue we check that we got data
			if(tweet.text !== undefined) {
			// Calculate how many hours ago was the tweet posted
			var date_tweet = new Date(tweet.created_at);
			var date_now   = new Date();
			var date_diff  = date_now - date_tweet;
			var hours      = Math.round(date_diff/(1000*60*60));
			
			// Build the html string for the current tweet
			var tweet_html = '<li>'+tweet.text.replace(/(\w+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+)/gi, '<a href="$1">$1</a>').replace(/[\@]+([A-Za-z0-9-_]+)/gi, '<a href="http://twitter.com/$1">@$1</a>')+'<span class="tweettime" ><a href="http://twitter.com/'+twitter_user+'/status/'+tweet.id_str+'">il y a '+hours+' h</a></span>'+'</li>';
			/*
			var tweet_html = '<div class="tweet_text">';
          tweet_html    += '<a href="http://www.twitter.com/';
          tweet_html    += twitter_user + '/status/' + tweet.id + '">';
          tweet_html    += tweet.text + '<\/a><\/div>';
          tweet_html    += '<div class="tweet_hours">' + hours;
          tweet_html    += ' hours ago<\/div>';
			*/
			// Append html string to tweet_container div
			$('#coming').empty();
			$('#tweets').append(tweet_html);
			}
		});
	});
	
	
	/* ----------------------------------------- */
	/* Slideshow                                 */
	/* ----------------------------------------- */
	
	// Garde à vous les slides
	var tabcss = {
		'position':'absolute',
		'top':'0',
		'left':'0',
	};
	var time_slide = 800;
	var delay = 3000;
	var interval;
	
	// Style
	$("img",$slideshow).css(tabcss);
	$slideshow.css('height','348px');
	$slide.css('height','348px');
	
	//Affichage unique de la 1er slide
	$("img:gt(0)",$slide).hide();
  	
  	//Ajout des puces
  	function addpuce() {
  		if ($("a",$slide).length) {
	  		var n = $("a",$slide).length; //number of slide
	  		var i = 0;
	  		var puce = '<div class="puces">';
	  		while(i != n) {
	  			puce += '<a href="#"><img src="/img/puce.png" alt="Slide '+(i+1)+'"></a>';
	  			i++;
	  		}
	  		puce += '</div>';
	  		$slideshow.append(puce);
  		}
  	}  	
  	
  	// Hop hop les puces
  	addpuce();
  	
  	// Who are you ?
  	var $puce = $(".puces");
	
	function puceover() {
		if($puce.length) {
			$("img", $puce).attr('src', '/img/puce.png');
			$(".current img", $puce).attr('src', 'img/puce-over.png');
		}
	}
	
	// Action bouton
	if($puce.length) {
		$("a:first-child", $puce).addClass("current");
		$('a:not("a.current")',$puce).live("click", function(event){
			var $puce_clique = $(this);
			$("a", $puce).removeClass();
			var slide_i = $puce_clique.index();	
			$puce_clique.parents(".puces").prev().find("a img:visible").stop(true, true).fadeOut('time_slide', function() {
				$(this).parents(".slides").find("a:eq("+slide_i+") img").fadeIn('time_slide');	
			});
			$puce_clique.addClass("current");
			puceover();
			event.preventDefault();
		});
		$('a.current',$puce).live("click", function(event){
			event.preventDefault();
		});
		puceover();
	}
	
	// Passage à la slide suivante
	function slide_next() {
		var $slideCurrent = $("img:visible",$slide);
		var $suivante = $slideCurrent.parent().last().next().find("img");
		if ($suivante.length<1)
		$suivante = $("a:first img",$slide);		
		$suivante.stop(true, true).fadeIn(time_slide);
		$slideCurrent.stop(true, true).fadeOut(time_slide);	
		var slide_i = $slideCurrent.parent().next().index();
		if (slide_i == -1)
		var slide_i = 0;
		var $puceCurrent = $slideCurrent.parents(".slides").next(".puces").find("a:eq("+(slide_i)+")");
		$("a", $puce).removeClass();
		$puceCurrent.addClass("current");
		puceover();
	}
	
	
	//Survol slide
	$slideshow.mouseenter(slide_stop);
	$slideshow.mouseleave(slide_start);
	
	
	// Auto
	function slide_start() {
		clearInterval(interval);
		interval = setInterval(function() { slide_next(); },delay);
	}

	function slide_stop() {
		clearInterval(interval);
	}
	
	slide_start();
	
	
	/* ----------------------------------------- */
	/* SlideFolio                                */
	/* ----------------------------------------- */
	
	function slidefolio_go() {
		var taille = $(window).width();
		if (taille>768) {
		
			// Style
			$("img",$slidefolio).css(tabcss);
			$slidefolio.css('height','348px');
			
			// Affichage unique de la 1er slide
			$("img:gt(0)",$slidefolio).hide();
			
			// Qui à la main ?
			$("img", $slidefolio).mouseenter(function() {
				var $thisfolio = $(this).parent();
				if ($("img",$thisfolio).length>1)
				$(this).css('cursor','pointer');
				
			});
			
			// Action image
			$("img", $slidefolio).click(function() {
				var $thisfolio = $(this).parent();
				if ($("img",$thisfolio).length>1) {
					var $imgCurrent = $("img:visible", $thisfolio);
					var $imgSuivante =$imgCurrent.last().next();
					if ($imgSuivante.length<1)
					$imgSuivante = $("img:first", $thisfolio);
					$imgSuivante.stop(true, true).fadeIn(time_slide);
					$imgCurrent.stop(true, true).fadeOut(time_slide);	
				}
			
			});
		}
	}
	
	slidefolio_go();

	/* ----------------------------------------- */
	/* Resize                                    */
	/* ----------------------------------------- */

	
	$(window).resize(function() {
		slidefolio_go();
		backtop_go();
	});
	
	
});

