/**
 * hitao.com background image process
 *
 * @author cnruhua<cnhruhua@gmail.com>
 */

/*
 {
    src:image path
    [,name:image name]
    [,fadeto：the boarder color of the image]
    [,action:how to display the image]
 }
 */

//object _b_ must be defined before

var HiBgImage = {
    target:null,
    randomImages:[],
    timingImages:[],
    chkInterval:600000, /*ten minutes*/
    /**
     * threshold
     *
     * options:
     * el:a function reference to get the target which used to be set background images
     * imgs:[] array defined the images to use
     * target:
     * chkInterval
     *
     */
    load:function(options){
        if(!options) return;
        var el = (options.el || function(){
            var _e = document.getElementById('content');
            if(_e){
                if(_e.className.indexOf('wing')>=0) return _e;
            }
            return null;
        })();
        if(!el) return;
        this.target = el;
        //process options.imgs
        var imgs = options.imgs;
        var len = imgs.length;
        if(len==0) return;
        if(options.chkInterval){
            this.chkInterval = parseInt(options.chkInterval,10)*60000;
        }
        for(var i=0;i<len;i++){
            this.parseAction(imgs[i]);
        }
        this.takeEffect(true);
        if(this.chkInterval>0){
            var me = this;
            window.setInterval(function(){
                me.takeEffect();
            },this.chkInterval);
        }
    },
    parseAction:function(img){
        var a = img.action;
        if(a == '!') return false;
        if(!a){
            this.randomImages.push(img);
            return true;
        }
        var as = a.split('&'),len=as.length;
        for(var i=0;i<len;i++){
            var rules = as[i].split('=');
            if(rules.length!=2) continue;
            var name = rules[0],raw = rules[1];
            if(name=='time'){
                var ts = raw.replace(/[\.:\s]*/g,'').split('-');
                img.time = [parseInt(ts[0],10),parseInt(ts[1],10)];
                img.timeType = (img.time[0]<2460) ? 1:2;
                this.timingImages.push(img);
            }else if(name=='random' && raw=='true'){
                this.randomImages.push(img);
            }
        }
        return true;
    },
    /*decision use which image,and use it*/
    takeEffect:function(overlay){
        overlay = overlay || false;
        //check timing images
        var imgs = this.timingImages;
        var len = imgs.length;
        if(len>0){
            var d = new Date(),foundimg=null;
            var year=d.getFullYear(),month=d.getMonth()+1,day=d.getDate(),hour = d.getHours(),minute=d.getMinutes();
            month = (month<10 ? '0':'') + month;
            day = (day<10 ? '0':'') + day;
            hour = (hour<10 ? '0':'') + hour;
            minute = (minute<10 ? '0':'') + minute;
            var dayTime = hour+''+minute;
            var fullTime = year+''+month+''+day+ dayTime;
            for(var i =0;i<len;i++){
                var img = imgs[i];
                if(img.timeType==1){        //compare by day,this image should be show every day in this time period
                    if(dayTime>=img.time[0] && dayTime<=(img.time[1]-this.chkInterval/60000)){
                        if(!foundimg) foundimg = img;
                        if(img.token && img.token == 0){
                            img.token = 1;
                            this.bg(img,overlay);
                            return;
                        }
                    }
                }else{      //compare by somedays,it is a long time
                    if(fullTime>=img.time[0] && fullTime<=(img.time[1]-this.chkInterval/60000)){
                        if(!foundimg) foundimg = img;
                        if(img.token && img.token == 0){
                            img.token = 1;
                            this.bg(img,overlay);
                            return;
                        }
                    }
                }
            }
            if(foundimg){
                for(i=0;i<len;i++){
                    imgs[i].token = 0;
                }
                this.bg(foundimg,overlay);
                return;
            }
        }
        imgs = this.randomImages;
        len=imgs.length;
        if(len>0){
            var img,j=-1;
            var ran = Math.floor(Math.random()*len+1);  //generate a random , then get a random image
            for(var i=ran;i<len;i++){
                img = imgs[i];
                if(img.token && img.token == 1) continue;
                else break;
            }
            if(i == len){
                for(j=0;j<ran;j++){
                    img = imgs[j];
                    if(img.token && img.token == 1 ) continue;
                    else break;
                }
            }
            //all images have been checked,then not find;
            if(j == ran){
                for(j=0;j<len;j++){      //reset token
                    imgs[j].token = 0;
                }
            }
            this.bg(img,overlay);
            return;
        }
    },
    bg:function(img,overlay){
        var ft = '#232323';
        if(img.fadeto && /^#([\da-fA-F]{3}){1,2}$/.test(img.fadeto)){
            ft = img.fadeto;
        }
        document.body.style.backgroundColor = ft;
        if(overlay){
            var me = this;
            window.setTimeout(function(){
                me.target.style.background="url("+img.src+") no-repeat scroll 50% 0 "+ft;
            },600);
        }
    }
};

var Y = YAHOO.util,
    D = Y.Dom,
    E = Y.Event,
    K = KISSY;
(function(){
    K.ready(function(){
		K.each(D.get('drop-nav').children,function(li){
            if(li.children.length<3) return;
            var ul = li.children[1];
			ul.style.left = (-li.offsetLeft) + 'px';
            var iframe = D.getNextSibling(ul);
            iframe.style.left =  (-li.offsetLeft) + 'px';
            iframe.style.height = ul.offsetHeight+'px';
            li._i = 0;
            E.on(li,'mouseleave',function(){
                window.clearTimeout(this._i);
                D.removeClass(this,'hover');
            });
            E.on(li,'mouseenter',function(){
                var self = this;
                self._i = window.setTimeout(function(){
                    D.addClass(self,'hover');
                },120);
            });
		});
        TB.Header.init();
        //change background image
        if(typeof _b_ !='undefined'){
            var a = {};
            a.imgs = _b_;
            HiBgImage.load(a);
        }
    });
    //判断当前哪个下拉菜单需要突出
    if(typeof _isIndexPage !='undefined' &&  _isIndexPage){
        D.addClass(D.get('m1'),'here');
    }else{
            var loc = window.location.href;
        if(loc.indexOf('starchannel.htm')>0){
            D.addClass(D.get('m8'),'here');
        }
    }

})();

String.prototype.trim=function(){
    return this.replace(/(^\s*)|(\s*$)/g,"");
}

function opSearch(fm){

}
