//scroller by charlie EISSEN from delasource
//version 2.7 22/08/2005
//modification du scroller
var CC ={};

CC.debug = function(str)
{
	CC.$('debug').innerHTML = CC.$('debug').innerHTML+'<br />'+str;
}

CC.extend = function(subClass, baseClass) {
   function inheritance() {}
   inheritance.prototype = baseClass.prototype;
   subClass.prototype = new inheritance();
   subClass.prototype.constructor = subClass;
   subClass.baseConstructor = baseClass;
   subClass.superClass = baseClass.prototype;
}

CC.$ = function (id)
{
	if( document.getElementById(id) )
	{
		return document.getElementById(id);
	}
	else
	{
		if( document.getElementById('debug') )
		{
			CC.debug('Element "'+id+'" does not exists in the DOM.');
		}
		return false;
	}
}

Function.prototype.bind = function(obj)
{
	var method = this;
	return function(e)
	{
		return method.call( obj , e );
	};
} 

CC.sprites = { };

CC.sprite = function( id )
{
    this.el = CC.$(id);
    this.id = id;
    this.css = this.el.style;
	this.w = this.el.offsetWidth;
	this.h = this.el.offsetHeight;
	this.x = this.el.offsetLeft;
	this.y = this.el.offsetTop;
	this.absy = CC.findPosY(this.el);
	//CC.debug(this.id+' : '+this.absy+' : '+this.el.nodeType);
    CC.sprites[this.id] = this;
};
CC.sprite.prototype =
{
    moveTo : function(x, y) {
		if(x!=null){
			this.css.left = (this.x = x) + "px";
		}
		if(y!=null){
			this.css.top = (this.y = y) + "px";
		}
    },
	
	resizeTo : function(h,w) {
		if( h!= null )
		{
			this.css.height = (this.h = h ) + 'px';
		}
		if( w!= null )
		{
			this.css.width = (this.w = w ) + 'px';
		}
	},

	debugCoor : function() {
		CC.$('debug').innerHTML = this.id+" : w="+this.w+"/h="+this.h+"/x="+this.x+"/y="+this.y;
	},
	alertCoor : function() {
		alert("w="+this.w+"/h="+this.h+"/x="+this.x+"/y="+this.y);
	},
	show : function() {
		this.css.visibility="visible";
	},
	hide : function() {
		this.css.visibility="hidden";
	},
	display_none : function() {
		this.css.display="none";
	},
	display_normal : function() {
		this.css.display="block";
	}
};

CC.sprite.getInstance = function(id)
{
	if( CC.$(id) )
	{
		if( CC.sprites[id] == null)
		{
			CC.sprites[id] = new CC.sprite( id );
		}
		return CC.sprites[id];
	}
};


CC.scrollers = {};
CC.scrollers.activeScroll = null;

