/********/
/* VARS */
/********/

var testimonialQuote = 0; //starting quote
var testimonialQuoteTimer = 5; //seconds

var headerQuote = 0; //starting quote
var headerQuoteTimer = 10; //seconds

var tabNum = 0; //starting tab
var tabTimer = 10; //seconds

var step = 1;

var canContinue = false;

var testimonialQuotes =
	[
		{
			quote : "We were signed up and loading patients into the EMR within 20 minutes. We abandoned our old EMR vendor within a week of signing up with Mitochon.",
			author : "Family Physician, CA"
		},
		{
			quote : "Mitochon\'s EMR is so simple to use that we hardly need to do any training for new hires.",
			author : "Dermatologist, TX"
		},
		{
			quote : "Our old EMR cost us $40,000 to install and at least $5000 to maintain. We can\'t believe this Mitochon EMR is free.",
			author : "Cardiologist, TX"
		},
		{
			quote : "It\'s like Hotmail for your EMR. So simple, effective and cost effective.",
			author : "OB/GYN, NV"
		}
	];

var headerQuotes =
	[
		{
			quote : "Patients need a clear, simple communication channel with their physician, which will allow them to become a true participant in their healthcare.",
			author : "Dr. Andre Vovan, Mitochon Systems CEO"
		},
		{
			quote : "Most EMR systems are too complex, expensive do not facilitate the movement of information. mEMR and mConnect do.",
			author : "Dr. Andre Vovan, Mitochon Systems CEO"
		},
		{
			quote : "EMR, PHR and HIE should not be separate technologies. They only have value when they are built into a single system.",
			author : "Mitochon Systems Core Value"
		},
		{
			quote : "EMR has to be easy to use and low cost if smaller practices are ever going to adopt on a wide scale.",
			author : "Dr. Andre Vovan, Mitochon Systems CEO"
		},
		{
			quote : "The Mitochon VMC is the result of the connectivity and exchange of information between physicians, patients and hospitals.",
			author : "Dr. Andre Vovan, Mitochon Systems CEO"
		},
		{
			quote : "In the end, at the core of a great healthcare are great physician-patient relationships.",
			author : "Dr. Andre Vovan, Mitochon Systems CEO"
		},
		{
			quote : "Physicians will adopt technology if it is easy to use, affordable and enhances physician-patient relationships.",
			author : "Mitochon Systems Core Value"
		}
	];
	
var tabText =
	[
	 	{
			text : "<ul><li>Best in class web-based EMR</li><li>Free EMR - no strings attached</li><li>No risk, SAAS model EMR</li></ul>",
			image : "icon-cycle.png",
			height : "158",
			width : "201",
			alt : ""
		},
		{
			text : "<ul><li>Get started in 5 minutes or less</li><li>No software to install</li><li>No servers to purchase - all you need is a scanner &amp; PC</li></ul>",
			image : "icon-5min.png",
			height : "162",
			width : "162",
			alt : ""
		},
		{
			text : "<ul><li>As easy as using online banking</li><li>Uses a familiar and intuitive interface</li><li>Minimal change to your workflow</li></ul>",
			image : "icon-simple.png",
			height : "163",
			width : "162",
			alt : ""
		},
		{
			text : "<ul><li>Use mConnect to connect &amp; share patient data with other physicians</li><li>Works with just about any EMR</li><li>Use mConnect separately from EMR</li></ul>",
			image : "icon-network.png",
			height : "90",
			width : "201",
			alt : ""
		},
		{
			text : "<ul><li>Reach meaningful use</li><li>Avoid reimbursement penalties</li><li>Earn and keep your $44k</li></ul>",
			image : "icon-certified.png",
			height : "165",
			width : "195",
			alt : ""
		},
		{
			text : "<ul><li>Mitochon is your free EMR option</li><li>Qualify for your $44k</li><li>Keep your $44k with a free EMR</li></ul>",
			image : "icon-44k.png",
			height : "72",
			width : "187",
			alt : ""
		}
	];

/*************/
/* FUNCTIONS */
/*************/

function rotateTestimonialQuote() {
	$('#quote-content p').fadeTo('slow', 0, function() {
		if (testimonialQuote < (testimonialQuotes.length - 1)) {
			testimonialQuote++;
		} else {
			testimonialQuote = 0;
		};
		$(this).empty();
		$(this).append('"' + testimonialQuotes[testimonialQuote].quote + '"').fadeTo('slow', 1);
	});
	$('#quote-author p').fadeTo('slow', 0, function() {
		$(this).empty();
		$(this).append(testimonialQuotes[testimonialQuote].author).fadeTo('slow', 1);
	});
}; // end function

function rotateHeaderQuote() {
	var randomNum = Math.floor( Math.random() * headerQuotes.length );
	$('#tagline-quote').fadeTo('slow', 0, function() {
		$(this).empty();
		$(this).append('"' + headerQuotes[randomNum].quote + '"').fadeTo('slow', 1);
	});
	$('#tagline-author').fadeTo('slow', 0, function() {
		$(this).empty();
		$(this).append(headerQuotes[randomNum].author).fadeTo('slow', 1);
	});
}; // end function

