<?php

    Header("Content-Type: image/png");
    $im_width = 300;
    if(isset($_GET["width"])){      
		$im_width  = $_GET["width"];
	}	    
    $im_height = 30;
    $im = ImageCreate($im_width,$im_height);

    $black = ImageColorAllocate($im, 0, 0, 0);
    $white = ImageColorAllocate($im, 255, 255, 255);
	$fond  = ImageColorAllocate($im, 80, 95, 112);
	imagecolortransparent($im, $fond);
	
    include("connection.php");

	imagefilledrectangle ($im, 0, 0, $im_width, $im_height, $fond);
  	
    if(isset($_GET["id"])){
      
		$prom_id = $_GET["id"];
      	//get all the data necessary to draw the picture...      	
		
		imageline ( $im, 0, $im_height/2, $im_width, $im_height/2, $black);

		$khoueiry = "SELECT \"BEGIN\", \"END\" FROM \"PROMOTER\" WHERE \"PROMOTER_ID\"=$prom_id;";		
		
		$stmt=pg_query($c,$khoueiry);

		if ($row=pg_fetch_array($stmt)) {
  			$begin = $row["BEGIN"];
  			$end = $row["END"];
  			$size = $end - $begin;
  			if(($begin*$end)<0){
  			  	//POSITION 0 does not exist
			    $size = $size-1;
			}
		  
			$khoueiry = "SELECT A.\"MOTIF_ID\", B.\"NAME\", B.\"RGB_RED\", B.\"RGB_GREEN\", B.\"RGB_BLUE\", A.\"SEQUENCE\", A.\"BEGIN\", A.\"STOP\" FROM \"MOTIF_COORD\" A, \"MOTIF_REGUL\" B WHERE A.\"PROMOTER_ID\"=$prom_id AND A.\"MOTIF_ID\"=B.\"MOTIF_ID\" ORDER BY A.\"BEGIN\";";
			$stmt=pg_query($c,$khoueiry);

			$motifs = array();
			$count = 0;
			$xx = array();
			$yy = array();
			$zz = array();
			while ($row=pg_fetch_array($stmt)) {
			  	$count++;
				$id=$row["MOTIF_ID"];	
				//not very efficient but...
				$xx[$id]=$row["RGB_RED"];
				$yy[$id]=$row["RGB_GREEN"];
				$zz[$id]=$row["RGB_BLUE"];
			  	$row["BEGIN"] = $row["BEGIN"] - $begin;
			  	if(($row["BEGIN"]*$begin)<0){ $row["BEGIN"] = $row["BEGIN"] - 1; }		  	
			  	$row["STOP"] = $row["STOP"] - $begin;
			  	if(($row["STOP"]*$begin)<0){ $row["STOP"] = $row["STOP"] - 1; }
			  	$row["COUNT"]=$count;
			  	$motifs[$id][] = $row;
			}
			
			$barheigth=10;
 		    $decal=5;

			foreach($motifs as $mot_id=>$motif_list){
			    $color  = ImageColorAllocate($im, $xx[$mot_id], $yy[$mot_id], $zz[$mot_id]);
       			//$color2 = ImageColorAllocate($im, $xx[$mot_id]-15, $yy[$mot_id]-15, $zz[$mot_id]-15);
    			//$color3 = ImageColorAllocate($im, $xx[$mot_id]-30, $yy[$mot_id]-30, $zz[$mot_id]-30);
				foreach($motif_list as $motif){
					$x1=($motif["BEGIN"]*$im_width)/$size;
			    	$x2=($motif["STOP"]*$im_width)/$size;
				    //imagefilledrectangle ($im, $x1, 0+$decal, $x2, $barheigth+$decal, $color3);
	                //imagefilledrectangle ($im, $x1, 3+$decal, $x2, $barheigth+$decal-3, $color2);
   					//imagefilledrectangle ($im, $x1, 5+$decal, $x2, $barheigth+$decal-5, $color);
					imagefilledrectangle ($im, $x1, $decal, $x2, $barheigth+$decal, $color);
   					   					
    				imagerectangle ($im, $x1, 0+$decal, $x2, $barheigth+$decal, $black);
	       			//ImageString($im, 0,  $x1+(($x2-$x1)/2), 1+$decal, $names[$mot_id], $black);
	       			if(!isset($_GET["nbrs"]) || $_GET["nbrs"]!="false"){
						if($decal>=9){
							ImageString($im, 0,  $x1+(($x2-$x1)/2)-1, $decal-$barheigth, $motif["COUNT"], $white);
						} else {
							ImageString($im, 0,  $x1+(($x2-$x1)/2)-1, $barheigth+$decal+1, $motif["COUNT"], $white);
						}
						//ImageString($im, 0,  $x1+(($x2-$x1)/2)-1, 1+$decal, $motif["COUNT"], $white);
					}	       			
	       			//Add a + or - on top of the drawing... extend a bit the height of the picture
				}
				if($decal==19){$decal=-2;}
    			$decal+=7;
			}		

		}

    } else {
      	imageline ( $im, 0, $im_height/2, $im_width, $im_height/2, $white);
    }
    
    ImagePNG($im);
    ImageDestroy($im);

?>
