/*
 * runOnLoad.js: portable registration for onload event handlers.
 * 
 * This module defines a single runOnLoad() function for portably registering
 * functions that can be safely invoked only when the document is fully loaded
 * and the DOM is available.
 *
 * Functions registered with runOnLoad() will not be passed any arguments when
 * invoked. They will not be invoked as a method of any meaningful object, and
 * the this keyword should not be used.  Functions registered with runOnLoad()
 * will be invoked in the order in which they were registered.  There is no
 * way to deregister a function once it has been passed to runOnLoad().
 *
 * In old browsers that do not support addEventListener() or attachEvent(),
 * this function relies on the DOM Level 0 window.onload property and will not
 * work correctly when used in documents that set the onload attribute
 * of their <body> or <frameset> tags.
 */
function runOnLoad(f) {
	if (runOnLoad.loaded)
		f(); // If already loaded, just invoke f() now.
	else
		runOnLoad.funcs.push(f); // Otherwise, store it for later
}

runOnLoad.funcs = []; // The array of functions to call when the document
// loads
runOnLoad.loaded = false; // The functions have not been run yet.

// Run all registered functions in the order in which they were registered.
// It is safe to call runOnLoad.run() more than once: invocations after the
// first do nothing. It is safe for an initialization function to call
// runOnLoad() to register another function.
runOnLoad.run = function() {
	if (runOnLoad.loaded)
		return; // If we've already run, do nothing

	for ( var i = 0; i < runOnLoad.funcs.length; i++) {
		try {
			runOnLoad.funcs[i]();
		} catch (e) { /* An exception in one function shouldn't stop the rest */
		}
	}

	runOnLoad.loaded = true; // Remember that we've already run once.
	delete runOnLoad.funcs; // But don't remember the functions themselves.
	delete runOnLoad.run; // And forget about this function too!
};

// Register runOnLoad.run() as the onload event handler for the window
if (window.addEventListener)
	window.addEventListener("load", runOnLoad.run, false);
else if (window.attachEvent)
	window.attachEvent("onload", runOnLoad.run);
else
	window.onload = runOnLoad.run;

runOnLoad(function() {
	(function() {
		var po = document.createElement('script');
		po.type = 'text/javascript';
		po.async = true;
		po.src = 'https://apis.google.com/js/plusone.js';
		var s = document.getElementsByTagName('script')[0];
		s.parentNode.insertBefore(po, s);
	})();

	$("ul li:last-child").addClass("last");
	$('.tell_a_friend').fancybox( {
		'showCloseButton' : false,
		'titlePosition' : 'inside',
		'width' : 405,
		'height' : 315,
		'url' : $(this).attr('href')
	});
	$('.contact_us').fancybox( {
		'showCloseButton' : false,
		'titlePosition' : 'inside',
		'width' : 405,
		'height' : 315,
		'url' : '/index/contact'
	});
	$('.greybox').click(function() { 
		if (this.href.indexOf('youtube.com/') > 0){
			$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'		: 680,
				'height'		: 495,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/') + '&fs=1',
				'type'			: 'swf',
				'swf'			: {
				   	 'wmode'		: 'transparent',
					 'allowfullscreen'	: 'true'
				}
			});
			return false;
		}else {
// fancybox( {
// 'type' : 'iframe',
// 'height' : 450
// });
			return false;
		}
	});
	
	$("a.fancyImage").fancybox();
	$("#suggester,.helpLink").fancybox(
			{
				'type': 	'iframe',
				'autoDimensions' : false,
					'width' : 422,
					'height' : 300,
					'showCloseButton' : false,
					'titlePosition' : 'inside',
					'transitionIn' : 'none',
					'transitionOut' : 'none',
					'padding' : 0,
					'margin' : 0,
					'scrolling': 'no'
			});
	
});

function twitterCallbackGW(twitters) {
	var statusHTML = [];
	for ( var i = 0; i < twitters.length; i++) {
		var username = twitters[i].user.screen_name;
		var usernamelong = '<div class="username">'
				+ twitters[i].user.screen_name + ' <span>'
				+ twitters[i].user.name + '</span></div>';
		var status = twitters[i].text
				.replace(
						/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g,
						function(url) {
							return '<a href="' + url + '" target="_blank">'
									+ url + '</a>';
						}).replace(
						/\B@([_a-z0-9]+)/ig,
						function(reply) {
							return reply.charAt(0)
									+ '<a href="http://twitter.com/'
									+ reply.substring(1) + '" target="_blank">'
									+ reply.substring(1) + '</a>';
						});
		statusHTML.push(status + ' <a class="date" href="http://twitter.com/'
				+ username + '/statuses/' + twitters[i].id_str
				+ '" target="_blank">' + relative_time(twitters[i].created_at)
				+ '</a>');
	}
	document.getElementById('twitter_update_list').innerHTML = statusHTML
			.join('');
}

function relative_time(time_value, rev) {
	var values = time_value.split(" ");
	if (rev) { return values[1] + " " + values[2]; }
	return values[2] + " " + values[1];
}

function facebookFeed(id) {
	jQuery.getFeed( {
		url : '/index/fbproxy',
		success : function(feed) {
			var item = feed.items[0];
			post = item.title;
			dat = relative_time(item.updated, true);
			$('#' + id).html(
					post + '<a href="' + item.link
							+ '" class="date" target="_blank">' + dat
							+ '</a></date>');
		}
	});
}

function fancyAlert(text, type) {
	if (!type) {
		type = 'error';
	}
	$.fancybox(
			'<div class="messageContainer '
				+ type
				+ '"><img src="/images/button_close_cross.png" alt="close" class="closeCross" onclick="$.fancybox.close()">'
				+ text
				+ '<br><img src="/images/button_close.png" alt="close" class="closeButton" onclick="$.fancybox.close()"></div>',
				{
						'autoDimensions' : false,
						'width' : 400,
						'height' : 310,
						'showCloseButton' : false,
						'titlePosition' : 'inside',
						'transitionIn' : 'none',
						'transitionOut' : 'none',
						'padding' : 0,
						'margin' : 0
				});
}
function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}
