﻿var ONE = {

	start: function() {
		this.tweakLayout();
		this.tweakButtons();
		this.addFuncionalities();
		this.addTooltips();
		this.addValidation();
	},
	
	
	tweakLayout: function() {
		$('html').addClass('js');
		
		// Sign inputs for IE
		if ($.browser.msie) {
			$('input').each(function() {
				if ($(this).attr('type') == 'text') {
					$(this).addClass('text');
				}
				else if ($(this).attr('type') == 'password') {
					$(this).addClass('password');
				}
				else if ($(this).attr('type') == 'checkbox') {
					$(this).addClass('checkbox');
				}
				else if ($(this).attr('type') == 'radio') {
					$(this).addClass('radio');
				}
			});
		}
		
		//Offer aligning
		$('#offer .offer-list').each(function() {
			var $this = $(this);
			var max = 0;
			$this.find('li').each(function() {
				if ($(this).height() > max) max = $(this).height();
			});
			$this.find('li').each(function() {
				var $li = $(this);
				$li.find('.order').css('padding-top', (22+max-$li.height()) + 'px');
			});
			
		});
		
	},
	
	tweakButtons: function() {
		$('input.button').each(function() {
			var $submit = $(this);
			var classes = this.className;
			var val = $submit.attr('value');
			var alt = $submit.attr('alt');
			$submit.after('<a class="' + classes + '" href="#"><span class="btn-l">' + val + '</span><span class="btn-r"></span></a>');
			$submit.next().click(function() {
				$submit.click();
				return false;
			});
			$submit.hide();
		});
	
	},
	
	addTooltips:function() {
		function showTip($el) {
			$el.bt({
				contentSelector: '$(this).next().html()',
				shrinkToFit: true,
				hoverIntentOpts: {
					interval: 0,
					timeout: 0
				},
				fill: '#fff',
				strokeWidth: 2,
				strokeStyle: '#838383',
				cssStyles: {color: '#888', fontWeight: 'normal', fontSize: '10px'},
				shrinkToFit: true,
				padding: 10,
				cornerRadius: 5,
				spikeLength: 15,
				spikeGirth: 15,
				positions: ['right', 'left', 'bottom']
			});
		}
		$('.help:not(#nav .help)').each(function() {
			var $this = $(this);
			if ($this.parent().hasClass('left-tip')) {
				$this.bt({
					contentSelector: '$(this).next().html()',
					shrinkToFit: true,
					hoverIntentOpts: {
						interval: 0,
						timeout: 0
					},
					fill: '#fff',
					strokeWidth: 2,
					strokeStyle: '#838383',
					cssStyles: {color: '#888', fontWeight: 'normal', fontSize: '10px'},
					shrinkToFit: true,
					padding: 10,
					cornerRadius: 5,
					spikeLength: 15,
					spikeGirth: 15,
					positions: ['left']
				});
			}
			else {
				showTip($this);
			}
		});
	},
	
	addFuncionalities: function() {
		//adding _blank
		$('a.blank').attr('target', '_blank');
		
		//captions to te-search
		$('#tibian-earth .field input').each(function() {
			var caption = 'wyszukaj w tibian earth';
			var $this = $(this);
			if ($this.val() == '' || $this.val() == caption) {
				$this.addClass('pre').val(caption);
				$this.focus(function() {
					$this.val('').unbind('focus').removeClass('pre');
				});
			}
		});
		
		//captions to shoutbox-search
		$('#domain-search-box .field input').each(function() {
			var caption = 'www.twoja-domena.pl';
			var $this = $(this);
			if ($this.val() == '' || $this.val() == caption) {
				$this.addClass('pre').val(caption);
				$this.focus(function() {
					$this.val('').unbind('focus').removeClass('pre');
				});
			}
		});
		$('#login-box input.login').each(function() {
			var caption = 'Twój login';
			var $this = $(this);
			if ($this.val() == '' || $this.val() == caption) {
				$this.addClass('pre').val(caption);
				$this.focus(function() {
					$this.val('').unbind('focus').removeClass('pre');
				});
			}
		});
		$('#login-box input.pass').each(function() {
			var caption = 'Hasło';
			var $this = $(this);
			if ($this.val() == '' || $this.val() == caption) {
				$this.hide().before('<input type="text" value="' + caption + '" class="pre" />');
				$this.prev().focus(function() {
					$(this).remove();
					$this.show().focus();
				});
			}
		});
		
		//comparison slider box
		$('#comparison .servers').each(function() {
			var $this = $(this);
			var $ul = $this.find('ul:first');
			var l = 0;
			$ul.find('li').each(function() {
				l += $(this).width() + 10;
			});
			$ul.width(l);
			$this.addClass('scrollable');
		});
	},
	
	showTooltips: function() {
		$('.help').each(function() {
			var $this = $(this);
			$this.find('.mark').tooltip({
				track: false,
				delay: 0,
				showURL: false,
				bodyHandler: function() {
					return '<div class="top">' + $this.find('.help-content').html() + '</div><div class="bottom"></div>';
				},
				extraClass: 'one-help',
				positionLeft: true,
				fixPNG: true,
				left: -10,
				top: 10
			});
		});

	},
	
	addValidation: function() {
		$('form.validate').each(function() {
			var $form = $(this);
			$form.find('.cell').each(function() {
				var $cell = $(this);
				$cell.find('input:not(:submit):not(type[file])').each(function() {
					var $input = $(this);
					$input.focus(function() {
						$cell.find('.error').remove();
						$input.removeClass('el-error');
					});
					$input.blur(function() {
						var ok = true;
						if ($input.hasClass('login')) {
							ok = isLongEnough($(this).val(), 4);
							errorMsg = 'Za krótka nazwa użytkownika';
							if (ok) {
								ok = isLogin($(this).val());
								errorMsg = 'Nazwa użytkownika zawiera nieprawidłowe znaki';
							}
						}
						if ($input.hasClass('sex')) {
							ok = isGroupChecked($input.attr('name'));
							errorMsg = 'Wybierz płeć';
						}
						if ($input.hasClass('group')) {
							ok = isGroupChecked($input.attr('name'));
							errorMsg = 'Musisz dokonać wyboru';
						}
						if ($input.hasClass('reg')) {
							ok = isChecked($input);
							errorMsg = 'Aby korzystać z serwisu musisz wyrazić zgodę';
						}
						if ($input.hasClass('email') || $input.hasClass('email2')) {
							ok = isMail($(this).val());
							errorMsg = 'Nieprawidłowy adres e-mail';
							if (ok) {
								if ($input.hasClass('email') && ($form.find('input.email2').val() != null && $form.find('input.email2').val() != '')) {
									ok = isPassEqual($form.find('input.email2').val(), $input.val());
									errorMsg = 'Podane adresy e-mail nie są jednakowe';
								}
								if ($input.hasClass('email2')) {
									ok = isPassEqual($form.find('input.email').val(), $input.val());
									errorMsg = 'Podane adresy e-mail nie są jednakowe';
								}
							}
						}
						if ($input.hasClass('tel')) {
							ok = isPhoneNo($input.val());
							errorMsg = 'Nieprawidłowy numer telefonu';
						}
						if ($input.hasClass('pass') || $input.hasClass('pass2')) {
							ok = isLongEnough($(this).attr('value'), 6);
							errorMsg = 'Hasło powinno mieć conajmniej 6 znaków';
							if (ok) {
								if ($input.hasClass('pass') && ($form.find('input.pass2').val() != null && $form.find('input.pass2').val() != '')) {
									ok = isPassEqual($form.find('input.pass2').val(), $input.val());
									errorMsg = 'Podane hasła nie są jednakowe';
								}
								if ($input.hasClass('pass2')) {
									ok = isPassEqual($form.find('input.pass').val(), $input.val());
									errorMsg = 'Podane hasła nie są jednakowe';
								}
							}
						}
						if ($input.hasClass('number')) {
							ok = isNumber($input.val());
							errorMsg = 'Nieprawidłowy numer';
						}
						if ($input.hasClass('alphanumeric')) {
							ok = isAlphanumeric($input.val());
							errorMsg = 'Pole może posiadać tylko litery i liczby';
						}
						if ($input.hasClass('house-number')) {
							ok = isHouseNumber($input.val());
							errorMsg = 'Nieprawidłowy numer domu';
						}
						if (!isNotBlank($input.val())) {
							ok = false;
							errorMsg = 'To pole jest wymagane';
						}
						if (!isNotDefault($input)) {
							ok = false;
							errorMsg = 'To pole jest wymagane';
						}
						
						if (!$input.hasClass('nr') || (isNotBlank($input.val()))) {
							if (ok && isNotDefault($input)) {
								$cell.find('.error').remove();
							}
							else {
								if (!$cell.find('.error').length) {
									ONE.displayError($cell, $input, errorMsg);
								}
							}
						}
					});
				});
				$cell.find('select:not(.nr)').each(function() {
					var $select = $(this);
					$select.focus(function() {
						$cell.find('.error').remove();
						$select.removeClass('el-error');
					});
					$select.blur(function() {
						if ($(this).val() == -1) {
							if (!$cell.find('.error').length) {
								ONE.displayError($cell, $select, 'Musisz dokonać wyboru');
							}
						}
					});
				});
				$cell.find('textarea').each(function() {
					var $textarea = $(this);
					$textarea.focus(function() {
						$cell.find('.error').remove();
						$textarea.removeClass('el-error');
					});
					$textarea.blur(function() {
						if (!$textarea.hasClass('nr') && (!isNotBlank($textarea.val()) || !isNotDefault($textarea))) {
							if (!$cell.find('.error').length) {
								ONE.displayError($cell, $textarea, 'To pole jest wymagane');
							}
						}
					});
				});
			});

			$form.submit(function() {
				if ($form.find('.error').length <= 0) {
					$form.find('input:not(:submit):not(type[file]):not(.nr)').blur();
					$form.find('select').blur();
					$form.find('textarea').blur();
					if ($form.find('.error').length > 0) {
						return false;
					}
					else return true;
				}
				else {
					return false;
				}
			});
		});
		
		function isChosen(val) {
			if (val != -1) return true;
			else return false;
		}
		function isNotBlank(val) {
			if (val != null && val != '') return true;
			else return false;
		}
		function isNotDefault($input) {
			return $input.val() != $input.attr('rel');
		}
		function isLongEnough(val, n) {
			if ((val != null && val != '') && val.length >= n) return true;
			else return false;
		}
		function getErrorMsg(msg) {
			var message = 	'<div class="error">' + msg + '</div>';
			return message;
		}
		function isMail(val) {
			var filter  = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if (filter.test(val)) return true;
			else return false;
		}
		function isPhoneNo(val) {
			var filter  = /^([0-9\-\+/ /])+$/;
			if (filter.test(val)) return true;
			else return false;
		}
		function isPassEqual(pass, pass2) {
			if (pass == pass2 && pass != null) return true;
			else return false;
		}
		function isNumber(val) {
			var filter  = /^([0-9])+$/;
			if (filter.test(val)) return true;
			else return false;
		}
		function isPostCode(val) {
			var filter  = /^[0-9][0-9]-[0-9][0-9][0-9]$/;
			if (filter.test(val)) return true;
			else return false;
		}
		function isLogin(val) {
			if (isLongEnough(val, 4)) {
				var filter  = /^([a-zA-Z0-9])+$/;
				if (filter.test(val)) return true;
				else return false;
			}
			else return false;
		}
		function isAlphanumeric(val) {
			var filter  = /^([a-zA-Z0-9])+$/;
			if (filter.test(val)) return true;
			else return false;
		}
		function isHouseNumber(val) {
			var filter  = /^([a-zA-Z0-9\/ ])+$/;
			if (filter.test(val)) return true;
			else return false;
		}
		function isPriceAmount(val) {
			var filter  = /^(([0-9])+)?([0-9][.,][0-9])?(([0-9])+)?$/;
			if (filter.test(val)) return true;
			else return false;
		}
		function isChecked($el) {
			if ($el[0].checked) {
				return true;
			}
			else return false;
		}
		function isGroupChecked(name) {
			var i = 0;
			$(':radio:checked').each(function() {
				if ($(this).attr('name') == name) {
					i++;
				}
			})
				
			if (i > 0) {
				return true;
			}
			else return false;
		}
	},
	
	displayError: function($cell, $inp, message) {
		if ($cell.find('.error').length == 0) {
			$inp.addClass('el-error');
			$cell.append('<div class="error">' + message + '</div>');
			$cell.find('.error').fadeIn(1000);
		}
	}
	
}

$(function() {
	ONE.start();
});
