/*
 * SimpleModal Contact Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2010 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: telephone.js 254 2010-07-23 05:14:44Z emartin24 $
 */

jQuery(function ($) {
	var telephone = {
		telephone: null,
		init: function () {
			$('#telephone-form input.telephone, #telephone-form a.telephone').click(function (e) {
				e.preventDefault();
                                
				// load the telephone form using ajax
				$.get("popupfiles/telephone.php?member_id="+this.id, function(data){
                                   
					// create a modal dialog with the data
					$(data).modal({
						closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
						position: ["15%",],
						overlayId: 'telephone-overlay',
						containerId: 'telephone-container',
						onOpen: telephone.open,
						onShow: telephone.show,
						onClose: telephone.close
					});
				});
                               
                               
			});
		},
		open: function (dialog) {
			// add padding to the buttons in firefox/mozilla
			if ($.browser.mozilla) {
				$('#telephone-container .telephone-button').css({
					'padding-bottom': '2px'
				});
			}
			// input field font size
			if ($.browser.safari) {
				$('#telephone-container .telephone-input').css({
					'font-size': '.9em'
				});
			}

			// dynamically determine height
			var h = 380;
			if ($('#telephone-subject').length) {
				h += 26;
			}
			if ($('#telephone-cc').length) {
				h += 22;
			}

			var title = $('#telephone-container .telephone-title').html();
			$('#telephone-container .telephone-title').html('Loading...');
			dialog.overlay.fadeIn(200, function () {
				dialog.container.fadeIn(200, function () {
					dialog.data.fadeIn(200, function () {
						$('#telephone-container .telephone-content').animate({
							height: h
						}, function () {
							$('#telephone-container .telephone-title').html(title);
							$('#telephone-container form').fadeIn(200, function () {
								$('#telephone-container #telephone-name').focus();

								

								// fix png's for IE 6
								if ($.browser.msie && $.browser.version < 7) {
									$('#telephone-container .telephone-button').each(function () {
										if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
											var src = RegExp.$1;
											$(this).css({
												backgroundImage: 'none',
												filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
											});
										}
									});
								}
							});
						});
					});
				});
			});
                       
                         
		},
		show: function (dialog) {
			$('#telephone-container .telephone-send').click(function (e) {
				e.preventDefault();
                                
				// validate form
				if (telephone.validate()) {
					var msg = $('#telephone-container .telephone-telephone');
					msg.fadeOut(function () {
						msg.removeClass('telephone-error').empty();
					});
					$('#telephone-container .telephone-title').html('Sending...');
					$('#telephone-container form').fadeOut(200);
					$('#telephone-container .telephone-content').animate({
						height: '80px'
					}, function () {
						$('#telephone-container .telephone-loading').fadeIn(200, function () {
							$.ajax({
								url: 'popupfiles/telephone.php',
								data: $('#telephone-container form').serialize() + '&action=send_telephone',
								type: 'post',
								cache: false,
								dataType: 'html',
								success: function (data) {
									$('#telephone-container .telephone-loading').fadeOut(200, function () {
										$('#telephone-container .telephone-title').html('Thank you!');
										msg.html(data).fadeIn(200);
									});
								},
								error: telephone.error
							});
						});
					});
				}
				else {
					if ($('#telephone-container .telephone-telephone:visible').length > 0) {
						var msg = $('#telephone-container .telephone-telephone div');
						msg.fadeOut(200, function () {
							msg.empty();
							telephone.showError();
							msg.fadeIn(200);
						});
					}
					else {
						$('#telephone-container .telephone-telephone').animate({
							height: '30px'
						}, telephone.showError);
					}
					
				}
			});

                        $('#telephone-container .telephone-login-send').click(function (e) {
				e.preventDefault();

				if (telephone.login_validate()) {
                                   var msg = $('#telephone-container .telephone-telephone');
					msg.fadeOut(function () {
						msg.removeClass('telephone-error').empty();
					});
					$('#telephone-container .telephone-title').html('L...');
					$('#telephone-container form').fadeOut(200);
					$('#telephone-container .telephone-content').animate({
						height: '80px'
					}, function () {
						$('#telephone-container .telephone-loading').fadeIn(200, function () {
							
									$('#telephone-container .telephone-loading').fadeOut(200, function () {
										$('#telephone-container .telephone-title').html('Thank you!');
										msg.html('<p>You have successfuly logged in, please close this window and try to view the telephone number</p>').fadeIn(200);
									});

						});
					});
				}
				else {
					if ($('#telephone-container .telephone-telephone:visible').length > 0) {
						var msg = $('#telephone-container .telephone-telephone div');
						msg.fadeOut(200, function () {
							msg.empty();
							telephone.showError();
							msg.fadeIn(200);
						});
					}
					else {
						$('#telephone-container .telephone-telephone').animate({
							height: '30px'
						}, telephone.showError);
					}

				}
				
			});
		},

		close: function (dialog) {
			$('#telephone-container .telephone-telephone').fadeOut();
			$('#telephone-container .telephone-title').html('Goodbye...');
			$('#telephone-container form').fadeOut(200);
			$('#telephone-container .telephone-content').animate({
				height: 40
			}, function () {
				dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(200, function () {
						dialog.overlay.fadeOut(200, function () {
							$.modal.close();
						});
					});
				});
			});
		},
		error: function (xhr) {
			alert(xhr.statusText);
		},
		validate: function () {
			telephone.telephone = '';
		
			if (!$('#telephone-container #telephone-telephone').val()) {
				telephone.telephone += 'telephone is required.';
			}

			if (telephone.telephone.length > 0) {
				return false;
			}
			else {
				return true;
			}
		},
                login_validate: function () {
			telephone.telephone = '';
                        if (!$('#telephone-container #telephone-user_password').val() || !$('#telephone-container #telephone-user_name').val()) {
				telephone.telephone = 'User name and Password is required.';
			} else {
                                  $.ajax({
                                    url: "popupfiles/telephone.php",
                                    type: "POST",
                                    cache: false,
                                    async:false,
                                    data: {action:'login_validate',
                                           user_name:$('#telephone-container #telephone-user_name').val(),
                                           user_password:$('#telephone-container #telephone-user_password').val()},
                                    success: function(theResponse)
                                    {
                                        if(theResponse == 'invalid_login'){
                                           telephone.telephone = 'Inavlid User name & password';
                                        }
                                    }
                                  });


                           

			}

			if (telephone.telephone.length > 0) {
				return false;
			}
			else {
				return true;
			}
		},
		showError: function () {
			$('#telephone-container .telephone-telephone').html($('<div class="telephone-error"></div>').append(telephone.telephone)).fadeIn(200);
		}
	};

	telephone.init();

});
