/**
 * Contact Window Controller
 *
 * @author Tyler Vano <tyler.vano@gmail.com>
 * @package TylersPortfolio
 * @subpackage ContactWindow
 */
 
/**
 * The "bind()" function extension from Prototype.js, extracted for general use
 *
 * @author Richard Harrison, http://www.pluggable.co.uk
 * @author Sam Stephenson (Modified from Prototype Javascript framework)
 * @license MIT-style license @see http://www.prototypejs.org/
 */
Function.prototype.bind = function(){
    // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Functions:arguments
    var _$A = function(a){return Array.prototype.slice.call(a);}

    if(arguments.length < 2 && (typeof arguments[0] == "undefined")) return this;

    var __method = this, args = _$A(arguments), object = args.shift();

    return function() {
      return __method.apply(object, args.concat(_$A(arguments)));
    }
}

 
/**
 * Constructor for contactWindowController
 *
 * @param {HTMLElement} element
 * @return null
 */
function contactWindowController ( )
{
	this.rootWindow = document.getElementById('contact-window');
	
	this.initialize();
}

// ------------------------------------------------------------------------------------------------------------------------

/**
 * Sets up the contact window and binds the controls.
 *
 * @return null
 */
contactWindowController.prototype.initialize = function ( )
{
	this.titlebar = $('.contact-window-title',this.rootWindow);
	this.closebutton = $('.contact-window-close',this.rootWindow);
	this.phoneNumberField = $('.contact-call-widget-input',this.rootWindow);
	this.callButton = $('.contact-call-widget-button',this.rootWindow);
	this.callToolbarButton = $('.contact-window-toolbar-call',this.rootWindow);
	this.sendToolbarButton = $('.contact-window-toolbar-send',this.rootWindow);
	this.discardToolbarButton = $('.contact-window-toolbar-discard',this.rootWindow);
	
	$(this.titlebar).bind( 'mousedown',
		this.startDragObserver.bind(this)
	);
	
	$(this.closebutton).bind( 'mouseup',
		this.closeContactWindow.bind(this)
	);
		
	$(this.phoneNumberField).bind( 'mousedown',
		this.preparePhoneNumberField.bind(this)
	);
	
	$(this.callButton).bind( 'click',
		this.placePhoneCall.bind(this)
	);
	
	this.fHideCallWidget = this.hideCallWidget.bind(this);
	this.fHideCallWidgetBlocker = this.blockHideCallWidget.bind(this);
	this.fShowCallWidget = this.showCallWidget.bind(this);
	
	$(this.callToolbarButton).bind( 'click', this.fShowCallWidget );
	$(this.sendToolbarButton).bind( 'click', this.sendEmail );
	$(this.discardToolbarButton).bind( 'click', this.closeContactWindow.bind(this) );
	
	$('.contact-me-link').bind('click', this.showContactWindow.bind(this));
	$('.footer-email').bind('click', this.showContactWindow.bind(this));

	$(this.phoneNumberField).mask("(999) 999-9999", {"placeholder":" "});
	$(this.phoneNumberField).val('Enter Your Phone #');
}

// ------------------------------------------------------------------------------------------------------------------------

/**
 * Displays the call widget.
 *
 * @param {Event} e
 * @return {Boolean}
 */
contactWindowController.prototype.showCallWidget = function ( e )
{
	e.stopPropagation();
	e.preventDefault();

	if ( document.all )
	{
		$('.contact-call-widget',this.rootWindow).show();
	}
	else
	{
		$('.contact-call-widget',this.rootWindow).fadeIn();
	}

	
	$(document).bind('mousedown', this.fHideCallWidget );
	$('.contact-call-widget',this.rootWindow).bind( 'mousedown', this.fHideCallWidgetBlocker );
	
	return false;
}

// ------------------------------------------------------------------------------------------------------------------------

/**
 * Hides the call widget.
 *
 * @param {Event} e
 * @return {Boolean}
 */