CC.scroller = function(id)
{
	CC.scroller.baseConstructor.call(this,id);

	var children = this.el.getElementsByTagName('DIV');
	var childrenl = children.length;
	for( i = 0; i < childrenl ; i++ )
	{
		children[i].id = this.id+children[i].className;

		if( children[i].className == 'arrowup' )
		{
			this.arrowup = CC.sprite.getInstance( children[i].id );
			this.arrowup.direction = 'up';
			this.arrowup.parent = this;

		}
		else if( children[i].className == 'arrowdown' )
		{
			
			this.arrowdown = CC.sprite.getInstance( children[i].id );
			this.arrowdown.direction = 'down';
			this.arrowdown.parent = this;

		}else if(  children[i].className == 'tacket' )
		{

			this.tacket = CC.sprite.getInstance( children[i].id );
			this.tacket.parent = this;

		}
		else if(  children[i].className == 'conteneur' )
		{
			
			this.conteneur = CC.sprite.getInstance( children[i].id );
		
		}
		else if(  children[i].className == 'contenu' )
		{
			
			this.contenu = CC.sprite.getInstance( children[i].id );
			
		}
		else if(  children[i].className == 'scrollbar' )
		{
			this.scrollbar = CC.sprite.getInstance( children[i].id );
			this.scrollbar.parent = this;
		}
	}

	CC.event.addEvent(this.tacket.el,'mousedown',this.dragStart.bind(this) );

	//CC.debug(this.rapport + ' / ' + this.contenu.h + ' / ' + this.conteneur.h + ' / ' + this.scrollbar.h + ' / ' + this.tacket.h + ' / ' + this.arrowdown.h + ' / ' + this.arrowup.h);
	
	if( this.contenu.h < this.conteneur.h )
	{
		this.scrollbar.hide();
	}
	
	//taille proportionnelle de l'ascenseur
	if( this.conteneur.h < this.contenu.h)
	{
		new_h = ( this.scrollbar.h -  this.arrowdown.h - this.arrowup.h ) * ( this.conteneur.h / this.contenu.h );
		if( new_h > this.tacket.h )
		{
			this.tacket.resizeTo( parseInt(new_h), null );
		}
	}
	
	this.rapport = ( this.contenu.h - this.conteneur.h ) / ( this.scrollbar.h - this.tacket.h - this.arrowdown.h - this.arrowup.h);
	

	/* mise en place sur les fleches haut et bas */
	CC.event.addEvent(this.arrowup.el,'mouseover',this.scrollStart.bind(this.arrowup) );
	CC.event.addEvent(this.arrowup.el,'mousedown',this.scrollStart.bind(this.arrowup) );
	CC.event.addEvent(this.arrowup.el,'mouseout',this.scrollStop.bind(this) );
	
	CC.event.addEvent(this.arrowdown.el,'mouseover',this.scrollStart.bind(this.arrowdown) );
	CC.event.addEvent(this.arrowdown.el,'mousedown',this.scrollStart.bind(this.arrowdown) );
	CC.event.addEvent(this.arrowdown.el,'mouseout',this.scrollStop.bind(this) );
	
	
	CC.event.addEvent(this.scrollbar.el,'click',this.scrollBarClick.bind(this) );
	this.currentEvent = null;
	this.timer = null;
};
CC.extend(CC.scroller, CC.sprite);


CC.scroller.prototype.dragStart = function(e)
{
	CC.debug(this.el.id);
	var y = CC.mouse.y(e);
	this.currentEvent= 'drag';
	this.tacket.start_y = y - ( this.absy + this.scrollbar.y + this.tacket.y );

	CC.event.addEvent(document,'mousemove',this.onDragGo = this.dragGo.bind(this) );
	CC.event.addEvent(document,'mouseup',this.onDragStop = this.dragStop.bind(this) );
	CC.event.stop_propagation(e);
}

CC.scroller.prototype.dragGo = function(e)
{
	var mouse_y = CC.mouse.y(e);
	
	//CC.debug(this.el.id);
	//calcul new y
	
	//var tacket = this.activeDrag.tacket;
	//var scroller = this.activeDrag;
	
	
	var new_tacket_y = mouse_y - ( this.absy + this.scrollbar.y + this.tacket.start_y );
	
	this.tacket.moveTo( null , new_tacket_y );

	if( this.tacket.y < this.arrowup.y + this.arrowup.h )
	{
		this.tacket.moveTo( null , this.arrowup.y + this.arrowup.h );
	}
	else if( this.tacket.y > this.scrollbar.h - this.arrowdown.h - this.tacket.h )
	{
		//CC.debug('on bouge');
		this.tacket.moveTo( null , this.scrollbar.h - this.arrowdown.h - this.tacket.h );
	}
	
	y_contenu = parseInt( ( this.tacket.y - ( this.arrowup.y + this.arrowup.h ) ) * this.rapport );
	
	this.contenu.moveTo( null , -y_contenu );
	
	CC.event.stop_propagation(e);
}

