var AD1 = {};
(function() {
    var slice = Array.slice || (function() {
        var _slice = Array.prototype.slice;
        return function(arr) {
            return _slice.apply(arr, _slice.call(arguments, 1))
        }
    })(),emptyFn = function() {
    },each = (function() {
        var getType = function(o) {
            if (typeof o == 'string') {
                return'string'
            }
            if (typeof o.length == 'number') {
                return'array'
            }
            if (typeof o == 'number') {
                return'number'
            }
            return'dictionary'
        },iterator_type = {string:function(fn, bind) {
            return iterator_type.array.call(this.split(''), fn, bind)
        },array:function(fn, bind) {
            for (var i = 0,len = this.length; i < len; i++) {
                fn.call(bind, this[i], i)
            }
        },number:function(fn, bind) {
            for (var i = 0; i < this; i++) {
                fn.call(bind, i, i)
            }
        },dictionary:function(fn, bind) {
            for (var i in this) {
                if (this.hasOwnProperty(i)) {
                    fn.call(bind, this[i], i)
                }
            }
        }},getIterator = function(o) {
            return o.each || o.forEach || iterator_type[getType(o)]
        };
        return function(numerable, fn, bind) {
            getIterator(numerable).call(numerable, fn, bind)
        }
    })(),map = function(numerable, fn, bind, is_dictionary) {
        var results;
        if (is_dictionary) {
            results = {};
            each(numerable, function(v, k) {
                results[k] = fn.call(this, v, k)
            }, bind)
        } else {
            results = [];
            each(numerable, function(v, k) {
                results.push(fn.call(this, v, k))
            }, bind)
        }
        return results
    },find = (function() {
        var _break = {};
        return function(numerable, fn, bind, with_index) {
            var result,index;
            try {
                each(numerable, function(v, k) {
                    if (fn.call(this, v, k) === true) {
                        result = v;
                        index = k;
                        throw _break;
                    }
                }, bind)
            } catch(e) {
                if (e != _break) {
                    throw e;
                }
            }
            return with_index ? [result,result === undefined ? -1 : index] : result
        }
    })(),findAll = function(numerable, fn, bind, with_indexes) {
        var results = [],indexes = [];
        each(numerable, function(v, k) {
            if (fn.call(this, v, k) == true) {
                results.push(v);
                indexes.push(k)
            }
        }, bind);
        return with_indexes ? [results,indexes] : results
    },invoke = function(numerable, method_name, arg1, arg2, argN) {
        var args = slice(arguments, 2);
        each(numerable, function(v, k) {
            v[method_name].apply(v, args)
        })
    },mix = function(r) {
        for (var i = 1; i < arguments.length; i++) {
            var s = arguments[i];
            if (s) {
                for (var j in s) {
                    r[j] = s[j]
                }
            }
        }
        return r
    },mixif = function(r) {
        for (var i = 1; i < arguments.length; i++) {
            var s = arguments[i];
            if (s) {
                for (var j in s) {
                    if (r[j] === undefined) {
                        r[j] = s[j]
                    }
                }
            }
        }
        return r
    },generateId = (function() {
        var id = 1;
        return function() {
            return'auto_gen_' + id++
        }
    })(),Class = {extend:(function() {
        var oc = Object.prototype.constructor;
        return function(sb, sp, overrides) {
            if (typeof sp == 'object') {
                overrides = sp;
                sp = sb;
                sb = overrides.constructor != oc ? overrides.constructor : function() {
                    sp.apply(this, arguments)
                }
            }
            var F = function() {
            },sbp,spp = sp.prototype;
            F.prototype = spp;
            sbp = sb.prototype = new F();
            sbp.constructor = sb;
            sb.superclass = spp;
            if (spp.constructor == oc) {
                spp.constructor = sp
            }
            mix(sbp, overrides);
            return sb
        }
    })(),create:function(proto, sp) {
        var ctor = function() {
            if (this.init) {
                this.init.apply(this, arguments)
            }
        };
        if (sp) {
            return Class.extend(ctor, sp, proto)
        } else {
            proto.constructor = ctor;
            ctor.prototype = proto;
            return ctor
        }
    }},ua = (function() {
        var o = {ie:0,opera:0,gecko:0,webkit:0,mobile:null};
        var ua = navigator.userAgent,m;
        if ((/KHTML/).test(ua)) {
            o.webkit = 1
        }
        m = ua.match(/AppleWebKit\/([^\s]*)/);
        if (m && m[1]) {
            o.webkit = parseFloat(m[1]);
            if (/ Mobile\//.test(ua)) {
                o.mobile = "Apple"
            } else {
                m = ua.match(/NokiaN[^\/]*/);
                if (m) {
                    o.mobile = m[0]
                }
            }
        }
        if (!o.webkit) {
            m = ua.match(/Opera[\s\/]([^\s]*)/);
            if (m && m[1]) {
                o.opera = parseFloat(m[1]);
                m = ua.match(/Opera Mini[^;]*/);
                if (m) {
                    o.mobile = m[0]
                }
            } else {
                m = ua.match(/MSIE\s([^;]*)/);
                if (m && m[1]) {
                    o.ie = parseFloat(m[1])
                } else {
                    m = ua.match(/Gecko\/([^\s]*)/);
                    if (m) {
                        o.gecko = 1;
                        m = ua.match(/rv:([^\s\)]*)/);
                        if (m && m[1]) {
                            o.gecko = parseFloat(m[1])
                        }
                    }
                }
            }
        }
        return o
    })(),getObjectToStringFn = function(assign_token, pair_separator, need_last, need_encode) {
        var encode = need_encode ? encodeURIComponent : function(k) {
            return k
        };
        if (need_last) {
            return function(o) {
                return map(o, function(v, k) {
                    return k + assign_token + encode(v) + pair_separator
                }).join('')
            }
        } else {
            return function(o) {
                return map(o, function(v, k) {
                    return k + assign_token + encode(v)
                }).join(pair_separator)
            }
        }
    };
    mix(AD1, {slice:slice,each:each,map:map,Class:Class,mix:mix,ua:ua,mix:mix,mixif:mixif,format:function(s, config) {
        return s.replace(/\{([^}]*)\}/g, (typeof config == 'object') ? function(m, i) {
            var ret = config[i];
            return ret == null && reserve ? m : ret
        } : config)
    },serializeStyles:getObjectToStringFn(':', ';', true, false),serializeAttrs:getObjectToStringFn('=', ' ', true, false),serializeQuery:getObjectToStringFn('=', '&', false, true)})
})();
AD1.DomUtil = (function() {
    var el_template = '<{tag} {attrs}style="{styles}"></{tag}>',a = AD1,patterns = {HYPHEN:/(-[a-z])/i,ROOT_TAG:/^body|html$/i};
    var walk = function(el, side) {
        for (var nel = el; nel; nel = nel[side]) {
            if (nel.nodeType == 1) {
                return nel
            }
        }
    },buildHTML = function(styles, attrs, tag) {
        return a.format(el_template, {tag:tag || 'div',attrs:a.serializeAttrs(attrs || {}),styles:a.serializeStyles(styles)})
    },toCamel = function(property) {
        if (!patterns.HYPHEN.test(property)) {
            return property
        }
        if (propertyCache[property]) {
            return propertyCache[property]
        }
        var converted = property;
        while (patterns.HYPHEN.exec(converted)) {
            converted = converted.replace(RegExp.$1, RegExp.$1.substr(1).toUpperCase())
        }
        propertyCache[property] = converted;
        return converted
    },getStyle = (function() {
        if (document.defaultView && document.defaultView.getComputedStyle) {
            return function(el, property) {
                var value = null;
                if (property == 'float') {
                    property = 'cssFloat'
                }
                var computed = document.defaultView.getComputedStyle(el, '');
                if (computed) {
                    value = computed[toCamel(property)]
                }
                return el.style[property] || value
            }
        } else if (document.documentElement.currentStyle && a.ua.ie) {
            return function(el, property) {
                switch (toCamel(property)) {case'opacity':var val = 100;try {
                    val = el.filters['DXImageTransform.Microsoft.Alpha'].opacity
                } catch(e) {
                    try {
                        val = el.filters('alpha').opacity
                    } catch(e) {
                    }
                }return val / 100;case'float':property = 'styleFloat';default:var value = el.currentStyle ? el.currentStyle[property] : null;return(el.style[property] || value)}
            }
        } else {
            return function(el, property) {
                return el.style[property]
            }
        }
    })(),setStyle = (function() {
        if (a.ua.ie) {
            return function(el, property, val) {
                switch (property) {case'opacity':el.style.filter = 'alpha(opacity=' + val * 100 + ')';if (!el.currentStyle || !el.currentStyle.hasLayout) {
                    el.style.zoom = 1
                }break;case'float':property = 'styleFloat';default:el.style[property] = val}
            }
        } else {
            return function(el, property, val) {
                if (property == 'float') {
                    property = 'cssFloat'
                }
                el.style[property] = val
            }
        }
    })(),getDocumentScrollLeft = function(doc) {
        doc = doc || document;
        return Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft)
    },getDocumentScrollTop = function(doc) {
        doc = doc || document;
        return Math.max(doc.documentElement.scrollTop, doc.body.scrollTop)
    },getXY = function(el) {
        var pos = [el.offsetLeft,el.offsetTop];
        var parentNode = el.offsetParent;
        var accountForBody = (a.ua.webkit && getStyle(el, 'position') == 'absolute' && el.offsetParent == el.ownerDocument.body);
        if (parentNode != el) {
            while (parentNode) {
                pos[0] += parentNode.offsetLeft;
                pos[1] += parentNode.offsetTop;
                if (!accountForBody && a.ua.webkit && getStyle(parentNode, 'position') == 'absolute') {
                    accountForBody = true
                }
                parentNode = parentNode.offsetParent
            }
        }
        if (accountForBody) {
            pos[0] -= el.ownerDocument.body.offsetLeft;
            pos[1] -= el.ownerDocument.body.offsetTop
        }
        parentNode = el.parentNode;
        while (parentNode.tagName && !patterns.ROOT_TAG.test(parentNode.tagName)) {
            if (getStyle(parentNode, 'display').search(/^inline|table-row.*$/i)) {
                pos[0] -= parentNode.scrollLeft;
                pos[1] -= parentNode.scrollTop
            }
            parentNode = parentNode.parentNode
        }
        return pos
    },$ = function(id) {
        if (typeof id == 'string') {
            return document.getElementById(id)
        }
        return id
    };
    return{get:$,buildHTML:buildHTML,createElement:function(html, attrs, tag) {
        if (typeof html != 'string') {
            html = buildHTML(html, attrs, tag)
        }
        var el = document.createElement('div');
        el.innerHTML = html;
        return el.firstChild
    },next:function(el) {
        return walk(el, 'nextSibling')
    },pre:function(el) {
        return walk(el, 'previousSibling')
    },children:function(el) {
        var results = [];
        for (var cel = el.firstChild; cel; cel = cel.nextSibling) {
            if (cel.nodeType == 1) {
                results.push(cel)
            }
        }
        return results
    },setStyle:function(el, k, v) {
        el = $(el);
        if (typeof k == 'object') {
            a.each(k, function(_v, _k) {
                setStyle(el, _k, _v)
            })
        } else {
            setStyle(el, k, v)
        }
    },getStyle:function(el, k) {
        return getStyle($(el), k)
    },getXY:function(el) {
        return getXY($(el))
    }}
})();
AD1.DomEventUtil = new function() {
    if (document.addEventListener) {
        this.listen = function(el, type, handler) {
            el.addEventListener(type, handler, false);
            return handler
        };
        this.unlisten = function(el, type, handler) {
            el.removeEventListener(type, handler, false)
        };
        this.stopPropagation = function(e) {
            e.stopPropagation()
        };
        this.preventDefault = function(e) {
            e.preventDefault()
        };
        this.getTarget = function(e) {
            return e.target
        }
    } else {
        this.listen = function(el, type, handler) {
            var actualHandler = function() {
                handler.call(el, window.event)
            };
            el.attachEvent('on' + type, actualHandler);
            return actualHandler
        };
        this.unlisten = function(el, type, actualHandler) {
            el.detachEvent('on' + type, actualHandler)
        };
        this.stopPropagation = function(e) {
            e.cancelBubble = true
        };
        this.preventDefault = function(e) {
            e.returnValue = false
        };
        this.getTarget = function(e) {
            return e.srcElement
        }
    }
    this.stop = function(e) {
        this.stopPropagation(e);
        this.preventDefault(e)
    }
};
AD1.Cookie = (function() {
    var encode = encodeURIComponent,decode = decodeURIComponent;
    return{set:function(name, value, encodeValue, options) {
        var text = encode(name) + "=" + (encodeValue ? encode(value) : value);
        if (options) {
            if (options.expires instanceof Date) {
                text += "; expires=" + options.expires.toGMTString()
            }
            if (options.path) {
                text += "; path=" + options.path
            }
            if (options.domain) {
                text += "; domain=" + options.domain
            }
            if (options.secure === true) {
                text += "; secure"
            }
        }
        document.cookie = text
    },get:function(shouldDecode) {
        var text = document.cookie;
        var cookies = {};
        var decodeValue = (shouldDecode === false ? function(s) {
            return s
        } : decode);
        if (/[^=]+=[^=;]?(?:; [^=]+=[^=]?)?/.test(text)) {
            var cookieParts = text.split(/;\s/g),cookieName = null,cookieValue = null,cookieNameValue = null;
            for (var i = 0,len = cookieParts.length; i < len; i++) {
                cookieNameValue = cookieParts[i].match(/([^=]+)=/i);
                if (cookieNameValue instanceof Array) {
                    cookieName = decode(cookieNameValue[1]);
                    cookieValue = decodeValue(cookieParts[i].substring(cookieNameValue[1].length + 1))
                } else {
                    cookieName = decode(cookieParts[i]);
                    cookieValue = cookieName
                }
                cookies[cookieName] = cookieValue
            }
        }
        return cookies
    },remove:function(name, options) {
        options = options || {};
        options.expires = new Date(0);
        this.set(name, "", options)
    }}
})();
(function() {
    var a = AD1,Class = a.Class,each = a.each,createElement = a.DomUtil.createElement,buildHTML = a.DomUtil.buildHTML;
    var getExtension = function(uri) {
        var query_string_index = uri.indexOf('?');
        return uri.substring(uri.lastIndexOf('.') + 1, query_string_index == -1 ? uri.length : query_string_index)
    },createObjParam = function(el, pName, pValue) {
        var p = document.createElement("param");
        p.setAttribute("name", pName);
        p.setAttribute("value", pValue);
        el.appendChild(p)
    };
    a.UriResource = Class.create({init:function(uri) {
        this.loc = uri
    },getType:function() {
        return(this.type || (this.type = getExtension(this.loc))).toLowerCase()
    },setType:function(sType) {
        this.type = sType
    }});
    a.AbstractFodder = Class.create({init:function(uri, config) {
        this.uri = uri;
        this.config = config
    },setClickUrl:function(url) {
        this.click_url = url
    },createElement:function() {
        throw new Error('not implemented');
    }});
    a.ImageFodder = Class.create({createElement:function() {
        var styles = {width:this.config.width + 'px',height:this.config.height + 'px',cursor:'pointer'};
        if (this.uri.getType() == 'png' && a.ua.ie != 0 && a.ua.ie < 7) {
            styles['filter'] = a.format('progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'{uri}\')', this.uri.loc)
        } else {
            styles['background-image'] = a.format('url({uri})', this.uri.loc)
        }
        var el = createElement(styles);
        var click_url = this.click_url;
        if (click_url) {
            el.onclick = function() {
                open(click_url)
            }
        }
        return el
    }}, a.AbstractFodder);
    var createFlash = a.ua.ie ? function(attObj, parObj) {
        parObj.movie = attObj.data;
        delete attObj.data;
        return createElement(a.format('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" {attrs}>{params}</object>', {attrs:a.map(attObj, function(v, k) {
            return k + '="' + v + '" '
        }).join(''),params:a.map(parObj, function(v, k) {
            return'<param name="' + k + '" value="' + v + '" />'
        }).join('')}))
    } : function(attObj, parObj) {
        var o = document.createElement("object");
        o.setAttribute('type', 'application/x-shockwave-flash');
        a.each(attObj, function(v, k) {
            o.setAttribute(k, v)
        });
        a.each(parObj, function(v, k) {
            createObjParam(o, k, v)
        });
        return o
    };
    var flash_default_config = {quality:'high',allowScriptAccess:'always',wMode:'Opaque',swLiveConnect:true};
    a.FlashFodder = Class.create({init:function(uri, config) {
        this.uri = uri;
        config.params = a.mixif(config.params || {}, flash_default_config);
        this.config = config
    },createElement:function() {
        var attObj = {data:this.uri.loc,width:this.config.width,height:this.config.height},parObj = this.config.params;
        var flashvars = this.config.flashvars || {};
        if (this.click_url) {
            flashvars.click_url = this.click_url
        }
        parObj.flashvars = a.serializeQuery(flashvars);
        var flash = createFlash(attObj, parObj);
        if (this.click_url) {
            var el = createElement({position:'relative',width:this.config.width + 'px',height:this.config.height + 'px'});
            var cover = createElement({position:'absolute',width:this.config.width + 'px',height:this.config.height + 'px',left:'0px',top:'0px',cursor:'pointer','background-color':'#fff',filter:'alpha(opacity=0)',opacity:0}, {'class':'absolute'});
            var click_url = this.click_url;
            cover.onclick = function() {
                open(click_url)
            };
            el.appendChild(flash);
            el.appendChild(cover);
            flash = el
        }
        return flash
    }}, a.AbstractFodder);
    a.FodderFactory = {create:function(uri, config) {
        if (uri.getType() == 'swf') {
            return new a.FlashFodder(uri, config)
        } else {
            return new a.ImageFodder(uri, config)
        }
    }}
})();
(function() {
    var a = AD1,Class = a.Class,each = a.each;
    a.AbstractDisplay = Class.create({init:function(id, order_id, click_url) {
        this.id = id;
        this.order_id = order_id;
        this.click_url = click_url
    },addFodder:function() {
        throw new Error('not implemented');
    },render:function(container) {
        throw new Error('not implemented');
    }});
    a.SingleFodderDisplay = Class.create({init:function() {
        a.AbstractDisplay.prototype.init.apply(this, arguments)
    },addFodder:function(fodder) {
        fodder.setClickUrl(this.click_url);
        this.fodder = fodder
    },render:function(container) {
        var el = this.fodder.createElement();
        container.appendChild(el)
    }}, a.AbstractDisplay);
    var display_map = {};
    a.DisplayFactory = {create:function(id, order_id, click_url, type) {
        var ctor = type ? (typeof type == 'string' ? display_map[type] : type) : a.SingleFodderDisplay;
        return new ctor(id, order_id, click_url)
    }};
    a.DisplayRecorder = (function() {
        var infos = {oid:[],loc:[],cid:[]};
        return{log:function(display, ad_location) {
            infos.oid.push(display.order_id);
            infos.loc.push(ad_location.id);
            infos.cid.push(display.id)
        },get:function() {
            return infos
        }}
    })()
})();
(function() {
    var a = AD1,Class = a.Class,each = a.each;
    a.ADLocation = Class.create({init:function(id) {
        this.id = id;
        this.displays = []
    },setDefaultDisplay:function(display) {
        this.default_display = display
    },setDisplayStrategy:function(display_strategy) {
        this.display_strategy = display_strategy
    },addDisplay:function() {
        this.displays.push.apply(this.displays, arguments)
    },getDisplays:function() {
        return this.displays
    },getDefaultDisplay:function() {
        return this.default_display
    },displayAD:function() {
        this.display_strategy.execute(this)
    }});
    a.AbstractADLocationDisplayStrategy = Class.create({execute:function(ad_location) {
        throw new Error('not implemented');
    }});
    a.ADLocationPlayFirstDisplayStrategy = Class.create({execute:function(ad_location) {
        var container = document.getElementById(ad_location.id),displays = ad_location.getDisplays(),default_display = ad_location.getDefaultDisplay(),display = displays[0] || default_display;
        a.DisplayRecorder.log(display, ad_location);
        display.render(container)
    }}, a.AbstractADLocationDisplayStrategy);
    a.ADLocationRotatableDisplayStrategy = Class.create({setRotationCount:function(rotation_count) {
        this.rotation_count = rotation_count
    },setRotationIndex:function(rotation_index) {
        this.rotation_index = rotation_index
    },execute:function(ad_location) {
        if (!this.rotation_count) {
            throw'must set rotation_count';
        }
        if (!this.rotation_index) {
            throw'must set rotation_index';
        }
        var container = document.getElementById(ad_location.id),displays = ad_location.getDisplays(),default_display = ad_location.getDefaultDisplay(),rotation_index = this.rotation_index % this.rotation_count,display = rotation_index > displays.length - 1 ? default_display : displays[rotation_index];
        a.DisplayRecorder.log(display, ad_location);
        display.render(container)
    }}, a.AbstractADLocationDisplayStrategy)
})();