function rotateTab() {
	$('#tab-text').fadeTo('slow', 0, function() {
		if (tabNum < (tabText.length - 1)) {
			tabNum++;
		} else {
			tabNum = 0;
		}; // end if
		$('#tab-info').empty();
		$('#tab-image').empty();
		$('.tab').css({'top' : '10px'});
		$('#tab-' + tabNum).css({'top' : '0'});
		$('#tab-info').append(tabText[tabNum].text);
		$('#tab-image').append('<img src="/wp-content/themes/mitochon_v1/images/' + tabText[tabNum].image + '" height="' + tabText[tabNum].height +'" width="' + tabText[tabNum].width +'" alt="' + tabText[tabNum].alt +'" />');
		$(this).fadeTo('slow', 1);
	});
}; // end function

function hoverTab(i) {
	$('#tab-info').empty();
	$('#tab-image').empty();
	$('.tab').css({'top' : '10px'});
	$('#tab-' + i).css({'top' : '0'});
	$('#tab-info').append(tabText[i].text);
	$('#tab-image').append('<img src="/wp-content/themes/mitochon_v1/images/' + tabText[i].image + '" height="' + tabText[i].height +'" width="' + tabText[i].width +'" alt="' + tabText[i].alt +'" />');
	tabNum = i;
}; // end function

function signupStep(direction) {
	if (direction == 'next') {
		step = step+1;
	} else if (direction == 'prev') {
		step = step-1;
	}; // end if
	$('#signup-form-display').fadeTo('fast', 0, function() {
		if (direction == 'next') {
			$('#signup-form-' + (step-1)).appendTo($('#signup-form-wrapper'));
		} else if (direction == 'prev') {
			$('#signup-form-' + (step+1)).appendTo($('#signup-form-wrapper'));
		}; // end if
		$(this).empty();
		$(this).append($('#signup-form-' + step));
		$(this).fadeTo('fast', 1);
		
		resetSignup();
		$('#signup-process-mid-' + (step-1)).css({'background-position' : '0 -61px'});
		$('#signup-process-step-' + step).css({'background-position' : '0 -61px'});
		$('#signup-process-mid-' + step).css({'background-position' : '0 -122px'});
	});
}; // end function

function resetSignup() {
	for (i=1; i<8; i++) {
		$('#signup-process-step-' + i).css({'background-position' : '0 0'});
		$('#signup-process-mid-' + i).css({'background-position' : '0 0'});
	};
}

function validateEmail(element) {
	var userEmail = $('#' + element).val().split("@");
	if (userEmail.length < 2) {
		$('#' + element).css({"background-position" : "0 -62px"});
		$('#' + element + '-error').empty();
		$('#' + element + '-error').show();
		$('#' + element + '-error').append('Valid email required');
	} else {
		var userEmailDomain = userEmail[1].toString().split(".");
		if (userEmailDomain.length < 2) {
			$('#' + element).css({"background-position" : "0 -62px"});
			$('#' + element + '-error').empty();
			$('#' + element + '-error').show();
			$('#' + element + '-error').append('Valid email required');
		} else {
			$('#' + element).css({"background-position" : "0 0"});
			$('#' + element + '-error').hide();
		}; // end if
	}; // end if
}; // end function

function validateElement(element,type) {
	if (type == "alpha") {
		var pattern = /[^a-zA-Z\s]/g;
		var response = 'Only letters allowed';
	} else if (type == "numeric") {
		var pattern = /[^0-9\s]/g;
		var response = 'Only numberes allowed';
	} else if (type == "alphanumeric") {
		var pattern = /[^a-z0-9\s]/i;
		var response = 'Only letters &amp; numbers';
	} else if (type == "email") {
		var pattern = /[^a-zA-Z0-9\._@-]/;
		var response = 'Valid email required';
	} else if (type == "phone") {
		var pattern = /[^\(0-9\)-]/;
		var response = 'Valid phone required';
	} else if (type == "not_empty") {
		var pattern = "Not Selected";
		var response = 'Required';
	}; // end if
	if ($('#' + element).val().search(pattern) != -1 || $('#' + element).val() == document.getElementById(element).defaultValue) {
		$('#' + element).css({"background-position" : "0 -62px", "color" : "#bd4141"});
		$('#' + element + '-error').empty();
		$('#' + element + '-error').show();
		$('#' + element + '-error').append(response);
		$('.button-next').attr('disabled', 'disabled');
		$('.button-next').css({"cursor" : "default", "background-position" : "0 -31px"});
	} else {
		$('#' + element).css({"background-position" : "0 0", "color" : "#7ac142"});
		$('#' + element + '-error').hide();
		$('.button-next').attr('disabled', '');
		$('.button-next').css({"cursor" : "pointer", "background-position" : "0 0"});
	}; // end if
	
}; // end function

