﻿/*
 *	01/02/2011 12:22
 *	Finalvlap 3.5 JavaScript Layer
**/

//# ---------------
//# Initialisations
//# ---------------
//# Animations header
tutoriaux_status		= 0;
tutoriaux_slidingDown	= false;
tutoriaux_slidingUp		= false;

//# Tracking ads
adstrack_over		= null;
adstrack_lastenter	= new Date();
adstrack_lastenter.setYear('2000');
rocket_plusone_over	= false;

//# -----------
//# DOM Ready !
//# -----------
jQuery(document).ready(function($){

try{
	gapi.plusone.go(); // rendu google +1, première chose faite au DOM ready pour permettre à jquery d'utiliser les éléments créés par le rendu (iframe rocket...)
} catch(ex) {}

//# =================
//# Animations header
//# =================
//# Sliders
$('#tutoriaux_wrap').hide();
$('#tutoriaux_spacemountain').hide();
$('#tutoriaux_handle').click(function(event){
	event.preventDefault();
	menuTutoriauxToggle();
	return false;
});

$('#login_wrap').hide();
$('#login_handle').click(function(event){
	event.preventDefault();
	$('#login_wrap').slideToggle();
	return false;
});

if( window.location.hash.indexOf('tutoriaux') == 1 ){
	menuTutoriauxToggle();
}

function menuTutoriauxToggle(){
	$('#login_wrap').slideUp(300);
	
	// Si aucune animation du menu n'est en cours
	if( tutoriaux_slidingDown == false && tutoriaux_slidingUp == false ){
		
		// Si le menu est masqué => on l'affiche
		if( tutoriaux_status == 0 ){
			//console.log('masqué, sliding down');
			tutoriaux_slidingDown = true;
			$('#tutoriaux_spacemountain').slideDown(200, tutoriaux_endSlideDown);
		}
		
		// Sinon il est sorti => on le rentre
		else{
			//console.log('affiché, sliding up');
			tutoriaux_slidingUp = true;
			$('#tutoriaux_wrap').slideUp(700, tutoriaux_endSlideUp);
			setTimeout('tutoriaux_endSlideUp()', 400);
		}
		
	}
}


//# ==========
//# Social hub
//# ==========
hub_offset_repos = $('#social_hub .liens a').first().css('right');
$('#social_hub .liens a').hover(
	function(){
		$(this).animate({
			right: '0px'
		}, 250);
	},
	function(){
		$(this).animate({
			right: hub_offset_repos
		}, 200);
	}
);

// Masquage accroche si fenêtre trop petite
PIVOT_LITTLESCREEN = 1280;
if( $(window).width() <= PIVOT_LITTLESCREEN ){
	$('#social_hub .accroche').hide();
	$('#rocketscience').hide();
}
$(window).resize(function(event){
	window_current_width	= $(window).width();
	// Fenêtre trop petite => on la masque
	if( window_current_width <= PIVOT_LITTLESCREEN ){
		$('#schub_accroche').fadeOut();
		$('#rocketscience').hide();
	}
	// Fenêtre suffisamment large => on l'affiche
	else{
		$('#schub_accroche').fadeIn();
		$('#rocketscience').show();
	}
});
// =====================
// FIN Animations header
// =====================


//# ========
//# Adstrack
//# ========
//# Surveillance frontière
$('.adswrap iframe').mouseover(function(){
	adstrack_lastenter = new Date();
	adstrack_over = $(this).parents('div.adswrap').attr('id');
}).mouseout(function(){
	adstrack_over = null;
});

$(window).focus();
$(window).blur(function(e){
	if(adstrack_over != null){
		adstrack_log();
	}
	
	if(rocket_plusone_over == true){
		// console.log('clic gg+1 iframe');
		$.post('/etc/ajax/stats-gplusone.php', {
			'mode'		: 'tracking_perso_blur',
			'source'	: 'rocket_plusone',
			'urlFrom'	: document.location.href,
			'rocket_plusone_over' : rocket_plusone_over
		});
	}
});


//# ================
//# Stats - outlinks
//# ================
$('#page a').mouseup(function(event){
	if( this.href.search('://'+serveurHost) == -1 ){
		$.post("/etc/ajax/stats-outlinks-tracker.php", {
			'urlFrom'	: document.location.href,
			'urlTo'		: this.href
		});
	}
});

//# ==================
//# Stats - social hub
//# ==================
$('#social_hub a').mouseup(function(event){
	var channel = $(this).attr('id');
	
	$.post("/etc/ajax/stats-social-hub-tracker.php", {
		'urlFrom'	: document.location.href,
		'urlTo'		: this.href
	});
	
	if( typeof(_gaq) != 'undefined' ){
		_gaq.push(['_trackEvent', 'Social Hub', 'Clic', channel]);
	}
});

//# ==============
//# Admin controls
//# ==============
$('.admin_controls a').click(function(e){
	e.preventDefault();
	
	var url = $(this).attr('href');
	var com = $(this).parent().parent();
	// var com_wrap = com.parent();
	
	var matches = com.attr('id').match(new RegExp('com-([0-9]+)'));
	var id_com = matches[1];
	
	// $.get(url, function(data, parent){ parent.fadeOut(); }); // marche pas, faudrait que le php retourne l'id du com traité
	$.get(url);
	
	com.html('Commentaire n°<b>'+id_com+'</b> supprimé. <a onclick="$.get(\'/admin/etc/actionCom.php?id='+id_com+'&type=publier\'); $(this).parent().html(\'Commentaire n°<b>'+id_com+'</b> rétabli.\'); return false;" href="#" style="color:green; font-weight:bolder;">Rétablir</a>.');
});

//# ========
//# Exemples
//# ========
$('.exemple .loading').hide();

//# Iframe exemple reload
$('.exemple .iframe_handle').mouseup(function(e){
	e.preventDefault();
	
	$(this).parent().children('.loading').fadeIn();
	
	var iframe		= $(this).parent().children('.iframe_wrap').children('iframe');
	var iframe_src	= $(this).parent().children('.iframe_src').html();

	iframe.empty();
	iframe.attr('src', iframe_src);
	iframe.load();
	
	setTimeout(function(){
		$('.exemple .loading').hide();
	}, 1000);
});

//# Iframe exemple - New window
$('.exemple .open-in-new-window').each(function(){
	var tmp = $(this).parent().parent().children('.iframe_src').html();
	$(this).html('<a target="_blank" href="'+tmp+'">Ouvrir cet exemple dans une nouvelle fenêtre.</a>');
});

//# ==================
//# Syntax Highlighter
//# ==================
if( typeof(SyntaxHighlighter) != 'undefined' ){
	SyntaxHighlighter.autoloader(
		'js jscript javascript  /script/syntaxhl/scripts/shBrushJScript.js',
		'php					/script/syntaxhl/scripts/shBrushPhp.js',
		'xml html				/script/syntaxhl/scripts/shBrushXml.js',
		'css					/script/syntaxhl/scripts/shBrushCss.js',
		'plain					/script/syntaxhl/scripts/shBrushPlain.js'
	);
	
	SyntaxHighlighter.all();
}

//# ================
//# Rocket Google +1
//# ================
window['rocket'] = $('#rocketfloat_mobile');
window['firet']  = $('#rocketfloat_mobile .fire.top');
window['fireb']  = $('#rocketfloat_mobile .fire.bottom');
window['LAST_SCROLL_OFFSET'] = $(window).scrollTop();
window['LAST_SCROLL_TIME']   = new Date().getTime();

//# Surveillance frontière
// $('#rocketfloat_mobile iframe') => il faut mettre les boutons +1 en mode explicit, pour que les iframes des boutons +1 soient créés lorsque ce code est éxécuté, et ça ne marche que sur firefox, pas avec chrome ni IE9, donc je met un sélecteur moins précis.
$('#rocketfloat_mobile').mouseover(function(){
	rocket_plusone_over = true;
}).mouseout(function(){
	rocket_plusone_over = false;
});

var top = window['rocket'].offset().top - parseFloat(window['rocket'].css('marginTop').replace(/auto/,0));
var y = $(this).scrollTop();

if (y >= top) {
	window['rocket'].addClass('fixed');
} else {
	window['rocket'].removeClass('fixed');
}

$(window).scroll(function (event) {
	var y = $(this).scrollTop();
	
	if (y >= top) {
		window['rocket'].addClass('fixed');
	} else {
		window['rocket'].removeClass('fixed');
	}
	
	// Rocket engines
	if( window['rocket'].hasClass('fixed') ){
		if( y > window['LAST_SCROLL_OFFSET'] ){
			// console.log('DOWN');
			window['firet'].addClass('on');
			window['fireb'].removeClass('on');
			window['LAST_SCROLL_DIRECTION'] = 'down';
		} else {
			// console.log('UP');
			window['firet'].removeClass('on');
			window['fireb'].addClass('on');
			window['LAST_SCROLL_DIRECTION'] = 'up';
		}
	}
	
	setTimeout(function(){
		if( new Date().getTime() - window['LAST_SCROLL_TIME'] > 50 ){
			window['firet'].removeClass('on');
			window['fireb'].removeClass('on');
			
			// Animation inertie fusée
			if( window['rocket'].hasClass('fixed') ){
				if( window['LAST_SCROLL_DIRECTION'] == 'down' ){
					window['rocket'].animate({
						top:	'+=5px'
					}, 50, function(){
						window['rocket'].animate({
							top:	'-=5px'
						}, 120);
					});
				} else {
					window['rocket'].animate({
						top:	'-=5px'
					}, 50, function(){
						window['rocket'].animate({
							top:	'+=5px'
						}, 120);
					});
				}
			}
		}
	},70);
	
	// Mise à jour variables
	window['LAST_SCROLL_OFFSET'] = y;
	window['LAST_SCROLL_TIME']   = new Date().getTime();
});


}); /* END DOM Ready */