contactWindowController.prototype.hideCallWidget = function ( e )
{
	e.stopPropagation();
	e.preventDefault();

	if ( document.all )
	{
		$('.contact-call-widget',this.rootWindow).hide();
	}
	else
	{
		$('.contact-call-widget',this.rootWindow).fadeOut();
	}

	
	$(document).unbind('mousedown', this.fHideCallWidget );
	$('.contact-call-widget',this.rootWindow).unbind( 'mousedown', this.fHideCallWidgetBlocker );
	
	return false;
}

// ------------------------------------------------------------------------------------------------------------------------

/**
 * Blocks clicks inside of the call widget.
 *
 * @param {Event} e
 * @return {Boolean}
 */
contactWindowController.prototype.blockHideCallWidget = function ( e )
{
	e.stopPropagation();
	
	return true;
}


// ------------------------------------------------------------------------------------------------------------------------

/**
 * Places a phone call through the web site.
 *
 * @param {Event} e
 * @return {Boolean}
 */

contactWindowController.prototype.placePhoneCall = function ( e )
{
	e.stopPropagation();
	e.preventDefault();
	
	var phoneNumber = this.phoneNumberField.val();
	var phoneNumberClean = "";
	var digits = String("0123456789");
	for ( var i = 0; i < phoneNumber.length; i++ )
	{
		if ( digits.indexOf(phoneNumber.charAt(i)) >= 0 )
		{
			phoneNumberClean += phoneNumber.charAt(i);
		}
	}
		
	if ( phoneNumberClean.length < 5 )
	{
		this.phoneNumberField.css('color','#911').val('Invalid Phone Number');
		return false;
	}

	if ( document.all )
	{
		this.phoneNumberField.hide();
		this.callButton.hide();	
	}
	else
	{
		this.phoneNumberField.fadeOut();
		this.callButton.fadeOut();
	}
	
	$('.contact-call-connecting',this.rootWindow).fadeIn();
	
	$.post(
		'./call.php'
		,{
			'phone': phoneNumberClean
		 }
		,function ( ) {
			;
		}
		,'text'
	);

	setTimeout(
		function ( ) {
			if ( document.all )
			{
				$('.contact-call-connecting').hide();
				$('.contact-call-widget').hide();
				$('.contact-call-widget-input').show();
				$('.contact-call-widget-button').show();
			}
			else
			{
				$('.contact-call-widget').fadeOut(
					'normal',
					function ( )
					{
						$('.contact-call-widget-input').show();
						$('.contact-call-widget-button').show();
						$('.contact-call-connecting').hide();
					}
				);
			}
		}
		,5000
	);
	return false;
}

// ------------------------------------------------------------------------------------------------------------------------

/**
 * Handles the initial click of the phone number field.
 *
 * @return null
 */

contactWindowController.prototype.preparePhoneNumberField = function ( )
{
	if ( $(this.phoneNumberField).css('color') != 'rgb(0, 0, 0)' )
	{
		$(this.phoneNumberField).css('color','#000').val('').focus();
	}
}

// ------------------------------------------------------------------------------------------------------------------------

/**
 * Sets up the contact window and binds the controls.
 *
 * @param {Event} e
 * @return {Boolean}
 */
contactWindowController.prototype.sendEmail = function ( e )
{
	e.stopPropagation();
	e.preventDefault();
	
 	$('.sending-notification-status').html( 'Sending&hellip;' );

	if ( document.all )
	{
	 	$('.sending-notification').show('normal');
	 	$('#contact-window').hide('normal');		
	}
	else
	{
	 	$('.sending-notification').fadeIn('normal');
	 	$('#contact-window').fadeOut('normal');	
	}

	$.post(
		'./sendEmail.php'
		,{
			 'sender':  $('.contact-window-sender' ,this.rootWindow).val()
			,'subject': $('.contact-window-subject',this.rootWindow).val()
			,'content': $('.contact-window-editor' ,this.rootWindow).val()
		 }
		,function ( data )
		 {
		 	$('.sending-notification-status').html( 'Sent.' );

			$('.contact-window-sender' ,this.rootWindow).val('');
			$('.contact-window-subject',this.rootWindow).val('');
			$('.contact-window-editor' ,this.rootWindow).val('');

			if ( document.all )
			{
				setTimeout( function ( ) { $('.sending-notification').hide(); }, 1500 );
			}
			else
			{
				setTimeout( function ( ) { $('.sending-notification').fadeOut('normal'); }, 1500 );
			}

		 }
		,'text'
	);
	
	return false;
}


