window.onload = function()
{	
  /* set stage */
  hide_all_markers();
  hide_all_lines();
  hide_all_schedules();
  
  /* hide all markers function*/
	function hide_all_markers(){
	  var markers = $$('div.marker');
  	$A(markers).each(function(a) {
  	  a.hide();
  	
  	  a.onmouseover = function(){
	      /*var elements = a.childElements();
	      elements[3].show();*/
	    };
	    
	    a.onmouseout = function(){
	      var temp_a =  a.identify();
	      var tab_a = temp_a.split("-");
	      $("line-" + tab_a[2]).removeClassName("highlight");
	      hide_all_schedules();
	    }
	    
  	});
	}

	/* hide all lines function*/
	function hide_all_lines(){
	  var highlight_lines = $$('div.highlight-line');
  	$A(highlight_lines).each(function(a) {a.hide();
  	  });
  }

  /* hide all schedules function*/
	function hide_all_schedules(){
    var schedules = $$('div.schedules');
  	$A(schedules).each(function(a) {a.hide();});
  }
  
	var lines = $$('div.line');
	$A(lines).each(function(a) {

		a.onmouseover = function(){

      if(a.hasClassName('highlight') == false) {
  			a.addClassName('highlight');
  		}
			
			var schedule = "schedules-" + this.identify();
			$(schedule).show();
			      
      var marker = "marker-" + a.identify();
      $(marker).show();
      
			var marker_highlight = "highlight-" + a.identify();
			$(marker_highlight).show();

		};
		
		a.onmouseout = function(){
		  
		  this.removeClassName('highlight');
		  var schedule = "schedules-" + a.identify();
			$(schedule).hide();
			
			var marker = "marker-" + a.identify();
			$(marker).hide();
			//hide_all_markers();
			var IE = document.all?true:false;
			if(!IE){
        hide_all_lines();
      }else{

        var highlight = "highlight-" + a.identify();
  			$(highlight).hide();
      }
		};
	});
	
	
	function appear_line(number){
	  hide_all_markers();
	  hide_all_lines();
  	var lines = $$('div.line');
  	$A(lines).each(function(a) {
	    a.removeClassName('highlight');
	  });
	  
	  
	  $("highlight-line-"+ number).show();
	  
	  //$("marker-line-" + number).show();
	  //new Effect.Appear("marker-line-" + number);
	  $("marker-schedules-line-" + number).show();
	  $("line-" + number).addClassName('highlight');
    $("schedules-line-"+ number).show();
    $("marker-line-" + number).show();
    //new Effect.Appear("marker-line-" + number, {duration : 0.5});
	  
	  function findPos(el) {
    	var x = y = 0;
    	if(el.offsetParent) {
    		x = el.offsetLeft;
    		y = el.offsetTop;
    		while(el = el.offsetParent) {
    			x += el.offsetLeft;
    			y += el.offsetTop;
    		}
    	}
    	return {'x':x, 'y':y};
    }
    
	   truc = number;
  	  $('highlight-line-'+ number).onmouseover = function(e){
        	var ev = e || window.event;
        	var IE = document.all?true:false;
        	//var pos = findPos(document.getElementById('highlight-line-'+truc));
        	var pos = document.getElementById('map');
        	var diffx = 0;
        	var diffy = 0;
        	if (IE) {
   
        	  diffx = ev.clientX + document.documentElement.scrollLeft;
        	  diffy = ev.clientY + document.documentElement.scrollTop; 
        	 
        	  
      	  }else{
        	  diffx =  ev.pageX;
        	  diffy =  ev.pageY;
          }
          
        	diffx -= pos.offsetLeft;
        	diffy -= pos.offsetTop;
          
        	$('marker-line-'+ truc).setStyle({
            left: diffx-20 +"px"
          });
          
          $('marker-line-'+ truc).setStyle({
            top: diffy-20 + "px"
          });

        
        /*
  	    var IE = document.all?true:false;
        if (!IE) document.captureEvents(Event.MOUSEMOVE)

        getMouseXY(id);
        var tempX = 0;
        var tempY = 0;

        function getMouseXY(e, id) {
          if (IE) { // grab the x-y pos.s if browser is IE
          document.body.scrollLeft = 0;
          document.body.scrollTop = 0;  
          tempX = event.clientX + document.body.scrollLeft;
          tempY = event.clientY + document.body.scrollTop;
          }
          else {  // grab the x-y pos.s if browser is NS
          tempX = e.pageX;
          tempY = e.pageY;
          alert(e.pageX);
          }  
          if (tempX < 0){tempX = 0;}
          if (tempY < 0){tempY = 0;}

          $('marker-line-'+ truc).setStyle({
            left: tempX-400 +"px"
          });
          
          $('marker-line-'+ truc).setStyle({
            top: tempY-400 + "px"
          });

          return true;
        }*/
  	  }
	}
	
	function line(number) {
	  hide_all_markers();
	  hide_all_lines();
	  //hide_all_schedules();
	  $("highlight-line-" + number).hide();
	  $("marker-line-" + number).hide();
	 // new Effect.Fade("marker-line-" + number, {duration : 0.5});
	  $("line-" + number).removeClassName('highlight');
	  var a = $("line-" + number);
	  var elements = a.childElements();
    elements[4].hide();
	}
	
	function attach_line_behavior(id, number){
	  var i = 1
	  for(i; i<= number; i++){
	    $('line-' + id + '-part-' + i).onmouseover = function(){
	      appear_line(id);
	    }
	  }
   

	  $('highlight-line-'+ id).onmousemove = function(){
	    hide_all_markers();
  	  hide_all_lines();
	    this.hide();
	    //hide_all_schedules();
	  }
	  
	  $('highlight-line-'+ id).onmouseout = function(){
	    hide_all_markers();
  	  hide_all_lines();
      line(id);
	  }
	}
	

	
	// line 1
	attach_line_behavior(1, 4);
  // line 2
  attach_line_behavior(2, 2);
  // line 3
  attach_line_behavior(3, 2);
  // line 4
  attach_line_behavior(4, 4);
  // line 5
  attach_line_behavior(5, 3);
  // line 6
  attach_line_behavior(6, 8);
  // line 7
  attach_line_behavior(7, 1);
  // line 8
  attach_line_behavior(8, 3);
  // line 9
  attach_line_behavior(9, 1);
  // line 10
  attach_line_behavior(10, 3);
  // line 11
  attach_line_behavior(11, 2);
  // line 12
  attach_line_behavior(12, 3);
  // line 13
  attach_line_behavior(13, 2);
  // line 14
  attach_line_behavior(14, 2);
  // line 15
  attach_line_behavior(15, 2);
  // line 16
  attach_line_behavior(16, 6);
  // line 17
  attach_line_behavior(17, 2);
  // line 18
  attach_line_behavior(18, 1);
  // line 19
  attach_line_behavior(19, 2);
  // line 20
  attach_line_behavior(20, 1);
  // line 21
  attach_line_behavior(21, 1);
}
