/*
	Wywołanie:
		
		new Dymek(el, txt, title, icon, width, height, top, left, style, delay, close, iconPoz);
		
		np. <div onm5ouseover="new Dymek(this, 'tekst', 'tytu');"></div>
		
		el - element nad którym pojawi się dymek, Object lub ID;
		txt - tresc; 
		title - tytul;
		icon - sciezka do img; (domyslnie bez ikonki)
		width - szerokosc;
		height - wysokosc;
		top - przesuniecie od gory;
		left - przesuniecie od lewej;
		style - poczatkowa nazwa styli; (domyslnie 'dymek')
		delay - po ilu sekundach ma zniknac dymek (domyslnie 0)
		close - false, true, lub tresc przycisku - przycisk zamykajacy dymek (delay jest pomijany) - (domyslnie false)
		iconPoz - right, left - pozycja ikony (domyslnie right);

	!! WYMAGANE !!
	
		el - zawsze trzeba podac Object lub ID elementu;
		txt - przy wywoływaniu Dymka lub w configu (dymekConf);
		width - przy wywoływaniu Dymka lub w configu (dymekConf);
		height - przy wywoływaniu Dymka lub w configu (dymekConf);
	
	!!!!!!!!!!!!!!
	
	!! UWAGA !! 
	 Jeżeli dymek "mruga" to znaczy, ze jest podana za mala wysokosc dymka.
	
	Jezeli czesc parametrow bedzie taka sama to mozna je ustawic w 'dymekConf'.
	
	var dymekConf = {
										title: "",
										icon: "",
										iconPoz: "",
										txt: "",
										close: "",
										width: "",
										height: "",
										top: "",
										left: "",
										style: "",
										delay: ""
									};
	
	na stronie: np: dymekConf.width = "120px";
	
	lub wstawic powyzsze (edytowane) linie na stronie gdzie bedzie wywolany dymek;
	
	Info pojawiajace sie w alertach można zmienić	odpowiednio edtujac:
	var dymekInfo = {
                 txt: "Nie podano tresci",
                 element: "Nie podano obiektu lub ID obiektu, nad którym ma sie pojawic Dymek",
                 elementBad: "Zly typ obiektu. Wymagany Object lub String",
                 elementNo: "Nie znaleziono takiego obiektu",
                 width: "Nie podano dlugosci Dymku",
                 height: "Nie podano wysokosci Dymku"
                };
	lub wstawiajac powyzsze (edytowane) linie na stronie gdzie bedzie wywolany Dymek.
	
*/



var Dymek = {};
var bigDymek = {};
var aDymek = {};

var dymekConf = {
                  title: "",
                  icon: "",
                  iconPoz: "",
                  txt: "",
                  close: "",
                  width: "",
                  height: "",
                  top: "",
                  left: "",
                  style: "",
                  delay: ""
                };

var dymekInfo = {
                 txt: "Nie podano tresci",
                 element: "Nie podano obiektu lub ID obiektu, nad którym ma sie pojawic Dymek",
                 elementBad: "Zly typ obiektu. Wymagany Object lub String",
                 elementNo: "Nie znaleziono takiego obiektu",
                 width: "Nie podano dlugosci Dymku",
                 height: "Nie podano wysokosci Dymku"
                };

Dymek = function(el, txt, title, icon, width, height, top, left, style, delay, close, iconPoz)
{
  aDymek = new bigDymek(el, txt, title, icon, width, height, top, left, style, delay, close, iconPoz);
};

bigDymek = function(el, txt, title, icon, width, height, top, left, style, delay, close, iconPoz)
{
  if(!el) { alert(dymekInfo.element); return false;} else { this.el = this.checkEl(el);};
  if(this.el == false) {return false};
  this.txt = (txt) ? txt : (dymekConf.txt) ? dymekConf.txt : "";
  if(this.txt == "") { alert(dymekInfo.txt); return false};
  this.title = (title) ? title : (dymekConf.title) ? dymekConf.title : "";
  this.icon = (icon) ? icon : (dymekConf.icon) ? dymekConf.icon : "";
  this.iconPoz = (iconPoz) ? iconPoz : (dymekConf.iconPoz) ? dymekConf.iconPoz : "right";
  this.width = (width) ? width : (dymekConf.width) ? dymekConf.width : "";
  this.height = (height) ? height : (dymekConf.height) ? dymekConf.height : "";
  this.top = (top) ? top : (dymekConf.top) ? dymekConf.top : 0;
  this.left = (left) ? left : (dymekConf.left) ? dymekConf.left : 0;
  this.close = (close) ? close : (dymekConf.close) ? dymekConf.close : false;
  this.delay = (delay) ? delay : (dymekConf.delay) ? dymekConf.delay : 0;
  if(this.delay > 0) this.delay = this.delay * 1000;
  this.style = (style) ? style : (dymekConf.style) ? dymekConf.style : "dymek";
  this.name = "dymekInfo";

  this.show();
};