function gatherInfo() {
	
	$('.signup-form-review-info').empty();
	
	$('#signup-form-review-name').append($('#signup-form-fname').val() + ' ' + $('#signup-form-lname').val());
	$('#signup-form-review-specialty').append($('#signup-form-specialty').val());
	$('#signup-form-review-email').append($('#signup-form-email').val());
	$('#signup-form-review-address').append($('#signup-form-address').val() + '<br />' + $('#signup-form-city').val() + ', ' + $('#signup-form-state').val() + '<br />' + $('#signup-form-zip').val());
	$('#signup-form-review-pphone').append($('#signup-form-pphone').val());
	$('#signup-form-review-sphone').append($('#signup-form-sphone').val());
	$('#signup-form-review-fax').append($('#signup-form-fax').val());
	$('#signup-form-review-dob').append($('#signup-form-dob-month').val() + ' / ' + $('#signup-form-dob-day').val() + ' / ' + $('#signup-form-dob-year').val());
	$('#signup-form-review-npi').append($('#signup-form-npi').val());
	$('#signup-form-review-ssn').append('XXX&mdash;XX&mdash;' + $('#signup-form-ssn').val());
	$('#signup-form-review-scanner').append($('#signup-form-scanner-make').val() + ' ' + $('#signup-form-scanner-model').val());
	
}; // end function

function show(element) {
	$(element).css({"display" : "block"});
}; // end function

function hide(element) {
	$(element).css({"display" : "none"});
}; // end function

/********/
/* INIT */
/********/

$(document).ready(function(){
						   
	/*******/
	/* NAV */
	/*******/
	
	$('.nav-dropdown').hide();
	
	/***********/
	/* TWITTER */
	/***********/
	
	var username = "mitochonemr";
	var count = "3";
	
	$.getJSON('http://twitter.com/status/user_timeline/' + username + '.json?count=' + count + '&callback=?', function(data){
		$.each(data, function(index, item){
			$('#twitter').append('<li>' + item.text.linkify() + '<br />' + relative_time(item.created_at) + '</li>');
		});
	});
	
	function relative_time(time_value) {
		var values = time_value.split(" ");
		time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
		var parsed_date = Date.parse(time_value);
		var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
		var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
		
		delta = delta + (relative_to.getTimezoneOffset() * 60);
	
		var r = '';
		if (delta < 60) {
			r = 'a minute ago';
		} else if(delta < 120) {
			r = 'couple of minutes ago';
		} else if(delta < (45*60)) {
			r = (parseInt(delta / 60)).toString() + ' minutes ago';
		} else if(delta < (90*60)) {
			r = 'an hour ago';
		} else if(delta < (24*60*60)) {
			r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
		} else if(delta < (48*60*60)) {
			r = '1 day ago';
		} else {
			r = (parseInt(delta / 86400)).toString() + ' days ago';
		};
	
		return r;
	}; // end function
	
	String.prototype.linkify = function() {
		return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
			return m.link(m);
		});
	};
	
	/**********************/
	/* TESTIMONIAL QUOTES */
	/**********************/
	
	$('#quote-content p').append(testimonialQuotes[0].quote);
	$('#quote-author p').append(testimonialQuotes[0].author);
	
	setInterval("rotateTestimonialQuote()", (testimonialQuoteTimer * 1000)); //milliseconds
	
	/*****************/
	/* HEADER QUOTES */
	/*****************/
	
	$('#tagline-quote').append(headerQuotes[0].quote);
	$('#tagline-author').append(headerQuotes[0].author);
	
	setInterval("rotateHeaderQuote()", (headerQuoteTimer * 1000)); //milliseconds
	
	/***************/
	/* BANNER TABS */
	/***************/
	
	$('#tab-info').append(tabText[0].text);
	$('#tab-image').append('<img src="/wp-content/themes/mitochon_v1/images/' + tabText[0].image + '" height="' + tabText[0].height +'" width="' + tabText[0].width +'" alt="' + tabText[0].alt +'" />');
	$('#tab-0').css({'top' : '0'});
	
	setInterval("rotateTab()", (tabTimer * 1000)); //milliseconds
	
	/***************/
	/* SIGNUP FORM */
	/***************/
	
	$('#signup-process-step-1').css({'background-position' : '0 -61px'});
	$('#signup-process-mid-1').css({'background-position' : '0 -122px'});
	$('#signup-form-display').append($('#signup-form-1'));
	
	$('#signup-form-submit').click(function() {
		$('#signup-form').submit();
	});
	
	$('.signup-form-step input').focus(function() {
		$(this).css({"color" : "#7ac142", "background-position" : "0 -31px"});
	});
	
	$('.signup-control-wrapper input').focus(function() {
		$(this).css({"background-position" : "0 0"});
	});
	
	$('#signup-form').keydown(function(event){
		if(event.keyCode == '13'){
			event.preventDefault();
			signupStep('next');
		}; // end if
	});

});