var DDSPEED = 1;
var DDTIMER = 15;
var Permanent = 'false'; 
var changeImg = 1;
var IsSlideOn = 0;
 
// main function to handle the mouse events //
function ddMenu(id,d, leftmargin)
{
    resetMenu(id); 
    changeSpanTextColor('white', id);
    changeDropdownImage('/images/buttonDown_On.png', id);
  
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');

  var x = document.getElementById(id);
  clearInterval(c.timer);
  if(d == 1)
  {
    x.className="dropdownC";
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight)
    {
        return
    }
    else if(!c.maxh)
    {
      c.style.display = 'block';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
   
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
  else
  {
      h.timer = setTimeout(function() { ddCollapse(c, id, d) }, 10);    
      x.className="dropdown";
  }
  
   setMargin(leftmargin);
}


function resetMenu(id)
{
    var c = document.getElementById(id + '-ddcontent');
    c.style.display = 'block';
    c.maxh = undefined;
}

// main function to handle the mouse events //
function ddMenuPermanent(id,d, leftmargin, strPermanent)
{ 
 resetMenu(id); 
 if(strPermanent == 'undefined')
  {
     strPermanent = 'false';
  }
  Permanent = strPermanent;  
  if(Permanent != 'true')
  {
    changeSpanTextColor('white', id);
    changeDropdownImage('/images/buttonDown_On.png', id);
  }
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');

  var x = document.getElementById(id);
  clearInterval(c.timer);
  if(d == 1)
  {
    if(Permanent != 'true')
    {
        x.className="dropdownC";
    }
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight)
    {
        return;
    }
    else if(!c.maxh)
    {
      c.style.display = 'block';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
   
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
  else
  {    
    h.timer = setTimeout(function() { ddCollapse(c, id, d) }, 10);      
    if(Permanent == 'true')
    {
        x.className="dropdownP";
    }
    else
    {
        x.className="dropdown";
    }
  }
  
   setMargin(leftmargin);

}

// main function to handle the mouse events //
function ddMenuPermanent350(id, d, leftmargin, strPermanent) 
{
    resetMenu(id);
    if (strPermanent == 'undefined') {
        strPermanent = 'false';
    }
    Permanent = strPermanent;
    if (Permanent != 'true') {
        changeSpanTextColor('white', id);
        changeDropdownImage('/images/buttonDown_On.png', id);
    }
    var h = document.getElementById(id + '-ddheader');
    var c = document.getElementById(id + '-ddcontent');

    var x = document.getElementById(id);
    clearInterval(c.timer);
    if (d == 1) {
        if (Permanent != 'true') {
            x.className = "dropdownC";
        }
        clearTimeout(h.timer);
        if (c.maxh && c.maxh <= c.offsetHeight) {
            return
        }
        else if (!c.maxh) {
            c.style.display = 'block';
            c.style.height = 'auto';
            c.maxh = c.offsetHeight;
            c.style.height = '0px';

        }
        c.timer = setInterval(function() { ddSlide(c, 1) }, DDTIMER);
    }
    else {
        h.timer = setTimeout(function() { ddCollapse(c, id, d) }, 10);
        if (Permanent == 'true') {
            x.className = "dropdownP_350";
        }
        else {
            x.className = "dropdown";
        }
    }

    setMargin(leftmargin);
}


function ddMenuPermanentbig(id, d, leftmargin, strPermanent) {
    resetMenu(id);
    if (strPermanent == 'undefined') {
        strPermanent = 'false';
    }
    Permanent = strPermanent;
    if (Permanent != 'true') {
        changeSpanTextColor('white', id);
        changeDropdownImage('/images/buttonDown_On.png', id);
    }
    var h = document.getElementById(id + '-ddheader');
    var c = document.getElementById(id + '-ddcontent');

    var x = document.getElementById(id);
    clearInterval(c.timer);
    if (d == 1) {
        if (Permanent != 'true') {
            x.className = "dropdownC";
        }
        clearTimeout(h.timer);
        if (c.maxh && c.maxh <= c.offsetHeight) {
            return
        }
        else if (!c.maxh) {
            c.style.display = 'block';
            c.style.height = 'auto';
            c.maxh = c.offsetHeight;
            c.style.height = '0px';

        }
        c.timer = setInterval(function () { ddSlide(c, 1) }, DDTIMER);
    }
    else {
        h.timer = setTimeout(function () { ddCollapse(c, id, d) }, 10);
        if (Permanent == 'true') {
            x.className = "dropdownbig";
        }
        else {
            x.className = "dropdown";
        }
    }

    setMargin(leftmargin);
}

// main function to handle the mouse events //
function ddMenuPermanentsmall(id, d, leftmargin, strPermanent) {
    resetMenu(id);
    if (strPermanent == 'undefined') {
        strPermanent = 'false';
    }
    Permanent = strPermanent;
    if (Permanent != 'true') {
        changeSpanTextColor('white', id);
        changeDropdownImage('/images/buttonDown_On.png', id);
    }
    var h = document.getElementById(id + '-ddheader');
    var c = document.getElementById(id + '-ddcontent');

    var x = document.getElementById(id);
    clearInterval(c.timer);
    if (d == 1) {
        if (Permanent != 'true') {
            x.className = "dropdownC";
        }
        clearTimeout(h.timer);
        if (c.maxh && c.maxh <= c.offsetHeight) {
            return
        }
        else if (!c.maxh) {
            c.style.display = 'block';
            c.style.height = 'auto';
            c.maxh = c.offsetHeight;
            c.style.height = '0px';

        }
        c.timer = setInterval(function() { ddSlide(c, 1) }, DDTIMER);
    }
    else {
        h.timer = setTimeout(function() { ddCollapse(c, id, d) }, 10);
        if (Permanent == 'true') {
            x.className = "dropdownP_small";
        }
        else {
            x.className = "dropdown";
        }
    }

    setMargin(leftmargin);
}

function setMargin(leftmargin)
{

   if (leftmargin == undefined)
    return;
    
    var istylecount = 0, iDropDownStyleSheet=0, idtCount=0;
    istylecount= document.styleSheets.length;
    var sStyleSheet, stylesheetname, dtName;
     for(iscount=0;iscount<istylecount; iscount++)
     {
        sStyleSheet = document.styleSheets[iscount];
        stylesheetname = sStyleSheet.href;
        if (stylesheetname != null) 
        {
            if (stylesheetname.indexOf("dropdown.css") > -1) {
                iDropDownStyleSheet = iscount;
                return;
            }
        }
     }
     
     if (document.all)
     {
         if (iDropDownStyleSheet > -1)
         {
              var count = document.styleSheets[iDropDownStyleSheet].rules.length;
               var icount=0;
              for(icount=0;icount< count; icount++)
                  {
                    dtName = document.styleSheets[iDropDownStyleSheet].rules[icount].selectorText;
                    dtName = dtName.toLowerCase();
                    if ( dtName.indexOf(".dropdownc dt") > -1)
                    //if ( dtName.indexOf(".dropdownC DT") > -1)
                    {
                      idtCount = icount;
                    }
                  }
                  if (leftmargin == "right")
                        document.styleSheets[iDropDownStyleSheet].rules[idtCount].style.margin ="0 0 0 3.7em";
         }
     }
     else
     {
         if (iDropDownStyleSheet > -1)
         {
              var count = document.styleSheets[iDropDownStyleSheet].cssRules.length;
               var icount=0;
              for(icount=0;icount< count; icount++) {
                  
                      dtName = document.styleSheets[iDropDownStyleSheet].cssRules[icount].selectorText;
                      if (dtName != undefined) {
                      dtName = dtName.toLowerCase();
                  
                    
                    if ( dtName.indexOf(".dropdownc dt") > -1)
                    //if ( dtName.indexOf(".dropdownC dt") > -1)
                    {
                      idtCount = icount;
                    }
              }
                  }

             if (leftmargin == "right")
                       document.styleSheets[iDropDownStyleSheet].cssRules[idtCount].style.margin ="0 0 0 3.7em";

         }
     }
}

function changeSpanTextColor(color, id) {       
       document.getElementById(id+'idtext').style.color = color;       
     }

function changeDropdownImage(imagepath, id)
 {
    document.getElementById(id+'iddispimage').src=imagepath;    
 }

// collapse the menu //
 function ddCollapse(c, id, d)
{
    changeSpanTextColor('#6B797E', id);
    changeDropdownImage('/images/buttonDown_Off.png', id);
    var currh = c.offsetHeight;
    c.style.height = 0 + 'px';
    c.style.opacity = currh / c.maxh;
    c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
    c.style.display = 'none';
    if (currh < 2 && d != 1) {
        clearInterval(c.timer);
    }
    c.timer = 2;
    IsSlideOn = 0;
    changeImg = 1;
    Permanent = 'false';
    //c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);  
}


function changeContent(id,strValue)
{
    var x=document.getElementById(id+'-tableSelected').rows
    var y=x[0].cells
    y[0].innerHTML="<span id='"+id+"-idtext'>"+strValue+"</span>"
}

// cancel the collapse if a user rolls over the dropdown //

function cancelHide(id, bgChange, strImage)
{
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  var x = document.getElementById(id);
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh)
  {
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
    
  if(Permanent != 'true')
  {    
    x.className="dropdownC";     
  }
  if (bgChange != undefined && strImage != undefined)
    {
        if(bgChange == 1)
        {
            changeImg = 1;
            setBackGround(id, strImage, 1);
        }
    }
  
 
}

function cancelHeader(id, bgChange, strImage, strImage1)
{
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  var x = document.getElementById(id);
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh)
  {
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  } 
  if(IsSlideOn == 0)
  {
      changeSpanTextColor('#6B797E', id);
      changeDropdownImage('/images/buttonDown_Off.png', id);
      if(Permanent != 'true')
      {    
        x.className="dropdownC";        
      }
      if (bgChange != undefined && strImage != undefined)
        {
            if(bgChange == 1)
            {
                if(changeImg != 0)
                {
                    changeImg = 1;
                    setBackGround(id, strImage, 1);
                    //changeImg = 0;
                }
            }
        }
  } 
  else
  {
    x.className="dropdownC"; 
    changeImg = 1;
                    setBackGround(id, strImage1, 1);
  } 
}

//// cancel the collapse if a user rolls over the dropdown //
//function cancelHide(id)
//{
//  var h = document.getElementById(id + '-ddheader');
//  var c = document.getElementById(id + '-ddcontent');
//  var x = document.getElementById(id);
//  clearTimeout(h.timer);
//  clearInterval(c.timer);
//  if(c.offsetHeight < c.maxh)
//  {
//    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
//  }
//   
//  if(Permanent != 'true')
//  {
//    x.className="dropdownC"; 
//  }
// 
//}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c,d)
{
  var currh = c.offsetHeight;  
  var dist;
  if(d == 1)
  {
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else
  {
    dist = (Math.round(currh / DDSPEED));
  }
  if(dist <= 1 && d == 1)
  {
    dist = 1;
  }
  //c.style.height = currh + (dist * d) + 'px';
  if(d == -1)
  {
    c.style.height = 0;    
  }
  else
  {
    c.style.height = currh + (dist * d) + 'px';    
  }
   
  c.style.opacity = currh / c.maxh;
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1))
  {
    clearInterval(c.timer);
  }  
  

}


function setBackGround(id, strImage, changeImg1)
{    
    var header = document.getElementById(id + '-ddheader');
    var img = header.style.backgroundImage;    
    if(changeImg == 1)
    {
        if(strImage == '')
        {
            header.style.backgroundImage = "";
        }
        else
        {
            header.style.backgroundImage = "url("+strImage+")";
        }    
    }

}

function onHeaderClick(id, intDirection, alignment, IsPermanent, bgChange, strImage)
{
    if(IsPermanent == 'true')
        ddMenuPermanent(id, intDirection,alignment, 'true');
    else
        ddMenu(id,intDirection, alignment, IsPermanent);
    
    if(bgChange == 1)
    {
        setBackGround(id, strImage, 1);
        changeImg = 0;    
    }
    IsSlideOn = 1;
}

function onContentEvent(id, intDirection, alignment, IsPermanent, bgChange, strImage)
{
    ddMenu(id, intDirection,alignment,IsPermanent);    
    if(bgChange == 1)
    {
        changeImg = 1;
        setBackGround(id, strImage, 1);
    }
    //IsSlideOn = 0;
}