CC.scroller.prototype.dragStop = function (e)
{
	CC.event.removeEvent(document,'mousemove', this.onDragGo );
	CC.event.removeEvent(document,'mouseup', this.onDragStop );
	CC.event.stop_propagation(e);
	this.currentEvent = null;
}






CC.scroller.prototype.scrollStart = function(e)
{
	//CC.debug('Scrollstart '+this.parent.el.id+'/ current event = '+this.parent.currentEvent);
	if( this.parent.currentEvent != 'fastautoscroll' )
	{
		//CC.debug('Scrollstart pas fastscroll');
		if (!e) var e = window.event;

		if( this.parent.currentEvent == null || this.parent.currentEvent == 'autoscroll' )
		{
			if( this.parent.currentEvent == 'autoscroll' )
			{
				this.parent.scrollStop();
			}
	
			switch( e.type )
			{
				case "mousedown" :
					this.parent.speed=3;
					this.parent.currentEvent = 'fastautoscroll';
					break;
				case "mouseup" :
				case "mouseover" : 
					this.parent.speed=1;
					this.parent.currentEvent = 'autoscroll';
					break;
			}
			
			this.parent.direction = this.direction;
	
			this.parent.timer = setTimeout( this.parent.scrollRun.bind(this.parent) , 20 );
	
			
		}
	}
	CC.event.stop_propagation(e);
	//CC.debug('ScrollStart sortie : '+this.parent.currentEvent+'/ timer = '+this.parent.timer);
	return null;
}

CC.scroller.prototype.scrollRun = function ()
{
	//CC.debug(this.el.id);
	if( this.direction == 'down' )
	{
		y = this.contenu.y - this.speed;
	}
	else
	{
		y = this.contenu.y + this.speed;
	}

	this.contenu.moveTo(null,y);

	if( this.contenu.y > 0 )
	{
		y = 0;
		this.contenu.moveTo(null,y);
	}

	if( this.contenu.y < this.conteneur.h - this.contenu.h)
	{
		y = this.conteneur.h - this.contenu.h;
		this.contenu.moveTo(null,y);
	}
		
	y_tacket = parseInt( y / this.rapport ) - ( this.arrowup.y + this.arrowup.h );
			
	this.tacket.moveTo( null , -y_tacket );

	this.timer = setTimeout( this.scrollRun.bind(this) , 20);
}

CC.scroller.prototype.scrollStop = function (e)
{
	//CC.debug('scrollStop : '+this.el.id+'/'+this.timer);
	clearTimeout( this.timer );
	this.currentEvent = null;
}


CC.scroller.prototype.moveTo = function (y)
{
	this.contenu.moveTo(null,y);

	if( this.contenu.y > 0 )
	{
		y = 0;
		this.contenu.moveTo(null,y);
	}

	if( this.contenu.y < this.conteneur.h - this.contenu.h)
	{
		y = this.conteneur.h - this.contenu.h;
		this.contenu.moveTo(null,y);
	}
		
	y_tacket = parseInt( y / this.rapport ) - ( this.arrowup.y + this.arrowup.h );
			
	this.tacket.moveTo( null , -y_tacket );
}








CC.scroller.getInstance = function(id) {
    if( CC.scrollers[id] == null)
	{
		CC.scrollers[id] = new CC.scroller( id );
	}
    return CC.scrollers[id];
};

