var lastId = 0;
var currentStatusID = 0;
var currentVal = 0;
var currentPrefix = 'Status';
var mousex = 0;
var mousey = 0;
function updateMouse(e){
  var e = e || window.event;
  mousex = e.pageX || e.clientX + document.body.scrollLeft || 0; 
  mousey = e.pageY || e.clientY + document.body.scrollTop || 0; 
}
addEvent(document,'mousemove',updateMouse,false);
if ( document.captureEvents ) {
  document.captureEvents(Event.MOUSEMOVE);
}

function getNewId() {
  lastId++;
  return "window_id_" + lastId;
}
function changeStatus(status,id,prefix){
  currentStatusID = id;
  currentVal = status;
  currentPrefix = prefix;
  openit(status);
}
function openit(status){
  var win = new Window(getNewId(), {
    className: "mac_os_x", width:180, height:155, zIndex: 100, resizable: true, 
    title: "Status", 
    showEffect:Effect.Appear,
    showEffectOptions: {duration: 0},
    hideEffect: Effect.Fade, 
    hideEffectOptions: {duration: 0},
    draggable:true
    })
  win.getContent().innerHTML = build_selection(status);
  // win.setStatusBar("Status bar info"); // Not nice in mac_os_x template
  win.setDestroyOnClose();
  win.setLocation(mousey - 110, mousex - 90);
  win.show(true);
}
function build_selection(status){
  var html = '<div style="padding: 1px 20px 0 20px;"><form id="change-status">' +  
             '<img src="images/icon-status-red.gif" width="16" height="16" alt="">      <input type="radio" class="no" name="status" value="-1" ' + (status==-1? 'checked':'') + ' style="width:20px;">  Inget salg <br>' + 
             '<img src="images/icon-status-grey.gif" width="16" height="16" alt="">   <input type="radio" class="no" name="status" value="0" ' + (status==0? 'checked':'') + ' style="width:20px;">  Ubehandlet <br>' + 
             '<img src="images/icon-status-green.gif" width="16" height="16" alt=""> <input type="radio" class="no" name="status" value="1" ' + (status==1? 'checked':'') + ' style="width:20px;">  Salg <br>' + 
             '<input type="button" name="submit" value="Oppdater" onclick="updateStatus(this);" style="margin: 10px 0 0 20px; width: 100px;">' +
             '</form></div>';
  return html;
}
function updateStatus(el){
  me = el.parentNode;
  var _checked = false;
  var _val = 0;
  var fields = me.getElementsByTagName('input');
  for (var i=0; i<fields.length; i++){
    if(fields[i].name == 'status'){
      if(fields[i].checked){
        _val = fields[i].value;    
        _checked = true;
        break;
      }
    }
  }
  Windows.closeAll();
  if(_checked){
      a = $(currentPrefix + '_' + currentStatusID);
    img = a.firstChild;
    a.setAttribute('tempID1',_val);            // Temporary storage for onclick behavior
    a.setAttribute('tempID2',currentStatusID); // Temporary storage for onclick behavior
    if(currentVal != _val){
      img.setAttribute('src','images/indicator-white-01.gif');
      a.removeAttribute('onclick');
      ajaxStatus(currentStatusID,_val);
    } else {
      a.setAttribute('onclick', function(){ status = this.tempID1; id = this.tempID2; changeStatus(status,id); });
    }
  }
}
function ajaxStatus(id,val){
  if(val == -1)
    updateStatusIcon(id,'images/icon-status-red.gif',val);
  else if(val == 0)
    updateStatusIcon(id,'images/icon-status-grey.gif',val);
  else 
    updateStatusIcon(id,'images/icon-status-green.gif',val);
}
function updateStatusIcon(currentStatusID,imgsrc,val){
      a = $(currentPrefix + '_' + currentStatusID);
    img = a.firstChild;
    img.setAttribute('src',imgsrc);
    a.setAttribute('onclick', function(){ status = this.tempID1; id = this.tempID2; changeStatus(status,id,currentPrefix); });
    $(currentPrefix).value = val;
}

