//For popup windows

function draw_pop()
{
  $('body').append('<div class="mainDiv" id="mainDiv' + num_pops + '" style="z-index:' + (num_pops*2+10) + '" onclick="hide(' + num_pops + ')"></div>');	
  $('body').append("<div class='popup' id='popup" + num_pops +"' style='z-index:" + (num_pops*2+11) + "'><table cellpadding='0' cellspacing='0'><tr><td colspan='3' class='popborder'><div style='height:10px'></div></td></tr><tr><td class='popborder'><div style='width:10px'></div></td><td><table cellspacing='0' border='0' width='100%'><tr><th class='popup_header'><h4 id='popup_title" + num_pops + "'>Loading...</h4><a href='javascript:hide(" + num_pops + ")'>close [x]</a></th></tr><tr><td class='popup_body' id='popup_message" + num_pops + "'><div align='center' style='padding-top:30px; padding-bottom:30px'><img src='http://www.tutortrove.com/images/frame_loading.gif'></div></td></tr></table></td><td class='popborder'><div style='width:10px'></div></td></tr><tr><td colspan='3' class='popborder'><div style='height:10px'></div></td></tr></table></div>"); 
}

var num_pops = 0;
function pop(poptitle, popmessage, popleft, poptop, popwidth, id) {
  if(!poptop) {
    poptop = 20;
  }
  if (!popwidth) {
    popwidth = 100;
  }

  num_pops++;
  draw_pop();
  if (id) {
    var position = $('#' + id).position();
	poptop = position['top']+(poptop);
	popleft = position['left']+(popleft);
  }
  if (poptitle.substring(0,9) == "ajaxpost=")
  {
  	$.ajax({
	  type: "POST",
   	  url: popmessage,
      data: poptitle + "&num_pops=" + num_pops,
      success: function(msg){
      	message = msg.split('|SPLIT|');
		$('#popup_message' + num_pops).html(message[1]);
		$('#popup_title' + num_pops).html(message[0]);
		$('#first_field' + num_pops).focus();
      },
      failure: function(msg){
        alert( "Error Loading page. Please check your connection and try again.");
      }
    });
    
  }
  else
  {
    $('#popup_message' + num_pops).html(popmessage);
    $('#popup_title' + num_pops).html(poptitle);
  }

  $('#popup' + num_pops).css({"left": popleft + 'px', "top": poptop + 'px', "width": popwidth + 'px', "display":"block"});
  var pageWidth = $(window).width();
  var pageHeight = $(window).height();
//  $('#mainDiv' + num_pops).css({"width": pageWidth + 'px', "height": pageHeight + 'px', "display":"block"});
 
  //make it draggable by the top
  $('#popup' + num_pops).draggable({
  	handle:'th',
  	containment: 'document'
  });
  
}

function hide(id) {
  $('#mainDiv' + id).remove();
  $('#popup' + id).remove();
}