CC.scroller.prototype.scrollBarClick = function (e)
{
	if( this.currentEvent == null /*&& wasindrag == 0*/ )
	{
		var y = CC.mouse.y(e);
		
		y = y - this.absy - this.scrollbar.y;

		if( y < this.tacket.y && y > this.arrowup.y + this.arrowup.h )
		{
			y = this.tacket.y - ( this.conteneur.h / this.rapport);
			this.currentEvent = "scrollbar";
			CC.debug('click1');
		}
		//CC.debug('y = '+y  +' > tackety = '+this.tacket.y+'  + tacketh = '+this.tacket.h+' && < arrowdown.y = '+this.arrowdown.y);
		if( y > this.tacket.y + this.tacket.h && y < this.arrowdown.y)
		{
			y = parseInt( this.tacket.y + ( this.conteneur.h / this.rapport ) );
			this.currentEvent = "scrollbar";
			CC.debug('click2');
		}

		if( this.currentEvent == "scrollbar" )
		{
			this.tacket.moveTo( null,y );
			if( this.tacket.y < this.arrowup.y + this.arrowup.h )
			{
				y = this.arrowup.y + this.arrowup.h;
				this.tacket.moveTo( null,y );
			}
			if( this.tacket.y > this.scrollbar.h - this.arrowdown.h - this.tacket.h )
			{
				y = this.scrollbar.h - this.arrowdown.h - this.tacket.h;
				this.tacket.moveTo( null,y );
			}
			
			y_contenu = ( this.tacket.y - ( this.arrowup.y + this.arrowup.h ) ) * this.rapport;
			
			this.contenu.moveTo( null , -y_contenu );
		}
		this.currentEvent = null;
	}
	//this.currentEvent=null;
	/*wasindrag = 0;*/
}



