
/* 
shawl.qiu code
class: XAjaxTab
version: 1.0
date: 2008-6-22

updated: 1.1, shawl.qiu, 2008-9-6 20:36:37, 为项添加在回调时的判断是否有属性“filter_func”, 主要为过滤数据用.
updated: ver, name, date, summary.
*/

if(window.HTMLElement)
{
  Event.prototype.__defineSetter__
  (
    "returnValue", function (bBool){ if(bBool) this.stopPropagation(); else this.preventDefault(); }
  );
  
  Event.prototype.__defineGetter__( "srcElement" , function (bBool){ return this.target; });

  HTMLElement.prototype.attachEvent =
  function(sEvtName, Func, bCaptures)
  {
   if(!bCaptures) bCaptures = false;
   sEvtName = sEvtName.slice(2);
   this.addEventListener(sEvtName, Func, bCaptures);
  };  
  document.attachEvent = HTMLElement.prototype.attachEvent;
  
  HTMLElement.prototype.detachEvent =
  function(evt, fn, bCaptures)
  {
   evt = evt.slice(2);
   this.removeEventListener(evt, fn, bCaptures);
  }; 
  document.detachEvent = HTMLElement.prototype.detachEvent;
}/* if(window.HTMLElement) */

if(typeof __XAjaxTab_Count) __XAjaxTab_Count = 0;

XAjaxTab.prototype.Main = XAjaxTab_Main;
XAjaxTab.prototype.Add = XAjaxTab_Add;

function XAjaxTab()
{
  var This = this;
  
  This.EventName = "onclick";  
  This.ContentDisplay = "block";
  
  This.CookieNamePrefix = "XAtCkName_";
  This.Callback = {};
  This.Callback.Content_Filter = undefined;
  
  This.ClickDelayMs = 0;
}/* function XAjaxTab() */

function XAjaxTab_Main()
{
  var This = this;
}/* function XAjaxTab_Main() */

function XAjaxTab_Add(MainObj, ContentObj, iDefaultDisplay, ExtObj)
{
  var This = this;
  if(!iDefaultDisplay) iDefaultDisplay = 0;
  if(!ExtObj) ExtObj = {};
  if(!ExtObj.ClassName) ExtObj.ClassName = "Tab";  
  
  var InstanceCount = __XAjaxTab_Count++;
  var CookieName = This.CookieNamePrefix+InstanceCount;  
  var CkDefaultSelected = fReadCookie(CookieName);
  if(/^[0-9]+$/.test(CkDefaultSelected)) iDefaultDisplay = parseInt(CkDefaultSelected);
  //defaultStatus =CkDefaultSelected
  
  MainObj.Ele = fSetObjectX(MainObj.Ele, "XAjaxTab MainObj.Ele is null!");
  MainObj.TagSet = fTagLevel(MainObj.Ele, MainObj.TagLevelAr);
  
  var MaxLen = MainObj.TagSet.length;
  if(iDefaultDisplay>=MaxLen) iDefaultDisplay = MaxLen-1;
  if(iDefaultDisplay<0) iDefaultDisplay = 0;
  
  ContentObj.Ele = fSetObjectX(ContentObj.Ele, "XAjaxTab ContentObj.Ele is null!");
  ContentObj.TagSet = fTagLevel(ContentObj.Ele, ContentObj.TagLevelAr);
  
  for(var i=0, j=ContentObj.TagSet.length; i<j; i++) ContentObj.TagSet[i].ContentDisplay = "none";
  
  var Obj = {MainObj:MainObj, ContentObj:ContentObj, iDefaultDisplay:iDefaultDisplay, ExtObj:ExtObj, This:This, CookieName: CookieName};
  
  for(var i=0, j=MainObj.TagSet.length; i<j; i++)
  {
    var Item = MainObj.TagSet[i];
    Item.attachEvent(This.EventName, XAjaxTab_Event);
    Item.Xat = {};
    Item.Xat.Obj = Obj;
    Item.Xat.ExtIndex = i;
    fRemoveClassX(Item, ExtObj.ClassName);
  }
  fAddClass(MainObj.TagSet[iDefaultDisplay], ExtObj.ClassName);  
  ContentObj.TagSet[iDefaultDisplay].ContentDisplay = This.ContentDisplay ;
  XAjaxTab_Event(false, MainObj.TagSet[iDefaultDisplay]);
}/* function XAjaxTab_Add(MainObj, ContentObj, iDefaultDisplay, ExtObj) */

