
// NOTE Denne fila krever jQuery.


// NOTE - DEPRECATED
//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
  if (window.XMLHttpRequest) {
    return new XMLHttpRequest();
  } else if(window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    document.getElementById('px_status').innerHTML = 
    '<i>Status: Klarte ikke &aring; lage XmlHttpRequest objektet.' +
    ' Oppgrader din nettleser.</i>';
  }
}
/// /////////////////////////////////////////////////////////////// ///


function setimg(id,img) {
  // Sett valgt ID til det bestemte bilde URL
  $("#"+id).attr('src',img);
}


/****************************************************
     Author: Eric King
     Url: http://redrival.com/eak/index.shtml
     This script is free to use as long as this info is left in
     Featured on Dynamic Drive script library (http://www.dynamicdrive.com)
****************************************************/
var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos){
if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=yes,directories=no,status=yes,menubar=no,toolbar=no,resizable=no';
win=window.open(mypage,myname,settings);}
/**********************************************************************************/

var animNotify1 = false;
var animNotify2 = false;
var notifyOn = false;
var checkTimer = false;
var lastCheckAccount = 0; // unix timestamp, receive from http server

function showNotify(msg) {
  notifyOn = true;
  $('#notify').css("display","inline");
  $('#notify').animate({"paddingTop": '3px'}, 100);
  $('#notify').animate({"paddingBottom": '2px'}, 100);
  $('#notify').animate({"height": '18px'}, 700);
  $('#notify').css("opacity","0.85")
  setTimeout(function(){$('#notify_text').html(msg)}, 800);
  animNotify1 = setInterval(function(){$('#notify_text').animate({"opacity": '1'}, 1000)}, 900);
  animNotify2 = setInterval(function(){$('#notify_text').animate({"opacity": '0.4'}, 1000)}, 1000);
  setTimeout(function(){$('#notify_close').html("<span style=\"font-size: 9px;\">[ <a href=\"#\" onclick=\"hideNotify();\">Skjul</a> ]</span>")}, 800);
}

function hideNotify() {
  notifyOn = false;
  clearInterval(animNotify1);
  clearInterval(animNotify2);
  $('#notify_close').html("");
  $('#notify_text').animate({"opacity": '0'}, 500);
  setTimeout(function(){$('#notify_text').text('')}, 210);
  $('#notify').animate({"paddingTop": '0px'}, 100);
  $('#notify').animate({"paddingBottom": '0px'}, 100);
  $('#notify').animate({"height": '0px'}, 700);
  setTimeout(function(){$('#notify').css("opacity","0")}, 800);
}


function checkAccount() {
  $.get('dycont.php?type=myAccount&lastCheck='+lastCheckAccount, function(data){
    var html = "";
    if ($(data).find('suspend').text() == 'true') {
      // Suspendert... Si i fra til brukeren.
      $('#minprofil').html("<p class=\"mb_head\">MIN PROFIL</p>" +
        "<br><br><br><br><br><div style=\"text-align: center;\">" + 
        "<a href=\"?GLOB_ACTN=logout\"><img src=\"img/btn/loggut.png\" class=\"btnani\" style=\"margin-left: 10px; border: none;\" alt=\"Logg ut\"></a></div>");
      showNotify("Din konto er blitt suspendert.");
    } else {
      $(data).find('request').each(function(){
        var $rq = $(this);
        var pmtot = 0;
        var gbtot = 0;
        var cmtot = 0;
        var pmnot = 0;
        var gbnot = 0;
        var cmnot = 0;
        if (($rq.find('none').text() != "none") && ($rq.find('none').text() != "noneval")) {
          timerNotify = false;
          if (notifyOn == true) {
            timerNotify = true;
            clearInterval(checkTimer);
            hideNotify();
          }
          if ($rq.find('pm').text() != "") {
            if (html != "") {
              html += " &nbsp;|&nbsp; ";
            }
            html += "Nye private meldinger: "+$rq.find('pm').text();
            pmnot = $rq.find('pm').text();
          }
          if ($rq.find('tpm').text() != "") {
            pmtot = $rq.find('tpm').text();
          }
         
         
          if ($rq.find('gb').text() != "") {
            if (html != "") {
              html += " &nbsp;|&nbsp; ";
            }
            html += "Nye innlegg i gjeseboka: "+$rq.find('gb').text();
            gbnot = $rq.find('gb').text();
          }
          if ($rq.find('tgb').text() != "") {
            gbtot = $rq.find('tgb').text();
          }
         
         
          if ($rq.find('cm').text() != "") {
            if (html != "") {
              html += " &nbsp;|&nbsp; ";
            }
            html += "Nye kommentarer: "+$rq.find('cm').text();
            cmnot = $rq.find('cm').text();
          }
          if ($rq.find('tcm').text() != "") {
            cmtot = $rq.find('tcm').text();
          }
         
         
          if ($rq.find('timestamp').text() != "") {
            lastCheckAccount = $rq.find('timestamp').text();
          }
          if (timerNotify == false) {
            showNotify(html);
          } else {
            setTimeout(function(){
              showNotify(html),
              checkTimer = setInterval(function(){checkAccount()}, 15000)
            }, 820);
          }
         
          $("#tinbox").html(pmtot+" ("+pmnot+")");
          $("#tgbook").html(gbtot+" ("+gbnot+")");
          $("#tcomment").html(cmtot+" ("+cmnot+")");
        }
        if ($rq.find('none').text() == "noneval") {
          if ($rq.find('tpm').text() != "") {
            pmtot = $rq.find('tpm').text();
          }
          if ($rq.find('tgb').text() != "") {
            gbtot = $rq.find('tgb').text();
          }
          if ($rq.find('tcm').text() != "") {
            cmtot = $rq.find('tcm').text();
          }
          $("#tinbox").html(pmtot+" ("+pmnot+")");
          $("#tgbook").html(gbtot+" ("+gbnot+")");
          $("#tcomment").html(cmtot+" ("+cmnot+")");
        }
      });
    }
  });
}

$(document).ready(function(){
  checkAccount(); // Do check right away.
  checkTimer = setInterval(function(){checkAccount()}, 15000); // Check account activity every 15th seconds.
});

$('input').mouseover(function(){
  if ($(this).attr('type') == "image") {
    $(this).animate({'opacity':'0.6'},100);
  }
}).mouseout(function(){
  $(this).animate({'opacity':'1.0'},100);
});
$('.btnani').mouseover(function(){
  $(this).animate({'opacity':'0.6'},100);
}).mouseout(function(){
  $(this).animate({'opacity':'1.0'},100);
});