/*
 * 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: photopassword.js 254 2010-07-23 05:14:44Z emartin24 $
 */

jQuery(function ($) {
	var photopassword = {
		photopassword: null,
		init: function () {
			$('#photopassword-form input.photopassword, #photopassword-form a.photopassword').click(function (e) {
				e.preventDefault();
                                
				// load the photopassword form using ajax
				$.get("popupfiles/photopassword.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: 'photopassword-overlay',
						containerId: 'photopassword-container',
						onOpen: photopassword.open,
						onShow: photopassword.show,
						onClose: photopassword.close
					});
				});
                               
                               
			});
		},
		open: function (dialog) {
			// add padding to the buttons in firefox/mozilla
			if ($.browser.mozilla) {
				$('#photopassword-container .photopassword-button').css({
					'padding-bottom': '2px'
				});
			}
			// input field font size
			if ($.browser.safari) {
				$('#photopassword-container .photopassword-input').css({
					'font-size': '.9em'
				});
			}

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

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

								

								// fix png's for IE 6
								if ($.browser.msie && $.browser.version < 7) {
									$('#photopassword-container .photopassword-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) {
			$('#photopassword-container .photopassword-send').click(function (e) {
				e.preventDefault();
                                
				// validate form
				if (photopassword.validate()) {
					var msg = $('#photopassword-container .photopassword-photopassword');
					msg.fadeOut(function () {
						msg.removeClass('photopassword-error').empty();
					});
					$('#photopassword-container .photopassword-title').html('Sending...');
					$('#photopassword-container form').fadeOut(200);
					$('#photopassword-container .photopassword-content').animate({
						height: '80px'
					}, function () {
						$('#photopassword-container .photopassword-loading').fadeIn(200, function () {
							$.ajax({
								url: 'popupfiles/photopassword.php',
								data: $('#photopassword-container form').serialize() + '&action=send_photopassword',
								type: 'post',
								cache: false,
								dataType: 'html',
                                                                async : false,
								success: function (data) {
									$('#photopassword-container .photopassword-loading').fadeOut(200, function () {
										//$('#photopassword-container .photopassword-title').html('Thank you!');
										msg.html(data).fadeIn(200);
                                                                                $.modal.close();
									});
								},
								error: photopassword.error
							});
						});
					});
				}
				else {
					if ($('#photopassword-container .photopassword-photopassword:visible').length > 0) {
						var msg = $('#photopassword-container .photopassword-photopassword div');
						msg.fadeOut(200, function () {
							msg.empty();
							photopassword.showError();
							msg.fadeIn(200);
						});
					}
					else {
						$('#photopassword-container .photopassword-photopassword').animate({
							height: '30px'
						}, photopassword.showError);
					}
					
				}
			});

                       
		},

		close: function (dialog) {
                    
			$('#photopassword-container .photopassword-photopassword').fadeOut();
			//$('#photopassword-container .photopassword-title').html('Goodbye...');
			$('#photopassword-container form').fadeOut(200);
			$('#photopassword-container .photopassword-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 () {
			photopassword.photopassword = '';
		
			if (!$('#photopassword-container #photopassword-user_password').val()) {
				photopassword.photopassword += 'photo password is required.';
			} else {

                                  $.ajax({
                                    url: "popupfiles/photopassword.php",
                                    type: "POST",
                                    cache: false,
                                    async:false,
                                    data: {action:'photo_password_validate',
                                           photo_password:$('#photopassword-container #photopassword-user_password').val(),
                                           member_id :$('#photopassword-container #member_id').val()},
                                    success: function(theResponse)
                                    {
                                        if(theResponse == 'invalid'){
                                           photopassword.photopassword +=  'Inavlid Photo Password';
                                        }
                                    }
                                  });





                        }

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

	photopassword.init();

});
