// JavaScript Document

$(document).ready(function() {
	$('.slideshow').crossSlide({
	  sleep: 3,
	  fade: 1
	}, [
	  {
		src:  'images/doctors_rotate.jpg',
		href: './why-rethink/doctors/'
	  },{
		src:  'images/economist_rotate.jpg',
		href: './why-rethink/economists/'
	  }, {
		src:  'images/seniors_rotate.jpg',
		href: './why-rethink/seniors/'
	  }, {
		src:  'images/smallbusiness_rotate.jpg',
		href: './why-rethink/small-businesses/'
	  }
	]);
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'fast', /* fast/slow/normal */
		padding: 40, /* padding for each side of the picture */
		opacity: 0.35, /* Value betwee 0 and 1 */
		showTitle: true, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
		callback: function(){}
	});

	$('a#join-us').click(function (e) {
		setCookie('viewedJoinUs','y',365);
		e.preventDefault();
		$('#join-us-popup').modal({
			onShow: function (dialog) {
			},
			onClose: function (dialog) {
				dialog.data.fadeOut('fast', function () {
					dialog.container.hide('fast', function () {
						dialog.overlay.slideUp('fast', function () {
							$.modal.close();
						});
					});
				});
			}
		});
	});
	/* Validation for forms */
 	$('#subscribe').validate({
		submitHandler: function(form) {
			$.post("utilities/subscribe.cfm", { lastname: $("#subLastName").val(), firstname: $("#subFirstName").val(), email: $("#subEmail").val(), action:"getupd" },
				function(data){
					if(trim(data) == 'Success.') {
						$("#join-us-popup").html('<strong>Thank You.</strong>');
						setTimeout('$.modal.close()',2000);
					} else {
						alert(trim(data));
					}
				}
			);
		}
	});
});


function triggerAlert(x) {
		$('#alertArea').text(x);
		$('#alertArea').fadeIn('fast');
}

function hideAlert() {
		$('#alertArea').fadeOut('fast');
}

function getCookie(Name){
	var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
	if (document.cookie.match(re)) //if cookie found
	return document.cookie.match(re)[0].split("=")[1] //return its value
	return null
}

function setCookie(name, value, days){
	var expireDate = new Date()
	//set "expstring" to either an explicit date (past or future)
	if (typeof days!="undefined"){ //if set persistent cookie
	var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
	document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+";"
	}
	else //else if this is a session only cookie setting
	document.cookie = name+"="+value+";"
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

var prototype = {};
prototype.string = {};

prototype.string.autolink = function (tweet, options){
	
  if(tweet.search(/(https?:\/\/[-\w\.]+:?\/[\w\/_\.]*(\?\S+)?)/) > -1) {
    tweet = tweet.replace(/(https?:\/\/[-\w\.]+:?\/[\w\/_\.]*(\?\S+)?)/g, "<a href='$1'>$1</a>");
  }

  if(tweet.search(/@\w+/) > -1) {
    tweet = tweet.replace(/(@)(\w+)/g, "$1<a href='http://twitter.com/$2'>$2</a>");
  }

  return tweet;
};
