
   var theJSON;
   //Creates pastel colors
   //Returns: hex of randomly generated color

   var colors = new Array();
   var i = 0;
   for (i=0;i<20;i++) {
      colors.push(pastel());
   }

   colors.push("#99B3FF");
   colors.push("#B399FF");
   colors.push("#E699FF");
   colors.push("#FF99E6");
   colors.push("#99E6FF");
   colors.push("#5C85FF");
   colors.push("#FFC71F");
   colors.push("#FFD65C");
   colors.push("#99FFB3");
   colors.push("#B3FF99");
   colors.push("#E6FF99");
   colors.push("#FFE699");

/*   Toggles the display of the visible classes x
     Ignores alternates
     Should maintain the state with deClass/enClass
     
     k: uid
*/
   function toggleBlock(k){
      var id = "." + k;
      if($(".subpanel input[type=checkbox][value="+k+"]").attr("checked") == false) {
         $(id).not(".alternate").fadeOut().addClass("view-disabled");
         $("#legend td:contains("+k+")f").parent("tr").remove();
         deClass(k);
         viewConflict();
      } else {
         //check validity first?
        
         var fail = false;
         for(var t in viewTime[k]) {
            //console.log(k + " " + t + " " + viewTime[k][t][0]);
            fail = fail || !noConflict(k, t, viewTime[k][t][0]);
         }
        
        if (fail && !ignoreConflict) {
          $(".subpanel input[type=checkbox][value="+k+"]").attr("checked",false);
          $(id).not(".alternate").hide();
          $("#console p").html('<span class="console-error">Conflict prevented</span>');
        } else {
         $(id).not(".alternate").hide().fadeIn().removeClass("view-disabled");
          $("#legend").append("<tr><td>"+k+"</td><td>"+theJSON[k].name+"</td><td>"+theJSON[k].fullname+"</td><td>"+theJSON[k].credits+"</td></tr>");
          enClass(k);
          viewConflict();
        }
      }
      countCredits();
   }

   //counts the number of credit hours of visible classes, by running through the
   //checkboxes
   function countCredits() {
       var counter = 0;
      $(".subpanel input[type=checkbox]:checked").each(function() {
         var x = $("."+$(this).attr("value")).find("h4").html();
         
           for (var k in theJSON) {
             if (removeSpaces(theJSON[k].uid) == removeSpaces($(this).attr("value"))) {
                 x = theJSON[k].credits.replace(/-[0-9]?$/g,"");
                 break;
             }
           }
         counter = counter + x*1;
      });
      $(".credits").find("h1").html(counter);
   }

   function displayDescription(b) {//needs to handle sections
     // $("."+b).not(alternate)
     $(".description .desc_content").html(
       "<h3>"+theJSON[b].name+"</h3><h2>"+theJSON[b].fullname+"</h2><h4>"+theJSON[b].credits+" credits</h4><p>"+theJSON[b].desc+"</p>"
       );   
       
     $("#console p").text(removeSpaces(theJSON[b].name) +": "+ theJSON[b].fullname.substr(0,70));
   }

   function postRender(st) {
     // $("input[type=hidden]").val($.toJSON(st));
     // $("#render_form").submit();
     if(st) {
       $("#console").hide();
       $("#sidebar").hide();
       
       $(".col.time").width(40);
       $(".col.saturday").hide();
       $(".tick").width(33);
       $(".col").not(".time").width(100);
       $(".block").width(95);
       
       $("#maincontent2").width(760);
       $("#container-wrap").css("border", "1px solid gray").css("padding", 10).css("margin", "15px auto").width(700);
       $("#container").width(700).css("float", "none");
       $("#schedule_link").click(function(){postRender(false);});
     } else {
       $("#console").show();
       $("#sidebar").show();
       
       
      $("#maincontent2").width(810);
       $(".col.time").width(85);
       $(".time").width(83);
       
       $(".col").not(".time").width(90);
       $(".block").width(82);
       $("#container-wrap").css("border", "none").css("padding", 0).css("margin", "0").width(665);
       $("#container").width(665).css("marginLeft","").css("marginTop","");
       $("#schedule_link").unbind('click').click(function(){postRender(true);});
     }
   }

