var imagE = function(params){
			var p = params || {};
			this.maxSize = p.maxSize || 600;
			this.cropType = p.cropType || 1;
			this.cropWidth = p.cropWidth || 0;
			this.cropHeight = p.cropHeight || 0;
			this.logo = p.logo || '';
			this.logoWidth = p.logoWidth || 50;
			this.logoHeight = p.logoHeight || 26;
			this.noClose = p.noClose || false;
			this.onSave = p.onSave || null;
			this.onSaveData = p.onSaveData || {};
			this.data = p.data || '';

			this.logo2 = p.logo2 || '';
			this.logo2Width = p.logo2Width || 50;
			this.logo2Height = p.logo2Height || 26;
			this.x = p.x || 100;
			this.y = p.y || 100;

			this.init = function(url){
				this.url = url;
				this.image = new Image();
				this.image.src = this.url;

				GUI.tools.getImageSize(this.url);

				this.status = {
					flipV: false,
					flipH: false,
					rotate: 0,
					crop: false,
					topLeftX: 0,
					topLeftY: 0,
					bottomRightX: 0,
					bottomRightY: 0,
					addLogo: false,
					addLogoImage: '',
					logoTopLeftX: 0,
					logoTopLeftY: 0
				}

				this.mainPanel.dom.content.innerHTML = '';
				this.imgContainer = document.createElement('div');
				this.imgContainer.className = 'image-container';
				this.imgContainer.style.width = this.maxSize + 'px';
				this.imgContainer.style.height = this.maxSize + 'px';
				this.imgContainer.appendChild(this.image);
				this.mainPanel.dom.content.appendChild(this.imgContainer);
				var thisObj = this;
				var imgTimer = setInterval(function(){
					if (T.imgWidth) {
						thisObj.imgWidth = T.imgWidth;
						thisObj.imgHeight = T.imgHeight;
						thisObj.wnd._resize({width: thisObj.imgWidth + 26, height: thisObj.imgHeight + 78});
						clearInterval(imgTimer);
					}
				}, 100);

			}

			//GUI
			var thisObj = this;

			this.toolbar = new GUI.toolbar({
				items: [new GUI.toolbarButton({
						ico: 'shape_flip_vertical',
						descr: 'отразить по вертикали',
						onClick: function(){
							if (thisObj.status.crop || thisObj.status.addLogo) return;
							if (thisObj.status.rotate == 0 || thisObj.status.rotate == 2) {
								thisObj.status.flipV = !thisObj.status.flipV;
							} else {
								thisObj.status.flipH = !thisObj.status.flipH;
							}
							thisObj.render();
						}
					}), new GUI.toolbarButton({
						ico: 'shape_flip_horizontal',
						descr: 'отразить по горизонтали',
						onClick: function(){
							if (thisObj.status.crop || thisObj.status.addLogo) return;
							if (thisObj.status.rotate == 1 || thisObj.status.rotate == 3) {
								thisObj.status.flipV = !thisObj.status.flipV;
							} else {
								thisObj.status.flipH = !thisObj.status.flipH;
							}
							thisObj.render();
						}
					}), new GUI.toolbarButton({
						ico: 'shape_rotate_anticlockwise',
						descr: 'повернуть против часовой стрелки',
						onClick: function(){
							if (thisObj.status.crop || thisObj.status.addLogo) return;
							thisObj.status.rotate > 0 ?
								thisObj.status.rotate-- :
								thisObj.status.rotate = 3;
							thisObj.render();
						}
					}), new GUI.toolbarButton({
						ico: 'shape_rotate_clockwise',
						descr: 'повернуть по часовой стрелке',
						onClick: function(){
							if (thisObj.status.crop || thisObj.status.addLogo) return;
							thisObj.status.rotate < 3 ?
								thisObj.status.rotate++ :
								thisObj.status.rotate = 0;
							thisObj.render();
						}
					}), new GUI.toolbarButton({
						ico: 'shape_handles',
						descr: 'обрезать',
						onClick: function(){
							if (thisObj.status.crop == false) {
								thisObj.cropStart();
							} else {
								//thisObj.cropEnd();
								thisObj.saveProcess();
							}
						}
					})
				]
			});

			if (params.logo) {
				this.toolbar.addItem(new GUI.toolbarSplit());
				this.toolbar.addItem(new GUI.toolbarButton({
					ico: 'picture_add',
					descr: 'добавить логотип',
					onClick: function(){
						if (thisObj.status.addLogo == false) {
							thisObj.addLogoStart();
						} else {
							thisObj.addLogoEnd();
						}
					}
				}));
			}

			if (params.logo2) {
				this.toolbar.addItem(new GUI.toolbarSplit());
				this.toolbar.addItem(new GUI.toolbarButton({
					ico: 'photo_add',
					descr: 'добавить второй логотип',
					onClick: function(){
						if (thisObj.status.addLogo == false) {
							thisObj.addLogoStart(2);
						} else {
							thisObj.addLogoEnd();
						}
					}
				}));
			}

			this.saveProcess = function(){
				var uri = 'http://nw2.ru/ajax.pl?uprav=56&image_url=' + thisObj.url +
					'&angle=' + (thisObj.status.rotate * 90) +
					'&flipH=' + thisObj.status.flipH +
					'&flipV=' + thisObj.status.flipV +
					'&topLeftX=' + thisObj.status.topLeftX +
					'&topLeftY=' + thisObj.status.topLeftY +
					'&bottomRightX=' + (thisObj.status.bottomRightX || GUI.tools.imgWidth) +
					'&bottomRightY=' + (thisObj.status.bottomRightY || GUI.tools.imgHeight) +
					'&addLogo=' + (thisObj.status.addLogo) +
					'&addLogoImage=' + (thisObj.status.addLogoImage) +
					'&logoTopLeftX=' + (thisObj.status.logoTopLeftX || 0) +
					'&logoTopLeftY=' + (thisObj.status.logoTopLeftY || 0) +
					'&newSizeX=' + (thisObj.cropWidth || 0) +
					'&newSizeY=' + (thisObj.cropHeight || 0) +
					'&data=' + thisObj.data;
					alert(uri);

				GUI.data.GET(uri, {}, function(data){
					thisObj.init(data.image_url);
					if (thisObj.status.crop) {
						thisObj.cropEnd();
					}
					if (!thisObj.noClose) thisObj.wnd.close();
					if (thisObj.onSave) thisObj.onSave(thisObj.onSaveData);
				});
			};

			this.toolbar.addItem(new GUI.toolbarSplit());
			this.toolbar.addItem(new GUI.toolbarButton({
				ico: 'disk',
				descr: 'сохранить изменения',
				onClick: this.saveProcess
			}));

			this.mainPanel = new GUI.panel({});

			this.wnd = new GUI.window({
				title: 'ImagEditor (' + (params.url || 'file name') + ')',
				ico: 'picture_edit',
				//align: 'center',
				x: this.x,
				y: this.y,
				//minWidth: this.maxSize + 26,
				//minHeight: this.maxSize + 78,
				width: this.imgWidth + 26,
				height: this.imgHeight + 78,
				animation: 200,
				removable: true,
				modal: true,
				resizable: false,
				items: [this.toolbar, this.mainPanel]
			});

			this.init(params.url);

			this.addLogoStart = function(num){
				if (this.status.crop) return;
				this.status.addLogo = true;
				if (this.status.rotate == 0 || this.status.rotate == 2) {
					GUI.tools.getImageSize(this.url);
					this.imgWidth = GUI.tools.imgWidth;
					this.imgHeight = GUI.tools.imgHeight;
				} else {
					GUI.tools.getImageSize(this.url);
					this.imgWidth = GUI.tools.imgHeight;
					this.imgHeight = GUI.tools.imgWidth;
				}

				var logo = GUI.dc('editor-logo');
				logo.style.backgroundImage = 'url(' + this.logo + ')';
				logo.style.width = this.logoWidth + 'px';
				logo.style.height = this.logoHeight + 'px';
				logo.style.left = '0px';
				logo.style.top = '0px';
				this.status.addLogoImage = this.logo;

				if (num && num == 2){
					logo.style.width = this.logoWidth2 + 'px';
					logo.style.height = this.logoHeight2 + 'px';
					logo.style.backgroundImage = 'url(' + this.logo2 + ')';
					this.status.addLogoImage = this.logo2;
				}
				this.mainPanel.dom.content.appendChild(logo);

				GUI.tools.bindDrag(logo, function(){
					if (parseInt(logo.style.width) + parseInt(logo.style.left) + GUI.tools.deltaX > thisObj.imgWidth ||
						parseInt(logo.style.left) + GUI.tools.deltaX < 0 ||
						parseInt(logo.style.height) + parseInt(logo.style.top) + GUI.tools.deltaY > thisObj.imgHeight ||
						parseInt(logo.style.top) + GUI.tools.deltaY < 0) return;

					logo.style.left = parseInt(logo.style.left) + GUI.tools.deltaX + 'px';
					logo.style.top = parseInt(logo.style.top) + GUI.tools.deltaY + 'px';
					thisObj.status.logoTopLeftX = parseInt(logo.style.left);
					thisObj.status.logoTopLeftY = parseInt(logo.style.top);
				});
			}

			this.addLogoEnd = function(){
				this.status.addLogo = false;
				this.mainPanel.dom.content.removeChild(this.mainPanel.dom.content.lastChild);
				this.status.logoTopLeftX = 0;
				this.status.logoTopLeftY = 0;
			}

			this.cropStart = function(){
				if (this.status.addLogo) return;
				this.status.crop = true;
				if (this.status.rotate == 0 || this.status.rotate == 2) {
					GUI.tools.getImageSize(this.url);
					this.imgWidth = GUI.tools.imgWidth;
					this.imgHeight = GUI.tools.imgHeight;
				} else {
					GUI.tools.getImageSize(this.url);
					this.imgWidth = GUI.tools.imgHeight;
					this.imgHeight = GUI.tools.imgWidth;
				}

				this.imgProp = this.imgWidth / this.imgHeight;

				var crop = GUI.dc('editor-crop');
				this.mainPanel.dom.content.appendChild(crop);

				if (this.cropType == 3) {
					crop.style.width = this.imgWidth + 'px';
					crop.style.height = this.imgHeight + 'px';

				} else {
					crop.style.width = this.imgWidth + 'px';
					crop.style.height = this.imgHeight + 'px';
				}

				if (this.cropType == 4) {
					if (this.imgWidth >= this.imgHeight) {
						crop.style.width = this.imgHeight + 'px';
						crop.style.height = this.imgHeight + 'px';
					} else {
						crop.style.width = this.imgWidth + 'px';
						crop.style.height = this.imgWidth + 'px';
					}
					this.imgProp = 1;
					this.cropType = 2;
				}

				crop.style.left = '0px';
				crop.style.top = '0px';

				crop.mover = GUI.dc('editor-crop-mover');
				crop.mover.style.width = parseInt(crop.style.width) - 10 + 'px';
				crop.mover.style.height = parseInt(crop.style.height) - 10 + 'px';
				crop.mover.style.margin = '5px';
				crop.appendChild(crop.mover);

				crop.boxSet = function(){
					this.topR.style.left = parseInt(this.style.width) - 10 + 'px';
					this.bottomR.style.left = parseInt(this.style.width) - 10 + 'px';
					this.bottomR.style.top = parseInt(this.style.height) - 10 + 'px';
					this.bottomL.style.top = parseInt(this.style.height) - 10 + 'px';
					this.mover.style.width = parseInt(this.style.width) - 10 + 'px';
					this.mover.style.height = parseInt(this.style.height) - 10 + 'px';

					thisObj.status.topLeftX = parseInt(this.style.left);
					thisObj.status.topLeftY = parseInt(this.style.top);
					thisObj.status.bottomRightX = parseInt(this.style.left) + parseInt(this.style.width);
					thisObj.status.bottomRightY = parseInt(this.style.top) + parseInt(this.style.height);
				}

				GUI.tools.bindDrag(crop.mover, function(){
					if (parseInt(crop.style.width) + parseInt(crop.style.left) + GUI.tools.deltaX > thisObj.imgWidth ||
						parseInt(crop.style.left) + GUI.tools.deltaX < 0 ||
						parseInt(crop.style.height) + parseInt(crop.style.top) + GUI.tools.deltaY > thisObj.imgHeight ||
						parseInt(crop.style.top) + GUI.tools.deltaY < 0) return;

					crop.style.left = parseInt(crop.style.left) + GUI.tools.deltaX + 'px';
					crop.style.top = parseInt(crop.style.top) + GUI.tools.deltaY + 'px';

					if (this.cropType < 3) crop.boxSet();
					//if (this.cropType < 4) crop.boxSet();
				});

				//if (this.cropType == 3) return;

				crop.topL = document.createElement('img');
				crop.topL.className = 'editor-crop-s';
				crop.topL.style.left = '0px';
				crop.topL.style.top = '0px';
				crop.topL.style.zIndex = 2;
				if (this.cropType != 3) crop.appendChild(crop.topL);

				crop.topR = document.createElement('img');
				crop.topR.className = 'editor-crop-s';
				crop.topR.style.left = this.imgWidth - 10 + 'px';
				crop.topR.style.top = '0px';
				crop.topR.style.zIndex = 3;
				if (this.cropType != 3) crop.appendChild(crop.topR);

				crop.bottomR = document.createElement('img');
				crop.bottomR.className = 'editor-crop-s';
				crop.bottomR.style.left = this.imgWidth - 10 + 'px';
				crop.bottomR.style.top = this.imgHeight - 10 + 'px';
				crop.bottomR.style.zIndex = 4;
				crop.appendChild(crop.bottomR);

				crop.bottomL = document.createElement('img');
				crop.bottomL.className = 'editor-crop-s';
				crop.bottomL.style.left = '0px';
				crop.bottomL.style.top = this.imgHeight - 10 + 'px';
				crop.bottomL.style.zIndex = 5;
				if (this.cropType != 3) crop.appendChild(crop.bottomL);

				crop.boxSet();

				GUI.tools.bindDrag(crop.topL, function(){
					if (parseInt(crop.style.width) - GUI.tools.deltaX > thisObj.imgWidth ||
						parseInt(crop.style.width) - GUI.tools.deltaX < 20 ||
						parseInt(crop.style.height) - GUI.tools.deltaY > thisObj.imgHeight ||
						parseInt(crop.style.height) - GUI.tools.deltaY < 20) return;

					if (thisObj.cropType == 1) {
						crop.style.left = parseInt(crop.style.left) + GUI.tools.deltaX + 'px';
						crop.style.width = parseInt(crop.style.width) - GUI.tools.deltaX + 'px';
						crop.style.height = parseInt(crop.style.height) - GUI.tools.deltaY + 'px';
						crop.style.top = parseInt(crop.style.top) + GUI.tools.deltaY + 'px';
					} else if (thisObj.cropType == 2 || thisObj.cropType == 3) {
						if (parseInt(crop.style.left) + GUI.tools.deltaX < 0 ||
							parseInt(parseInt(crop.style.width) / thisObj.imgProp) + parseInt(crop.style.top) > thisObj.imgHeight) return;
						crop.style.left = parseInt(crop.style.left) + GUI.tools.deltaX + 'px';
						crop.style.width = parseInt(crop.style.width) - GUI.tools.deltaX + 'px';
						crop.style.height = parseInt(parseInt(crop.style.width) / thisObj.imgProp) + 'px';
					}
					crop.boxSet();
				});

				GUI.tools.bindDrag(crop.topR, function(){
					if (parseInt(crop.style.width) + GUI.tools.deltaX > thisObj.imgWidth ||
						parseInt(crop.style.width) + GUI.tools.deltaX < 20 ||
						parseInt(crop.style.height) - GUI.tools.deltaY > thisObj.imgHeight ||
						parseInt(crop.style.height) - GUI.tools.deltaY < 20) return;

					if (thisObj.cropType == 1) {
						crop.style.width = parseInt(crop.style.width) + GUI.tools.deltaX + 'px';
						crop.style.height = parseInt(crop.style.height) - GUI.tools.deltaY + 'px';
						crop.style.top = parseInt(crop.style.top) + GUI.tools.deltaY + 'px';
					} else if (thisObj.cropType == 2 || thisObj.cropType == 3) {
						if (parseInt(crop.style.left) + parseInt(crop.style.width) + GUI.tools.deltaX > thisObj.imgWidth ||
							parseInt((parseInt(crop.style.width) + GUI.tools.deltaX)/ thisObj.imgProp) + parseInt(crop.style.top) > thisObj.imgHeight) return;
						crop.style.width = parseInt(crop.style.width) + GUI.tools.deltaX + 'px';
						crop.style.height = parseInt(parseInt(crop.style.width) / thisObj.imgProp) + 'px';
					}

					crop.boxSet();
				});

				GUI.tools.bindDrag(crop.bottomR, function(){
					if (parseInt(crop.style.width) + GUI.tools.deltaX > thisObj.imgWidth ||
						parseInt(crop.style.width) + GUI.tools.deltaX < 20 ||
						parseInt(crop.style.height) + GUI.tools.deltaY > thisObj.imgHeight ||
						parseInt(crop.style.height) + GUI.tools.deltaY < 20) return;

					if (thisObj.cropType == 1) {
						crop.style.width = parseInt(crop.style.width) + GUI.tools.deltaX + 'px';
						crop.style.height = parseInt(crop.style.height) + GUI.tools.deltaY + 'px';
					} else if (thisObj.cropType == 2 || thisObj.cropType == 3) {

						if (thisObj.cropType == 3 && parseInt(crop.style.width) + GUI.tools.deltaX < thisObj.cropWidth) return;

						if (parseInt(crop.style.left) + parseInt(crop.style.width) + GUI.tools.deltaX > thisObj.imgWidth ||
							parseInt((parseInt(crop.style.width) + GUI.tools.deltaX)/ thisObj.imgProp) + parseInt(crop.style.top) > thisObj.imgHeight) return;
						crop.style.width = parseInt(crop.style.width) + GUI.tools.deltaX + 'px';
						crop.style.height = parseInt(parseInt(crop.style.width) / thisObj.imgProp) + 'px';

					//	if (thisObj.cropType == 3){
					//		thisObj.image.style.width = crop.style.width;
					//		thisObj.image.style.height = crop.style.height;
					//	}
					}

					crop.boxSet();
				});

				GUI.tools.bindDrag(crop.bottomL, function(){
					if (parseInt(crop.style.width) - GUI.tools.deltaX > thisObj.imgWidth ||
						parseInt(crop.style.width) - GUI.tools.deltaX < 20 ||
						parseInt(crop.style.height) + GUI.tools.deltaY > thisObj.imgHeight ||
						parseInt(crop.style.height) + GUI.tools.deltaY < 20) return;

					if (thisObj.cropType == 1) {
						crop.style.left = parseInt(crop.style.left) + GUI.tools.deltaX + 'px';
						crop.style.width = parseInt(crop.style.width) - GUI.tools.deltaX + 'px';
						crop.style.height = parseInt(crop.style.height) + GUI.tools.deltaY + 'px';
					} else if (thisObj.cropType == 2) {
						if (parseInt(crop.style.left) + GUI.tools.deltaX < 0 ||
							parseInt((parseInt(crop.style.width) + GUI.tools.deltaX)/ thisObj.imgProp) + parseInt(crop.style.top) > thisObj.imgHeight) return;
						crop.style.left = parseInt(crop.style.left) + GUI.tools.deltaX + 'px';
						crop.style.width = parseInt(crop.style.width) - GUI.tools.deltaX + 'px';
						crop.style.height = parseInt(parseInt(crop.style.width) / thisObj.imgProp) + 'px';
					}
					crop.boxSet();
				});
			}

			this.cropEnd = function(){
				this.status.crop = false;
				this.mainPanel.dom.content.removeChild(this.mainPanel.dom.content.lastChild);
				this.status.bottomRightX = 0;
				this.status.bottomRightY = 0;
			}

			this.render = function() {
				var canvas = document.createElement('canvas');

				if (canvas.getContext) {

					canvas.id = 'c' + this.imageId;
					canvas.width = this.image.width;
					canvas.height = this.image.height;
					if (this.imgContainer.childNodes.length > 1) {
						this.imgContainer.removeChild(this.imgContainer.lastChild);
					}
					this.imgContainer.appendChild(canvas);
					this.image.style.display = 'none';

					context = canvas.getContext('2d');
					var image = new Image();
					image.src = this.image.src;
					context.save();

					var x = 0, y = 0;

					switch(this.status.rotate){
						case 0 :
							canvas.setAttribute('width', image.width);
							canvas.setAttribute('height', image.height);
							if (this.status.flipH) {
								x -= image.width;
								context.scale(-1, 1);
							}
							if (this.status.flipV) {
								y -= image.height;
								context.scale(1, -1);
							}
							this.wnd._resize({width: this.imgWidth + 26, height: this.imgHeight + 78});
							break;
						case 1 :
							canvas.setAttribute('width', image.height);
							canvas.setAttribute('height', image.width);
							context.rotate(this.status.rotate * 90 * Math.PI / 180);
							y -= image.height;

							if (this.status.flipH) {
								x -= image.width;
								context.scale(-1, 1);
							}
							if (this.status.flipV) {
								y += image.height;
								context.scale(1, -1);
							}
							this.wnd._resize({height: this.imgWidth + 78, width: this.imgHeight + 26});
							break;
						case 2 :
							canvas.setAttribute('width', image.width);
							canvas.setAttribute('height', image.height);
							context.rotate(this.status.rotate * 90 * Math.PI / 180);
							x -= image.width;
							y -= image.height;
							if (this.status.flipH) {
								x += image.width;
								context.scale(-1, 1);
							}
							if (this.status.flipV) {
								y += image.height;
								context.scale(1, -1);
							}
							this.wnd._resize({width: this.imgWidth + 26, height: this.imgHeight + 78});
							break;
						case 3 :
							canvas.setAttribute('width', image.height);
							canvas.setAttribute('height', image.width);
							context.rotate(this.status.rotate * 90 * Math.PI / 180);
							x -= image.width;
							if (this.status.flipH) {
								x += image.width;
								context.scale(-1, 1);
							}
							if (this.status.flipV) {
								y -= image.height;
								context.scale(1, -1);
							}
							this.wnd._resize({height: this.imgWidth + 78, width: this.imgHeight + 26});
							break;
					}
					context.drawImage(image, x, y);
					context.restore();
				} else {
					this.status.flipH ?
						this.image.style.filter = 'fliph' :
						this.image.style.filter = '';
					this.status.flipV ?
						this.image.style.filter += ' flipv' :
						this.image.style.filter += '';
					this.image.style.filter += ' progid:DXImageTransform.Microsoft.BasicImage(Rotation=' + this.status.rotate + ')';
				}
			}
		}
		GUI.changeTheme('gray');


function fff(){


var url = "ajax.pl?uprav=" + 53
  + "&user=" +  11
 + "&rnd="+Math.random();


  xmlHttp.open("GET", url, true);
  xmlHttp.onreadystatechange = function() {divloadfoto1();};
  xmlHttp.send(null);}

function divloadfoto1() {
        if(xmlHttp.readyState == 3 ) {}
        else {if(xmlHttp.readyState == 4) {



document.getElementById("divfotoall").style.display = 'block';
document.getElementById("spanfotoall").innerHTML = xmlHttp.responseText;


}}}