var studie_url_rewrite = 'Permalenke inn til helside til skolen. Adressen blir http://domene/skoler/NAVNITEKSTFELT.html<br><br>Det er ikke bra å endre denne adressen om noen allerede har startet å lenke inn til den. Dette fordi at ved å endre denne, så endrer du lenken inn til fullsiden - altså bryter du eldre lenker inn til siden.';
var studie_en_parent_id = 'For studie.no og educationscandinavia.info skal helsidene lenkes mot hverandre, altså norsk til engelsk og visa versa. I dette feltet skriver du inn ID nummer på skolen på motsatt portal.<br><br>Eks, denne skolen er ID 3 på studie.no, mens den har ID 44 på educationscandinavia.info, du skal da skrive 44 på studie.no (lenke korrekt inn til samme skole på scandinavia) og 3 på scandinavia så den lenker korrekt inn til studie.no.';



  function _iframe_refreshimage(path,imgx,imgy){
    seed = new Date;
    $('bilde').src = path + '?seed=' + seed.getSeconds();
    if(document.all && !window.opera)
      $('bilde').style.setAttribute("cssText",'width:' + imgx + ';height:' + imgy);
      else
      $('bilde').setAttribute("style",'width:' + imgx + ';height:' + imgy);
  }
  function _iframe_refreshlogo(path,imgx,imgy){
    seed = new Date;
    $('logo').src = path + '?seed=' + seed.getSeconds();
    if(document.all && !window.opera)
      $('logo').style.setAttribute("cssText",'width:' + imgx + ';height:' + imgy);
      else
      $('logo').setAttribute("style",'width:' + imgx + ';height:' + imgy);
  }
  function _refresh(id){
    $(id).src = 'http://www.studie.no/' + $(id + '_src').value;
    if(document.all && !window.opera)
      $(id).style.setAttribute("cssText",'');
      else
      $(id).setAttribute("style",'');
  }

  /* Quick resize script for the iFrames */
  function resize_iframe(id,direction){
    var step = 50;
    el = document.getElementById(id);
    var currentY = el.height;
    if(direction=='down'){
      if(currentY>75){
        el.height = (parseInt(currentY) - parseInt(step)) + "px";
      } else {
        el.height = 50;
      }
    } else {
      el.height = (parseInt(currentY) + parseInt(step)) + "px";
    }
      Set_Cookie(id,el.height,100000);
  }

  function resize_textarea(id,direction){
    var step = 50;
    el = document.getElementById(id);
    var currentY = el.style.height;
    if(direction=='down'){
      if(currentY>75){
        el.style.height  = (parseInt(currentY) - parseInt(step)) + "px";
        $('zo_ordre').style.height  = (parseInt(currentY) - parseInt(step)) + "px";
      } else {
        el.style.height  = 50;
        $('zo_ordre').style.height = 50;
      }
    } else {
      el.style.height  = (parseInt(currentY) + parseInt(step)) + "px";
      $('zo_ordre').style.height  = (parseInt(currentY) + parseInt(step)) + "px";
    }
      Set_Cookie(id,el.style.height ,100000);
  }

/* URL Opener system, update the SEO parts */
        function update_url_path(){
          var before = $('url-preview-text').innerHTML;
          var path = $('url-path').options[$('url-path').selectedIndex].value;
          if(path == '')
            update_url_path_kategori();
            else
            $('url-preview-path').innerHTML = path;
          var after = $('url-preview-text').innerHTML;
          if(before != after)
            highlight_url_preview();
        }
        function update_url_rewrite(){
          var before = $('url-preview-text').innerHTML;
          $('url-preview-page').innerHTML = $('url-rewrite').value;
          var after = $('url-preview-text').innerHTML;
          if(before != after)
            highlight_url_preview();
        }
        function update_url_path_kategori(){
          var path = $('url-path').options[$('url-path').selectedIndex].value;
          if(path == ''){
            var currentTime = new Date();
            var cachekill = currentTime.getTime();
            var opt = {
              method: 'post',
              postBody: 'local=' + $('local').value + '&kategori=' + $('cat_field').value + '&rand=' + cachekill,
              onSuccess: function(t) {
                if(t.responseText != $('url-preview-path').innerHTML)
                  highlight_url_preview();
                $('url-preview-path').innerHTML = t.responseText;
              },
              on404: function(t) {
                alert('Error 404: location "' + t.statusText + '" was not found.');
              },
              onFailure: function(t) {
                alert('Error ' + t.status + ' -- ' + t.statusText);
              }
            }
            new Ajax.Request('studie.ajax.get.url_path.php', opt);
          }
        }
        function init_url_rewrite(){
          update_url_path();
          update_url_rewrite();
        }
        ELO.functionsToCallOnload.push("init_url_rewrite()");

        function highlight_url_preview(){
          new Effect.Highlight('url-preview-text', {
              duration: 1.0,
              from: 1,
              to:0
          });

        }


