var Site = {
	init: function() {
		Site.TextSize.init({selector: '#siteUtil .textSize', cookieName: 'textsize', targeted: 'siteContent'})
		Site.exitsite.init()		
		Site.Slideshow.init({parentSelector: 'slideshow', slideSelector: '.slide', delay: 3000})		
		new SmoothScroll({ duration: 500 });
	},
	
	TextSize: {
				cName: null,
				opts: { duration: 60 },
				scale: ['1.0em', '1.2em', '1.3em', '1.4em'],
				myScale: null,
				els: null,
				curr: 0,
				init: function(obj) {
					this.els = $$(obj.selector)	
					
					if (this.els.length == 0) return
					
					this.cName = obj.cookieName					
					if (!$chk(Cookie.read(this.cName))) Cookie.write(this.cName, 0, this.opts) //if doesnt exist, set cookie to 0					
					this.myScale = parseInt(Cookie.read(this.cName)) || 0 //in case cookies are disabled use 0								
								
					this.els.each(function(el, i) {
						el.set('href', 'javascript:void(0);')
						el.addEvent('click', function() {
							this.processScale(el, i)
						}.bind(this))
					}, this)
					this.targetEl = $(obj.targeted) || $(document.body)
					this.targetEl.setStyles({'font-size':this.scale[this.myScale]})			
				},
				setCookie: function() {
					Cookie.write(this.cName, this.myScale, this.opts)
				},
				processScale: function(el, index) {					
					switch (index) {
						case 1:
							this.myScale = (this.myScale + 1 > this.scale.length - 1) ? this.scale.length - 1 : this.myScale + 1				
							break;
						case 0:
							this.myScale = (this.myScale - 1 < 0) ? 0 : this.myScale - 1
							break;
					}	
					this.setCookie()
					this.targetEl.setStyle('font-size', this.scale[this.myScale])	
				}		
	},
	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 + '&p=true',
                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

        }
    },
    
	Slideshow: {
		show: null,
		counter: 0,
		init: function(opts) {
			this.papa = $(opts.parentSelector);
			this.slides = (this.papa) ? this.papa.getElements(opts.slideSelector) : new Array();

			if (!this.papa || this.slides.length == 0) return
	
			this.slides.each(function(el, i) {
				el.setStyles({
					position: 'absolute',
					zIndex: this.slides.length - i					
				})
				if (i != 0) el.set('opacity', 0)
			}, this)
			$clear(this.show)
			this.show = this.runShow.periodical(opts.delay || 2000, this)
		},
		
		runShow: function() {
			if (this.counter == this.slides.length - 1) {
				$clear(this.show);
				return;
			}
			this.slides[this.counter].tween('opacity', 0)
			this.slides[this.counter + 1].tween('opacity', 1)
			
			this.counter++;						
		}
	}		
	
	
}

window.addEvent('domready', function() {
	Site.init();
})