/**
 * @author aktet
 */

var Load = {
	load : function(url, idUpdate, params, messageDuration, processFunction) {
		if (processFunction == undefined) {
			processFunction = function () {};
		}
		var options = {
			url: url,
			type: "POST",
			data: params,
			success: function(responseText, statusText){
				if (statusText == "success") {
					Preload.standartPreload();
					if (responseText.typeMsg == "message") {
						Message.message(responseText, messageDuration);
					}					
					if (responseText.typeMsg != "error") {
						if (idUpdate != undefined || idUpdate != '') {
							$("#" + idUpdate).html(responseText.response);	
						}
					}
					processFunction();
				}
			},
			dataType: "json"
		};
		Preload.standartPreload();
		$.ajax(options);
	},
	pload : function (url, idUpdate) {
		Preload.standartPreload();
		if (idUpdate != undefined) {
			$('#' + idUpdate).load(url);
			Preload.standartPreload();
		}
	},
	newWindow : function(url) {
		window.open(url, '_blank');
	},
	empty : function(divId) {
		$(document).ready(function () {
			$('#' + divId).html('');
		});	
	},
	locationHref : function(url) {
		location.href = url;
	},
	enlargePic : function(pic, id) {	
		var p = $("#"+id);
		var position = p.position();
		var pos = position.top > 400 ? position.top - 200 : 0;
		var div = $('#pictureLarge');
		if (div) {
			$('#pictureLarge').remove();
		}
		var div = jQuery('<div><img src="/admin/pictures/natural/name/'+pic+'/" onClick="$(\'#pictureLarge\').hide();"/></div>').attr('id', 'pictureLarge');
		div.appendTo(document.body);
		div.css({
			"position": "absolute",
			"width" : "100%",
			"text-align" : "center",
			"margin-top" : pos,
			"z-index" : "9999"
		});
	},
	removePic : function (id, anyway) {
		if (confirm('Удалить?')) {
			Utilit.ajaxDelete('id:{$value->tm_id}', '/admin/pictures/remove/id/'+id+'/anyway/'+anyway);
			$("#picture"+id).remove();
			$("#pictureDelete"+id).remove();
		}
	},
	removeStage : function (name, path, id) {
		if (confirm('Удалить?')) {
			Utilit.ajaxDelete('id:'+ name, '/admin/pictures/remove-stage/path/'+path+'/id/'+id);
			$("#picture"+name).remove();
			$("#pictureDelete"+name).remove();
		}
	},
	loadContact : function () {
		var val = $('#clientVal').val();
		if (val == 0) {
			$('#addNewBuild input[name="new[client_title]"]').val('');
			$('#addNewBuild select[name="new[client_status]"] option[value=0]').attr('selected', true);
			$('#addNewBuild input[name="new[client_address]"]').val('');
			$('#addNewBuild input[name="new[client_website]"]').val('');
			$('#addNewBuild input[name="new[client_email]"]').val('');
			$('#addNewBuild textarea[name="new[client_additional]"]').val('');
		} else {
			var options = {
				url: '/admin/clients/get-for-new-build-form/',
				data: {'id': val},
				type: "POST",
				success: function(responseText, statusText){
					if (statusText == "success") {
						Preload.standartPreload();
						$('#addNewBuild input[name="new[client_title]"]').val(responseText.title);
						$('#addNewBuild select[name="new[client_status]"] option[value='+responseText.status+']').attr('selected', true);
						$('#addNewBuild input[name="new[client_address]"]').val(responseText.address);
						$('#addNewBuild input[name="new[client_website]"]').val(responseText.website);
						$('#addNewBuild input[name="new[client_email]"]').val(responseText.email);
						$('#addNewBuild textarea[name="new[client_additional]"]').val($.base64Decode(responseText.additional));		
					}
				},
				dataType: "json"				
			};
			Preload.standartPreload();
			$.ajax(options);
		}
	},
	checkCity : function () {
		var val = $('#addNewBuild input[name="new[city]"]').val();
		if (val == 'Минск' || val == 'минск') {
			$('.forMinsk').show();
		} else {
			$('.forMinsk').hide();
		}
	},
	save: function(url, form, idUpdate, proFunc) {
		if (proFunc == undefined) {
			proFunc = function () {};
		}
		var options = {
			url: url,
			type: "POST",
			success: function(responseText, statusText){
				if (statusText == "success") {
					Preload.standartPreload();
					
					Message.parse(form, responseText);
					if (responseText.typeMsg != "error") {
						if (idUpdate != undefined) {
							$("#" + idUpdate).html(responseText.response);
						}
						proFunc();
					}
				}
			},
			dataType: "json"
		};
		Preload.standartPreload();
		$("#" + form).ajaxSubmit(options);
	},
	saveAndLoad: function(url, form, idUpdate, loadId, loadUrl){
		var options = {
			url: url,
			type: "POST",
			success: function(responseText, statusText){
				if (statusText == "success") {
					Preload.standartPreload();
					
					Message.parse(form, responseText);
					if (responseText.typeMsg != "error") {
						if (idUpdate != undefined) {
							$("#" + idUpdate).html(responseText.response);	
						}
						if (loadId != undefined) {
							$("#" + loadId).load(loadUrl);
						}
					}
				}
			},
			dataType: "json"
		};
		Preload.standartPreload();
		$("#" + form).ajaxSubmit(options);
	},
	saveImg: function(url, form, idUpdate, urlUpdate){
		var options = {
			url: url,
			type: "POST",
			success: function(responseText, statusText){
				if (statusText == "success") {
					Preload.standartPreload();
					
					Message.parse(form, responseText);
					if (responseText.typeMsg != "error") {
						if (idUpdate != undefined) {
							Load.load(idUpdate, urlUpdate);	
						}
					}
				}
			},
			dataType: "json"
		};
		Preload.standartPreload();
		$("#" + form).ajaxSubmit(options);
	},
	editContent : function (url, titleTxt, width, height) {
		var dialObj	= new Boxy("<div id='content_dialog'></div>",
		 	{title: titleTxt,
			center : false,
			modal: true,
			closeText: "<img src='/images/close.gif' alt='close' width=17 height=17 border=0>",
			afterHide : function () {
				this.unload()
			},
			afterShow : function () {
				this.tween(width, height);
			}
		});
		Load.load('content_dialog', url);
	},
	deleteOne : function(messag, divId, url) {
		if (confirm(messag)) {
			Load.load(divId, url);
		}
	},
	deleteChecked : function (formId, url , idUpdate, classDelete, message) {
		num = $("input." + classDelete + "[checked]").length;
		var isGoAction = false;
		if (num > 0) {
			if (message != null) {
				if (confirm(message)) {
					isGoAction = true;
				}
			} else {
				isGoAction = true;
			}
		}
		if (isGoAction) {
			Load.save(url, formId, idUpdate);
		}	
	}
}
var MenuControls = {
	lightItemTable : function (object, parent) {
		$('tr').removeClass('bgLight');
		if (parent == null) {
			$(object).addClass('bgLight');
		} else {
			$(object).parent('td').parent('tr').addClass('bgLight');
		}
	}/*,
	remItemConnect: function (object) {
		$(object).parent('div').remove();
	},
	addAdminToConnect: function (id) {
		if ($('#connect_admin_' + id).attr('id') == undefined) {
			$.post('/client-accounts/html-connect-admin',
					  {id: id},
					  function onAjaxSuccess(data)
						{
						  $('#connect_admin').append(data);
						}
					);
					

		}	
		
	},
	addUserToConnect: function (id) {
		if ($('#connect_user_' + id).attr('id') == undefined) {
			$.post('/client-accounts/html-connect-user',
					  {id: id},
					  function onAjaxSuccess(data)
						{
						  $('#connect_user').append(data);
						}
					);
					

		}	
		
	}*/
}


