var nietvouwen = false;
function RGBAHex(rgb){
  if (!RGBAHex.hexvals){RGBAHex.hexvals = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"];}
  var ret = "#";
  rgb[3] *= 255;
  ret += (RGBAHex.hexvals[parseInt((rgb[3] / 16), 10)] + RGBAHex.hexvals[parseInt((rgb[3] % 16), 10)]);
  ret += (RGBAHex.hexvals[parseInt((rgb[0] / 16), 10)] + RGBAHex.hexvals[parseInt((rgb[0] % 16), 10)]);
  ret += (RGBAHex.hexvals[parseInt((rgb[1] / 16), 10)] + RGBAHex.hexvals[parseInt((rgb[1] % 16), 10)]);
  ret += (RGBAHex.hexvals[parseInt((rgb[2] / 16), 10)] + RGBAHex.hexvals[parseInt((rgb[2] % 16), 10)]);
  return ret;
}
function RGBHex(rgb){
  if (!RGBHex.hexvals){RGBHex.hexvals = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"];}
  var ret = "#";
  ret += (RGBHex.hexvals[parseInt((rgb[0] / 16), 10)] + RGBHex.hexvals[parseInt((rgb[0] % 16), 10)]);
  ret += (RGBHex.hexvals[parseInt((rgb[1] / 16), 10)] + RGBHex.hexvals[parseInt((rgb[1] % 16), 10)]);
  ret += (RGBHex.hexvals[parseInt((rgb[2] / 16), 10)] + RGBHex.hexvals[parseInt((rgb[2] % 16), 10)]);
  return ret;
}
function EasyColor(elem, col){
  var rgb = $.getRGB(col);
  if (rgb[3]){
    if (jQuery.support.rgba){
      $.cssRule(elem, "background-color", col);
    }else{
      var hex = RGBAHex(rgb);
      $.cssRule(elem, "background", "transparent");
      $.cssRule(elem, "-ms-filter", "progid:DXImageTransform.Microsoft.gradient(startColorstr="+hex+",endColorstr="+hex+")");
      $.cssRule(elem, "filter", "progid:DXImageTransform.Microsoft.gradient(startColorstr="+hex+",endColorstr="+hex+")");
      $.cssRule(elem, "zoom", "1");
    }
  }else{
    $.cssRule(elem, "background-color", col);
  }
};
function SetColor(lastcol, time){
  var k = $.getRGB(lastcol);
  var lastdol = "rgba("+Math.floor(k[0]/2)+","+Math.floor(k[1]/2)+","+Math.floor(k[2]/2)+",0.9)";
  $(".logo").stop(true).animate({backgroundColor:lastcol}, time);
  $("a").stop(true).animate({color:lastcol}, time);
  $(".dock").stop(true).animate({backgroundColor:lastdol}, time);
  $(".contentholder").stop(true).animate({backgroundColor:lastdol}, time);
  $(".menu_cont").stop(true).animate({backgroundColor:lastdol}, time);
  $(".thultip").stop(true).animate({backgroundColor:lastdol}, time);
  $("div.jGrowl div.jGrowl-notification, div.jGrowl div.jGrowl-closer").stop(true).animate({backgroundColor:lastdol}, time);
  $(".newstoptxt, .newstitle, .newsfooter").stop(true).animate({color:lastcol}, time);
  setTimeout(function(){
    $.cssRule("a:visited, a:hover, a:active, a:link, a:active, a", "color", lastcol);
    EasyColor(".logo", lastcol);
    EasyColor(".dock", lastdol);
    EasyColor(".menu_cont", lastdol);
    EasyColor(".contentholder", lastdol);
    EasyColor("div.jGrowl div.jGrowl-notification, div.jGrowl div.jGrowl-closer", lastdol);
    EasyColor(".thultip", lastdol);
    $.cssRule(".newstoptxt, .newstitle, .newsfooter", "color", lastcol);
  }, time);
};
var lasttxt = "";
$(function(){
  $.RSRNotifyCallback(function(m){
    switch (m.soort){
      case "balk":
        if (m.txt != lasttxt){
          lasttxt = m.txt;
          $("#bb_txt").stop(true, true).fadeOut("slow", function(){
            var html = m.txt;
            if (m.lid){html = html+" - <i>"+m.lid+"</i>";}
            if (m.tijd){html = html+" @ "+$.RSRTime(m.tijd);}
            if (m.link){html = "<a href=\""+m.link+"\" target=\"mainframe\">"+html+"</a>";}
            $("#bb_txt").html(html).fadeIn("slow");
          });
        }
        var a = $.RSRNotifyAll();
        var r = "";
        var html = "";
        for (b in a){
          if (a[b].soort == "balk"){
            html = a[b].txt;
            if (a[b].lid){html = html+" - <i>"+a[b].lid+"</i>";}
            if (a[b].tijd){html = html+" @ "+$.RSRTime(a[b].tijd);}
            if (a[b].link){html = "<a href=\""+a[b].link+"\" target=\"mainframe\">"+html+"</a>";}
            r += html+"<br>";
          }
        }
        $("#bb_list").html(r);
        break;
      default:
        if (m.link){html = "<a href=\""+m.link+"\" target=\"mainframe\">"+m.txt+"</a>";}else{html = m.txt;}
        $.jGrowl(html, {sticky:true});
        break;
    }
  });
  $.RSRNotifyNext();
  setInterval($.RSRNotifyNext, 5000);
  $("#bb_txt").mouseover(function(){$.RSRNotifyDG(true);});
  $("#bb_txt").mouseout(function(){$.RSRNotifyDG(false);});
  $("body").append('<div style="jGrowl top-right"></div>');
  $.getScript("/online.js.php");
  setInterval(function(){$.getScript("/online.js.php?res="+screen.width +"x"+ screen.height+"&random="+Math.round(Math.random()*50000));}, 60000);
  
  $(".menu_but").live("click", function(){
    if ($(this).children(":visible").length > 0){
      nietvouwen = true;
      setTimeout(function(){nietvouwen = false;}, 600);
    }
    $(this).children().slideToggle(500, "easeOutBounce");
  });
  $(".menu_but.topmenu").live("mousemove", function(){
    if (!nietvouwen){
      if ($(this).children(".menu_cont.topmenu:animated").length == 0){
        if ($(".menu_cont.topmenu:visible").length > 0){
          $(".menu_cont.topmenu").not($(this).children()).slideUp(500, "easeOutBounce");
          $(this).children().slideDown(500, "easeOutBounce");
        }
      }
    }
  });
  $(".contents").click(function(){
    $(".menu_cont.topmenu").slideUp(500, "easeOutBounce");
  });
  $(".dock_close").live("click", function(){$(this).parent().parent().draggable("destroy").remove();});
  $(document).konami(function(){
    var tmp = "", tmpb = "", tmpc = "";
    $("body").append("<div id='kleurmagie' style='position:fixed;z-index:5;left:50%;top:50%;width:200px;height:200px;margin-left:-100px;margin-top:-100px;'><div id='farbes'></div><input type='button' value='Opslaan' id='farbsave'></div>");
    $("#farbsave").click(function(){
      $.getJSON("/settings.php?n=kleur&s="+tmp);
      $.getJSON("/settings.php?n=bgkleur&s="+tmpb);
      $.getJSON("/settings.php?n=nabgkleur&s="+tmpc);
      $("#kleurmagie").remove();
    });
    $("#farbes").farbtastic(function(fb){
      tmp = "rgb("+Math.round(fb.rgb[0]*255)+","+Math.round(fb.rgb[1]*255)+","+Math.round(fb.rgb[2]*255)+")";
      tmpb = "rgba("+Math.round(fb.rgb[0]*127)+","+Math.round(fb.rgb[1]*127)+","+Math.round(fb.rgb[2]*127)+",0.9)";
      tmpc = "rgb("+Math.round(fb.rgb[0]*127)+","+Math.round(fb.rgb[1]*127)+","+Math.round(fb.rgb[2]*127)+")";
      SetColor(tmp, 500);
    });
    $.farbtastic("#farbes").setColor(RGBHex($.getRGB($(".logo").css("background-color"))));
  }, {code: $.ToCC("KLEUR"), name: "kleur"});
});

