//replaces j2

var col_monday = "";
var col_tuesday = "";
var col_wednesday = "";
var col_thursday = "";
var col_friday = "";
var col_saturday = "";
var col_sunday = "";
var types = new Array("lec","sem","dis","lab","cln","fld","ind","rsc","stu","ta");

var typelist = {lec:"Lecture",sem:"Seminar",dis:"Discussion",lab:"Laboratory",cln:"Client",fld:"Field",ind:"Independent",rsc:"Research",stu:"study",ta:"TA"};
var viewstate = {};
var viewTime = {};
var conflicted = false;

/*****************************************************************************
******************* RENDER-INIT helper FUNCTIONS *****************************
*****************************************************************************/
//@days : takes in a string of the days, a subset of the letters MTWRF
//@block : the code for the day
//populate appends the block to the appropriate day variable
function populate(days,block){
	if (days.indexOf('M') > -1) {
		col_monday = col_monday + block;
	}
	if (days.indexOf('T') > -1) {
		col_tuesday = col_tuesday + block;
	}
	if (days.indexOf('W') > -1) {
		col_wednesday = col_wednesday + block;
	}
	if (days.indexOf('R') > -1) {
		col_thursday = col_thursday + block;
	}
	if (days.indexOf('F') > -1) {
		col_friday = col_friday + block;
	}
}

function columnsToDom(){
  if (col_monday.length > 0) {
    $(".monday").append(col_monday);
    col_monday = "";
    }
  if (col_tuesday.length > 0) {
    $(".tuesday").append(col_tuesday);
    col_tuesday = "";
    }
  if (col_wednesday.length > 0) {
    $(".wednesday").append(col_wednesday);
    col_wednesday = "";
    }
  if (col_thursday.length > 0) {
    $(".thursday").append(col_thursday);
    col_thursday = "";
    }
  if (col_friday.length > 0) {
    $(".friday").append(col_friday);
    col_friday = "";
    }
}

/*****************************************************************************
******************* RENDER-INIT FUNCTIONS ************************************
*****************************************************************************/


/*retrieve class information from a class block in the xml
  return: associative array ('unit'):
  full name, credit hours, description,
  start time, end time, days held, duration fo the class, and the position for 
  the div
  */
function parseunit(x){
  var unit = $.extend(true,{},x);
  return unit;
}

/* 
  createDiv takes in a class object, x, and generates the divs associated with it.
  */
function createDiv(x) {
  var cls = x;//parseunit(x); //200ms?
  var nameID = cls.uid;
  var radios = "";
  viewTime[nameID] = [];
  
  for (var j in types) {
    if (cls[types[j]]) {
      var type = types[j];
      var dropid = "."+type+"."+nameID;
      var showfirst = true;
      //var chosen = cls["option"][type];
      var count = 0;
      for (var c in cls[type]) count++;
            viewTime[nameID][type] = [];
        for (var i in cls[type]){
            var thisunit = cls[type][i];
            if (thisunit.days == null) continue; //classes with no meeting
            var alt = thisunit["alt"];

            var block = "<div class = 'block alternate "+
                          type + " " + nameID + ((count > 1) ? " hasMore" : "") + "'";
            block += " st='" + alt + "' ";
            block += "style = '";
            block += "margin-top: " + (thisunit["position"]) + "px;";
            block += "height: " + (thisunit["duration"]) + "px' >";
  			
            //expand schedule size as necessary
            
            var newH = thisunit.position + thisunit.duration;
                
            if (newH > 600) {
              newH = 60* Math.ceil(newH/60);
                if (newH >= $(".col:first").height()) {
                var last = $(".tick:last").text();
                for (var k = 0; k < (newH-600)/60; k++) {
                    if (6 + k > last)
                        $(".col.time").append('<div class = "tick">'+(6+k)+'</div>');
                }
                $(".col").css("height", newH + "px");
                $("#container").css("height", newH + "px");
              }
            }
            
            //displays the 'chosen' block, based on option field (old)
            // show the first one by default
            if (showfirst) {
              showfirst = false;
              block = block.replace("alternate","");
              block += "<h3>"+cls.name+"</h3>"+
                  "<h2>"+cls["fullname"]+"</h2>"+
                  "<h4>"+cls["credits"]+" credits</h4>"+
                  "<h5>"+time2nice(thisunit["start"])+"-"+time2nice(thisunit["end"])+"</h5>"+
                  "<p>"+nameID+"</p>";
              viewTime[nameID][type] = [alt, thisunit["start"], thisunit["end"]]
            }
            
            block += "<h6>"+thisunit["room"]+"</h6>";
            block += "</div>";
            populate(thisunit["days"],block);
        }
    
        
      //insert them in dom
      columnsToDom();
      
      // Dropped: swaptime
      // Hover: check for collision
      var name = removeSpaces(cls.name);
      with({'t' :type, 'n': nameID}) {
        $(dropid).droppable({
    			accept: dropid,
    			activeClass: 'alternateactive',
          tolerance: 'pointer',
    			drop: function(ev, ui) {
                        swapTime(t,n,$(this).attr('st'));},
    			over: function(ev,ui) {
                        var to = $(this);
                        var to_top = to.offset().top;
                        var to_bot = to_top+to.height();

                        to
                        .siblings('.block')
                        .not('.alternate').each(function() {
                          var top = $(this).offset().top;
                          var bot = top + $(this).height();

                          if (top >= to_top && top < to_bot || bot > to_top && bot <= to_bot) {
                            //$(this).css("background","red");
                          }
                        });
    			}
    		})
    		.not('.alternate').droppable("disable")
                    		  .css("background",colors.pop());
      }
    }
  }//end of for types
  
  
  // $("."+nameID).not('.alternate').append(radios);
  
  var checked;
	if ($(x).attr("show") == "true") {
		checked = true;
	} else {
		$("."+ nameID).not('.alternate').css("display","none").addClass("view-disabled");
		checked = false;
	}
	
	//Create display checkboxes
	$("<label></label>")
		.append($("<input type = 'checkbox' />").attr("value",nameID).attr("checked", checked))
		.append(cls.name)
		.append("<br />")
		.appendTo("form[name=selectedClasses]");
  
}