/* wyswietla dymek */
bigDymek.prototype.show = function()
{
  if(document.getElementById(this.name))
    {
      this.closeDymek();
      this.show();
    } else {

      var pos = this.getPosition(this.el);
      if(isNaN(pos.x) | isNaN(pos.y))
      	{ alert(dymekInfo.elementNo); return false };

      var nDymek = document.createElement("table");
        nDymek.id = this.name;
        nDymek.cellPading = 0;
        nDymek.cellSpacing = 0;
        nDymek.border = 0;
        nDymek.style.tableLayout = "fixed";
        nDymek.style.position = "absolute";
        nDymek.className = this.style+"_table";
        if(this.width != "")
          { nDymek.style.width = this.width; } else { alert(dymekInfo.width); return false;};
        if(this.height != "")
          { nDymek.style.height = this.height; } else { alert(dymekInfo.height); return false;};
        nDymek.style.top = (pos.y - parseInt(this.height) + parseInt(this.top)) + "px";
        nDymek.style.left = (pos.x + parseInt(this.left)) + "px";

        var hTr = nDymek.insertRow(0);
        var hTd = hTr.insertCell(0);
          hTd.innerHTML = this.title;
          hTd.className = this.style+"_header";

        if(this.close != false)
          {
            var cTd = hTr.insertCell(1);
              cTd.innerHTML = (this.close != "" && this.close != true) ? this.close : "X";
              cTd.className = "dymek_close";
              cTd.onmouseover = function () { this.className = "dymek_close_over";};
              cTd.onmouseout = function () {  this.className = "dymek_close";};
              cTd.onmousedown = function () { this.className = "dymek_close_down"; };
              cTd.onmouseup = function () {  this.className = "dymek_close_over";};
              cTd.onclick = function () { aDymek.closeDymek(); };
          };

        var sTr = nDymek.insertRow(1);
        var sTd = sTr.insertCell(0);
            sTd.className = this.style+"_txt";
          if(this.close != false)
            { sTd.colSpan = "2"; };
          this.createTxt(sTd);

        var dTr = nDymek.insertRow(2);
        var dTd = dTr.insertCell(0);
            dTd.className = this.style+"_footer";
          if(this.close != false)
            { dTd.colSpan = "2"; };


        document.body.appendChild(nDymek);

        if(this.close == false)
          {
            if(this.delay > 0)
              {
                this.el.onmouseout = function() {setTimeout(function() {aDymek.closeDymek();},  aDymek.delay);};
              } else {
                this.el.onmouseout = function() { aDymek.closeDymek();};
              };
          };
    };
};

/* tworzy tabelke z trescia i z ikonka */
bigDymek.prototype.createTxt = function(sTd)
{
  var sTable = document.createElement("table");
      sTable.cellPading = 0;
      sTable.cellSpacing = 0;
      sTable.border = 0;
      sTable.className = this.style+"_txt_table";
  var sTr = sTable.insertRow(0);

  var lTd = sTr.insertCell(0);
    lTd.style.width= "2px";

  if(this.iconPoz == "right")
    {
      var tTd = sTr.insertCell(1);
      var dTd = sTr.insertCell(2);
        dTd.style.width= "2px";
      var iTd = sTr.insertCell(3);
    } else {
      var iTd = sTr.insertCell(1);
      var dTd = sTr.insertCell(2);
        dTd.style.width= "2px";
      var tTd = sTr.insertCell(3);
    };

  var pTd = sTr.insertCell(4);
    pTd.style.width= "2px";

    tTd.className = this.style+"_txt_td";
    tTd.innerHTML = this.txt;

    iTd.className = this.style+"_icon_td";
	
	if(this.icon != "")
		{
  		var pImg = document.createElement("img");
      pImg.src = this.icon;
      pImg.border = 0;
      pImg.className = this.style+"_icon_img";

      iTd.appendChild(pImg);
    };

  sTd.appendChild(sTable);

};

/* sprawdza czy element nad ktorym ma sie pojawic dymek istnieje lub jest obiektem */
bigDymek.prototype.checkEl = function(el)
{
  if(typeof el == "object" | typeof el == "string")
    {
      if(typeof el == "object")
        return el;
      if(typeof el == "string") {
          if(!document.getElementById(el))
            { alert(dymekInfo.elementNo); return false;} else { return document.getElementById(el); };
        };
    } else {
      alert(dymekInfo.elementBad);
      return false;
    };
    return false;
};

/* zamyka dymek */
bigDymek.prototype.closeDymek = function()
{
  if(document.getElementById(this.name))
    document.body.removeChild(document.getElementById(this.name));
};

/* zwraca pozycje elementu nad ktorym ma sie pojawic dymek */
bigDymek.prototype.getPosition = function(e)
{
  var left = 0;
  var top  = 0;

  while (e.offsetParent)
    {
      left += e.offsetLeft;
      top  += e.offsetTop;
      e     = e.offsetParent;
    };

  left += e.offsetLeft;
  top  += e.offsetTop;

  return {x:left, y:top};
};