function XAjaxTab_Event(e, XEle)
{
  if(!XEle){ e = e||window.event; XEle = e.srcElement;};  
  var Obj = XEle.Xat.Obj, This = Obj.This, ExtObj = Obj.ExtObj;    
  e.returnValue = false;
  
  //alert(XEle.innerHTML)
  
  fAddCookie(Obj.CookieName, XEle.Xat.ExtIndex);
  
  for(var i=0, j=Obj.MainObj.TagSet.length; i<j; i++)
  {
    var Item = Obj.MainObj.TagSet[i];
    fRemoveClassX(Item, Obj.ExtObj.ClassName);
  }
  fAddClass(Obj.MainObj.TagSet[XEle.Xat.ExtIndex], Obj.ExtObj.ClassName+"_Current");
  
  for(var i=0, j=Obj.ContentObj.TagSet.length; i<j; i++) 
  {
    Obj.ContentObj.TagSet[i].style.display = "none";
  }
  Obj.ContentObj.TagSet[XEle.Xat.ExtIndex].style.display = This.ContentDisplay;
  
  var RefTag = Obj.ContentObj.TagSet[XEle.Xat.ExtIndex];
  RefTag.ContentDisplay = This.ContentDisplay ;
    
  if(typeof ExtObj.ItemEventCallabck=="function") ExtObj.ItemEventCallabck(XEle, RefTag, This, Obj);
  if(XEle.Xat.IsRequested) return false;
  else 
  {
    fAddClass(RefTag, ExtObj.ClassName+"_Loading");    
  }
  
  setTimeout
  (
    function()
    {
      fXhExt(XEle.href, XAjaxTab_Callback, [XEle, RefTag, This, Obj]);
    }
    , This.ClickDelayMs
  );
    
  XEle.Xat.IsRequested = true;
  return false;
}/* function XAjaxTab_Event(e) */

function XAjaxTab_Callback(Ar, XEle, RefTag, This, Obj)
{
  var filter_func = XEle.getAttribute("filter_func");
  if(filter_func!=null)
  {
    if(typeof window[filter_func]=="function")
    {
      Ar = window[filter_func](Ar);
    }
  }
  
  RefTag.innerHTML = Ar[0];
  
  fRemoveClassX(RefTag, Obj.ExtObj.ClassName+"_Loading");
  
  if(typeof Obj.ExtObj.InitItemDoneCallback=="function") Obj.ExtObj.InitItemDoneCallback(XEle, RefTag, This, Obj);
}/* function XAjaxTab_Callback(Ar, XEle, RefTag, This, Obj) */

function fTagLevel(Ele, TagAr, ResultAr)
{/* shawl.qiu code, return array */
  if(!ResultAr) ResultAr = [];
  var TagArLen = TagAr.length;  
  if(TagArLen === 0) return ResultAr;  
  if(TagArLen === 1)
  {
    for(var i = 0, j = Ele.childNodes.length; i < j; i++ )
    {
      var TempChild = Ele.childNodes[i];
      
      if(TempChild.nodeType === 1)
      {
        if(TempChild.tagName == TagAr[0].toUpperCase())
        {
          ResultAr.push(TempChild);
        }
      }
    }
    return ResultAr;
  }
  
  for(var i = 0, j = Ele.childNodes.length; i < j; i++ )
  {
    var TempChild = Ele.childNodes[i];    
    if(TempChild.nodeType === 1)
    {
      if(TempChild.tagName == TagAr[0].toUpperCase())
      {
        var TempAr = TagAr.slice();
        TempAr.shift();
        arguments.callee(TempChild, TempAr, ResultAr);
      }
    }
  }
  return ResultAr;
}/* function fTagLevel(Ele, TagAr, ResultAr) */

function fSetObjectX(Obj, sMsg, Dcu, bNoAlter)
{/* shawl.qiu code, return Element */
  if(!Obj) return null;
  if(!sMsg) sMsg = "无法获得对象!";
  if(!Dcu) Dcu = document;
  if(!bNoAlter) bNoAlter = false; 
  switch(Obj.constructor)
  {
    case String: Obj = Dcu.getElementById(Obj); break;   
    case Array: Obj = Obj[0]; break;
  } 
  if(Obj==null&&bNoAlter==false) throw new Error(sMsg); 
  return Obj;
}/* function fSetObjectX(Obj, sMsg, Dcu, bNoAlter) */

function fXhExt(sUrl, oFunc, ExtArgAr, sMethod)
{/* shawl.qiu code, void return */
 if(!sMethod) sMethod = "GET";
 var xh;
  
 try{ xh = new XMLHttpRequest();} 
 catch(e){
  try{ xh = new ActiveXObject('microsoft.xmlhttp');} 
  catch(e){ try{ xh = new ActiveXObject("Msxml2.XMLHTTP");} catch(e){} }
 }
 xh.open(sMethod, sUrl);
 xh.onreadystatechange =
  function()
  {
   if(xh.readyState == 4 && xh.status == 200)
   {
    if(oFunc)
    {
     if(ExtArgAr)
     {
      if(ExtArgAr.constructor==Array){ ExtArgAr.unshift([xh.responseText]); oFunc.apply(null, ExtArgAr); }
      else oFunc([xh.responseText]);
     }
     else oFunc([xh.responseText]);
    } 
    else alert(xh.responseText);
   }
  };
 
 xh.send(null);
}/* end function fXhExt(sUrl, oFunc, ExtArgAr, sMethod) */

