//////////////////////
///// JSI WIDGET /////
//////////////////////

function jsiWidget (pVariableName,
                    pWidgetId)
{
   //
   // attributes
   //
   this.Name = pVariableName;
   this.Id = pWidgetId; 
   this.Element = jsiById (this.Id);
   this.eBorder;
   this.borderType;
   this.eTab;
   this.eTabBody;
   this.Header;
   this.Footer;
   this.Content;
   this.ContentWrapper;
   this.ContentBody;
   this.vSBar;
   this.hSBar;
   this.Navigation;
   //
   // functions
   //
   this.AddHeader     = jsiWidget_AddHeader;
   this.AddFooter     = jsiWidget_AddFooter;
   this.AddContent    = jsiWidget_AddContent;
   this.ClearContent  = jsiWidget_ClearContent;
   this.AddNavigation = jsiWidget_AddNavigation;
   this.AdjustContent = jsiWidget_AdjustContent;
   this.AddTab        = jsiWidget_AddTab;
   //
   // constructor
   //
   this.borderType = this.Element.getAttribute("jsiborder");
   this.eBorder = jsiCreateElement ("div", "", "border", this.Element);
   this.eBorder.style.height="100%";
   this.eBorder.style.position="relative";
   this.eBorder.style.overflow="visible";
   
   jsiSetClass (this.Element, this.borderType);
   this.Element.style.overflow="visible";

   jsiCreateElement ("div", "", "ul", this.eBorder);
   jsiCreateElement ("div", "", "um", this.eBorder);
   jsiCreateElement ("div", "", "ur", this.eBorder);
   jsiCreateElement ("div", "", "ml", this.eBorder);
   jsiCreateElement ("div", "", "mm", this.eBorder);
   jsiCreateElement ("div", "", "mr", this.eBorder);
   jsiCreateElement ("div", "", "ll", this.eBorder);
   jsiCreateElement ("div", "", "lm", this.eBorder);
   jsiCreateElement ("div", "", "lr", this.eBorder);


   this.Header                  = jsiCreateElement ("div", this.Id + "_wheader", "header", this.Element);
   this.Header.style.visibility = "hidden";

   this.Footer                  = jsiCreateElement ("div", this.Id + "_wfooter", "footer", this.Element);
   this.Footer.style.visibility = "hidden";

   this.Content              = jsiCreateElement ("div", this.Id + "_wcontent", "content", this.Element);
   this.Content.style.top    = jsiGetCSSProperty (this.Header, "top") + "px";
   this.Content.style.bottom = jsiGetCSSProperty (this.Footer, "bottom") + "px";

   this.ContentWrapper              = jsiCreateElement ("div", this.Id + "_wcontentwrapper", "contentwrapper", this.Content);
   this.ContentWrapper.style.top    = "0";
   this.ContentWrapper.style.bottom = "0";
   this.ContentWrapper.style.left   = "0";
   this.ContentWrapper.style.right  = "0";

   this.ContentBody              = jsiCreateElement ("div", this.Id + "_wcontentbody", "contentbody", this.ContentWrapper);
   this.ContentBody.style.top    = "0";
   this.ContentBody.style.bottom = "0";
   this.ContentBody.style.left   = "0";
   this.ContentBody.style.right  = "0";

   this.hSBar = new jsiHorSBar (this.Name + '.hSBar', this.Id);
   this.vSBar = new jsiVerSBar (this.Name + '.vSBar', this.Id);
 
}

function jsiWidget_AddHeader (pHTML, pSize) {

   this.Header.innerHTML = pHTML;   
   // if font size is given
   if (pSize) this.Header.style.fontSize = pSize + "px";
   this.Header.style.visibility = "visible";
   this.Content.style.top = jsiGetCSSProperty (this.Header, "top") + jsiGetCSSProperty (this.Header, "height") + "px";

}

function jsiWidget_AddFooter (pHTML, pSize) {

   this.Footer.innerHTML = pHTML;
   // if font size is given
   if (pSize) this.Footer.style.fontSize = pSize + "px";
   this.Footer.style.visibility = "visible";
   this.Content.style.bottom = jsiGetCSSProperty (this.Footer, "bottom") + jsiGetCSSProperty (this.Footer, "height") + "px";

}

