var pi = parseInt;
var pf = parseFloat;

var isIE = function() {
  if (navigator.userAgent.indexOf('Opera') != -1) return false;
  if (navigator.userAgent.indexOf('MSIE') == -1) return false;
  return true;
};

var windowSize = function(whichWindow) {
  if (!whichWindow) whichWindow = window;
  var winW = 800;
  var winH = 600;
  if (pi(navigator.appVersion)>3) {
    if (navigator.appName=="Netscape") {
    winW = whichWindow.innerWidth;
    winH = whichWindow.innerHeight;
    }
    if (this.isIE()) {
    winW = whichWindow.document.documentElement.clientWidth;
    winH = whichWindow.document.documentElement.clientHeight;
    }
  }
  return { 
    'height': winH,
    'y' : winH,
    'width' : winW,
    'x' : winW
  };
};

var setWindowOffset = function(whichWindow, nameSpace) {
  if (!whichWindow) whichWindow = window;
  var x = 500;
  var y = 500;
  var w = windowSize(whichWindow);
  whichWindow.resizeTo(x,y);
  var s = windowSize(whichWindow);
  var delta = { 'x' : w.x-s.x, 'y' : w.y-s.y };
  var offset = { 'x' : x-s.width, 'y' : y-s.height};
  whichWindow.resizeTo(x + delta.x, y + delta.y);
  nameSpace.windowOffset = offset;
  nameSpace.origWindowSize = w;
}

var windowResize = function(whichWindow, width, height, nameSpace) {
  if (!nameSpace) {
    nameSpace = {};
    setWindowOffset(whichWindow, nameSpace);
  }
  try {
    whichWindow.resizeTo(width + nameSpace.windowOffset.x, height + nameSpace.windowOffset.y);
  } catch(e) {}
}

var run = function() {
  for (var i=0; i < runQueue.length; i++) runQueue[i]();
};

var previewTemplate = function() {
  var grade = document.getElementById('klass_0_field_grades');
  if (!grade) return false;
  grade = grade.value;
  if (grade == '-') {
    alert("Please select a grade above.");
    return false;
  }
  var template = document.getElementById('grade_' + grade + '_template');
  if (!template) {
    alert("There is no template for that grade yet");
    return false;
  }
  template = template.value;
  var newWindow = window.open('class__' + template + '.html');
  var grade_radio = document.getElementById('template_type_grade');
  if (grade_radio) grade_radio.checked='checked'; 
  return false;
}

var previewSupplyList = function() {
  var grade = document.getElementById('grade');
  if (!grade) return false;
  grade = grade.value;
  if (grade == '-') {
    alert("Please select a grade.");
    return false;
  }
  var template = document.getElementById('grade_' + grade + '_template');
  if (!template) {
    alert("There is no template for that grade yet");
    return false;
  }
  template = template.value;
  var newWindow = window.open('class__' + template + '.html');
  var grade_radio = document.getElementById('template_type_grade');
  if (grade_radio) grade_radio.checked='checked'; 
  return false;
}


function get_var(name) {
  var l = window.location.toString().split('?');
  if (!l.length) return false;
  l = l[1].split('&');
  for (var i=0; i < l.length; i++) if (l[i].split('=')[0] == name) return l[i].split('=').length ? l[i].split('=')[1] : true;
  return false
}

var browseCategory = function() {
  var cPath_sel = document.getElementById('categories_id');
  if (!cPath_sel) return;
  pack_id = document.getElementById('pack_id');
  if (!pack_id) return;
  pack_id = parseInt(pack_id.value);
  var cPath = parseInt(cPath_sel.options[cPath_sel.selectedIndex].value);
  window.location = '/index.php?main_page=index&cPath=' + cPath + '&pack_id=' + pack_id;
}

window.myPopup = null;
function openPopup(path) {
  window.myPopup = window.open('/index.php?main_page=' + path, 'popup', "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=0,height=415,width=630");
  return false;
}
var embiggen = function(url) {
  if (url) window.nexturl = url;
  if (!window.ns) {
    window.ns = {};
    setWindowOffset(window, window.ns);
  }
  var s = windowSize();
  windowResize(window, s.x+20, s.y+20, window.ns);
  window.moveBy(-10, -10);
  var n = windowSize();
  if ((s.x == n.x && s.y == n.y) || (n.x > 790 && n.y > 500)) {
    if (window.nexturl) return window.location=window.nexturl;
    return;
  }
  window.setTimeout(embiggen, 50, url);
  return false;
}