function fReadCookie(sKey,  bDecode,  bIsKeySet)
{/* shawl.qiu script, void return; */
  if(!bIsKeySet) bIsKeySet  =  false;
  
  if(!sKey) return sCookie;
  var sCookie  =  document.cookie,  CookieSet  =  sCookie.split(";"),  CookieSetCount  =  0,  CookieSetLen  =  CookieSet.length;
  
  if(bIsKeySet)
  {
    var AllKeyRe  =  new RegExp("^"  +  sKey  +  "\\b\=",  ""),  PartKeyRe  =  new RegExp("[=&]\\b"  +  sKey  +  "\\b\=([^=]+)(&|$)",  "");
    
    while(CookieSetCount  <  CookieSetLen)
    {
      var sCur  =  CookieSet[CookieSetCount].replace(/^\s+|\s+$/g,  "");      
      if(AllKeyRe.test(sCur))
      {
        var MyKey  =  sCur.replace(AllKeyRe,  "");        
        if(bDecode) return decodeURIComponent(MyKey);
        return MyKey;
      }
      else if(PartKeyRe.test(sCur))
      {
        var MyKey  =  "";
        sCur.replace(PartKeyRe, function($1,  $2){ MyKey = $2; });        
        if(bDecode) return decodeURIComponent(MyKey);
        return MyKey;
      }
      CookieSetCount++  ;
    }/* while(CookieSetCount  <  CookieSetLen) */
  }
  else
  {
    var AllKeyRe  =  new RegExp("^"  +  sKey  +  "\\b\=",  "");    
    while(CookieSetCount  <  CookieSetLen)
    {
      var sCur  =  CookieSet[CookieSetCount];      
      if(sCur  ==  "")return "";
      sCur  =  sCur.replace(/^\s+|\s+$/g,  "");
      
      if(sCur.split("=").length  >  2) { CookieSetCount++  ; continue; }
      
      if(AllKeyRe.test(sCur))
      {
        var MyKey  =  sCur.replace(AllKeyRe,  "");        
        if(bDecode) return decodeURIComponent(MyKey);
        return MyKey;
      }
      CookieSetCount++  ;
    }
  }
  return "";
}/* function fReadCookie(sKey,  bDecode,  bIsKeySet) */

function fAddCookie(sKey,  sValue,  iSecond,  bEncode,  sPath)
{/* shawl.qiu script, void return; Func: fFormat */ 
  if(!sKey) {throw new Error('cookie key is null!'); return; }  
  if(typeof sValue=="undefined") sValue = "";
  sValue = sValue.toString();
  if(!iSecond)iSecond  =  60  *  60  *  24  *  30  *  7;/* 60*60*24*30*365; 一年 */
  if(!sPath) sPath  =  "/";
  
  if(bEncode)
  {
    sKey  =  encodeURIComponent(sKey);
    sValue  =  encodeURIComponent(sValue).replace(/\%3D/g,  "=").replace(/\%26/g,  "&");
  }
  var sCookie  =  "",  ExpiresDt  =  new Date();
  ExpiresDt.setSeconds(ExpiresDt.getSeconds()  +  iSecond);
  sCookie  =  fFormat("{0}={1}; expires={2}; path={3}",  sKey,  sValue,  ExpiresDt.toGMTString(),  sPath);
  document.cookie  =  sCookie;
}/* function fAddCookie(sKey, sValue, iSecond, bEncode, sPath) */

function fFormat(sStr)
{/* shawl.qiu code, return string */
  var Len = arguments.length, Re = null;  
  switch(Len)
  {
    case 0: return "";
    case 1: return sStr;
  }
  for(var i=1, j=0; i<Len; i++, j++)
  {
    Re = new RegExp(["\\{", j, "\\}"].join(""), "g");
    sStr = sStr.replace(Re, arguments[i]);
  }   
  return sStr;
}/* function fFormat(sStr) */

function fRemoveClassX(XEle, XClass)
{/* shawl.qiu code, void return */
  if(!XClass) throw new Error("XClass 不能为空!");
  if(!XEle.className) return;
  if(XEle.className!="") 
  {
    var Re = new RegExp("\\b"+XClass+"\\b\\s*", "");
    XEle.className = XEle.className.replace(Re, "");
  }
}/* function fRemoveClassX(XEle, XClass) */

function fAddClass(XEle, XClass)
{/* shawl.qiu code, void return */
  if(!XClass) throw new Error("XClass 不能为空!");
  if(XEle.className!="") 
  {
    var Re = new RegExp("\\b"+XClass+"\\b\\s*", "");
    XEle.className = XEle.className.replace(Re, "");
    XEle.className = XClass+" "+XEle.className;
  }
  else XEle.className = XClass;
}/* end function fAddClass(XEle, XClass) */