// ------------------------------------------------------------------------------------------------------------------------

/**
 * Sets up the contact window and binds the controls.
 *
 * @param {Event} e
 * @return {Boolean}
 */
contactWindowController.prototype.closeContactWindow = function ( e )
{		
	e.stopPropagation();
	e.preventDefault();
	
	if ( document.all )
	{
		$(this.rootWindow).hide();
	}
	else
	{
		$(this.rootWindow).fadeOut('fast');
	}
	
	return false;
}


// ------------------------------------------------------------------------------------------------------------------------

/**
 * Sets up the contact window and binds the controls.
 *
 * @param {Event} e
 * @return {Boolean}
 */
contactWindowController.prototype.showContactWindow = function ( e )
{
	e.preventDefault();
	e.stopPropagation();
	
	var left = Math.round(document.body.clientWidth/2)-262;
	
	
	$(this.rootWindow).css('left',left+'px');
	
	if ( document.all )
	{
		$(this.rootWindow).css('top',(document.documentElement.scrollTop+150)+'px');
		$(this.rootWindow).show();
	}
	else
	{
		$(this.rootWindow).css('top',(window.scrollY+150)+'px');
		$(this.rootWindow).fadeIn('fast');
	}

	return false;
}


// ------------------------------------------------------------------------------------------------------------------------

/**
 * Find an object's position on the screen.
 *
 * @param {HTMLElement} obj
 * @return {Object}
 */
contactWindowController.prototype.findPos = function (obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			var origcurleft = curleft;
			curleft += obj.offsetLeft;
			if (curleft < 0) { 
				curleft = origcurleft;
			}
			curtop += obj.offsetTop;
		}
	}
	return { 'x': curleft, 'y': curtop };
}

// ------------------------------------------------------------------------------------------------------------------------

/**
 * Start the drag observer.
 *
 * @param {Event} e
 * @return {Boolean}
 */
contactWindowController.prototype.startDragObserver = function ( e )
{
	if ( ! e ) var e = window.event;

	if( e.pageX || e.pageY){ 
		var coords = {x:e.pageX, y:e.pageY}; 
	} 
	else
	{
		var coords = { 
			x:e.clientX + document.body.scrollLeft - document.body.clientLeft, 
			y:e.clientY + document.body.scrollTop  - document.body.clientTop 
		}; 
	}

	var objectPos = this.findPos( this.rootWindow );
	
	this.serverInspectorDragOffset = { x: coords.x - objectPos.x, y: coords.y - objectPos.y };
	
	document.body.onmousemove = this.handleDragEvent.bind(this);
	document.body.onmouseup = this.stopDragObserver.bind(this);
	
	return false;
}

// ------------------------------------------------------------------------------------------------------------------------

/**
 * Handle drag event.
 *
 * @param {Event} e
 * @return {Boolean}
 */
contactWindowController.prototype.handleDragEvent = function ( e )
{
	if ( ! e ) var e = window.event;

	if( e.pageX || e.pageY){ 
		var coords = {x:e.pageX, y:e.pageY}; 
	} 
	else
	{
		var coords = { 
			x:e.clientX + document.body.scrollLeft - document.body.clientLeft, 
			y:e.clientY + document.body.scrollTop  - document.body.clientTop 
		}; 
	}

	this.rootWindow.style.left = ( coords.x - this.serverInspectorDragOffset.x ) + 'px';
	this.rootWindow.style.top = ( coords.y - this.serverInspectorDragOffset.y ) + 'px';
}

// ------------------------------------------------------------------------------------------------------------------------

/**
 * Destructs the drag observer.
 *
 * @param {Event} e optional
 * @return null
 */
contactWindowController.prototype.stopDragObserver = function ( e )
{
	document.body.onmousemove = null;
	document.body.onmouseup = null;
}
