<%@ page language = "java"%>
<%@ page import = "java.util.*"%>
<%@ page import = "java.sql.*"%>
<%@ page import = "java.io.*"%>
<%@ page import = "java.net.*"%>
<%@ page import = "ajax.*"%>

<link rel="stylesheet" href="css/mysite.css" type="text/css"/>
<script type="text/javascript" src="js/ajax.js"></script>
<br>

<%!

public String printTag(String name, int size) {
      String toRet="";
			toRet += ("<a style=\"font-size: "+size+"px\" class=\"cloud_link\"onmouseover=\"javascript:cloudOver(this);\" onmouseout=\"javascript:cloudOut(this);\" onclick=\"javascript:getBandsFromTag('"+name+"','main_window'); disableForum();\"   >");
      toRet += (name+"</a>");
      return toRet;
}

%>


<%

      int max_font_size=30;
      int min_font_size=12;

		Connection conn = null;
  String bandname="";
           try
           {
               String userName = "root";
               String password = "";
               String url = "jdbc:mysql://localhost/band";
               Class.forName ("com.mysql.jdbc.Driver").newInstance();
               conn = DriverManager.getConnection (url, userName, password);
               System.out.println ("Database connection established");
           
           
      Statement s = conn.createStatement ();
			String query= ("SELECT name, COUNT(tagged.band_id) AS num FROM tagged,tags WHERE tagged.tag_id = tags.id GROUP BY tagged.tag_id");
			System.out.println(query);			
			s.executeQuery (query);
			ResultSet rs = s.getResultSet ();
              
      String tagname="";
      out.println("<div class=\"cloud\">");
      out.println("<div align=\"center\"><font size=5><b>Tag Cloud</b></font></div><br><br>");
      
      Vector<Tag> v = new Vector<Tag>();
      int countall=0;
           
		while (rs.next ())
		{
			
			v.add(new Tag (rs.getInt("num"),rs.getString("name")));
			countall+=rs.getInt("num");
			
		}
		
		int min=Integer.MAX_VALUE;
		int max=0;
		
		
		for (int i=0; i<v.size();i++) {
		int num = v.elementAt(i).getId();
		if (num>max) max=num;
		if (num<min) min=num;
		}
		
		
		
		for (int i=0; i<v.size();i++) {
		int size =  min_font_size + ( (v.elementAt(i).getId() - min)) * (max_font_size - min_font_size) / (max-min);		
		out.println(printTag(v.elementAt(i).getTagName(), size));
		}
		
		
		
		out.println("</div>");
		
		rs.close ();
		s.close ();
 
	}
           
                      catch (Exception e)
           {
               System.err.println ("database error");
               System.err.println (e);
           }
           
           finally
           {
               if (conn != null)
               {
                   try
                   {
                       conn.close ();
                       System.out.println ("Database connection terminated");
                   }
                   catch (Exception e) { /* ignore close errors */ }
               }
           }
           

%>










