//centering:
$.fn.center = function(){
	var el = $(this), ww = $(window).width(), wh = $(window).height(), ew = el.outerWidth(), eh = el.outerHeight();
		var mh = ((ww - ew) / 2) + 'px', pos = el.css('position');
		if(pos == 'absolute' || pos == 'fixed'){
			el.css({ 'left': mh });
		} else {
			el.css({ 'margin-left': mh, 'margin-right': mh });
		}

		var mv = ((wh - eh) / 2) + 'px';
		if(pos == 'absolute' || pos == 'fixed'){
			el.css({ 'top': mv });
		} else {
			el.css({ 'margin-top': mv });
		}
	return el;
}

//modal window:
$.fn.modal = function(arg){
	var el = $(this);
	if(el.hasClass('noanim')){
		var speed = 0, easing = 'linear';
	} else {
		var speed = 250, easing = 'easeInQuad';
	}
	if(!el.data('modal') && arg == 'open'){
		el.data('modal', true).addClass('modal-open');
		el.before('<div class="modal-back"></div>');
		el.prev('.modal-back').stop().animate({'opacity':1}, speed, easing);
		el.css({'opacity': 0, 'display': 'block'}).center();
		if($('.modal-header', el).length){
			$('.modal-wrap', el).height(el.height() - $('.modal-wrap', el).position().top);
		}
		el.stop().animate({'opacity':1}, speed, easing, function(){
			//do something
		});
		el.parent().on('click', 'a.close-modal, .modal-back', function(){
			el.modal('close');
			return false;
		});

		$(window).smartresize(function(){
			el.modal('resize');
		});

		$(document).bind('keydown.modal', function(e) { 
			if (e.keyCode == 27) {
				$('.modal-open').each(function(){
					if($(this).data('modal') == true){
						$(this).modal('close');
					}
				});
			}
		});

	} else if(arg == 'close'){
		el.stop().animate({'opacity':0}, speed, easing, function(){
			$(this).data('modal', false).removeClass('modal-open');
			$(this).css('display', 'none');
		});
		el.prev('.modal-back').stop().animate({'opacity':0}, speed, easing, function(){
			$(this).remove();
		});
		el.removeAttr('style');
		el.removeData('modal originalHeight originalWidth originalWrapHeight bodyScroll');
		$(document).unbind('keypress.modal');

	} else if(arg == 'resize'){
		el.css({
			'top': ($(window).height()-el.height())/2 + 'px',
			'left': ($(window).width()-el.width())/2 + 'px'
		});
		el.find('.modal-wrap').stop().css({'height': el.height()-$('.modal-header', el).outerHeight(true) + 'px'});
		el.center();
	}

	return el;
}

//form validation 'lite':
$.fn.validation = function(submitFn){
	var $el = $(this);
	$el.bind('submit', function(){
		if($el.data('validated') == true) return true;
		$r = $el.find('.required');
		$r.each(function(){
			if($(this).val()==''){
				$(this).addClass('wrong');
			} else {
				$(this).removeClass('wrong');
			}
		});
		if($el.find('.wrong').length){
			return false;
		} else {
			$el.data('validated', true);
			if(typeof(submitFn) == 'function'){
				submitFn.call($el);
			} 
			return false;
		}
	});

	$el.on('change', '.wrong', function(){
		if($(this).val() != ''){
			$(this).removeClass('wrong');
		}
	});
	
	return $el;
}

//serialize form data into JSON:
$.fn.serializeJSON = function() {
	var json = {};
	jQuery.map($(this).serializeArray(), function(n, i){
		json[n['name']] = n['value'];
	});
	return json;
};
	

$(function(){
////resize handler:
	var reSize = function(){
		var wh = $(window).height(),
				mh = $('#main').height();
				mho = $('#main').outerHeight(true);
				xh = $('footer').height()+mho-mh;

	////adjust content height:
		if(mh < wh - xh) $('#main').height(wh-xh);
		
	////centering:
		$('div.center').each(function(){
			var el = $(this),
					eh = el.outerHeight();
			if(eh < wh - xh){
				el.css({
					'margin-top': (wh - xh - eh) / 2+ 'px'
				});
			}
			
		});
	}
	reSize();
	$(window).smartresize(function(){ reSize() });

////skin select boxes:
	$('select.skinned').prettySelect();
	
	$('.newsDateSelect select').change(function(){
		$(this).closest('form').submit();
	});
	
////skin checkboxes:
	$('.t .c').prettyCheckboxes();

////mask images:
	$('img.mask1, img.mask2').each(function(){
		var el = $(this),
				c = el.hasClass('mask1') ? 'mask1-wrap' : 'mask2-wrap';
		el.wrap('<div class="' + c + '" />"');
		el.parent('.' + c).append('<span class="mask"></span>')
	});

//alternative method - using wrapper in html:
	$('div.mask1-wrap, div.mask2-wrap').each(function(){
		var el = $(this);
		if(el.find('img').length && ! el.find('.mask').length){
			el.append('<span class="mask"></span>')
		}
	});

////open the anketa:
	$('.anketaLink a.anketa').click(function(){
		$('#anketa').modal('open');
		return false;
	});

////do the validation:
	$('#anketa form').validation(function(){
		var $a = $(this);
		$.post($a.attr('action'), $a.serializeJSON(), function(data) {
 			$('#anketa').find('.modal-wrap').html('').end()
			.find('h2').text('Ваша анкета отправлена.').addClass('done').end()
			.animate({'height': 22}, 300, function(){ $(this).addClass('done'); });
		});
	});
		
	$('.feedback form').validation(function(){
		$(this).submit();
	});

});

