DialogClass = function(){}

DialogClass.prototype = {
	show	: function(url, title, item, config){
		var $loading = $('<img src="/public/images/loadingAnimation.gif" alt="loading">');
		var $dialog = $('<div></div>').append($loading.clone());
		var baseParams = {
				modal: true,
				title: title,
				width: 350,
				height: 500,
				buttons: {
					'Отправить': function(){
						var error = false;
						var validation = $("form .valid",this);
						if (validation) {
							validation.removeClass('error-ico');
							validation.each(function(index, el){
								if ($(this).hasClass('email')) {
									//valid email
									validRegExp = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
									strEmail = el.value;
									if (strEmail.search(validRegExp) == -1) {
										error = true;
										$(this).addClass('error-ico');
									}
								}
								else 
									if ($(this).hasClass('textarea')) {
										if (!el.value || el.value == el.title) {
											error = true;
											$(this).addClass('error-ico');
										}
									}
									else {
										if (!el.value || el.value == el.alt) {
											error = true;
											$(this).addClass('error-ico');
										}
									}
							});
						}
						if (!error) {
							var form = $('form',this);
							if(form.length){
								var str = form.serialize();
								$.post('/', str + '&item_id=' + item, function(response){
									var params;
									//eval("params = " + response);
									params = response;
									if (params.success) {
										$dialog.dialog('close');
										if(params.text){
											$('<div>'+params.text+'</div>').dialog();
										}
									}else{
										$(params.errors).each(function(ind,el){$("input[name='"+el+"']",form).addClass('error-ico');});
										$(".validateTips",form).text(params.errortext);
									}
								},'json');
							}else{
								$dialog.dialog('close');
							}
						}
					}
				}

			};
		if(config){
			for(key in config){
				baseParams[key] = config[key];
			}
		}
		$dialog
			.load('/',{Event:url,item_id:item})
			.dialog(baseParams);	
	}
}

$(document).ready(function() {
	PopupForm = new DialogClass();
});
