var objLightbox;
var objOverlay;
var objLoading;
var loading_img = new Image();
loading_img.src = '/library/images/assets/loader.gif';
var overlay_img = new Image();
overlay_img.src = '/library/css/overlay.png';

function isEmail(strValue)
{
  	var objRE = /^([\w-.]+)@([\w-.]+)\.([a-zA-Z]){2,4}$/;
	return (strValue != '' && objRE.test(strValue));
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll()
{
	var yScroll;
	var xScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} 
	else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} 
	else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	
	if (self.pageXOffset) {
		xScroll = self.pageXOffset;
	} 
	else if (document.documentElement && document.documentElement.scrollLeft){	 // Explorer 6 Strict
		xScroll = document.documentElement.scrollLeft;
	} 
	else if (document.body) {// all other Explorers
		xScroll = document.body.scrollLeft;
	}

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



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize()
{
	var xScroll, yScroll;
	
	if (window.innerHeight && (window.scrollMaxY || window.scrollMaxX)) {	
		xScroll = document.body.scrollWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} 
	else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} 
	else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} 
	else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} 
	else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} 
	else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} 
	else {
		pageWidth = xScroll;
	}

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

//
// showLightbox()
// Preloads images. Pleaces new image in lightbox then centers and displays.
//
function showLightbox(obj)
{
	// prep objects
	objOverlay = document.getElementById('overlay');
	objLoading = document.getElementById('loading');
	objLightbox = document.getElementById(obj);

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	//alert(arrayPageSize[0]+'x'+arrayPageSize[1]+'  '+ arrayPageSize[2] +'x' + arrayPageSize[3]);
	
	// center the lightbox
	objLightbox.style.display = 'block';
	objLightbox.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLightbox.offsetHeight) / 2) + 'px');
	objLightbox.style.left = (arrayPageScroll[0] + ((arrayPageSize[2] - 20 - objLightbox.offsetWidth) / 2) + 'px');
	
	/*objLoading.style.display = 'block';
	objLoading.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoading.offsetHeight) / 2) + 'px');
	objLoading.style.left = (arrayPageScroll[0] + ((arrayPageSize[2] - 20 - objLoading.offsetWidth) / 2) + 'px');*/

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.width = (arrayPageSize[0] + 'px');
	objOverlay.style.display = 'block';	
}

//
// hideLightbox()
//
function hideLightbox()
{

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';
}

window.onresize = resizeWindow;

function resizeWindow() {
	if (objOverlay && objOverlay.style.display == 'block') {
		
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		if (objLightbox.style.display == 'block') {
			objLightbox.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLightbox.offsetHeight) / 2) + 'px');
			objLightbox.style.left = (arrayPageScroll[0] + ((arrayPageSize[2] - 20 - objLightbox.offsetWidth) / 2) + 'px');
		}
		else if (objLoading.style.display == 'block') {
			objLoading.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoading.offsetHeight) / 2) + 'px');
			objLoading.style.left = (arrayPageScroll[0] + ((arrayPageSize[2] - 20 - objLoading.offsetWidth) / 2) + 'px');
		}
	
		objOverlay.style.height = (arrayPageSize[1] + 'px');
		objOverlay.style.width = (arrayPageSize[0] + 'px');
	}
}

// Contact us CONTROLLER
function hideContactUs()
{
	$('name').value = '';
	$('email').value = '';
	$('url').value = '';
	$('message').value = '';
	hideLightbox();
}

function submitContactUsInfo()
{
	if ($('name').value == '') {
		alert('Please type in your name.');
		$('name').select();
		return false;
	}
	if (!isEmail($('email').value)) {
		alert('Please type in your email address.');
		$('email').select();
		return false;
	}
	if ($('subject').value == 'Suggest a Site' &&  $('url').value == '') {
		alert('Please type in the URL of the site you are talking about.');
		$('subject').select();
		return false;
	}
	if ($('message').value == '') {
		alert('Please type in your message.');
		$('message').select();
		return false;
	}
	
	new Ajax.Request
	(
		'/library/logic/func.php', 
		{ 
			onSuccess: function(r) {
				if (r.responseText == 'OK') {
					$('name').value = '';
					$('email').value = '';
					$('url').value = '';
					$('message').value = 'Thank you. Your message was sent successfully.';
				}
				else {
					alert(r.responseText);	
				}
			},
			method: 'post', 
			parameters:'func=contact_us&name='+escape($('name').value)+'&email='+escape($('email').value)+'&url='+escape($('url').value)+'&subject='+escape($('subject').value)+'&message='+escape($('message').value),  
			asynchronous:true
		}
	);	
}

function voteOnSite(elem, site_id, vote)
{
	var old_score = $(site_id+'_score').innerHTML;
	$(site_id+'_score').innerHTML = '<img src="'+loading_img.src+'" style="position: absolute; top: 31px; left: 100px;" /></div>';
	new Ajax.Request
	(
		'/library/logic/func.php', 
		{ 
			onSuccess: function(r) {
				var info = eval('(' + r.responseText + ')');
				if (info.data[0].status == '0') {
					$(elem).style.backgroundColor = '#ccc';
					$(site_id+'_desc').innerHTML = 'My Rating:';
					$(site_id+'_score').innerHTML = '<div>'+info.data[0].score+'</div>';
					$(site_id+'_total').innerHTML = info.data[0].total;
				}
				else {
					showLightbox('msg');
					$('msg_data').innerHTML = info.data[0].msg;	
					$(site_id+'_score').innerHTML = old_score;
				}
			},
			method: 'post', 
			parameters:'func=vote&site='+escape(site_id)+'&vote='+escape(vote),  
			asynchronous:true
		}
	);
}

function blinklist()
{
	u=location.href;
	p=/https/;
	httpvar=(u.search(p)!=-1)? 'https' : 'http';
	s=document.body.appendChild(document.createElement('script'));
	s.id='fs';
	s.language='javascript';
	void(s.src=httpvar+'://www.blinklist.com/Theme/Script/fav.js');
	window.document.body.appendChild(s);	
}