var F = (function(){
function cpu(t,b,c,d) {return c*((t=t/d-1)*t*t+1)+b;};

function evaliceFocus(id, scrollElem, handleElem, currentClass, speed, duration){
  var flag = 0;
  var timer,scrollInterval=0;
  var step;
  
  for(var i=0; i<scrollElem.length; i++){// add event
    (function(i){
      handleElem[i].onmouseover = function(){
        clearTimeout(timer);
        thisPic(i);
        setHandle(i);
      };
      handleElem[i].onmouseout = function(){
        clearTimeout(timer);
        flag = i+1;
        if(flag == scrollElem.length)flag=0;
        timer = setTimeout(function(){scrollFocus()},speed);
      };
      scrollElem[i].onmouseover = function(){
        clearTimeout(timer);
      };
      scrollElem[i].onmouseout = function(){
        clearTimeout(timer);
        flag = i+1;
        if(flag == scrollElem.length)flag=0;
        timer = setTimeout(function(){scrollFocus()},speed);
      };
    })(i);
  };
  
  function thisPic(index){
    var picobj = F.$(id);
    var current=0;
	var count=0;
	var step=scrollElem[0].offsetHeight;
	if(index!=current&&count>duration/8){
		return;
	}
	clearInterval(scrollInterval);

	var span=index-current;
	var begin_value=picobj.scrollTop;
	var chang_in_value=span*step+(current*step-begin_value);
	scrollInterval=setInterval(function(){doit(begin_value,chang_in_value)},10);
	
	function doit(b,c){
		picobj.scrollTop=cpu(count,b,c,duration);
		count++;
		if(count==duration){
			clearInterval(scrollInterval);
			scrollInterval=0;
			count=0;
			picobj.scrollTop=b+c;
			current=index;
		}
	};
  };
  
  function setHandle(count){
    for(var i=0; i<handleElem.length; i++){
      handleElem[i].className = "";
    };
    handleElem[count].className = currentClass;
  };
  
  function scrollFocus(){
    thisPic(flag);
    setHandle(flag);
    flag++;
    if(flag == scrollElem.length)flag=0;
    timer = setTimeout(function(){scrollFocus()},speed);
  };
  scrollFocus();
};

getElementsByClassName = function(className, tag, parent){
    parent = parent || document;
    tag = tag || "*";
    var allTags = (tag === "*" && parent.all) ? parent.all : parent.getElementsByTagName(tag);
    var classElems = [];
    className = className.replace(/\-/g, "\\-");
    var regex = new RegExp("(^|\\s)" + className + "(\\s|$)");
    var elenemt;
    for(var i=0; i<allTags.length; i++){
      elem = allTags[i];
      if(regex.test(elem.className)){
        classElems.push(elem);
      }
    }
    return classElems;
};

function slowdown(element,theClass,start,space,speed,_f){
  var count = 0;
  var speed = speed || 50;
  function place(){
    element.style[theClass] = cpu(count,start,space,speed) +"px";
    count++;
    if(count === speed){
      clearTimeout(command);
      count = 0;
      element.style[theClass] = space + start + "px";
      if(_f)_f();
      return false;
    }
    var command = setTimeout(place,15);
  }
  place();
};

function slowdown_scroll(element,start,space,speed,_f){
  var count = 0;
  var speed = speed || 50;
  function place(){
    element.scrollTop = cpu(count,start,space,speed);
    count++;
    if(count === speed){
      clearTimeout(command);
      count = 0;
      element.scrollTop = space + start;
      if(_f)_f();
      return false;
    }
    var command = setTimeout(place,15);
  }
  place();
};

return{
  $:function(id){
    return typeof(id)=="object" ? id : document.getElementById(id);
  },
  
  $$:function(className, tag, parent){
    return getElementsByClassName(className, tag, parent);
  },
  
  tabLink:function(elem){
    var _li = elem.getElementsByTagName("li");
    for(var i=0; i<_li.length; i++){
      _li[i].onmouseover = function(){
        /*if(this.className.replace("current") < 0){
          this.className += " current";
        }*/
        this.className += " current";
      };
      _li[i].onmouseout = function(){
        this.className = this.className.replace(" current", "");
      };
    };
  },
  
  focus:function(id, scrollElem, handleElem, currentClass, speed, duration){
    evaliceFocus(id, scrollElem, handleElem, currentClass, speed, duration);
  },
  
  sideMouseover:function(elems){
    //alert(elems.length);
    for(var i=0; i<elems.length; i++){
      elems[i].onmouseover = function(){
        this.getElementsByTagName("div")[0].style.display = "block";
      };
      elems[i].onmouseout = function(){
        this.getElementsByTagName("div")[0].style.display = "none";
      };
    }
  },
  
  openAsk:function(){
    var _open = document.getElementById("askOpen");
    var _close = document.getElementById("askClose");
    var _questions = document.getElementById("questions");
    var myTime, _height;
    var flag = 0;
    _questions.style.display = "block";
    _height = parseInt(_questions.offsetHeight);
    _questions.style.display = "none";
    
    _open.onclick = function(){
      //alert(_questions.style.display);
      if(flag == 0 && _questions.style.display !== "block"){
        flag = 1;
        _questions.style.display = "block";
        slowdown(_questions, "height", 0, _height, 25, function(){flag = 0});
      }else{
        flag = 1;
        slowdown(_questions, "height", _height, -_height, 20, function(){
          _questions.style.display = "none";
          flag = 0;
        });
      };
      
    };
    _close.onclick = function(){
      flag = 1;
      slowdown(_questions, "height", _height, -_height, 20, function(){
        _questions.style.display = "none";
        flag = 0;
      });
    };
  },
  
  myDialog:function(){
    var elem = document.getElementById("questions");
    var _li = elem.getElementsByTagName("li");
    for(var i=0; i<_li.length; i++){
      _li[i].getElementsByTagName("a")[0].onclick = function(){
        //alert(i);
        var myHTML = this.parentNode.getElementsByTagName("div")[0].innerHTML;
        myHTML = '<div class="pop">'+ myHTML +'</div>';
        Dialog({msg:myHTML, lock:true});
        return false;
      };
    };
  }

};


})();