/**************************************************************
******************* STATE FUNCTIONS ***************************
***************************************************************

  These functions maintain the internal model of the state, 
  which is stored in the variable viewstate.
  
*/

/*
  enclass(c) adds the class "c" to the viewstate
  u: uid
*/
function enClass(u) {  
  var cls = $('.'+u).not('.alternate');
  for (var i in types) {
    var t = types[i];
    var v = cls.filter('.'+t).eq(0).attr('st');
    if (v != null && v != "") {
      if (!viewstate[u]) {
        viewstate[u] = new Object();
        viewstate[u]["uid"] = u;
        viewstate[u].name = removeSpaces(theJSON[u].name);
      }
      viewstate[u][t] = v;
    }
  }
}

function deClass(u) {
  delete viewstate[u];
}

/*
  Takes in a xml representation of all the classes and parses it for visible
  classes. Appends the json representation of the state to gstate, using the 
  insert id of the json (generated when the json rep is stored in mysql)
*/
function getState(n) {
  var state = $.extend(true,{},viewstate);
  state.more = {};
  state.more.shared = false;
  state.more.primary = false;
  state.more.events = $.extend(true,{},eventstate);
  
  if ($("input[type=text][name="+n+"]").attr("value").length > 0) {
    state.more.name = $("input[type=text][name="+n+"]").attr("value");
    $("input[type=text][name="+n+"]").attr("value", "");
  } else {
    state.more.name = "Saved State";
  }

  with({json_st : $.toJSON(state)}) {
  $.post("ajax/st_savermv.php", 
         {'add': json_st}, 
         function(st_id) {
           if(st_id != "fail") {
             
             $(".state")
             .append($("<label><input type='radio' name='state' value = "+st_id+" /></label>")
             .append(state.more.name))
             .find("input").attr("checked",true);}       

             if(n == 'quickname') {
               var x = "<tr><td class='first'><a target='_share' href='/share/"+code+"/"+st_id+"'>"+state.more.name+"</a></td>";
               x += "<td><input type='checkbox'";
               x += " name='share' value='"+st_id+"' id='share' /></td></tr>";
               $("#console_menu_actions_share table").append(x);
             }

             
             //deepcopy
             globalstate[st_id] = $.extend(true,{},state);
             
             return true;
           }
        );
  }
  
  //setup DOM
  return false;
}

/*
  changes the view to the saved state as indicated by the radio buttons
*/  
function displayState() {
  //reset gui
	$(".block").not(".alternate").hide().addClass("view-disabled");
	var j = $("input[type=radio][name=state]:checked").attr("value");
	$(".panel input[type=checkbox]").attr("checked",false);
  var ignore = ignoreConflict;
  ignoreConflict = true;
  
    //this block catches change in section types
    //e.g. dis -> lec
  for (var k in globalstate[j]) {
    if (k == "more") continue;
    for (var l in types) {
        var t = types[l];
        if (!theJSON[k])
          delete globalstate[j][k];
        else if (!theJSON[k][t])
          delete globalstate[j][k][t];
      }
  }
  
	viewstate = $.extend(true,{},globalstate[j]);
	

	
	var legnd = $("#legend")
	legnd.html("");
	
	// Classes, k
	for (var k in globalstate[j]) {
	  // catch updates to the schedule etc.
    if(!theJSON[k]) continue;
    
    uid = globalstate[j][k]["uid"];
	  var cls = $("input[type=checkbox][value="+uid+"]");
    
	  if(cls.size() > 0){
	    cls.attr("checked",true);
	    legnd.append("<tr><td>"+k+"</td><td>"+theJSON[k].name+"</td><td>"+theJSON[k].fullname+"</td><td>"+theJSON[k].credits+"</td></tr>");
	    for (var l in types) { //meetings
        var t = types[l];
	      var i = globalstate[j][k][t];
	      
	      if (i != null) {
	        $('.'+uid).not(".alternate").show().removeClass("view-disabled");
    	    swapTime(t,uid,globalstate[j][k][t]);
  	    }
      }
	  } else { //check if it exists before deleting?
	    delete viewstate[k];
	  }
	}
	
	// Events, l
	
	if (globalstate[j]) {
  	eventstate = $.extend(true,{},globalstate[j]["more"]["events"]);
  	for (var l in globalstate[j]["more"]["events"]) {
  	  $.cqe.showEvent(l);
  	}
  }
	
  ignoreConflict = ignore;
	countCredits();
}