//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
CC.mouse = {};
CC.mouse.p = function (e)
	{
		var pos = new Array();
	
		if (!e) var e = window.event;
	
		if (e.pageX || e.pageY)
		{
			pos['x'] = e.pageX;
			pos['y'] = e.pageY;
		}
		else if (e.clientX || e.clientY)
		{
			pos['x'] = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			pos['y'] = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		
		return pos;
	}

	CC.mouse.y = function (e)
	{
		var pos = CC.mouse.p(e);
		return pos['y'];
	}
	CC.mouse.x = function (e)
	{
		var pos = CC.mouse.p(e);
		return pos['x'];
	}

CC.findPos = function(obj)
{
	if( obj.id == 'mettrelid' )
	{
		//CC.debug('startttttt id : '+obj.id+' : ');
		var curleft = curtop = 0;
		if (obj.offsetParent)
		{
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
			//CC.debug(obj.id+' : '+obj.offsetTop+' : ');
			while (obj = obj.offsetParent)
			{
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
				//CC.debug(obj.id+' : '+obj.offsetTop+' : ');
			}
		}
	}else{
		var curleft = curtop = 0;
		if (obj.offsetParent)
		{
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
			while (obj = obj.offsetParent)
			{
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
		}
	}
		return [curleft,curtop];
}

CC.findPosY = function(obj)
{
	var tab = CC.findPos(obj);
	return tab[1];
}

CC.event = {};

CC.event.addEvent = function( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}


/**
 *	Remove an event from an object
 */
CC.event.removeEvent = function ( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}


/**
 *	Stop event propagation
 */
CC.event.stop_propagation = function ( e )
{
	// For ie:
	CC.event.prevent_default(e);
	
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	
	
	
};

CC.event.prevent_default = function(e)
{
	if (!e) var e = window.event;
	e.returnValue = false;
	if (e.preventDefault) e.preventDefault();
};

/****************************/
/****************************/
CC.dom = {};
//
//CC.dom.getElementsByClassName = function (class,tag,rootNode)
//{	
//	tag = tag || '*';
//	rootNode = rootNode || document;
//
//	var returnNodes = [];
//	CC.debug('le');
//	var nodes = rootNode.getElementsByTagName(tag);
//	var l = nodes.length;
//	
//	for( var i = 0; i < l ; i++ )
//	{
//		if ( this.hasClass( nodes[i],class ) )
//		{
//			returnNodes.push( nodes[i] );
//		}
//	}
//	return returnNodes;
//};
//
//CC.dom.addClass = function(el, className)
//{
//	el = CC.dom.$(el);
//	if( !this.hasClass( el,className ) )
//	{
//		el.className = el.className+' '+className;
//	}
//};
//
CC.dom.hasClass = function(el,className)
{
	el = CC.dom.$(el);
	var reg = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
	return reg.test(el.className);
}
//
CC.dom.$ = function(el)
{
	if( !el )
	{
		return getElementsByTagName('*');
	}
	else if (typeof el != 'string' && !(el instanceof Array) )
	{
		return el;
	}
	
	else if (typeof el == 'string')
	{
		return document.getElementById(el);
	}
	else
	{
		//el = array of id
		var nodes = [];
		var l = el.length;
		for (var i = 0; i < l; ++i)
		{
			nodes[i] = CC.dom.$(el[i]);
		}
		return nodes;
	}

	return null;
};

/*****************************************/
CC.window = {};
CC.window.popupCentered = function(e)
{
	var height = 569;
	var width = 450;
	var top = ( screen.availHeight - height ) / 2;
	var left = ( screen.availWidth - width ) / 2;
	var wname = 'video';
	popup = window.open(this.href,wname,'top=' + top + ',left=' + left + ',width=' + width + ',height=' + height + ',status=no,resizable=no,scrollbars=no');
	popup.focus();
	CC.event.stop_propagation(e);
}

CC.window.openWindow = function(e)
{
	window.open(this.href);
	CC.event.stop_propagation(e);
}

function manageLangueDiv()
{
	if( CC.$('langues').style.left == '-205px' )
	{
		CC.$('langues').style.left = '0';
		CC.$('langues').style.clip = 'rect(auto auto auto auto)';
	}
	else
	{
		CC.$('langues').style.clip = 'rect(0 auto auto 205px)';
		CC.$('langues').style.left = '-205px';
	}
}


window.onload = function()
{
	CC.$('debug').innerHTML = 'loade';
	//CC.debug( 'main : '+CC.$('main').offsetTop+'/'+CC.findPosY(CC.$('main')) );
//	var height = CC.$('main').offsetTop + CC.$('content').offsetTop;
//	CC.debug( 'content : '+CC.$('content').offsetTop+'/'+CC.findPosY(CC.$('content'))+'/'+height );
//	height = height +CC.$('subcontent').offsetTop;
//	CC.debug( 'subcontent : '+CC.$('subcontent').offsetTop+'/'+CC.findPosY(CC.$('subcontent'))+'/'+ height);
//	height = height +CC.$('accueil_lastnews').offsetTop;
//	CC.debug( 'accueil_lastnews : '+CC.$('accueil_lastnews').offsetTop+'/'+CC.findPosY(CC.$('accueil_lastnews'))+'/'+height);
//	height = height +CC.$('accueil_lastnewsscrollbar').offsetTop;
//	CC.debug( 'accueil_lastnewsscrollbar : '+CC.$('accueil_lastnewsscrollbar').offsetTop+'/'+CC.findPosY(CC.$('accueil_lastnewsscrollbar'))+'/'+height);
	//var scroll1 = CC.sprite.getInstance('accueil_lastnews');
	//var scroll1 = CC.sprite.getInstance('accueil_lastnewsscrollbar');
	//scroll1.debugCoor();
	//var scrollers = CC.dom.getElementsByClassName('cc_scroll');
	
	if( CC.$('langues') )
	{
		CC.$('langues').onclick = manageLangueDiv;
	}
	
	if( CC.$('ae_scroller') )
	{
		var scroll1 = CC.scroller.getInstance('ae_scroller');
		var scroll2 = CC.scroller.getInstance('aln_scroller');
	}
	else if( CC.$('societe_scroller') )
	{
		var scroller3 = CC.scroller.getInstance('societe_scroller');
	}
	else if( CC.$('distribution_scroller') )
	{
		var scroller4 = CC.scroller.getInstance('distribution_scroller');
	}
	else if( CC.$('projets_scroller') )
	{
		var scroller4 = CC.scroller.getInstance('projets_scroller');
	}
	
	if( CC.$('presse_scroller') )
	{
		var scroller4 = CC.scroller.getInstance('presse_scroller');
	}
	
	if( CC.$('contacts_scroller') )
	{
		var scroller4 = CC.scroller.getInstance('contacts_scroller');
	}
	
	if( CC.$('tv1_scroller') )
	{
		var scroller4 = CC.scroller.getInstance('tv1_scroller');
	}
	
	if( CC.$('tv_scroller') )
	{
		var scroller4 = CC.scroller.getInstance('tv_scroller');
	}
	
	if( CC.$('slp') )
	{
		var slp = CC.$('slp');
		slp = slp.getElementsByTagName('A');
		for( var i = 0, l = slp.length; i < l ; i++ )
		{
			CC.event.addEvent( slp[i], 'click', CC.window.popupCentered.bind( slp[i] ) );
		}
	}
	
	if( CC.$('projet_popup_scroller') )
	{
		var scroller4 = CC.scroller.getInstance('projet_popup_scroller');
	}
	
	if( CC.$('boutikon1_scroller') )
	{
		var scroller4 = CC.scroller.getInstance('boutikon1_scroller');
	}
	
	if( CC.dom.hasClass(document.body,'distribution') )
	{
		var apopup = CC.$('distribution_scrollercontenu');
		apopup = apopup.getElementsByTagName('a');
		var l = apopup.length;
		for( i = 0; i < l ; i++ )
		{
			if( apopup[i].href.indexOf('popup.php') != -1 )
			{
				
				CC.event.addEvent( apopup[i], 'click', CC.window.popupCentered.bind( apopup[i] ) );
			}
		}
	}
	
	/* pour la distribution */
	if( CC.dom.hasClass(document.body,'distribution_type') )
	{
		var apopup = CC.$('distribution_scroller');
		apopup = apopup.getElementsByTagName('A');
		for( var i = 0, l = apopup.length; i < l ; i++ )
		{
			if( apopup[i].className == 'toolbar_ad1' )
			{
				CC.event.addEvent( apopup[i], 'click', CC.window.popupCentered.bind( apopup[i] ) );
			}
			else if( apopup[i].className == 'toolbar_ad4' )
			{
				CC.event.addEvent( apopup[i], 'click', CC.window.openWindow.bind( apopup[i] ) );
			}
		}
	}
	
	if( CC.dom.hasClass(document.body,'projets_type') )
	{
		var apopup = CC.$('projets_scroller');
		apopup = apopup.getElementsByTagName('A');
		
		for( var i = 0, l = apopup.length; i < l ; i++ )
		{
			//alert(apopup[i].href);
			if( apopup[i].className == 'toolbar_ap1' )
			{
				CC.event.addEvent( apopup[i], 'click', CC.window.popupCentered.bind( apopup[i] ) );
			}
		}
		
		if( CC.$('FUTjson') )
		{
			var FUTjson = new CC.sprite('FUTjson');
			scroller4.moveTo( -FUTjson.el.offsetTop );
			//scroller4.moveTo(-20);
		}
	}
	
	if( CC.dom.hasClass(document.body,'presse_rp') )
	{
		var apopup = CC.$('presse_scrollercontenu');
		apopup = apopup.getElementsByTagName('A');
		for( var i = 0, l = apopup.length; i < l ; i++ )
		{
			CC.event.addEvent( apopup[i], 'click', CC.window.popupCentered.bind( apopup[i] ) );
		}
	}

	if( CC.dom.hasClass(document.body,'tv_videos') )
	{
		var apopup = CC.$('tv_scrollercontenu');
		apopup = apopup.getElementsByTagName('A');
		for( var i = 0, l = apopup.length; i < l ; i++ )
		{
			CC.event.addEvent( apopup[i], 'click', CC.window.popupCentered.bind( apopup[i] ) );
		}
	}
	
	
}