//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
//
//SlideForm Class
//Dependence jquery.js
//
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

clsSlideForm = new Object();
clsSlideForm = function(){


  //*******************************************************
  // Propaty
  //*******************************************************
  //=======================================================
  // Stract
  //=======================================================
  //Instance Name
  this.strInsName = "sf";

  //SlideFormPropaty
  this.objSFValue = {
    "min_width"   : 46,
    "max_width"   : 486,
    "pos_left"    : 855,
    "pos_top"     : -1008,
    "open_step"   : 50,
    "open_speed"  : 10,
    "slie_speed"  : 5,
    "open_status" : "close"
  }
  
  //Image Root
  this.strImgRoot = "";
  //this.strImgRoot = "../../";

  //=======================================================
  // Objects
  //=======================================================
  //Slide Menu Area
  this.objSlideForm = new Object();

  //Slide Menu Tab
  this.objSlideTab  = new Object();

  //=======================================================
  // Propaty
  //=======================================================
  //Debug Flag
  this.blnDebug = 0;

  //*******************************************************
  // Constructor
  //*******************************************************

  //*******************************************************
  // Init
  //*******************************************************
  this.init = function(){

    //=====================================================
    //Debugger
    //=====================================================
    if (this.blnDebug){
      this.setDebug();
    }

    //=====================================================
    //IE6 hack
    //=====================================================
    try {
      document.execCommand('BackgroundImageCache', false, true);
    } catch(e) {}

    //=====================================================
    //Set Object
    //=====================================================
    this.objSlideForm = $("#slide_form")[0];
    this.objSlideTab = $("#sf_tab")[0];
      
    //=====================================================
    //Set Event
    //=====================================================
    //MenuOpen
    var strFunction = "this.togglemenu();";
    $("#sf_tab").click(this.getFnc(strFunction, this));

    //=====================================================
    //Exec Method
    //=====================================================
    this.slidemenu();
    
  }

  //*******************************************************
  // SlideMenu
  //*******************************************************
  this.slidemenu = function(){
    this.slidemenu_exec();
    setTimeout(this.getFnc("this.slidemenu();", this), this.objSFValue['slie_speed']);
  }
  this.slidemenu_exec = function(){

    var intFormTop = 0;

    var objContainer  = document.getElementById('main');
    var intScrollPos  = document.body.scrollTop || document.documentElement.scrollTop;
    var intOffsetPos  = this.objSlideForm.offsetTop;
    var intPosTop     = 0
    var intLimitTop   = this.objSFValue['pos_top'];
    var intLimitBttom = objContainer.offsetHeight - this.objSlideForm.offsetHeight;

    //Set Position
    intPosTop = Math.abs(intScrollPos + intLimitTop - intOffsetPos);
    if (intPosTop == 0){ return false; }
    if (intScrollPos < 0){ return false; }

    var intCoefficient = 10;
    if (intPosTop < 10){
      intCoefficient = intPosTop;
    }
    intFormTop =  intOffsetPos + Math.floor((intScrollPos + intLimitTop - intOffsetPos) / intCoefficient);

    //Overflow Init
    if (intLimitBttom < intFormTop){
      intFormTop = intLimitBttom;
    }
//    else if(intLimitTop >= intFormTop){
//      intFormTop = intLimitTop;
//    }

    this.objSlideForm.style.top = intFormTop + "px";
  }

  //*******************************************************
  // ShowMenu / HideMenu
  //*******************************************************
  this.togglemenu = function(){
    this.togglemenu_exec();
  }
  this.togglemenu_exec = function(){

    //=====================================================
    //Define
    //=====================================================
    var intNowWidth = this.objSlideForm.offsetWidth;
    var intMinWidth = this.objSFValue['min_width'];
    var intMaxWidth = this.objSFValue['max_width'];
    var intOpenStep = this.objSFValue['open_step'];
    var intPosLeft  = this.objSFValue['pos_left'];
    var strOpenStat = this.objSFValue['open_status'];
    var blnStop     = 0;
    var objIcon     = $("#tab_img")[0];

    //=====================================================
    //Set Values
    //=====================================================
    //Set Width , Set Position
    if (strOpenStat == "close"){
      intNowWidth += intOpenStep;
      if (intNowWidth >= intMaxWidth){ 
        intNowWidth = intMaxWidth;
        strOpenStat = "open";
        objIcon.className = "tab_r";
        blnStop = 1;
      }
    }else{
      intNowWidth -= intOpenStep;
      if (intNowWidth <= intMinWidth){ 
        intNowWidth = intMinWidth;
        strOpenStat = "close";
        objIcon.className = "tab_l";
        blnStop = 1;
      }
    }
    this.objSlideForm.style.width = (intNowWidth) + "px";
    this.objSlideForm.style.left = (intPosLeft - intNowWidth + intMinWidth) + "px";

    //=====================================================
    //Status Change
    //=====================================================
    this.objSFValue['open_status'] = strOpenStat;

    //=====================================================
    //Set Interval
    //=====================================================
    if (blnStop != 1){
      setTimeout(this.getFnc("this.togglemenu();", this), this.objSFValue['open_speed']);
    }

    // //=====================================================
    // //Debug
    // //=====================================================
    // this.debug("<br>" + strOpenStat + ":" + intNowWidth, 0);

  }







  //*******************************************************
  // Event Capture
  //*******************************************************
  this.addEvent = function(objElement, strListener, objFunction){
    try{
      objElement.addEventListener(strListener, objFunction, false);
    }catch(e){
      objElement.attachEvent("on"+strListener, objFunction);
    }
  }

  //*******************************************************
  // FunctionObject Generate
  //*******************************************************
  this.getFnc = function(vstrFunction, vobjInstanse){
    var strFunction = vstrFunction.replace(/this\./g,"vobjArges.");
    
    var objFunction = (function(vobjArges){
      return function(){ eval(strFunction) };
    })(vobjInstanse);
    
    return objFunction;
  }

  //*******************************************************
  // Checke Undefined
  //*******************************************************
  this.isUnDef = function(vobjTarget){
    if (vobjTarget == undefined || vobjTarget == "undefined"){
      return 1;
    }
    return 0;
  }


  //*******************************************************
  // Out Debug Area
  //*******************************************************
  this.setDebug = function(){

    if (document.getElementById('_debug')){ return; }

    var intAlpha = 75;
    var strWrite = "";
    strWrite += "<div id=\"_debug\" ";
    strWrite += "style=\"";
    strWrite += "position:fixed; top:0; left:0; ";
    strWrite += "z-index:999; ";
    strWrite += "max-height:100%; ";
    strWrite += "overflow:hidden; ";
    strWrite += "border:1px solid black; background:#ffffff; ";
    strWrite += "filter: alpha(opacity="+intAlpha+"); ";
    strWrite += "-moz-opacity:0."+intAlpha+"; ";
    strWrite += "opacity:0."+intAlpha+";);\">debug</div>\n";
    document.body.innerHTML += strWrite;
  }

  //*******************************************************
  // Debug Print
  //*******************************************************
  this.debug = function(vstrString, vblnPreserve){
    if (!document.getElementById('_debug')){ return; }
    
    var objDebug = document.getElementById('_debug');
    if (vblnPreserve){
      objDebug.innerHTML = vstrString;
    }else{
      objDebug.innerHTML += vstrString;
    }
  }


}

//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
//
// Const
//
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
sf = new clsSlideForm();
sf.addEvent(window, "load", function(){
  sf.init();
});


//=========================================================