//#>>-----------------------------------------------------------------------------------\
//#>>>------------------------------------------------------------------------------------\
//#>>>>>------------------------------------------------------------------------------------->
//#>>>------------------------------------------------------------------------------------/
//#>>-----------------------------------------------------------------------------------/



//# ------------
//# Fonctions JS
//# ------------

//#	Ouvre une vidéo dans une popup
function videolink(link){
	window.open(link.href,'popup_videolink','status=no , toolbar=no, location=no, menubar=no, resizable=yes, top=50, left=50');
}

//# Ouvre une popup de téléchargement stockmotion
function popup_stockmotion(link, id_item){
	var width	= 500;
	var height	= (260 + 50);
	var haut	= (screen.height-height)/2 - 50;
	var gauche	= (screen.width-width)/2;
	window.open(
		link.href,
		'popup_'+id_item,
		'status=no , toolbar=no, location=no, menubar=no, resizable=no, top='+haut+', left='+gauche+', directories=no, width='+width+', height='+height
	);
}

//# Animations header
function tutoriaux_endSlideDown(){
	$('#tutoriaux_wrap').slideDown({ duration: 1000, easing: 'easeOutBounce'});
	tutoriaux_slidingDown = false;
	tutoriaux_slidingUp = false;
	tutoriaux_status = 1;
}
function tutoriaux_endSlideUp(){
	$('#tutoriaux_spacemountain').slideUp(300);
	tutoriaux_slidingDown = false;
	tutoriaux_slidingUp = false;
	tutoriaux_status = 0;
}

//# Callback google +1
function plusone_callback_default(param)	{plusone_callback(param, 'NULL');}
function plusone_callback_rocket_side(param){plusone_callback(param, 'rocket_side');}
function plusone_callback_tuto_top(param)	{plusone_callback(param, 'tuto_top');}
function plusone_callback_tuto_bottom(param){plusone_callback(param, 'tuto_bottom');}
function plusone_callback_news_top(param)	{plusone_callback(param, 'news_top');}
function plusone_callback_news_ariane(param){plusone_callback(param, 'news_ariane');}

// Fonction qui transmet le callback natif fournis par les boutons google +1
function plusone_callback(param, source){
	$.post('/etc/ajax/stats-gplusone.php', {
		'mode'		: 'callback',
		'param'		: param,
		'source'	: source
	});
}

//# Adstrack - log ajax
function adstrack_log(){
	// console.log('clic adsense :',adstrack_over);
	$.post('/etc/ajax/stats-adstrack.php', {
		'urlFrom'		: document.location.href,
		'adstrack_over' : adstrack_over
	});
}
