var Lightbox = Class.create();

Lightbox.prototype = {
	initialize: function() {
		if (!document.getElementsByClassName){ return; }
		var anchors = document.getElementsByClassName('lbOn');

		for (var i=0; i<anchors.length; i++) anchors[i].onclick = function () {
			myLightbox.start(this); 
			return false;
		}

		var objBody = document.getElementsByTagName("body").item(0);
		
		var objOverlay = document.createElement("div");
		objOverlay.id = 'overlay'
		objOverlay.onclick = function() {
			myLightbox.end(); 
		}
		objBody.appendChild(objOverlay);
		
		var objLightbox = document.createElement("div");
		objLightbox.id = 'lightbox'
		objLightbox.innerHTML = '<div id="lbLoader"></div><div id="lbContent"></div>'
		objBody.appendChild(objLightbox);
	
		overlayEffect = new fx.Opacity(objOverlay, { duration: 300 });	
		overlayEffect.hide();
		
		lbEffect =  new fx.Opacity(objLightbox, { duration: 350 });
		lbEffect.hide();
	},
	
	start: function(caller) {	

		var arrayPageSize = getPageSize();
		$('overlay').style.height = arrayPageSize[1] + 'px';
		overlayEffect.custom(0,0.8);

		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		$('lightbox').style.top = (Math.round(arrayPageScroll[1] + ((arrayPageSize[3]-500) / 2))) + 'px'
		$('lightbox').className = 'loading'

		lbEffect.custom(0,1);
		
		this.loadContent(caller.href)
	},

	end: function() {
		lbEffect.custom(1,0);
		overlayEffect.custom(0.8,0);
	},
	
	loadContent: function(href) {
		if(href.search(/\?/)==-1) href+= '?notitle=1'
		else href+='&notitle=1'
		xmlHttpReq = getXmlHttpReq();
		xmlHttpReq.open('GET',href)
		xmlHttpReq.onreadystatechange = function() {
			if (xmlHttpReq.readyState==4) {
				$('lbContent').innerHTML = xmlHttpReq.responseText
				$('lightbox').className = ''
				prepConfigForm()
			}
		}
		xmlHttpReq.send(null)
	}
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) yScroll = self.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop) yScroll = document.documentElement.scrollTop;
	else if (document.body) yScroll = document.body.scrollTop;

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	
	var yScroll;
	
	if (window.innerHeight && window.scrollMaxY) yScroll = window.innerHeight + window.scrollMaxY;
	else if (document.body.scrollHeight > document.body.offsetHeight) yScroll = document.body.scrollHeight;
	else yScroll = document.body.offsetHeight;

	var windowHeight;
	if (self.innerHeight) windowHeight = self.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight) windowHeight = document.documentElement.clientHeight;
	else if (document.body) windowHeight = document.body.clientHeight;
	
	if(yScroll < windowHeight) pageHeight = windowHeight;
	else pageHeight = yScroll;

	arrayPageSize = new Array(0,pageHeight,0,windowHeight) 
	return arrayPageSize;
}

function initLightbox() { myLightbox = new Lightbox(); }

var xmlHttpReq;

function getXmlHttpReq() {
	if (window.XMLHttpRequest) req = new XMLHttpRequest();
	else if (window.ActiveXObject) req = new ActiveXObject("Microsoft.XMLHTTP");
	return req;
}

function prepConfigForm() {
	if($('settings')) {
		var lb = document.getElementsByClassName('favechk')
		for(var i=0; i<lb.length; i++) {
			lb[i].onclick = function() {
				this.parentNode.className = this.checked?'favechecked':'faveunchecked'
				if(this.checked) {
					var f = this.parentNode.nextSibling.firstChild
					f.checked = true
					if(f.onclick) f.onclick()
				}
			}
			lb[i].onclick()
		}
		lb = document.getElementsByClassName('confchk')
		for(var i=0; i<lb.length; i++) {
			lb[i].onclick = function() {
				this.parentNode.className = this.checked?'checked':'unchecked'
				if(!this.checked) {
					var f = this.parentNode.previousSibling.firstChild
					f.checked = false
					f.onclick()
				}
			}
			lb[i].onclick()
		}
	}
}

function saveConfig() {
	if($('sitelist')) {
		var sites = $('sitelist').innerHTML.split(',')
		var fave = new Array()
		var hide = new Array()
		for (var i=0; i<sites.length; i++) {
			if($('fave_' + sites[i]).checked) fave.push(sites[i])
			if(!$('show_' + sites[i]).checked) hide.push(sites[i])
		}
		fave = fave.join(',')
		hide = hide.join(',')
		document.cookie = 'fave=' + fave + '; expires=Sat, 1 Jan 2050 20:00:00 UTC; path=/'
		document.cookie = 'hide=' + hide + '; expires=Sat, 1 Jan 2050 20:00:00 UTC; path=/'
		myLightbox.end()
		document.location.href = '/dailydeals'
	}
}
