// www.hey.fr 2011


function disableSelection(target){
    if(typeof target.onselectstart!="undefined"){ //IE route
        target.onselectstart=function(){return false}
	}else if (typeof target.style.MozUserSelect!="undefined"){ //Firefox route
        target.style.MozUserSelect="none";
	}else{ //All other route (ie: Opera)
        target.onmousedown=function(){return false}
    	target.style.cursor = "default";
	}
}
function $(id){
	return document.getElementById(id);
}

var nbitems=6;
var speed=10;
var inter, items;

function init(rubrique, id){
	var rub=new Array('roughs','photoroughs','illustration','animatics','contact','plaquette');
	for(var i=0;i<nbitems;i++){
		$("p"+i).i=i;
		$("p"+i).rubrique=rub[i];
		$("p"+i).open=false;
		$("b"+i).style.cursor="pointer";
		$("b"+i).style.cursor="hand";
		disableSelection($("b"+i))
		$("p"+i).speed=$("i"+i).offsetHeight/45
		$("b"+i).onclick=press;
		$("p"+i).style.height=17+"px";
		$("i"+i).style.top=-$("i"+i).offsetHeight+"px"; //init des positions
	}
	if(rubrique!=""){
		show(rubrique, id);
	}
}
function press(e){
	  e = e || event;
	  var target = e.srcElement || e.target;
	  items=new Array(false,false,false);
	  var current=target.parentNode;
	  for(var i=0;i<nbitems;i++){
		  var item=$("p"+i);
		  if(current!=item){
			  item.open=false;
			  $("b"+i).style.color="#666"; // les boutons
			  $("b"+i).onmouseover=function(e){this.style.color="#000"}
			  $("b"+i).onmouseout=function(e){this.style.color="#666"}
		  }else{
			  current.open=current.open?false:true;
			  $("b"+i).style.color="black";
			  $("b"+i).onmouseover=function(e){this.style.color="#000"}
			  $("b"+i).onmouseout=function(e){this.style.color="#000"}			  
			  var rubrique=item.rubrique;
		  }
	  }
	  clearInterval(inter);
	  inter=setInterval("move('"+rubrique+"')", 1);
}
	//----------------------------------------------------------------
	temp=0;
	function check(rubrique,open){
		var check=true;
		for(var i=0;i<nbitems;i++){
			if(!items[i]) check=false;
		}
		if(check){
			clearInterval(inter);
			positions();
			if(open && !(rubrique=="contact" || rubrique=="plaquette")){
				document.location.href="./"+rubrique;
			}
		}
	}
	function move(rubrique){ // ici les positions sont fausses (car tien compte de la vitesse dans la position) corrigée parpositions()
		var py, pi, item;
		for(var i=0;i<nbitems;i++){
			item=$("p"+i);
			if(item.open){ // ouverture
				if(parseInt($("i"+item.i).style.top)<0){
					py=item.offsetHeight+speed+"px";
					pi=parseInt($("i"+item.i).style.top)+speed+"px";
					item.style.height=py
					$("i"+item.i).style.top=pi;
				}else{
					items[item.i]=true;
					check(rubrique,true);
				}
			}else{ // fermeture
				if(parseInt($("i"+item.i).style.top)>-$("i"+item.i).offsetHeight){
					py=item.offsetHeight-speed+"px";
					pi=parseInt($("i"+item.i).style.top)-speed+"px";			
					item.style.height=py;
					$("i"+item.i).style.top=pi;
				}else{
					items[item.i]=true;
					check(rubrique,false);
				}
			}
		}
		
	}
	function show(rubrique, id){
			var item;
			var rub={roughs:0,photoroughs:1,illustration:2,graphisme:2,animatics:3,moodboards:3}
			a=$("p"+rub[rubrique]);
			b=$("i"+rub[rubrique]);
			c=$("b"+rub[rubrique]);
			$("p"+rub[rubrique]).open=true;
			c.style.color="black";	
			positions();
	}
	function positions(){
		for(var i=0;i<nbitems;i++){
			var item=$("p"+i);
			if(item.open){ //ouvert
				$("i"+i).style.top=0+"px";
				item.style.height=$("i"+i).offsetHeight+17+"px";
			}else{ //fermé
				$("i"+i).style.top=-$("i"+i).offsetHeight+"px";
				item.style.height=17+"px";
			}
		}	
	}
/*---------------------------------------------------------*/

function mail_core(x){
	var m="";
	for(i=0;i<x.length;++i){
		m+=String.fromCharCode(1^x.charCodeAt(i));
	}
	return m;	
}

function mail(x){
	window.location=mail_core('l`hmun;'+x);
}

function mail_write(x){
	document.writeln(mail_core(x));
}

/*---------------------------------------------------------*/

function setScroll(y){
	$("maincontent").scrollTop=y 
}

/*---------------------------------------------------------*/
function swap(id, over){
	if(over){
		for(i=0;i<nbpages;i++){
			if(i!=id){
				$('page_'+i).style.display="none";
			}else{
				$('page_'+i).style.display="block";
			}
		}
		$('page').style.display="none";
		$('pages').style.display="block";
	}else{
		$('page').style.display="block";
		$('pages').style.display="none";
	}
}