function init_helper(json){
      theJSON = $.evalJSON(json);

      for (var c in theJSON) {
         createDiv(theJSON[c]);
      };

      /** load first state */
      $("input[type=radio][name=state]:first").attr("checked","checked");
           displayState();

      /****************      UI EFFECTS...      ******************/

      $("form[name=selectedClasses] input[type=checkbox]").bind(($.browser.msie ? "click" : "change"),(function(){
         toggleBlock($(this).attr("value"));
      }));

      $("form[name=selectedClasses] label").mouseover(function(){
         var x = $(this).find("input").attr("value");
         displayDescription(x);
      });

      $(".block").mouseover(function(){
         var desc = $(this).find('p').text();
         if(desc != "")
         displayDescription(desc);
      })

      $("input[name=state][type=radio]").live("click",function(){
         displayState();
      });

      //UI EFFECTS... dragging and dropping
      $(".block.hasMore").draggable({
         helper: "clone",
         opacity: ".8",
         zIndex: 100
      });
      $(".alternate").draggable("disable");
      
      $(".hasMore").hover(
        function(){
          //$(this).not(".alternate").css("border-bottom","1px solid black").css("border-right","2px solid black");
          $(this).not(".alternate").fadeTo("fast",.6);
        },
        function(){
          //$(this).not(".alternate").css("border","");
	$(this).not(".alternate").fadeTo("fast",1);
        });

      $(".tick:even").css('background','rgb(238, 238, 238)');
      
      $(".description").draggable({containment:'document'}).resizable();
      
      // hack to take care of .alternateactive not disappearing sometimes.
      $(document).mouseup(function() {$('.alternateactive').removeClass('alternateactive');});

      /***
      CONTROLS
      **/
      
      
      
      $(document).not('input').keypress(function(e){
        
        //Binny V A, openjs.com
  				var element;
  				if(e.target) element=e.target;
  				else if(e.srcElement) element=e.srcElement;
  				if(element.nodeType==3) element=element.parentNode;

  				if(element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') return;

  			//Find Which key is pressed
  			if (e.keyCode) kcode = e.keyCode;
  			else if (e.which) kcode = e.which;
  			
        if (kcode > 47 && kcode < 58) {
          if (kcode == 48) {
            $('.subpanel input[type=checkbox]').eq(9).trigger('click').trigger('change');
          } else {
            $('.subpanel input[type=checkbox]:eq('+(kcode - 49)+')').trigger('click').trigger('change');
          }
        }
      });
      
      // check for IE: change doesn't work in ie
      $("input[name=show_desc]").bind(($.browser.msie ? "click" : "change"),(function(){
         if($(this).attr("checked") == true) {
            $(".description").fadeIn();
         } else {
            $(".description").fadeOut();
         }
      }));
      
      $("input[name=show_room]").bind(($.browser.msie ? "click" : "change"), (function(){
         if($(this).attr("checked") == true) {
            $(".block h6").show();
         } else {
            $(".block h6").hide();
         }
      }));
      
      $("input[name=show_time]").bind(($.browser.msie ? "click" : "change"), (function(){
         if($(this).attr("checked") == true) {
            $(".block h5").show();
         } else {
            $(".block h5").hide();
         }
      }));
      $("input[name=show_conflict]").bind(($.browser.msie ? "click" : "change"), (function(){
         if($(this).attr("checked") == true) {
            ignoreConflict=true;
         } else {
            ignoreConflict=false;
         }
      }));
      
      
      countCredits();
  
}

function scheduleinit() {
  $.ajax({
     type: "GET",
     url: "ajax/getpe.php",
     dataType: "text",
     success: function(json) {
          $.cqe.init(json);
        } //end success function xml

     });
  $.ajax({
     type: "GET",
     url: "scripts/getxml.php", 
     dataType: "text",
     success: function(json) {
          init_helper(json);
        } //end success function xml
      
     });



}