var Site = {
    init: function () {
        Site.exitsite = new Exitsite({
            contentProcessor: function (content) {
                var continueBtn = content.getElement(".continueBtn")[0] || new Element("div"),
				backBtn = content.getElement(".backBtn")[0] || new Element("div"),
				closeBtn = content.getElement(".closeBtn")[0] || new Element("div");
                
                [backBtn, closeBtn].each(function (btn, i) {
                    btn.addEvent("click", function () {
                        this.hide();
                    } .bind(this));
                }, this);

                continueBtn.addEvent("click", function () {
                    (function () {
                        this.hide();
                    }).delay(1000, this);
                } .bind(this));
                backBtn.set("href", "javascript:void(0);");

                return content;
            }
        });
    }    
};

window.addEvent("domready", function() {
	Site.init();
});

var Exitsite = new Class({
	Implements: [Events, Options],
	
	options: {
		exitSelector: ".exitlink",
		siteSelector: "",
		requestFilter: "#exitContent",
		contentProcessor: false
	},
	
	initialize: function(opts) {
		this.setOptions(opts || {});
		
		this.siteID = document.id(this.options.siteSelector) || document.id(document.body)
		this.exits = $$(this.options.exitSelector);
		
		this.resizeBound = this.resizeOverlay.bind(this);
		
		this.attach();
	},
	
	attach: function() {		
		this.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', '');
		this.setLoader(true);
		
		window.addEvent('resize', this.resizeBound).addEvent('scroll', this.resizeBound);
		
		var myrequest = new Request.HTML({
			url: exit,
			method: 'get',
			evalScripts: false
		});
		myrequest.addEvents({
			'success': function(responseTree, responseElements, responseHTML, responseJavaScript) {
				var myElements = responseElements.filter(this.options.requestFilter);
				this.setContent(myElements);
			}.bind(this),
			'failure': function(xhr) {
				this.hide()
			}.bind(this)
		});
		
		myrequest.send();
	},
	setContent: function(content) {				
		var exitel = (this.options.contentProcessor) ? this.options.contentProcessor.apply(this, [content]) : content;
		
		this.wrapIn.adopt(exitel);
		this.wrap.show();
		
		this.setLoader();
		this.resizeOverlay();
	},
	build: function() {
		this.wrap = new Element('div', { id: 'exitWrap', styles: { 'display': 'none'} }).adopt(this.wrapIn = new Element('div', { 'class': 'inner' })).inject(document.id(document.body), 'top');
		this.overlay = new Element('div', { id: 'exitOverlay', styles: { 'display': 'none'} }).set('opacity', 0.3).inject(document.id(document.body), 'top');
	},
	hide: function() {
		this.overlay.setStyle('display', 'none');
		this.wrap.setStyle('display', 'none');
		
		window.removeEvent('resize', this.resizeBound).removeEvent('scroll', this.resizeBound);
		
		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.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');
		
		this.loaderBound = (this.loaderBound) ? this.loaderBound : function() {
			var toppos = (window.getSize().y / 2 + window.getScrollTop()).toInt()
			this.overlay.setStyle('backgroundPosition', 'center ' + toppos + 'px')		
		}.bind(this);

		window[fn]('resize', this.loaderBound);
		window[fn]('scroll', this.loaderBound);

		if (state) this.loaderBound();
	},
	trash: function() {
		this.overlay.destroy();
		this.wrap.destroy();
		this.overlay = null;
		this.wrap = null;
	}	
	
});


var TextSize = new Class({
	Implements: Options,
	
	options: {
		cookieName: null,
		cookieOpts: { duration: 60, path: "/" },
		scale: ['0.8125em', '1.0em', '1.1em', '1.2em'],
		selector: "",
		targeted: ""
	},
	
	myScale: null,
	els: null,
	curr: 0,
	
	initialize: function(obj) {
		this.setOptions(obj);
		
		this.els = $$(this.options.selector);

		if (this.els.length == 0) return;
		
		this.cName = this.options.cookieName;
		this.opts = this.options.cookieOpts;
		
		if (!$chk(Cookie.read(this.cName))) Cookie.write(this.cName, 0, this.opts);

		this.myScale = parseInt(Cookie.read(this.cName)) || 0;

		this.els.each(function(el, i) {
			if (el.get("tag") === "a") el.set('href', 'javascript:void(0);');
			el.addEvent('click', function() {
				this.processScale(el, i)
			}.bind(this));
		}, this);

		this.targetEl = $$(this.options.targeted);

		this.loadScale(this.myScale.toInt());
	},
	
	setCookie: function() {
		Cookie.write(this.cName, this.myScale.toString(), this.opts);
	},
	
	processScale: function(el, index) {
		var scale;		
		
		if (index === 0) scale = Math.min(this.myScale + 1, this.options.scale.length - 1)
		else scale = Math.max(0, this.myScale - 1)

		this.myScale = scale;
		this.loadScale();
	},
	
	loadScale: function() {
		this.setCookie();
		this.targetEl.setStyle('font-size', this.options.scale[this.myScale]);
	}
});