function removeState() {
  //reset 
	var j = $("input[type=radio][name=state]:checked").attr("value");
  $.post("ajax/st_savermv.php", 
         {'rid': j},
         function(x){
           $("input[type=radio][name=state]:checked").parent("label").remove();
           delete globalstate[j];
         }
        )
}


/*

*/
//takes a div, returns a string of the time of the class represented by the div
function getBlockTime(block){
   var start = block.css("margin-top");
   start = parseInt(start.substring(0,start.length-2)) + 480;
   var end = start + parseInt(block.css("height")) + 10;
   start = min2time(start);
   end = min2time(end);
   return start + "-" + end;
}

function viewConflict() {
  for (var i in viewstate) {
    for (var j in viewTime[i]) {
      if (!noConflict(i,j,viewTime[i][j][0])) {
        $("#containerinfo").show();
        conflicted = true;
        return true;
      }
    }
  }
  
    $("#containerinfo").fadeOut();
  conflicted = false;
  return false;
}

/*
 checks for conflicts
*/
function noConflict(uid, t, a) {
  if (!theJSON[uid][t] || !theJSON[uid][t][a]) return true;
  var start = theJSON[uid][t][a]["start"];
  var end = theJSON[uid][t][a]["end"];
  var days = theJSON[uid][t][a]["days"];
  
  for (var x in viewstate) {
    if (x == uid || x == "more") continue;
      for (var y in viewstate[x]) {
        if (y == "uid" || y == "name" || !theJSON[x]) continue;
        
        var target = theJSON[x][y][viewstate[x][y]];
        var daylap = false;
        
        for (var i = 0; i < days.length; i++) {
          if(target["days"].indexOf(days.charAt(i)) != -1) {
              daylap = true;
          }
        } 
        
        if (daylap && ((start >= target["start"] && start < target["end"]) ||
                          (end > target["start"] && end <= target["end"]) ||
                        start <= target["start"] && end >= target["end"])) {
          // console.log(theJSON[uid]["name"] + " " + start + " " + end + " " + y + " " 
          //+ theJSON[x]["name"] +" "+ target["start"] + " " + target["end"]);
          return false;
        }
      }
  }
  
  for (var e in eventstate) {
    if (!thePEvents[e]) continue;
    var target = thePEvents[e];
    var daylap = false;
    for (var i = 0; i < days.length; i++) {
      if(target["days"].indexOf(days.charAt(i)) != -1) {
          daylap = true;
      }
    } 
    
    if (daylap && ((start >= target["stime"] && start < target["etime"]) ||
                      (end > target["stime"] && end <= target["etime"]) ||
                    start <= target["stime"] && end >= target["etime"])) {
      // console.log(theJSON[uid]["name"] + " " + start + " " + end + " " + y + " " 
      //+ theJSON[x]["name"] +" "+ target["start"] + " " + target["end"]);
      return false;
    }
  }
  return true;
}

/*
    t: type, e.g. "dis"
    c: uid, e.g. "2442"
    a:  #, e.g. "101" for "DIS 201"
*/
function swapTime(t, c, a){
  var type = '.'+ t;
  var alt = '.' + c;
  var originBlock = $(type+alt).not('.alternate');
  var to = $(type+alt+'[st='+a+']');
  
  /* Intersection detection */
  
  if (!ignoreConflict && !noConflict(c, t, a)) return;
   // End intersection detection 
   
   if (to.hasClass('alternate')) {
	    var room = to.eq(0).find("h6").text();
	    var oldroom = originBlock.eq(0).find("h6").text();
      to
      .html(originBlock.html())
      .draggable("enable").droppable("disable")
      .css("background",originBlock.css("background"))
      .removeClass('alternate')
      .find("h5").html(getBlockTime(to))
	    .siblings("h6").html(room);

      originBlock
      .addClass('alternate')
      .draggable("disable")
      .droppable("enable")
      .html("<h6>"+oldroom+"</h6>")
      .css("background","none");
   }
   
   to.hide().fadeIn(500);

   viewTime[c][t] = [a, theJSON[c][t][a]["start"], theJSON[c][t][a]["end"]];
   viewstate[c][t]=a;
   viewConflict();
}

function initCal(gstate) {
  var state = $.extend(true,{},viewstate);
  $("form[name=calify] input[name=add]").val($.toJSON(state));
  document.calify.submit();
  return true;
}