// accountinfo box
// written by garbage
// copyright by primusportal

// version 0.4 beta   
// :D
var _refresh_interval = 40*1000;
var _url = "/global_data/infobox/";
var _old_count = 0;
var _timer_accountinfo = null;
var _divname = "__accountinfobox_div";
var _boxdata = null;
var _position = null;

 function showBox(_data) {
	   showbox = false;
	   var _dummy = new Object();
	   
	   if (_data != null) {
	       if (_data instanceof Array) {
	       		_dummy = _data;
	       } else {
	       		eval("_dummy = " + _data);
	       		
	       }
	   }
	   // check if there is new data to display:
	   if (_dummy != null && _dummy.length>_old_count) {
	     showbox = true;
	     _old_count = _dummy.length;
	   }
	   if (showbox)
	     buildBoxHTML(_dummy);

	   _boxdata = _dummy;  
	   // and finally start timer:
	   
	   
	   return true;
 }
 
 function buildBoxHTML(_box) {
	  html = "<table border=\"0\" cellspacing=\"8\" width=\"168\">";
	
	  // fill box with contents:
	  for (i=0; i<_box.length; i++) {
	     html += "<tr><td valign=\"top\" width=\"17\">";
	     if (_box[i]['icon'] != null)
	       html += "<img src=\"" + _box[i]['icon'] + "\" width=\"16\" height=\"16\" alt=\"\" />";
	     else
	       html += "&nbsp;";
	     html += "</td><td>";
	     // build title:
	     switch (_box[i]['subject']) {
	      case 'Default':
	       html += "<a href=\"javascript:openNews(" + i + ");\">" + _box[i]['title'] + "</a>";
	       break;
	      default:
	       html += "<a href=\"javascript:clickAccountinfo("+ i + ");\">" + _box[i]['title'] + "</a>";
	       break;
	     }
	     html += "&nbsp;<a href=\"javascript:hideAccountnews(" + i + ");\">(X)</a>";
	     html += "</td></tr>";
	  }
	  
	  html += "</table>";

	  var e = $("#" + _divname);
	  $("#" + _divname + "_content").html(html);
	  e.css("display","none");  
	  position_accountinfobox();
	  e.fadeIn(2000);
 }
 
 function getData() {
	  $.getJSON(_url + "update.php", function(ret) {
		showBox(ret);
	  });
 }
 function openNews(id) { text = _boxdata[id]['text'];hideAccountnews(id);$.facebox(text); }
 function hideAccountinfoBox() { $.post(_url + "click.php", {remall: true}, function(ret) { $("#" + _divname).fadeOut(); }); }
 function hideAccountnews(id,redir) { $.post(_url + "click.php", {id: _boxdata[id]['id']}, function(r) { if(redir!=null) document.location.href = redir;_old_count=0;$("#"+_divname).hide();showBox(r);return true; }); }
 function clickAccountinfo(id) { link = _boxdata[id]['link'];hideAccountnews(id,link); }

 function getScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
      yScroll = self.pageYOffset;
      xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
      yScroll = document.documentElement.scrollTop;
      xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
      xScroll = document.body.scrollLeft;	
    }
    return new Array(xScroll,yScroll) 
  }
 function getWindowHeight() {
    var windowHeight
    if (self.innerHeight) {	// all except Explorer
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowHeight = document.body.clientHeight;
    }	
    return windowHeight
  }
  

 function position_accountinfobox() {
	  var e = $("#" + _divname);
	  e.css("position","absolute");
	  var scroll = getScroll();
	  
	  if (accountinfobox_settings != '') {
	    if (_position == null)
	    	eval("_position = " + accountinfobox_settings);
	    	
	    e.css("left", _position.left + "px");
	    e.css("top", (parseInt(scroll[1]) + parseInt(_position.top)) + "px");
	  } else {
	  	e.css("right", "0px");
	  	e.css("top",(getWindowHeight()+scroll[1]-e.height())+"px");
	  }

	  
 } 
 
$(document).ready(function() {
	_timer_accountinfo = window.setInterval("getData()", _refresh_interval);
	$(window).resize(function() { position_accountinfobox(); });
	$(window).scroll(function() { position_accountinfobox(); });
	
	// make infobox draggable:
	$("#" + _divname).draggable( {stop: function(e, ui) { 
		newpos_left = ui.absolutePosition.left;
		newpos_top = ui.absolutePosition.top;
		$.post(_url + "store_new_position.php", {"left": newpos_left, "top": newpos_top}, function(ret) {
			_position.left = newpos_left;
			_position.top  = newpos_top;
		});
	},	containment: "window"});
});