function hover(imgName, imgSrc) {
	document.images[imgName].src = imgSrc;
}

function initNavigation() {
	var hashPos = window.location.href.indexOf('#');
	var len = window.location.href.length;

	if (hashPos != -1 && window.location.href.slice(len - 1, len) != '#') {
		var hash = window.location.href.substr(hashPos + 1, 10);
		
		if (hash == 'createlink') {
			openPopup('createlink');
		} else if (hash == 'submiturls') {
			openPopup('submiturls');
		} else {
			openPopup('info', hash);
		}
		
	}
}

function displayAdsChick() {
	var r = Math.ceil(Math.random() * 2);
	document.getElementById('adchick' + r).style.display = "block";
	document.getElementById('youradhere' + r).style.display = "block";
}

function showResults(img, html, margin) {
	document.getElementById('loading').style.display = 'none';
	document.getElementById('spl_img').style.display = 'inline';
	document.getElementById('spl_img').src = img;
	document.getElementById('youwatched_links').innerHTML = html;
	document.getElementById('web_sticker').style.marginLeft = margin + "px";
	document.getElementById('web_sticker').style.display = "inline";
}

function clickSplash(elem) {
    if (elem.src.indexOf('splash_loading') == -1) {
		openPopup('createlink');
	}
}

function eventBind(elem, eventName, callback) {
	if (elem.addEventListener) {
		elem.addEventListener(eventName, callback, true);
	} else {
		elem.attachEvent('on' + eventName, callback);
	}
}

function sendReport(sites) {
	var hash = getHash();
	if (hash) {
		(new Image()).src = './r/?h=' + hash + '&s=' + escape(sites.toString());
	}
}


function getHash() {
	var index = window.location.href.indexOf('?');
	var retValue = false;
	
	if (index != -1) {
		retValue = window.location.href.substr(index + 1, 100);
	}
	
	return retValue;
}

// function checkAfter() {
// 	var node = document.getElementById('to_email');
// 	
// 	if (node.value == '' || !emailIsOk(node.value)) {
// 		node.className = 'text mandatory';
// 		return false;
// 	}
// }

// function checkForms(group) {	
// 	var status = true;
// 	if (group) {
// 		var elements = [
// 		  'gr_from_email',
// 		  'gr_from_name'
// 		];
// 	} else {
// 		var elements = [
// 		  'to_name',
// 		  'from_email',
// 		  'from_name'
// 		];
// 	}
// 	
// 	if (checkForms.working) {
// 		return false;
// 	} else {
// 		for (var i = 0; i < elements.length; i++) {
// 			var node = document.getElementById(elements[i]);
// 			var error = node.value == '' || (elements[i].indexOf('email') != -1 && !emailIsOk(node.value));
// 			
// 			node.className = error ? 'text mandatory' : 'text';
// 			if (error && status) {
// 				status = false;
// 				node.focus();
// 			}
// 		}
// 
// 		if (status) {
// 			checkForms.working = true;
// 		}		
// 	}
// 	
// 	return status;
// }

function validateForm(inputArray, defaultsArray) {
	var status = true;
	for (var i in inputArray) {
		var node = document.getElementById(inputArray[i]);
		// is not empty / default value / unvalid email?
		var error = node.value == '' || (defaultsArray && node.value == defaultsArray[i]) || (inputArray[i].indexOf('email') != -1 && !emailIsOk(node.value));
		// mark if error
		node.className = error ? 'text mandatory' : 'text';
		// focus on first node
		if (error && status) {
			status = false;
			node.focus();
		}
	}
	return status;
}

// function validateForms() {
// 	var val = document.getElementById('urls').value;
// 	if (!val || val.indexOf('site1.com') != -1) {
// 		alert('no sites specified...')
// 		return false;
// 	} else {
// 		$('#submiturls_message').toggle();
// 		$('#thankyou_message').toggle();
// 		return true;
// 	}
// }

// function fbLogin() {
//   var email = document.getElementById("fb_email");
//   if (emailIsOk(email.value)) {
//     email.className = 'text';
//     document.getElementById('fb-iframe').src = "/fbc/?from_email=" + escape(email.value);
//     document.getElementById('fb-iframe-container').style.display = "block";
// 	document.getElementById('contact-popup-holder').style.display = 'none';
//   } else {
//     email.className = 'text mandatory';
//   }
// 
//   showDiv('contact-popup-holder');
// }

