var isIE6 = (jQuery.browser.msie && jQuery.browser.version == '6.0');
var isIE7 = (jQuery.browser.msie && jQuery.browser.version == '7.0');

(function($) {
	/**
	 * common
	 *
	 */
	$(function() {
		// remove the drag function for textarea on ff4+ safari etc...
		$('textarea').css('resize', 'none');
		
	});
	
	
	
	/**
	 * replace tag "LI" to "DIV" on page "About" for ie
	 *
	 */
	$(function() {
		var $container = $('ul.list-02');
		if ( !$container.size() ) return false;
		if ( isIE6 || isIE7 ) {
			var $node = $('<div class="clear"></div>');
			$container.find('li.clear').replaceWith($node);
		}
	});
	
	
	
	/**
	 * text shadow for page title
	 *
	 */
	$(function() {
		$('h1.page-title').css('text-shadow', '#ffe8e8 1px 1px 1px');
	});
	
	
	
	/**
	 * trigger the button
	 *
	 */
	$(function() {
		$('input.button').each(function() {
			var _this = $(this);
			var $parent = _this.parent().parent().parent();
			if ( $parent.hasClass('item-content') ) {
				var $form = _this.parents('form:first');
				_this.parent().click(function() {
					$form.submit();
					return false;
				});
				_this.parent().parent().click(function() {
					$form.submit();
					return false;
				});
			}
		});
	});
	
	
	
	/**
	 * the slider
	 *
	 */
	$(function() {
		var $container = $('#the-slider-content');
		if ( !$container.size() ) return false;
		
		var $nav = $('#the-slider-nav');
		
		$container.cycle({
			pager: $nav,
			speed: 1000,
			timeout: 4000
		});
	});
	
	
	
	/**
	 * to solve the Chrome default font-size is smaller than 12px
	 *
	 */
	$(function() {
		var styles = '<style type="text/css">html,body{-webkit-text-size-adjust:none;}</style>';
		$('head').append(styles);
	});
	
	
	
	/**
	 * tooltip text for input
	 *
	 */
	$(function() {
		$('form.the-form-style label.label').each(function() {
			var $theLabel = $(this);
			var $parent   = $theLabel.parent();
			var $theInput = $parent.find('input.input, textarea');
			
			// init
			if ( $.trim($theInput.val()) == '' ) {
				$theLabel.show();
			}
			
			// focus and blur
			$theInput.focus(function() {
				$theLabel.hide();
			}).blur(function() {
				if ( $.trim($theInput.val()) == '' ) {
					$theLabel.show();
				}
			});
		});
	});
	
	
	
	/**
	 * beautify the checkbox
	 *
	 */
	$(function() {
		var $inputCheckboxes = $('form.the-form-style input.checkbox').each(function() {
			var _this = $(this);
			
			// beautify
			var $parent = _this.parent();
			var $theCheckbox = $('<span class="mn-checkbox"></span>');
			$parent.append($theCheckbox);
			
			// bind event
			$theCheckbox.click(function() {
				if ( _this.attr('checked') ) {
					_this.removeAttr('checked');
				} else {
					_this.attr('checked', 'checked');
				}
				_this.trigger('change');
			});
			
			// highlight the selected items
			_this.change(function() {
				$inputCheckboxes.each(function() {
					var $input = $(this);
					var $parent = $input.parent();
					if ( $input.attr('checked') ) {
						$parent.addClass('has-checked');
					} else {
						$parent.removeClass('has-checked');
					}
				});
			});
		});
	});
	
	
	
	/**
	 * calculator the BMI
	 *
	 */
	$(function() {
		var $form = $('#form-bmi-calculator');
		if ( !$form.size() ) return false;
		
		var $feet   = $form.find('input[name=feet]');
		var $inches = $form.find('input[name=inches]');
		var $pound  = $form.find('input[name=pound]');
		var $BMI    = $form.find('input[name=bmi]');
		
		// remove the result when page onload
		$BMI.val('');
		
		// restrict the user input
		$form.find('input.input').keyup(function() {
			var _this = $(this);
			_this.val( _this.val().replace(/[^0-9.]/g,'') );
		});
		
		// calculator the BMI
		var getBMI = function(feet, inches, pound) {
			inches = 12 * feet + parseInt(inches);
			return ((pound * 703) / (inches * inches)).toFixed(1);
		};
		
		// calc
		$form.submit(function() {
			// get the values
			var feet   = $.trim($feet.val());
			var inches = $.trim($inches.val());
			var pound  = $.trim($pound.val());
			
			// incorrect
			if ( feet == '' || inches =='' || pound =='' || isNaN(feet) || isNaN(inches) || isNaN(pound) ) {
				$BMI.val('0');
				return false;
			}
			
			// calc
			var result = getBMI(feet, inches, pound);
			$BMI.val(result);
			
			return false;
		});
	});
	
	
	
	/**
	 * submit the contact form
	 *
	 */
	$(function() {
		var $form = $('#form-contact');
		if ( !$form.size() ) return false;
		
		
		
		/**
		 * re-genarator the captcha
		 *
		 */
		var $captcha = $form.find('img.captcha');
		var captchaURL = $captcha.attr('src');
		
		var reGenerateCaptcha = function() {
			var newCaptchaURL = captchaURL + '?rand=' + (new Date().getTime());
			$captcha.attr('src', newCaptchaURL);
		};
		$captcha.click(reGenerateCaptcha);
		
		
		
		/**
		 * ajax submit the form
		 *
		 */
		var $inputs = {};
		// get all the input 
		var $inputs = {
			name: {
				item:  $form.find('div.item-name'),
				input: $form.find('input[name=name]')
			},
			phone: {
				item:  $form.find('div.item-phone-number'),
				input: $form.find('input[name=phone]')
			},
			email: {
				item:  $form.find('div.item-email'),
				input: $form.find('input[name=email]')
			},
			height: {
				item:  $form.find('div.item-height'),
				input: $form.find('input[name=height]')
			},
			weight: {
				item:  $form.find('div.item-weight'),
				input: $form.find('input[name=weight]')
			},
			content: {
				input: $form.find('textarea[name=content]')
			},
			captcha: {
				item:  $form.find('div.item-captcha'),
				input: $form.find('input[name=captcha]')
			},
			email2me: {
				input: $form.find('input[name=email-to-me]')
			},
			terms: {
				input: $form.find('input[name=accept-terms]')
			},
			button: {
				input: $form.find('input.button')
			},
			all: $form.find('input, textarea')
		};
		
		var $loading = $form.find('p.loading');
		var loadingHtml = $loading.html();
		
		var showMessage = function(text) {
			$loading.html('<span style="font-weight:bold;color:' + (arguments[1] == '1' ? 'white' : 'red') + ';font-size:12px;">' + arguments[0] + '</span>').show();
		};
		
		// do not autocomplete
		$form.attr('autocomplete', 'off');
		
		// submit the form
		$form.submit(function() {
			$loading.html(loadingHtml).show();
			$form.find('div.item-warn').removeClass('item-warn');
			$inputs.all.attr('disabled', 'disabled');
				
			// ajax send email
			$.post('contactus.php', {
				ajax: '1',
				name: $inputs.name.input.val(),
				phone: $inputs.phone.input.val(),
				email: $inputs.email.input.val(),
				height: $inputs.height.input.val(),
				weight: $inputs.weight.input.val(),
				content: $inputs.content.input.val(),
				'accept-terms': $inputs.terms.input.attr('checked') ? 1 : 0,
				'email-to-me': $inputs.email2me.input.attr('checked') ? 1 : 0,
				captcha: $inputs.captcha.input.val()
			}, function() {
				$loading.hide();
				$inputs.all.removeAttr('disabled');
				reGenerateCaptcha();
				
				if ( arguments[0] == '' ) {
					showMessage();
					return false;
				}
				var obj = eval('(' + arguments[0] + ')');
				
				
				// mail success
				if ( obj.status == '1' ) {
					$inputs['name']['input'].val('').trigger('blur');
					$inputs['phone']['input'].val('').trigger('blur');
					$inputs['email']['input'].val('').trigger('blur');
					$inputs['height']['input'].val('').trigger('blur');
					$inputs['weight']['input'].val('').trigger('blur');
					$inputs['content']['input'].val('').trigger('blur');
					$inputs['email2me']['input'].removeAttr('checked').trigger('change');
					$inputs['terms']['input'].removeAttr('checked').trigger('change');
					$inputs['captcha']['input'].val('').trigger('blur');
					showMessage(obj.message, 1);
					return false;
				}
				
				// mail fail
				if ( obj.status == '-1' ) {
					if ( obj.error ) {
						if ( obj.error.terms ) {
							showMessage(obj.error.terms);
							delete obj.error.terms;
						}
						
						// infomation incomplete
						for ( var x in obj.error ) {
							$inputs[x]['item'].addClass('item-warn');
						}
					} else {
						// mail fail
						showMessage(obj.message);
					}
					return false;
				}
				
				showMessage(obj.message);
				return false;
			});
			
			return false;
		});
		
		
	});
	
})(jQuery);
