var Site = {
    init: function() {
        Site.textSize.init({
            cookieName: 'textSize',
            textTarget: 'contentWrap',
            trackId: 'textTrack',
            knobId: 'textKnob'
        });
        Site.exitsite.init();
        Site.transcripts.init();
        new SmoothScroll({ duration: 500 });
    },
    textSize: {
        cName: null,
        opts: {
            duration: 60
        },
        myScale: null,
        els: null,
        curr: 0,
        init: function(obj) {
            if (!$(obj.trackId)) return

            this.textTarget = $(obj.textTarget) || $(document.body)
            this.cName = obj.cookieName

            //if doesnt exist, set to 0
            if (!$chk(Cookie.read(this.cName))) Cookie.write(this.cName, 0, this.opts)

            this.myScale = parseInt(Cookie.read(this.cName)) || 0

            this.slider = new Slider($(obj.trackId), $(obj.knobId), {
                snap: true,
                steps: 7,
                onTick: function(pos) {
                    this.knob.setStyle('left', this.step * 7)
                }
            })

            this.slider.knob.addEvents({
                'mousedown': function() {
                    this.addClass('on')
                },
                'mouseup': function() {
                    this.removeClass('on')
                }
            })

            this.processScale(this.myScale)
            this.slider.set(this.myScale)

            this.slider.addEvents({
                'complete': function(step) {
                    this.knob.removeClass('on')
                },
                'change': function(step) {
                    this.processScale(step)
                    this.setCookie(step)
                } .bind(this)
            })

        },
        setCookie: function(index) {
            this.myScale = index
            Cookie.write(this.cName, index, this.opts)
        },
        getSize: function(index) {
            return index * 5 + 100
        },
        processScale: function(index) {
            this.textTarget.setStyle('font-size', this.getSize(index) + '%')
        }
    },
    exitsite: {
        exitLinkClass: '.exitlink',
        targetId: '',


        init: function() {
            this.siteID = $('sitewrap') || $(document.body)

            var exits = $$(this.exitLinkClass)
            exits.each(function(el, i) {
                el.store('exitlink', el.get('href'))

                el.set({
                    href: 'javascript:void(0);',
                    target: ''
                })

                el.addEvent('click', function() {
                    this.show(el)
                } .bind(this))
            }, this)
        },
        show: function(el) {
            var exit = el.retrieve('exitlink')
            if (!exit) return

            this.build()
            this.resizeOverlay()
            this.overlay.setStyle('display', '')//.addClass('overlay-loading')
            this.setLoader(true)
            //this.wrap.setStyle('display', '')
            window.addEvent('resize', this.resizeOverlay.bind(this)).addEvent('scroll', this.resizeOverlay.bind(this))

            var myrequest = new Request.HTML({
                url: exit,
                method: 'get',
                evalScripts: false
            })
            myrequest.addEvents({
                'success': function(responseTree, responseElements, responseHTML, responseJavaScript) {
                    this.setContent(responseTree)
                } .bind(this),
                'failure': function(xhr) {
                    this.hide()
                } .bind(this)
            })
            myrequest.send()
        },
        setContent: function(content) {
            var mycontent = null
            for (var i = 0; i < content.length; i++) {
                if ($type(content[i]) == 'element') {
                    mycontent = content[i]
                    break
                }
            };
            if (!content) {
                this.hide()
                return
            }

            var exitel = mycontent.getElement('div.exitWrap')
            exitel.getElement('a.noExit').set('href', 'javascript:void(0);')

            this.wrapIn.adopt(exitel)
            this.wrap.show()
            this.setLoader()
            this.resizeOverlay()
        },
        build: function() {
            this.wrap = new Element('div', { id: 'myWrap', styles: { 'display': 'none'} }).adopt(this.wrapIn = new Element('div', { 'class': 'inner' })).inject($(document.body), 'top')
            this.overlay = new Element('div', { id: 'myOverlay', styles: { 'display': 'none'} }).set('opacity', 0.3).inject($(document.body), 'top')
        },
        hide: function() {
            this.overlay.setStyle('display', 'none')
            this.wrap.setStyle('display', 'none')
            window.removeEvents('resize').removeEvents('scroll')
            this.isOpen = false
            this.trash()
        },
        resizeOverlay: function() {
            var sitedim = this.siteID.getCoordinates()
            var boxdim = this.wrap.getCoordinates()
            var windim = window.getSize()

            styles = {}
            styles.top = (window.getScrollTop().toInt() + (windim.y / 2).toInt()) - (boxdim.height.toInt() / 2)
            //styles.top = window.getScrollTop() + 190
            styles.left = (sitedim.left + (sitedim.width / 2).toInt()) - (boxdim.width.toInt() / 2)

            this.wrap.setStyles(styles)
            this.overlay.setStyles({ top: window.getTop(), height: window.getScrollHeight() });
        },
        setLoader: function(state) {
            var fn = (state) ? 'addEvent' : 'removeEvent'
            var cfn = (state) ? 'addClass' : 'removeClass'

            this.overlay[cfn]('overlay-loading')

            var loaderPosition = function() {
                var toppos = (window.getSize().y / 2 + window.getScrollTop()).toInt()
                this.overlay.setStyle('backgroundPosition', 'center ' + toppos + 'px')
            } .bind(this)

            window[fn]('resize', loaderPosition)
            window[fn]('scroll', loaderPosition)

            if (state) loaderPosition()
        },
        trash: function() {
            this.overlay.destroy()
            this.wrap.destroy()
            this.overlay = null
            this.wrap = null

        }
    },
    transcripts: {
        init: function() {
            var trans = $$('.transcriptlink')
            trans.each(function(el, i) {
                el.store('transcript', el.get('href'))
                el.set({
                    href: 'javascript:void(0);',
                    target: ''
                })
                el.addEvent('click', function() { this.view(el)}.bind(this))
            }, this)
        },
        view: function(el) {
            var href = el.retrieve('transcript')
            if (!href) return
            window.open(href,"transcriptWindow", "menubar=0,resizable=1,width=600,height=250,scrollbars=1,status=0"); 
        }
    }
}


window.addEvent('domready', function() {
	Site.init();
})