/*
* myFocus JavaScript Library v1.2.4
* Open source under the BSD & GPL License.
* 
* @Author  koen_lee@qq.com
* @Blog    http://hi.baidu.com/koen_li/
* 
* @Date    2011/07/20
*/
(function () {
    var q = {
        defConfig: {
            pattern: 'mF_fscreen_tb',
            trigger: 'click',
            txtHeight: 'default',
            wrap: true,
            auto: true,
            time: 4,
            index: 0,
            waiting: 20,
            delay: 100,
            css: true,
            path: '/js/pattern/',
            autoZoom: false
        },
        pattern: {},
        extend: function () {
            var a = arguments,
                l = a.length,
                i = 1,
                parent = a[0];
            if (l === 1) {
                i = 0, parent = this.pattern
            }
            for (i; i < l; i++) {
                for (var p in a[i]) if (!(p in parent)) parent[p] = a[i][p]
            }
        }
    };
    var r = {
        $: function (a) {
            return typeof a === 'string' ? document.getElementById(a) : a
        },
        $$: function (a, b) {
            return (this.$(b) || document).getElementsByTagName(a)
        },
        $$_: function (b, c) {
            var d = [],
                a = this.$$(b, c);
            for (var i = 0; i < a.length; i++) {
                if (a[i].parentNode === c) d.push(a[i]);
                i += this.$$(b, a[i]).length
            }
            return d
        },
        $c: function (a, b) {
            var c = this.$$('*', b),
                a = a.replace(/\-/g, '\\-'),
                reg = new RegExp('(^|\\s)' + a + '(\\s|$)'),
                arr = [];
            for (var i = 0, l = c.length; i < l; i++) {
                if (reg.test(c[i].className)) {
                    arr.push(c[i]);
                    break
                }
            }
            return arr[0]
        },
        $li: function (a, b) {
            return this.$$_('li', this.$c(a, b))
        },
        wrap: function (a, b) {
            var c = document.createElement('div');
            c.className = b;
            a[0].parentNode.insertBefore(c, a[0]);
            for (var i = 0; i < a.length; i++) c.appendChild(a[i])
        },
        wrapIn: function (a, b) {
            a.innerHTML = '<ul class=' + b + '>' + a.innerHTML + '</ul>'
        },
        addList: function (a, b) {
            var s = [],
                ul = this.$$('ul', a)[0],
                li = this.$$_('li', ul),
                img, n = li.length,
                num = b.length;
            for (var j = 0; j < num; j++) {
                s.push('<ul class=' + b[j] + '>');
                for (var i = 0; i < n; i++) {
                    img = this.$$('img', li[i])[0];
                    s.push('<li>' + (b[j] == 'num' ? ('<a>' + (i + 1) + '</a>') : (b[j] == 'txt' && img ? li[i].innerHTML.replace(/\<img(.|\n|\r)*?\>/i, img.alt) + '<p>' + img.getAttribute("text") + '</p>' : (b[j] == 'thumb' && img ? '<img src=' + (img.getAttribute("thumb") || img.src) + ' />' : ''))) + '<span></span></li>')
                };
                s.push('</ul>')
            };
            a.innerHTML += s.join('')
        }
    },
        CSS = {
            style: function (o, a) {
                var v = (this.isIE ? o.currentStyle : getComputedStyle(o, ''))[a],
                    pv = parseFloat(v);
                return isNaN(pv) ? v : pv
            },
            setOpa: function (o, a) {
                o.style.filter = "alpha(opacity=" + a + ")", o.style.opacity = a / 100
            },
            removeClass: function (o, a) {
                var b = o.className,
                    reg = "/\\s*" + a + "\\b/g";
                o.className = b ? b.replace(eval(reg), '') : ''
            }
        },
        Anim = {
            animate: function (a, f, g, h, i, j) {
                var k = f === 'opacity',
                    F = this,
                    opacity = F.setOpa,
                    am = typeof g === 'string',
                    st = (new Date).getTime();
                if (k && F.style(a, 'display') === 'none') a.style.display = 'block', opacity(a, 0);
                var l = F.style(a, f),
                    b = isNaN(l) ? 1 : l,
                    c = am ? g / 1 : g - b,
                    d = h || 800,
                    e = F.easing[i || 'easeOut'],
                    m = c > 0 ? 'ceil' : 'floor';
                if (a[f + 'Timer']) clearInterval(a[f + 'Timer']);
                a[f + 'Timer'] = setInterval(function () {
                    var t = (new Date).getTime() - st;
                    if (t < d) {
                        k ? opacity(a, Math[m](e(t, b * 100, c * 100, d))) : a.style[f] = Math[m](e(t, b, c, d)) + 'px'
                    } else {
                        clearInterval(a[f + 'Timer']), k ? opacity(a, (c + b) * 100) : a.style[f] = c + b + 'px', k && g === 0 && (a.style.display = 'none'), j && j.call(a)
                    }
                }, 13);
                return F
            },
            fadeIn: function (a, b, c) {
                this.animate(a, 'opacity', 1, b == undefined ? 400 : b, 'linear', c);
                return this
            },
            fadeOut: function (a, b, c) {
                this.animate(a, 'opacity', 0, b == undefined ? 400 : b, 'linear', c);
                return this
            },
            slide: function (a, b, c, d, e) {
                for (var p in b) this.animate(a, p, b[p], c, d, e);
                return this
            },
            stop: function (a) {
                for (var p in a) if (p.indexOf('Timer') !== -1) clearInterval(a[p]);
                return this
            },
            easing: {
                linear: function (t, b, c, d) {
                    return c * t / d + b
                },
                swing: function (t, b, c, d) {
                    return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b
                },
                easeIn: function (t, b, c, d) {
                    return c * (t /= d) * t * t * t + b
                },
                easeOut: function (t, b, c, d) {
                    return -c * ((t = t / d - 1) * t * t * t - 1) + b
                },
                easeInOut: function (t, b, c, d) {
                    return ((t /= d / 2) < 1) ? (c / 2 * t * t * t * t + b) : (-c / 2 * ((t -= 2) * t * t * t - 2) + b)
                }
            }
        },
        Init = {
            set: function (p, b, c) {
                if (typeof b !== 'boolean') c = b, b = false;
                var F = this,
                    cont = 0;
                p.pattern = p.pattern || F.defConfig.pattern, p.path = p.path == undefined ? F.defConfig.path : p.path, p.S = p.pattern + '-' + p.id;

                function show() {
                    if (cont == 2) {
                        if (p.autoZoom) F.fixIMG(p.id, p.width, p.height);
                        F.pattern[p.pattern].call(F, p, F);
                        c && c()
                    }
                };

                function ready() {
                    var a = F.$(p.id);
                    a.style.height = 314 + 'px';
                    F.loadPattern(p.pattern, p.path, function () {
                        F.extend(p, F.pattern[p.pattern].cfg, F.defConfig);
                        p.width = p.width || F.style(a, 'width'), p.height = p.height || F.style(a, 'height');
                        F.initCSS(p), a.className += ' ' + p.pattern + ' ' + p.S, a.style.height = '';
                        cont += 1, show()
                    });
                    F.onloadIMG(a, p.waiting == undefined ? F.defConfig.waiting : p.waiting, function () {
                        cont += 1, show()
                    })
                };
                if (b) {
                    ready();
                    return
                }
                if (window.attachEvent) {
                    (function () {
                        try {
                            ready()
                        } catch (e) {
                            setTimeout(arguments.callee, 0)
                        }
                    })()
                } else {
                    F.addEvent(document, 'DOMContentLoaded', ready)
                }
            },
            initCSS: function (p) {
                var a = [],
                    w = p.width,
                    h = p.height,
                    oStyle = document.createElement('style');
                oStyle.type = 'text/css';
                if (p.wrap) this.wrap([this.$(p.id)], p.pattern + '_wrap');
                if (p.css) a.push('.' + p.S + ' *{margin:0;padding:0;border:0;list-style:none;}.' + p.S + '{position:relative;width:' + w + 'px;height:' + h + 'px;overflow:hidden;font:12px/1.5 Verdana;text-align:left;background:#fff;visibility:visible!important;}.' + p.S + ' .loading{position:absolute;z-index:9999;width:100%;height:100%;color:#666;text-align:center;padding-top:' + 0.3 * h + 'px;background:#fff url(loading.gif) center ' + 0.4 * h + 'px no-repeat;}.' + p.S + ' .pic{position:relative;width:' + w + 'px;height:' + h + 'px;overflow:hidden;}.' + p.S + ' .txt li,.' + p.S + ' .txt li span,.' + p.S + ' .txt-bg{width:' + w + 'px;height:' + p.txtHeight + 'px!important;line-height:' + p.txtHeight + 'px!important;overflow:hidden;}.' + p.S + ' .txt li p a{display:inline;}');
                if (p.css && p.autoZoom) a.push('.' + p.S + ' .pic li{text-align:center;width:' + w + 'px;height:' + h + 'px;}');
                if (oStyle.styleSheet) {
                    oStyle.styleSheet.cssText = a.join('')
                } else {
                    oStyle.innerHTML = a.join('')
                }
                var b = this.$$('head', document)[0];
                b.insertBefore(oStyle, b.firstChild)
            }
        },
        Method = {
            isIE: !(+[1, ]),
            switchMF: function (a, b, c, d, e) {
                return "var _F=this,_ld=_F.$c('loading',box),less=" + c + ",_tn,first=true,_dir=" + d + "||'left',_dis=_dir=='left'||_dir=='right'?par.width:par.height,_wp=less&&(" + e + "||pics),index=par.index,_t=par.time*1000;if(less){_wp.style[_dir]=-_dis*n+'px';index+=n;}if(_ld)box.removeChild(_ld);var run=function(idx){(" + a + ")();var prev=index;if(less&&index==2*n-1&&_tn!=1){_wp.style[_dir]=-(n-1)*_dis+'px';index=n-1}if(less&&index==0&&_tn!=2){_wp.style[_dir]=-n*_dis+'px';index=n}if(!less&&index==n-1&&idx==undefined)index=-1;if(less&&idx!==undefined&&index>n-1&&!_tn&&!first) idx+=n;var next=idx!==undefined?idx:index+1;if(" + b + ")(" + b + ")();index=next;_tn=first=null;};run(index);if(_t&&par.auto)var auto=setInterval(function(){run()},_t);_F.addEvent(box,'mouseover',function(){if(auto)clearInterval(auto)});_F.addEvent(box,'mouseout',function(){if(auto)auto=setInterval(function(){run()},_t)});for(var i=0,_lk=_F.$$('a',box),_ln=_lk.length;i<_ln;i++) _lk[i].onfocus=function(){this.blur();}"
            },
            bind: function (a, b, c) {
                return "for (var j=0;j<n;j++){" + a + "[j].index=j;if(" + b + "=='click'){" + a + "[j].onmouseover=function(){if(this.index!=index)this.className+=' hover'};" + a + "[j].onmouseout=function(){_F.removeClass(this,'hover')};" + a + "[j].onclick=function(){if(this.index!=index) {run(this.index);return false}};}else if(" + b + "=='mouseover'){" + a + "[j].onmouseover=function(){var self=this;if(" + c + "==0){if(self.index!=index){run(self.index);return false}}else " + a + ".d=setTimeout(function(){if(self.index!=index) {run(self.index);return false}}," + c + ")};" + a + "[j].onmouseout=function(){clearTimeout(" + a + ".d)};}else{alert('Error Setting : \"'+" + b + "+'\"');break;}}"
            },
            toggle: function (a, b, c) {
                return "var _stop=false;" + a + ".onclick=function(){this.className=this.className=='" + b + "'?'" + c + "':'" + b + "';if(!_stop){clearInterval(auto);auto=null;_stop=true;}else{auto=true;_stop=false;}}"
            },
            scroll: function (a, b, c, d, e) {
                return "var scPar={},scDis=" + c + ",scN=Math.floor(" + d + "/2),scDir=parseInt(" + a + ".style[" + b + "])||0,scIdx=next>=n?next-n:next,scDur=" + e + "||500,scMax=scDis*(n-" + d + "),scD=scDis*scIdx+scDir;if(scD>scDis*scN&&scIdx!==n-1) scPar[" + b + "]='-'+scDis;if(scD<scDis&&scIdx!==0) scPar[" + b + "]='+'+scDis;if(scIdx===n-1) scPar[" + b + "]=-scMax;if(scIdx===0) scPar[" + b + "]=0;_F.slide(" + a + ",scPar,scDur);"
            },
            turn: function (a, b) {
                return a + ".onclick=function(){_tn=1;run(index>0?index-1:n-1);};" + b + ".onclick=function(){_tn=2;var tIdx=index>=2*n-1?n-1:index;run(index==n-1&&!less?0:tIdx+1);}"
            },
            alterSRC: function (o, a, b) {
                var c = this.$$('img', o)[0];
                c.src = b ? c.src.replace(eval("/" + a + "\\.(?=[^\\.]+$)/g"), '.') : c.src.replace(/\.(?=[^\.]+$)/g, a + '.')
            },
            onloadIMG: function (a, b, c) {
                var d = this.$$('img', a),
                    len = d.length,
                    count = 0,
                    ok = false;
                for (var i = 0; i < len; i++) {
                    d[i].onload = function () {
                        count += 1;
                        if (count == len && !ok) {
                            ok = true, c()
                        }
                    };
                    if (this.isIE) d[i].src = d[i].src
                };
                if (b === true) return;
                var t = b === false ? 0 : b * 1000;
                setTimeout(function () {
                    if (!ok) {
                        ok = true, c()
                    }
                }, t)
            },
            fixIMG: function (a, b, c) {
                var d = this.$$('img', a),
                    len = d.length,
                    IMG = new Image();
                for (var i = 0; i < len; i++) {
                    IMG.src = d[i].src;
                    if (IMG.width / IMG.height >= b / c) {
                        d[i].style.width = b + 'px';
                        d[i].style.marginTop = (c - b / IMG.width * IMG.height) / 2 + 'px'
                    } else {
                        d[i].style.height = c + 'px'
                    }
                }
            },
            loadPattern: function (a, b, c) {
                if (!b) {
                    c();
                    return
                }
                var d = document.createElement("script"),
                    css = document.createElement("link"),
                    src = b + a + '.js',
                    href = b + a + '.css';
                d.type = "text/javascript", d.src = src;
                css.rel = "stylesheet", css.href = href;
                this.$$('head')[0].appendChild(css);
                this.$$('head')[0].appendChild(d);
                if (this.isIE) {
                    d.onreadystatechange = function () {
                        if (d.readyState == "loaded" || d.readyState == "complete") c()
                    }
                } else {
                    d.onload = function () {
                        c()
                    }
                }
                d.onerror = function () {
                    alert('Not Found (404): ' + src)
                }
            },
            addEvent: function (a, c, d) {
                var b = this.isIE,
                    e = b ? 'attachEvent' : 'addEventListener',
                    t = (b ? 'on' : '') + c;
                a[e](t, d, false)
            }
        };
    q.extend(q, r, CSS, Anim, Init, Method);
    q.set.params = function (a, p) {
        q.pattern[a].cfg = p
    };
    myFocus__AGENT__ = q;
    if (typeof myFocus === 'undefined') myFocus = myFocus__AGENT__;
    if (typeof jQuery !== 'undefined') {
        jQuery.fn.extend({
            myFocus: function (p, a) {
                if (!p) p = {};
                p.id = this[0].id;
                if (!p.id) p.id = this[0].id = 'mF__NAME__';
                myFocus__AGENT__.set(p, true, a)
            }
        })
    }
})();
