var slider = {}

//config
slider.steps = 20
slider.jQajax = true //false
slider.adjustParentScroll = true //false
slider.defaultClassName = 'slider'
slider.defaultTitleTag = 'span' //label, b, i, 
slider.defaultContentTag = 'div' //span
slider.defaultLinkTag = 'a' // it can be force to href attribute to another tag, but its not valid 
slider.writeCss = true //false
//end

function fnIsIE4() { 
  var browser=navigator.appName;
  var b_version=navigator.appVersion;
  var version=parseFloat(b_version);
  return (browser == 'Microsoft Internet Explorer' && version == 4)? true : false;    
}
boolIE4 = fnIsIE4();

if (slider.writeCss) {
  document.write('<style type="text/css">\n') 
  document.write('.' + slider.defaultClassName + ' ' + slider.defaultTitleTag + '{cursor:pointer;}\n')
  document.write('.' + slider.defaultClassName + ' ' + slider.defaultContentTag + '{display:none;height:0px;overflow:hidden;}\n')
  document.write('</style>')
}

slider.fnProcess = function(strSection, obj) {
  switch (strSection) {
    case 'title':
    if (this.jQajax) {
     strContent = $.ajax({
        url: ".fn.php?fn=strip_tags&data=" + obj.innerHTML,
        async: false
       }).responseText;
      (strContent.length > 0)? obj._title = strContent : obj._title = obj.title;
      
      }else {
      obj._title = obj.title
      }
      obj.title = "ava " + strContent      
    break;

    case 'content':
      obj.style.display = "none"
  //    obj.style.visibility = "visible"
    break;
  }

}


slider._do = function(obj, contents) {
  step = 5 
  var boolR

  for (var i = 0; i < contents.length; i++){
    if (i != obj._no) {
      if (contents[i].offsetHeight > 0 && contents[i].style.display == "block") {
        step = contents[i].scrollHeight/this.steps
        var toHeight = contents[i].offsetHeight - step
        contents[i].style.height = ((toHeight > 0)? toHeight : 0) + 'px'
        boolR = true   
      }
      if (toHeight <= 0) {
        this.arrTitles[i].title = "ava " + this.arrTitles[i]._title;       
        contents[i].style.display = "none"
      }
    }         
  } 

  if (obj.style.display == "none") {
    if (obj._link) {
    strContent = $.ajax({
      url: obj._link,
      async: false
    }).responseText;
    obj._link = 0
    obj.innerHTML = strContent 
    }

    obj.style.display = "block"
    obj.style.height = "1 px"   
  }
  
  if (obj.scrollHeight > obj.offsetHeight) {
    step = obj.scrollHeight/this.steps
    obj.style.height = obj.clientHeight + step + 'px'
    boolR = true       
  }else {
    obj.style.height = obj.scrollHeight + 'px'
    this.arrTitles[obj._no].title = "avatud : " + this.arrTitles[obj._no]._title
    if (this.adjustParentScroll) {
      this.arrTitles[obj._no].parentNode.parentNode.scrollTop = this.arrTitles[obj._no].offsetTop
    }     
  }
  
  if (boolR) {
    window["slider_timer"+obj._no] = setTimeout(function(){slider._do(obj, contents)}, 10)
  }else {
    if (typeof window["slider_timer"+obj._no] != "undefined") {
      window.clearTimeout(window["slider_timer"+obj._no])
    }  
  }
}
 
slider._slide = function(contents, obj, e) {
  arrOpen = new Array()
  var bool = (!boolIE4)? ((e.ctrlKey == 1)? true : false) : ((window.event && event.ctrlKey)? true : false);  
  
  if (bool != true) {    
    arrOpen = contents    
  }    

  this._do(contents[obj._no], arrOpen)
       
}

slider.init = function(className, titleTag, contentTag, linkTag, classNameOpen, intSteps) {
  className = (typeof className != "undefined" && className.length > 0)? className : 'slider';
  titleTag = (typeof titleTag != "undefined" && titleTag.length > 0)? titleTag : 'span';
  contentTag = (typeof contentTag != "undefined" && contentTag.length > 0)? contentTag : 'div';
  classNameOpen = (typeof classNameOpen != "undefined" && classNameOpen.length > 0)? classNameOpen : 'slider open';  
  linkTag = (typeof linkTag != "undefined" && linkTag.length > 0)? linkTag : 'a';
  this.steps = (typeof intSteps != "undefined" && intSteps.length > 0)? intSteps : this.steps;
      
  var sliders = gradualFader.collectElementbyClass(className)
  var titles = new Array()
  var contents = new Array()

	for (var i = 0; i < sliders.length; i++){ 
    titles[i] = sliders[i].getElementsByTagName(titleTag)[0]
    titles[i]._no = i
    contents[i] = sliders[i].getElementsByTagName(contentTag)[0]
    contents[i]._no = i
    
    if (this.jQajax && linkTag.length > 0) {
      var recLinkTag = sliders[i].getElementsByTagName(linkTag)
      contents[i]._link = 0
      if (recLinkTag.length > 0) {
        contents[i]._link = recLinkTag[0].href
      }
    }   

    this.fnProcess('content', contents[i])
    this.fnProcess('title', titles[i])

  }
  this.arrTitles = titles
  this.arrSliders = sliders

  var tempArr = []
  var toOpenRE = new RegExp("(^|\\s+)" + classNameOpen + "($|\\s+)", "i")   
	for (var i = 0; i < sliders.length; i++){   
    titles[i].onclick = function(e){slider._slide(contents, this, e)}
    if (typeof sliders[i].className=="string" && sliders[i].className.search(toOpenRE)!=-1) {
      this._do(contents[i], tempArr)
    }
  }  

}

