// JavaScript Document

// Dénomination:
// nomtrigger_main
// nomtrigger_fond
// nomtrigger_trigger
// nomwin_main
// nomwin_fixe
// nomwin_move

$(document).ready(function() {
	var main = new Scroller('trigger', 'content');		   
});

var Up_Information_Timeout;
var Up_Information_On = false;

jQuery(function( $ ){
	/**
	 * Demo binding and preparation, no need to read this part
	 */
		//borrowed from jQuery easing plugin
		//http://gsgd.co.uk/sandbox/jquery.easing.php
		$.easing.elasout = function(x, t, b, c, d) {
			var s=1.70158;var p=0;var a=c;
			if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
			if (a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
		};
		$('a.back').click(function(){
			$(this).parents('div.pane').scrollTo( 0, 800, { queue:true } );
			$(this).parents('div.section').find('span.message').text( this.title );
			return false;
		});
		//just for the example, to stop the click on the links.
		$('ul.links').click(function(e){
			e.preventDefault();
			var link = e.target;
			link.blur();
			if( link.title )
				$(this).parent().find('span.message').text(link.title);
		});
	
});

function Show_Up_Information(value) {
	if (Up_Information_On) return false;
	Up_Information_On = true;
	$("#Up_Information").fadeTo(0,0).html(value).show().fadeTo('normal', 0.8, function() {});
	Up_Information_Timeout = setTimeout("Hide_Up_Information()", 5000);
}

function Hide_Up_Information(value) {
	clearTimeout(Up_Information_Timeout);
	$("#Up_Information").fadeTo('slow', 0, function() {
		$(this).html('');
		Up_Information_On = false;
	});
	
}

function Scroller(trig, win) {
	var monscroller_main = '#' + trig + '_main';
	var monscroller_fond = '#' + trig + '_fond';
	var monscroller_trigger = '#' + trig + '_trigger';
	var monwin_main = '#' + win + '_main';
	var monwin_fixe = '#' + win + '_fixe';
	var monwin_move = '#' + win + '_move';
		
	var height_3 = $(monwin_move).height();
    var heightBox_3 = $(monwin_fixe).height() ;

	var trigger_y_3 = $(monscroller_trigger).offset().top;

	// Initialise le scroller pour capter le mousewheel
	//$(monwin_move).mousewheel(function() {Show_Up_Information('not implented yet !');});

	var debug = false;
	if (debug) alert(
		'monscroller_main:' + monscroller_main + '\n' +
		'monscroller_fond:' + monscroller_fond + '\n' +
		'monscroller_trigger:' + monscroller_trigger + '\n' +
		'monwin_main:' + monwin_main + '\n' +
		'monwin_fixe:' + monwin_fixe + '\n' +
		'monwin_move:' + monwin_move + '\n' +
		'height_3:' + height_3 + '\n' +
		'heightBox_3:' + heightBox_3 + '\n' +
		'trigger_y_3:' + trigger_y_3 + '\n');

	if(isNaN(trigger_y_3)) trigger_y_3 = 0;
	//alert(trigger_y_3);

	//if (height_3 > heightBox_3) {
	  $(monscroller_trigger).draggable({
		  axis: 'y',
		  containment: 'parent',
		  drag: function() {
  
			  var y = $(this).offset().top - trigger_y_3;
			  var heightBox = $(monwin_fixe).height();			
			  var height = $(monwin_move).height()-heightBox;
			  var height_scroll = $(monscroller_fond).height() - $(this).height();
			  y2 = Math.round(height/height_scroll*y);
			 $(monwin_fixe).scrollTop(y2);
			 //$(monwin_fixe).scrollTo( { top:y2 }, 800);//, {easing:'elasout'} );
			 // ( '#target-examples', 800, {easing:'elasout'} )
			 // $paneOptions.scrollTo( 'li:eq(15)', 2500, { easing:'elasout' } );
			  
			  //$("#info").html('y ['+y+'] height_scroll ['+height_scroll+'] height['+height+'] y2['+y2+']');
  
		  }
	  })
    //}
}


// Argument 1 : Le trigger
// Argument 2 : Le conteneur

