// JavaScript Document

//var relative_path = '/chatterblast/trunk/';
//var relative_path = '/trunk/';
var relative_path = '/';

function executeSubmitForm( ){

	if( $( 'submit' ) ){
	
		
		// remove any click events just in case
		$( 'submit' ).removeEvents( 'submit' );
		
		// look for submit buttons and set click event	
		$( 'submit' ).addEvent( 'click', function( event ) {	
			
			if( validateForm( ) ){
			
				// ajaximit
				var myRequest = new Request({
					method:		'post',
					url:			relative_path + 'content_form_process.cfm',
					onRequest: 	function( ) {
						$( 'showForm' ).set( 'html', '<img src="' + relative_path + 'images/loading.gif" width="128" height="15" style="display: block; margin: 0pt auto;" />' );
					},
					onSuccess:	function( html ) {
						// insert the new html
						$( 'showForm' ).set( 'html', html );
						
						// reinstantiate all events
						initialize( );
						
					}
				}).send({
					data: $( 'contentForm' )
				});
	
				event.stop( );
				
			}
		});
	
	}

}

function validateForm( ){
	
	if( $$( '.required' ) ){
		
		var fields = $$( '.required' );
		var i;
		var valid = 1;
		
		for( i=0; i<fields.length; i++ ){
		
			if( fields[i].value == '' || fields[i].value == fields[i].title ){
			
				alert( 'The ' + fields[i].id.replace( "_", " " ) + ' field is a required field.');
				valid = 0;
				break;
			
			}
		
		}
	
	}
	
	return valid;
}

function initialize( ){
	
	executeSubmitForm( );
	
}

// Blog Sidebar Twitter info
function twitterCallback2(twitters) {

	var statusHTML = [];

	for (var i = 0; i < twitters.length; i++) {

		var username = twitters[i].user.screen_name;
		var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function (url) {
			return '<a href="' + url + '">' + url + '</a>';
		}).replace(/\B@([_a-z0-9]+)/ig, function (reply) {
			return reply.charAt(0) + '<a target="_blank" href="http://www.twitter.com/' + reply.substring(1) + '">' + reply.substring(1) + '</a>';
		});
		statusHTML.push('<li><span>' + status + '</span><br /> <a target="_blank" href="http://twitter.com/' + username + '/statuses/' + twitters[i].id + '" class="time_stamp">' + relative_time(twitters[i].created_at) + '</a></li>');

	}
	document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');

}



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);

	if (delta < 60) {
		return 'less than a minute ago';
	} else if (delta < 120) {
		return 'about a minute ago';
	} else if (delta < (60 * 60)) {
		return (parseInt(delta / 60)).toString() + ' minutes ago';
	} else if (delta < (120 * 60)) {
		return 'about an hour ago';
	} else if (delta < (24 * 60 * 60)) {
		return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
	} else if (delta < (48 * 60 * 60)) {
		return '1 day ago';
	} else {
		return (parseInt(delta / 86400)).toString() + ' days ago';
	}

}

function ValidateBlogComment( ) {
	
	// Validate input
	if( $( 'Name' ).value == "" || $( 'Name' ).value == "Name" ) {
		alert( 'Please enter your name.' );
		$( 'Name' ).focus();
		return false;
	} else if( !checkEmail( document.BlogCommentForm.Email ) ) {
		alert( 'Please enter your valid email address.' );
		$( 'Email' ).focus();
		return false;
	} else if( $( 'Comment' ).value == "" || $( 'Comment' ).value == "Comment" ) {
		alert( 'Please enter a comment.' );
		$( 'Comment' ).focus();
		return false;
	} else if( $( 'THP' ).value == "" ) {
		
		// Create new FX's
		var FormFx = new Fx.Tween( $( 'BlogCommentForm' ), { duration: '500', link: 'chain' } );
		if( $( 'comment_form' ) ) { FormFx.start( 'opacity', '1', '1' ); }
		
		var ProcessingFx = new Fx.Tween( $( 'comment_processing' ), { duration: '500', link: 'chain' } );
		if( $( 'comment_processing' ) ) { ProcessingFx.start( 'opacity', '0', '0' ); }
		
		var CompleteFx = new Fx.Tween( $( 'comment_complete' ), { duration: '500', link: 'chain' } );
		if( $( 'comment_complete' ) ) { CompleteFx.start( 'opacity', '0', '0' ); }
		
		// Begin responce
		new Request({
			method:		'post',
			url:			'AJAX_processing.cfm?AJAXRequest=blog_comment',
			link:		'cancel',
			onRequest:	function() {
							FormFx.start( 'opacity', '1', '0' );
							FormFx.start( 'display', 'block', 'none' );
							ProcessingFx.start( 'opacity', '0', '1' );
							//CompleteFx.start( 'opacity', '0', '0' );
						},
			onFailure:	function() {
							//FormFx.start( 'opacity', '0', '0' );
							ProcessingFx.start( 'opacity', '1', '0' );
							$( 'comment_complete' ).set( 'html', '<p>There was an error processing your comment.<br /><br />Please try again.</p>' );
							CompleteFx.start( 'opacity', '0', '1' );
							
							$( 'Name' ).value		= "Name";
							$( 'Email' ).value		= "Email (Will Not Display)";
							$( 'Link' ).value		= "Website";
							$( 'Comment' ).value	= "Comment";

						},
			onSuccess:	function() {
							//FormFx.start( 'opacity', '0', '0' );
							ProcessingFx.start( 'opacity', '1', '0' );
							$( 'comment_complete' ).set( 'html', '<p>Comment submitted successfully!<br /><br />Your comment will appear pending approval.</p>' );
							CompleteFx.start( 'opacity', '0', '1' );

							$( 'Name' ).value		= "Name";
							$( 'Email' ).value		= "Email (Will Not Display)";
							$( 'Link' ).value		= "Website";
							$( 'Comment' ).value	= "Comment";

						}
		}).send( $( 'BlogCommentForm' ) );
				
		return false;
		
	}
}


// Basic Regex validations (cross-form)
function checkEmail( formElement ) {
	//alert('run email check');
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(formElement.value))){		
		return false;	
	} else {
		return true;
	}
}



window.addEvent( 'domready', function( ) {
	
	initialize( );
	
	if( $( 'home_flash' ) ) {
		
		var flashvars = {};
		var params    = {};
		params.wmode = 'transparent';
		params.menu = 'false';
		params.allowscriptaccess = 'always';
		var attributes = {};
		swfobject.embedSWF( 'flash/home_piece.swf', 'home_flash', '1000', '371', '9.0.0', 'flash/expressInstall.swf', flashvars, params, attributes );
		
	}


});