/*
 * SimpleModal Contact Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2009 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: contact.js 185 2009-02-09 21:51:12Z emartin24 $
 *
 */

$(document).ready(function () {
	$('#login').click(function (e) {
		e.preventDefault();
		var myrandom=Math.round(Math.random()*2);
		//alert(myrandom);
		// load the contact form using ajax
		$.get("question.php?act=login&"+myrandom, function(data){
			// create a modal dialog with the data
			
			$(data).modal({
				close: false,
				position: ["15%",],
				overlayId: 'contact-overlay',
				containerId: 'contact-container',
				onOpen: contact_login.open,
				onShow: contact_login.show,
				onClose: contact_login.close
				
			});
			
		});
	});


});

var contact_login = {
	message: null,
	open: function (dialog) {
		// add padding to the buttons in firefox/mozilla

		// dynamically determine height
		var h = 180;
		if ($('#contact-subject').length) {
			h += 26;
		}
		var title = $('#contact-container .contact-title').html();
		$('#contact-container .contact-title').html('Loading...');
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200, function () {
					$('#contact-container .contact-content').animate({
						height: h
					}, function () {
						$('#contact-container .contact-title').html(title);
						$('#contact-container form').fadeIn(200, function () {
							$('#contact-container #username').focus();

					

							// fix png's for IE 6
							if ($.browser.msie && $.browser.version < 7) {
								$('#contact-container .contact-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) {
		$('#contact-container .contact-send').click(function (e) {
															  
			e.preventDefault();
			// validate form
			if (contact_login.validate()) {
				
				$('#contact-container .contact-message').fadeOut(function () {
					$('#contact-container .contact-message').removeClass('contact-error').empty();
				});
				//$('#contact-container .contact-title').html('Sending...');
				//$('#contact-container form').fadeOut(200);
				$('#contact-container .contact-content').animate({
					height: '150px'
				}, function () {
					
					$('#contact-container .contact-loading').fadeIn(200, function () {
					    var url = 'username='+$.trim($('#username').val()) +'&password='+$.trim($("#contact-password").val())+'&act=send_login';
						//alert(url);
						$.ajax({
							url: 'question.php',
							data: url ,
							type: 'post',
							cache: false,
							dataType: 'text',
							complete: function (xhr) {
								//
								if(xhr.responseText=='err'){
									//alert('用户名或密码错误!');
									$('#contact-container .contact-message').html('用户名或密码错误,请重新输入!').fadeIn(200);
									$('#username').val('');
									$("#contact-password").val('')
									}else{
										$('#contact-container .contact-loading').fadeOut(200, function () {
											$('#contact-container .contact-title').html('谢谢!');
											$('#contact-container .contact-message').html(xhr.responseText).fadeIn(200);
										});
										window.location.reload();
									}
								
								
							},
							error: contact_login.error
						});
					});
				});
			}
			else {
				if ($('#contact-container .contact-message:visible').length > 0) {
					var msg = $('#contact-container .contact-message div');
					msg.fadeOut(200, function () {
						msg.empty();
						contact_login.showError();
						msg.fadeIn(200);
					});
				}
				else {
					$('#contact-container .contact-message').animate({
						height: '30px'
					}, contact_login.showError);
				}
				
			}
		});
	},
	close: function (dialog) {
		$('#contact-container .contact-message').fadeOut();
		$('#contact-container .contact-title').html('再见...');
		$('#contact-container form').fadeOut(200);
		$('#contact-container .contact-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 () {
		contact_login.message = '';
		if (!$('#contact-container #username').val()) {
			contact_login.message += '姓名不能为空. ';
		}
		if (!$('#contact-container #contact-password').val()) {
			contact_login.message += '密码不能为空. ';
		}		
		if (contact_login.message.length > 0) {
			return false;
		}
		else {
			return true;
		}	
	},	
	showError: function () {
		$('#contact-container .contact-message')
			.html($('<div class="contact-error">').append(contact_login.message))
			.fadeIn(200);
	}
};