function jsiWidget_ClearContent () {

   this.ContentBody.innerHTML = "";
   this.AdjustContent ();

}

function jsiWidget_AddContent (pHTML) {

   this.ContentBody.innerHTML += pHTML;
   this.AdjustContent ();

}

function jsiWidget_AddNavigation () {

   jsiRemoveElement (this.Id + "_navigation");

   this.Navigation = jsiCreateElement ("div", this.Id + "_navigation", "navigation", this.Content);
   this.Navigation.style.left   = "0";
   this.Navigation.style.bottom = "0";
   this.Navigation.style.right  = "0";

   this.AdjustContent ();

}

function jsiWidget_AddTab (pTitle, pHeight, pTooltip) {

   jsiRemoveElement (this.Id + "_tab");
   this.eTab = jsiCreateElement ("div", this.Id + "_tab", "tab", this.eBorder);
   if (pTooltip != "") {
   	this.eTab.setAttribute ("jsiTooltip", pTooltip);
      jsiAddTooltip (this.Id + "_tab");
   }
   if (pHeight) this.eTab.style.height = pHeight + "px";
   jsiCreateElement ("div", "", "tul",   this.eTab);
   jsiCreateElement ("div", "", "tur",   this.eTab);
   jsiCreateElement ("div", "", "tml",   this.eTab);
   jsiCreateElement ("div", "", "tmr",   this.eTab)
   jsiCreateElement ("div", "", "tll",   this.eTab);
   jsiCreateElement ("div", "", "tlr",   this.eTab);
   this.eTabBody = jsiCreateElement ("div", "", "tbody", this.eTab);
   this.eTabBody.innerHTML = pTitle;

}

function jsiWidget_AdjustContent () {
var xBottom=0;

   // eğer widget'da navigasyon var ise content navigasyon yüksekliği kadar yukarı çekilir 
   if (this.Navigation) {
      xBottom += this.Navigation.clientHeight;
      xBottom += jsiGetCSSProperty (this.Navigation, "bottom");
   }
   this.ContentWrapper.style.bottom = xBottom + "px";

   // eğer content yüksekliği widget'a sığmıyor ise vertical scrollbar gösterilir
   if (this.ContentBody.scrollHeight > this.ContentBody.clientHeight) {
      this.vSBar.eVSBar.style.visibility = "visible";
      this.ContentBody.style.right = this.vSBar.eVSBar.clientWidth + jsiGetCSSProperty (this.vSBar.eVSBar, "right") + "px";
   }
   // eğer content yüksekliği widget'a sığıyor ise vertical scrollbar gösterilmez
   else {
        this.vSBar.eVSBar.style.visibility = "hidden";
        this.ContentBody.style.right = "0";
   }

   // eğer content genişliği widget'a sığmıyor ise horizontal scrollbar gösterilir
   if (this.ContentBody.scrollWidth > this.ContentBody.clientWidth) {
      this.hSBar.eHSBar.style.visibility = "visible";
      this.ContentBody.style.bottom = this.hSBar.eHSBar.clientHeight + jsiGetCSSProperty (this.hSBar.eHSBar, "bottom") + "px";
   }
   // eğer content genişliği widget'a sığıyor ise horizontal scrollbar gösterilmez
   else {
        this.hSBar.eHSBar.style.visibility = "hidden";
        this.ContentBody.style.bottom = "0";
   }

   // eğer hem vertical, hemde horizontal scrollbar gösteriliyor ise
   // sağ alt köşede scrollbar'ların kesişiminde, üstüste gelmesinler diye boşluk bırakılır
   if (this.vSBar.eVSBar.style.visibility == "visible" && this.hSBar.eHSBar.style.visibility == "visible") {
      this.vSBar.eVSBar.style.bottom = this.hSBar.eHSBar.clientHeight + "px";
      this.hSBar.eHSBar.style.right  = this.vSBar.eVSBar.clientWidth  + "px";
   }

   this.vSBar.ButtonPosition();
   this.hSBar.ButtonPosition();

}

