/** 
 * bug to fix: Flickering CSS Background Images in IE6
 * conditions: when IE's cache settings are "Every visit to the page" (a common developer's setting) 
 */
if (document.all) {
    try {
        document.execCommand("BackgroundImageCache", false, true);
    } catch (err) {}
}
var kf={};
(function() {
var YUD = YAHOO.util.Dom;
var YUE = YAHOO.util.Event;

kf.menu = {
    elBase:null,
    elNavAni:null,
    regionNavAni:null,
    oMainNav:null,
    selectedSubNav:null,
    selectedAnchorClass:null,
    menuDelay:750,
    init:function() {
        var elMenuBarLbl,i,menuItems;
        this.elBase = YUD.get("main_nav");
        this.elNavAni = YUD.get("nav_hover");
        if ( this.elNavAni ) {
            this.regionNavAni = YUD.getRegion(this.elNavAni);
            this.regionNavAni.width = this.regionNavAni.right - this.regionNavAni.left;
            this.regionNavAni.height = this.regionNavAni.bottom - this.regionNavAni.top;
        }
        if ( this.elBase ) {
            this.oMainNav = new YAHOO.widget.MenuBar("main_nav",{autosubmenudisplay:true,hidedelay:this.menuDelay,lazyload:true});
            this.oMainNav.render();
            menuItems = this.oMainNav.getItems();
            if ( kf_config.currentSection ) {
                this.selectedAnchorClass = "m_sec_"+kf_config.currentSection;
            }
            for (i = 0; i < menuItems.length; i++) {
                if (menuItems[i]._oSubmenu) {
                    menuItems[i]._oSubmenu.showEvent.subscribe(this._onMenuShow,{el:menuItems[i]._oAnchor},this);
                    menuItems[i]._oSubmenu.hideEvent.subscribe(this._onMenuHide,{el:menuItems[i]._oAnchor},this);
                }
                if (this.selectedAnchorClass && YUD.hasClass(menuItems[i]._oAnchor,this.selectedAnchorClass)) {
                    this.selectedSubNav = menuItems[i]._oSubmenu.element;
                }
            }
            YUE.on(this.elBase,"mouseover",this._handleMouseOver,this,true);
            YUE.on(this.elBase,"mouseout",this._handleMouseOut,this,true);
        }
    },
    _onMenuHide:function(e_type,arg,param) {
        this.hideNavAni(param.el);
        if (this.selectedSubNav && !YUD.hasClass(this.selectedSubNav,"nav_selected")) {
            YUD.addClass(this.selectedSubNav,"nav_selected");
        }
    },
    _onMenuShow:function(e_type,arg,param) {
        if ( !YUD.hasClass(param.el, this.selectedAnchorClass) ) {
            this.showNavAni(param.el);
            if (this.selectedSubNav) {
                YUD.removeClass(this.selectedSubNav,"nav_selected");
            }
        }
    },
    _handleMouseOver:function(e) {
        var target;
        target = e.target || e.srcElement;
        if ( target && (
            YUD.hasClass(target,"m_sec_news") ||
            YUD.hasClass(target,"m_sec_book") ||
            YUD.hasClass(target,"m_sec_getinvolve") ||
            YUD.hasClass(target,"m_sec_contact")
            )
        ) {
            this.showNavAni(target);
        }
    },
    _handleMouseOut:function(e) {
        var target;
        target = e.target || e.srcElement;
        if ( target && (
            YUD.hasClass(target,"m_sec_news") ||
            YUD.hasClass(target,"m_sec_book") ||
            YUD.hasClass(target,"m_sec_getinvolve") ||
            YUD.hasClass(target,"m_sec_contact")
            )
        ) {
            this.hideNavAni(target);
        }
    },
    _handleMouseOut:function(e) {
        var target;
        target = e.target || e.srcElement;
        if ( YUD.hasClass(target,"yuimenubaritemlabel") ) {
            this.hideNavAni(target);
        }
    },
    showNavAni:function(el) {
        var r;
        r = YUD.getRegion(el);
        this.elNavAni.style.top = (r.top - this.regionNavAni.height) + "px";
        this.elNavAni.style.left = (r.left + (r.right - r.left -this.regionNavAni.width)/2) + "px";
        this.elNavAni.style.visibility = "visible";
    },
    hideNavAni:function(el) {
        this.elNavAni.style.visibility = "hidden";
    },
    later:function (msec, method) {
        var that = this, args = Array.prototype.slice.apply(arguments, [2]); 
        if (typeof method === 'string') { 
            method = that[method]; 
        } 
        setTimeout(function () { method.apply(that, args); }, msec); 
        return that; 
    }
};

kf.quoteAni = {
    oAni:null,
    currentQuote:-1,
    quoteImgs:[],
    init:function() {
        var elQuoteCont = YUD.getElementsByClassName("quote_count","div").shift();
        if (elQuoteCont) {
            this.quoteImgs = YUD.getElementsBy(function(){return true;},"img",elQuoteCont);
            this.startFadeIn();
        }
    },
    startFadeIn:function() {
        var r;
        do {
            r = parseInt(Math.random()*4,10);
        } while ( r === this.currentQuote );
        var attributes = {
            opacity: { from:0,to: 1 }
        };
        this.oAni = new YAHOO.util.Anim(this.quoteImgs[r], attributes,1.5,YAHOO.util.Easing.easeOut);
        this.oAni.animate();
        this.currentQuote = r;
        this.later(6000,this.startFadeOut);
    },
    later:function (msec, method) {
        var that = this, args = Array.prototype.slice.apply(arguments, [2]); 
        if (typeof method === 'string') { 
            method = that[method]; 
        } 
        setTimeout(function () { method.apply(that, args); }, msec); 
        return that; 
    },
    startFadeOut:function() {
        var attributes = {
            opacity: { from:1,to: 0 }
        };
        this.oAni = new YAHOO.util.Anim(this.quoteImgs[this.currentQuote], attributes,1.5,YAHOO.util.Easing.easeOut);
        this.oAni.animate();
        this.later(3000,this.startFadeIn);
    }
};

kf.martial_gallery = {
    sets:7,
    captions:[
        "Re-enactment of Yuenu's sword discourse, a famous passage from Lu's Spring and Autumn.<br />This will be shown at the art exhibition.<br />越女論劍,呂氏春秋裡著名的一段.",
        "Oscar Lam demonstrating a famous staff set from Hung Kuen called Monkey Staff.<br />林浩全演譯洪拳的行者棒.",
        "Omar Ma and Geng Zhichu demonstrating Duanbing or Chinese fencing techniques.<br />馬廉禎和耿之矗演譯短兵.",
        "Omar Ma demonstrating the whip-stick.<br />馬廉禎演譯鞭杆.",
        "Master Liu Shuchun demonstrating techniques from Sun's Martial Arts System.<br />劉樹春老師表演孫氏武學.",
        "Students from Hunan Province Zhengfan Culture & Media Company Ltd. displaying their skills with nunchaku.<br />湖南振藩文化的學生示範雙節棍.",
        "International Guoshu Association's martial arts team.<br />中華國術總會學員演示傳統武術套路."
    ],
    setImgs:[4,4,4,2,4,4,7],
    currentSet:0,
    currentImg:0,
    elBase:null,
    elFimg:null,
    elCaption:null,
    init:function() {
        this.elBase = YUD.get("g_martial");
        this.elFimg = YUD.getElementsByClassName("focus-img","img",this.elBase).shift();
        this.elCaption = YUD.getElementsByClassName("caption","span",this.elBase).shift();
        this.elMask = YUD.getElementsByClassName("mask","div",this.elBase).shift();
        this.elLoading = YUD.getElementsByClassName("loading","div",this.elBase).shift();
        this.elArrowLeft = YUD.getElementsByClassName("arrow-left","img",this.elBase).shift();
        this.elArrowRight = YUD.getElementsByClassName("arrow-right","img",this.elBase).shift();
        YUE.on(this.elBase,"click",this._handleClick,this,true);
        YUE.on(this.elBase,"mouseover",this._handleMouseOver,this,true);
        YUE.on(this.elBase,"mouseout",this._handleMouseOut,this,true);
        YUE.on(this.elFimg,"load",this.hideLoading,this,true);
    },
    _handleClick:function(evt) {
        var target,idx;
        target = evt.target || evt.srcElement;
        if ( target.className.match(/show-set-/) ) {
            idx = target.className.match(/show-set-(\d+)/)[1];
            idx = parseInt(idx,10);
            this.showSet(idx-1);
        } else if ( YUD.hasClass(target,"cmd-next-img") ) {
            this.showNextImg();
        } else if ( YUD.hasClass(target,"cmd-prev-img") ) {
            this.showPrevImg();
        }
    },
    _handleMouseOver:function(evt) {
        var target = evt.target || evt.srcElement;
        if ( YUD.hasClass(target,"arrow-trigger-left") ) {
            this.elArrowLeft.style.display = "block";
        } else if ( YUD.hasClass(target,"arrow-trigger-right") ) {
            this.elArrowRight.style.display = "block";
        }
    },
    _handleMouseOut:function(evt) {
        var target = evt.target || evt.srcElement;
        if ( YUD.hasClass(target,"arrow-trigger-left") ) {
            this.elArrowLeft.style.display = "none";
        } else if ( YUD.hasClass(target,"arrow-trigger-right") ) {
            this.elArrowRight.style.display = "none";
        }
    },
    showSet:function(idx) {
        this.currentSet = idx;
        this.currentImg = 0;
        this._showImg(this.currentImg);
        this.elCaption.innerHTML = this.captions[idx];
    },
    showNextImg:function() {
        this.currentImg++;
        if ( this.currentImg >= this.setImgs[this.currentSet] ) {
            this.currentImg = 0;
        }
        this._showImg(this.currentImg);
    },
    showPrevImg:function() {
        this.currentImg--;
        if ( this.currentImg < 0 ) {
            this.currentImg = this.setImgs[this.currentSet] - 1;
        }
        this._showImg(this.currentImg);
    },
    _showImg:function(idx) {
        var setNum,imgNum;
        this.showLoading();
        setNum = this.currentSet+1;
        imgNum = idx + 1;
        if ( setNum < 10 ) {
            setNum = "0" + setNum;
        }
        if ( imgNum < 10 ) {
            imgNum = "0" + imgNum;
        }
        this.elFimg.src = "img/martial/set"+setNum+"_"+imgNum+".jpg";
    },
    showLoading:function() {
        this.elMask.style.visibility = "visible";
        this.elLoading.style.visibility = "visible";
    },
    hideLoading:function() {
        this.elMask.style.visibility = "hidden";
        this.elLoading.style.visibility = "hidden";
    }
}

}());

kf.menu.init();
kf.quoteAni.init();
kf.martial_gallery.init();