// function closeFBPopup() {
// 	alert('closeFBPopup')
// 	document.getElementById('fb-iframe-container').style.display = "none";
// 	document.getElementById('contact-popup-holder').style.display = 'none';
// }

function emailIsOk(value) {
	if (value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
		return true;
	} else {
		return false;	
	}
}

function showLink(link, toName) {
	//checkForms.working = false;
	if (toName) {
		// individual link
		document.getElementById('instructions').innerHTML = "Copy &amp; paste this link into an email or IM message to "+toName+".";
		document.getElementById('instructions2').innerHTML = '<br/><small><a href="#" onclick="openPopup(\'createlink\'); return false;">create more links</a></small>';
	} else {
		// groupd link
		document.getElementById('instructions').innerHTML = "Post this link on your Facebook, twitter, blog or anywhere else.";
	}
	document.getElementById('your_link').value = link;
	document.getElementById('your_link').size = link.length;
	displayOkText(true);
	document.getElementById('your_link').select();
}

function finishForm(email) {
	//checkForms.working = false;
	// document.getElementById('ok-text').innerHTML = '<h3>* Message sent successfully *</h3><br />' +
	// 	'An e-mail was sent to <a href="mailto:' + email + '">' + email + '</a>. ' +
	// 	'You will get a full report with all the porn sites your friend watched as soon as he/she clicks the link.' +
	// 	'<p><br /><a href="#" onclick="openPopup(\'createlink\');" id="more">Invite more friends</a></p>' +
	// 	'<p><br />Something on your mind? <a href="mailto:ofcourse@didyouwatchporn.com">Mail us</a>.</p>';
}

function displayOkText(mode) {
	if (mode) {
		showDiv('yourlink');
		hideDiv('createlink');
	} else {
		hideDiv('yourlink');
		hideDiv('createlink');
	}
}

function hideDiv(id) {
	document.getElementById(id).style.display = 'none';
}

function showDiv(id) {
	document.getElementById(id).style.display = 'block';
}

function resetForm() {
	if (finishForm.yourEmail) {
		document.getElementById('from_email').value = finishForm.yourEmail;
		document.getElementById('gr_from_email').value = finishForm.yourEmail;	
	}

	if (finishForm.yourName) {
		document.getElementById('from_name').value = finishForm.yourName;
		document.getElementById('gr_from_name').value = finishForm.yourName;	
	}
	
	document.getElementById('to_name').value = '';
	
	displayOkText(false);
	//document.getElementById('fb-iframe').src = 'about:blank';
	return false;
}

function openPopup(div, sub) {
	// main container
	hideDiv('info');
	hideDiv('createlink');
	hideDiv('yourlink');
	hideDiv('submiturls');
			
	if (sub) {
		if (!document.getElementById('info_' + sub)) {
			return;
		}
		
		hideDiv('info_wtf');
		hideDiv('info_faq');
		hideDiv('info_privacy');
		hideDiv('info_advertise');
		hideDiv('info_thelist');
		showDiv('info_' + sub);
		
		// navigation
		document.getElementById('menu_wtf').style.backgroundColor = 'transparent';
		document.getElementById('menu_faq').style.backgroundColor = 'transparent';
		document.getElementById('menu_privacy').style.backgroundColor = 'transparent';
		document.getElementById('menu_advertise').style.backgroundColor = 'transparent';
		document.getElementById('menu_thelist').style.backgroundColor = 'transparent';
		document.getElementById('menu_' + sub).style.backgroundColor='#DDDDDD';
		
		if (window.pageTracker) {
			pageTracker._trackPageview('Popup' + sub);	
		}
	} else if (div == 'createlink') {
		resetForm();
		window.location.href='#createlink';
		if (window.pageTracker) {
			pageTracker._trackPageview('Popup createlink');	
		}
	} else if (div == 'submiturls') {
		window.location.href='#submiturls';
		if($('#submiturls_message').css('display') == 'none') {
			$('#submiturls_message').css('display', 'block');
			$('#thankyou_message').css('display', 'none');
		}
		if (window.pageTracker) {
			pageTracker._trackPageview('Popup submiturls');	
		}
	}
	
	showDiv(div);
	showDiv('contact-popup-holder');
	
	return false;
}


function closePopup() {
  // var iframe = document.getElementById('fb-iframe');
  //   iframe.style.display = "none";
  //   iframe.src = "about:blank";
  resetForm();
  document.getElementById('contact-popup-holder').style.display = 'none';
  window.location.href='#';
  return false;
}