var Form = function(){
	var
		that = this,
		body = '',
		caption = '',
		height = 0,
		minHeight = '100px',
		icon = false,
		left = -1,
		maxButton = false,
		minButton = false,
		moveable = false,
		name = '',
		resizeable = false,
		startUpPosition = 'center',
		styleFocus = '',
		styleNoFocus = '',
		top = -1,
		width = 0,
		template = '',
		form,
		form_style,
		form_body,
		onmove, form_caption, 
		onmove_nw, onmove_n, onmove_ne, onmove_w, onmove_e, onmove_sw, onmove_s, onmove_se,
		onmove_func,
		created = 0,
		showed = 0,
		hash_num = Math.round(Math.random() * 100),
		action = {
			exit: function(){ that.hide(); }
		};
	
	var x_offset = y_offset = 0, header_width = 0;
	var form_x = form_y = form_w = form_h = 0;
	var clientWidth = jWps.clientWidth();
	var clientHeight = jWps.clientHeight();
	
	var form_stack = [], stack_id = 0;
	var stack = {
		add: function(elem){
			if(form_stack[stack_id] === undefined){
				elem.stack_id = stack_id;
				form_stack[stack_id] = elem;
				stack_id++;
			}
		},
		remove: function(elem){
			if(form_stack[elem.stack_id] !== undefined){
				delete form_stack[elem.stack_id];
				stack_id--;
			}
		}
	}
	
	// Установка пользовательских параметров
	this.setup = function(s){
		created = showed = 0;
		if(s.body !== undefined && typeof body === 'string'){
			body = s.body;
		}
		if(s.caption !== undefined){
			caption = s.caption;
		}
		if(s.height !== undefined){
			height = s.height;
		}
		if(s.icon !== undefined){
			icon = s.icon;
		}
		if(s.left !== undefined){
			left = s.left;
		}
		if(s.maxButton !== undefined){
			maxButton = s.maxButton;
		}
		if(s.minButton !== undefined){
			minButton = s.minButton;
		}
		if(s.moveable !== undefined){
			moveable = s.moveable;
		}
		if(s.name !== undefined){
			name = s.name;
		}
		if(s.resizeable !== undefined){
			resizeable = s.resizeable;
		}
		if(s.startUpPosition !== undefined){
			startUpPosition = s.startUpPosition;
		}
		if(s.styleFocus !== undefined){
			styleFocus = s.styleFocus;
		}
		if(s.top !== undefined){
			top = s.top;
		}
		if(s.width !== undefined){
			width = s.width;
		}
		if(s.minHeight !== undefined){
			minHeight = s.minHeight + 'px';
		}
		
		if(left == -1){
			left = (clientWidth - width) / 2;
		}
		if(top == -1){
			top = ((clientHeight - height) / 2) + (document.body.scrollTop || document.documentElement.scrollTop) - 150;
		}
	};
	
	function initVars(){
		if(name != ''){
			form_style = form.style;
			form_body = jWps('.wps_form_body', form);
			header_width = jWps('.wps_form_header', form)[0].clientHeight;
			form_caption = jWps('.wps_form_caption', form);
		}
		
		if(moveable == true){
			onmove = jWps('.wps_form_caption', form);
			form_x = left;
			form_y = top;
			form_w = width;
			form_h = height;
		}
		
		if(resizeable == true){
			onmove_nw = jWps(form).find('.nw');
			onmove_n  = jWps(form).find('.n');
			onmove_ne = jWps(form).find('.ne');
			onmove_w  = jWps(form).find('.w');
			onmove_e  = jWps(form).find('.e');
			onmove_sw = jWps(form).find('.sw');
			onmove_s  = jWps(form).find('.s');
			onmove_se = jWps(form).find('.se');
		}
		
		return;
	}
	
	function create(){
		if(created == 0){
			var tpl_minButton  = '<div class="minButton"></div>';
			var tpl_maxButton  = '<div class="maxButton"></div>';
			var tpl_icon       = '<div class="icon"></div>';
			
			var template = '<div class="nw"></div><div class="n"></div><div class="ne"></div>'+
				'<div class="w"></div><div class="e"></div>'+
				'<div class="sw"></div><div class="s"></div><div class="se"></div>'+
				'<div class="background"></div>'+
				'<div class="wps_form_header">'+
					'<div class="wps_form_buttons">[#minButton][#maxButton]<div class="exitButton"></div></div>'+
					'<div class="wps_form_caption">[#icon][#caption]</div>'+
				'</div>'+
				'<div class="wps_form_body">[#body]</div>';
			
			template = jWps.replace('[#caption]', caption, template);
			template = jWps.replace('[#body]', body, template);
			
			if(icon == true){
				template = jWps.replace('[#icon]', tpl_icon, template);
			}else{
				template = jWps.replace('[#icon]', "", template);
			}
			
			if(minButton == true){
				template = jWps.replace('[#minButton]', tpl_minButton, template);
			}else{
				template = jWps.replace('[#minButton]', "", template);
			}
			
			if(maxButton == true){
				template = jWps.replace('[#maxButton]', tpl_maxButton, template);
			}else{
				template = jWps.replace('[#maxButton]', "", template);
			}
			
			var className = 'wps_form';
			if(moveable == true) className+= ' moveable';
			if(resizeable == true) className+= ' resizeable';
			
			form = jWps.create('div', {
				'id': name,
				'class': className,
				'style': {'left':left+'px', 'top':top+'px', 'width':width+'px', 'min-height':minHeight}
			});
			
			if(height != 0) form.style.height = height+'px';
			
			jWps(form).html(template);
			jWps('.exitButton', form)[0].onclick = action.exit;
			
			created = 1;
		}
	}
	
	this.show = function(){
		if(showed == 0){
			if(created == 0){
				create();
				created = 1;
			}
			
			var back_lock = jWps.create('div', {'id':'form_back_lock'+hash_num, 'class':'form_back_lock', 'style':{'width':jWps.documentWidth()+'px', 'height':jWps.documentHeight()+'px','position':'absolute','top':'0','left':'0','z-index':'900','display':'block'}});
			back_lock.innerHTML = '<div class="form_back_lock_logo"></div>';
			jWps('body').append(back_lock);
			jWps('body').append(form);
			
			// Инициализация переменных, для быстрого обращения к элементам формы
			initVars();
			
			for(i in form_stack){
				var elem = form_stack[i];
				form_body.append(elem.getObject());
				elem.activate();
			}
			
			// Связываем события с формой
			if(moveable == true){
				onmove.bind('mousedown', that.formMouseDown);
				onmove.bind('mouseup', that.formMouseUp);
			}
			
			if(resizeable == true){
				onmove_nw.bind('mousedown', that.formMouseDown);
				onmove_nw.bind('mouseup', that.formMouseUp);
				
				onmove_n.bind('mousedown', that.formMouseDown);
				onmove_n.bind('mouseup', that.formMouseUp);
				
				onmove_ne.bind('mousedown', that.formMouseDown);
				onmove_ne.bind('mouseup', that.formMouseUp);
				
				onmove_w.bind('mousedown', that.formMouseDown);
				onmove_w.bind('mouseup', that.formMouseUp);
				
				onmove_e.bind('mousedown', that.formMouseDown);
				onmove_e.bind('mouseup', that.formMouseUp);
				
				onmove_sw.bind('mousedown', that.formMouseDown);
				onmove_sw.bind('mouseup', that.formMouseUp);
				
				onmove_s.bind('mousedown', that.formMouseDown);
				onmove_s.bind('mouseup', that.formMouseUp);
				
				onmove_se.bind('mousedown', that.formMouseDown);
				onmove_se.bind('mouseup', that.formMouseUp);
			}
			
			showed = 1;
		}
	};
	
	this.hide = function(){
		if(showed == 1){
			jWps('#form_back_lock'+hash_num).remove();
			jWps(form).remove();
			showed = 0;
		}
	}
	
	this.getObject = function(){
		return form;
	}
	
	this.add = function(elem){
		stack.add(elem);
		if(showed == 1){
			form_body.append(elem.getObject());
			elem.activate();
		}
	}
	
	this.remove = function(elem){
		stack.remove(elem);
		elem.deactivate();
		jWps(elem.getObject()).remove();
	}
	
	this.caption = function(newCaption){
		if(typeof newCaption === 'string'){
			form_caption.html(newCaption);
		}
	}
	
	this.body = function(newBody){
		if(typeof newBody === 'string'){
			form_body.html(newBody);
		}
	}
	
	this.width = function(newWidth){
		if(newWidth !== undefined && newWidth > 0){
			width = newWidth;
			if(created == 1){
				form_style.width = width + 'px';
			}
		}
	}
	
	var old_form_x, old_form_y;
	var old_form_w, old_form_h;
	
	this.formMouseDown = function(e){
		old_form_x = form_x, old_form_y = form_y;
		old_form_w = form_w, old_form_h = form_h;
		
		var target_id = e.target.className;
		that.setMoveFunction(target_id, e);
		return false;
	};
	
	this.formMouseUp = function(e){
		jWps('body').unbind('mousemove', onmove_func);
		jWps('body').unbind('drag', onmove_func);
	};
	
	this.setMoveFunction = function(index, e){
		if(onmove_func){
			jWps('body').unbind('mousemove', onmove_func);
			jWps('body').unbind('drag', onmove_func);
		}
		
		preonmove_functions[index](e);
	
		jWps('body').bind('mousemove', onmove_func);
		jWps('body').bind('drag', onmove_func);
	};
	
	var preonmove_functions = {
		'wps_form_caption': function(e){
			x_offset = form_x - mousePosX(e);
			y_offset = form_y - mousePosY(e);
			onmove_func = that.formMouseMove;
		},
		'nw': function(e){
			x_offset = form_x - mousePosX(e);
			y_offset = form_y - mousePosY(e);
			
			
			onmove_func = function(e){
				form_x = mousePosX(e) + x_offset;
				form_y = mousePosY(e) + y_offset;
				
				var x_norm = Math.min(old_form_x + form_w, Math.max(0, form_x));
				var y_norm = Math.min(old_form_y + form_h, Math.max(0, form_y));
				
				updateFormPosition(form_x, form_y);
				updateFormSize(old_form_w + old_form_x - x_norm, old_form_h + old_form_y - y_norm);
				return false;
			};
		},
		'n': function(e){
			y_offset = form_y - mousePosY(e);
		
			onmove_func = function(e){
				form_y = mousePosY(e) + y_offset;
				var y_norm = Math.min(old_form_y + form_h, Math.max(0, form_y));
				
				updateFormSize(-1, old_form_h + old_form_y - y_norm);	
				updateFormPosition(-1, y_norm);
				
				return false;
			};
		},
		'ne': function(e){
			
			onmove_func = function(e){
				
				return false;
			};
		},
		'w': function(e){
			
			onmove_func = function(e){
				
				return false;
			};
		},
		'e': function(e){
			
			onmove_func = function(e){
				
				return false;
			};
		},
		'sw': function(e){
			
			onmove_func = function(e){
				
				return false;
			};
		},
		's': function(e){
			
			onmove_func = function(e){
				
				return false;
			};
		},
		'se': function(e){
			
			onmove_func = function(e){
				
				return false;
			};
		}
	};
	
	this.formMouseMove = function(e){
		form_x = mousePosX(e) + x_offset;
		form_y = mousePosY(e) + y_offset;
		updateFormPosition(form_x, form_y);
		return false;
	};
	
	function updateFormPosition(x, y){
		if(x > -1 && (x + form_w) < clientWidth){
			form_style.left = x + 'px';
		}
		if(y > -1 && (y + form_h) < clientHeight){
			form_style.top = y + 'px';
		}
	};
	
	function updateFormSize(w, h){
		if(w > 200){
			form_style.width = w + 'px';
		}
		if(h > 100){
			form_style.height  = h + 'px';
		}
	};
	
	var mousePosX = function(e){ return e.pageX; };
	var mousePosY = function(e){ return e.pageY; };
};


var Button = function(){
	var that = this,
		left = -1,
		top = -1,
		right = -1,
		bottom = -1,
		width = 75,
		height = 15,
		text = "",
		styleUp = "wps_button",
		styleDown = "wps_button_down",
		action = function(){},
		button,
		created = 0,
		activated = 0;
	
	this.setup = function(s){
		if(s.left !== undefined){
			if(s.left > -1 && typeof s.left === 'number'){
				left = s.left + 'px';
			}
			if(typeof s.left === 'string'){
				left = s.left;
			}
		}
		
		if(s.top !== undefined && s.top > -1){
			top = s.top + 'px';
		}
		
		if(s.right !== undefined){
			if(s.right > -1 && typeof s.right === 'number'){
				right = s.right + 'px';
			}
			if(typeof s.right === 'string'){
				right = s.right;
			}
		}
		
		if(s.bottom !== undefined && s.bottom > -1){
			bottom = s.bottom + 'px';
		}
		
		if(s.width !== undefined){
			if(s.width > -1){
				width = s.width;
			}else if(typeof s.width === 'string' && s.width == 'auto'){
				width = 'auto';
			}
		}
		
		if(s.height !== undefined && s.height > 0){
			height = s.height;
		}
		
		if(s.text !== undefined && typeof s.text === 'string'){
			text = s.text;
		}
		
		if(typeof s.styleUp === 'string'){
			styleUp = s.styleUp;
		}
		
		if(typeof s.styleDown === 'string'){
			styleDown = s.styleDown;
		}
		
		if(typeof s.action === 'function'){
			action = s.action;
		}
	}
	
	this.getObject = function(){
		if(created == 0){
			create();
			created = 1;
		}
		return button;
	}
	
	this.activate = function(){
		if(activated == 0){
			jWps(button).bind('mousedown', toggleButtonStyle);
			jWps(button).bind('mouseup', toggleButtonStyle);
			jWps(button).bind('mouseout', setDefaultStyle);
			jWps(button)[0].onclick = action;
			activated = 1;
		}
	}
	
	this.deactivate = function(){
		jWps(button).unbind('mousedown', toggleButtonStyle);
		jWps(button).unbind('mouseup', toggleButtonStyle);
		jWps(button).bind('mouseout', setDefaultStyle);
	}
	
	function create(){
		button = jWps.create('div', {
			'class': styleUp
		});
		
		/*
		var html = '<table cellpadding="0" cellspacing="0"><tr>'+
					'<td class="left" width="16"></td>'+
					'<td class="text">'+ text +'</td>'+
					'<td class="right" width="16"></td>'+
				'</tr></table>';
		*/
		var html = '<a href="javascript:;" class="default-button"><span>'+text+'</span></a>';
		
		jWps(button).html(html);
		
		//if(typeof width === 'number') button.style.minWidth = width + 'px';
		
		if(left != -1) button.style.left = left;
		if(top != -1) button.style.top = top;
		if(right != -1) button.style.right = right;
		if(bottom != -1) button.style.bottom = bottom;
		
		//jWps(button).text(text);
	}
	
	function toggleButtonStyle(e){
		jWps(button).toggleClass(styleUp, styleDown);
	}
	
	function setDefaultStyle(e){
		jWps(button).removeClass(styleDown).addClass(styleUp);
	}
};



var MessageBox = function(){
	var that = this,
		left = (jWps.clientWidth() - width) / 2,
		top = (jWps.clientHeight() - height) / 2,
		width = 300,
		height = undefined,
		w_offset = 8,
		text = "",
		caption = "",
		type = 1,
		action = {
			abort: function(){},
			cancel: function(){},
			ignore: function(){},
			no: function(){},
			ok: function(){ that.hide(); },
			retry: function(){},
			yes: function(){}
		}
		icon = "",
		button = {
			abort: null,
			cancel: null,
			ignore: null,
			no: null,
			ok: null,
			retry: null,
			yes: null
		},
		form = null,
		created = 0,
		showed = 0;
	
	this.setup = function(s){
		// Обнуляем данные
		form = null; created = 0; showed = 0;
		
		if(s.left !== undefined && s.left > -1){
			left = s.left;
		}
		
		if(s.top !== undefined && s.top > -1){
			top = s.top;
		}

		if(s.width !== undefined && s.width > 0){
			width = s.width;
			
		}
		
		if(s.height !== undefined && s.height > 0){
			height = s.height;
		}
		
		if(s.text !== undefined && typeof s.text === 'string'){
			text = s.text;
		}
		
		if(s.caption !== undefined && typeof s.caption === 'string'){
			caption = s.caption;
		}
		
		if(s.wOffset !== undefined){
			w_offset = s.wOffset;
		}
		
		if(s.type !== undefined && (typeof s.type === 'string' || (typeof s.type === 'number' && s.type > -1 && s.type < 6))){
			if(typeof s.type === 'string'){
				switch(s.type){
					case 'ABORTRETRYIGNORE': type = 0; if(width < 400) width = 400; break;
					case 'OK': type = 1; break;
					case 'OKCANCEL': type = 2; break;
					case 'RETRYCANCEL': type = 3; break;
					case 'YESNO': type = 4; break;
					case 'YESNOCANCEL': type = 5; break;
				}
			}else{
				type = s.type;
			}
		}
		
		for(var i in s.action){
			if(typeof s.action[i] === 'function' && action[i] !== undefined){
				action[i] = s.action[i];
			}
		}
		
		if(typeof s.icon === 'string' && (s.icon == 'error' || s.icon == 'warning' || s.icon == 'information' || s.icon == 'question')){
			icon = s.icon;
		}
	}
	
	this.show = function(){
		if(showed == 0){
			if(created == 0){
				create();
			}
			form.show();
			showed = 1;
		}
	}
	
	this.hide = function(){
		if(showed == 1){
			form.hide();
			showed = 0;
		}
	}
	
	function create(){
		if(created == 0){
			var body;

			if(icon == 'error' || icon == 'warning' || icon == 'information' || icon == 'question'){
				body = '<table class="wps_message_box_icon"><tr>'+
					'<td class="icon"><div class="' + icon +'"></div></td>'+
					'<td class="text">' + text + '</td>'+
				'</tr></table>';
			}else{
				body = '<div class="wps_message_box">' + text + '</div>';
			}
			body+= '<div class="wps_control_panel"></div>'
		
			form = new Form();
			form.setup({
				width: width,
				height: height,
				minButton: false,
				maxButton: false,
				moveable: true,
				name: "message_box_form",
				caption: caption,
				body: body
			});
			
			switch(type){
				case 0:
					var width_abort = width_retry = 86, width_ignore = 131,
						left_start = (width - (width_abort + width_retry + width_ignore + 20 - w_offset)) / 2;
					
					form.add(getButtonAbort({left: left_start  , bottom: 10}));
					form.add(getButtonRetry({left: (left_start + width_abort + 10), bottom: 10}));
					form.add(getButtonIgnore({left: (left_start + width_abort + width_retry + 20), bottom: 10}));
					break;
				case 1:
					form.add(getButtonOk({left: ((width - w_offset - 86) / 2), bottom: 10}));
					break;
				case 2:
					var width_ok = width_cancel = 86,
						left_start = (width - (width_ok + width_cancel + 10 - w_offset)) / 2;
						
					form.add(getButtonOk({left: left_start, bottom: 10}));
					form.add(getButtonCancel({left: (left_start + width_ok + 10), bottom: 10}));
					break;
				case 3:
					var width_retry = width_cancel = 86,
						left_start = (width - (width_retry + width_cancel + 10 - w_offset)) / 2;
						
					form.add(getButtonRetry({left: left_start, bottom: 10}));
					form.add(getButtonCancel({left: (left_start + width_retry + 10), bottom: 10}));
					break;
				case 4:
					var width_yes = width_no = 86,
						left_start = (width - (width_yes + width_no + 10 - w_offset)) / 2;
						
					form.add(getButtonYes({left: left_start, bottom: 10}));
					form.add(getButtonNo({left: (left_start + width_yes + 10), bottom: 10}));
					break;
				case 5:
					var width_yes = width_no = width_cancel = 86,
						left_start = (width - (width_yes + width_no + width_cancel + 20 - w_offset)) / 2;
					
					form.add(getButtonYes({left: left_start  , bottom: 10}));
					form.add(getButtonNo({left: (left_start + width_yes + 10), bottom: 10}));
					form.add(getButtonCancel({left: (left_start + width_yes + width_no + 20), bottom: 10}));
					break;
			}
			return form;
			created = 1;
		}
	}
	
	function getButtonAbort(p){
		var button = new Button();
		button.setup({
			left: p.left,
			top: p.top,
			bottom: p.bottom,
			right: p.right,
			text: uiLang.abort,
			action: action.abort
		});
		return button;
	}
	
	function getButtonCancel(p){
		var button = new Button();
		button.setup({
			left: p.left,
			top: p.top,
			bottom: p.bottom,
			right: p.right,
			text: uiLang.cancel,
			action: action.cancel
		});
		return button;
	}
	
	function getButtonIgnore(p){
		var button = new Button();
		button.setup({
			left: p.left,
			top: p.top,
			bottom: p.bottom,
			right: p.right,
			width: 120,
			text: uiLang.ignore,
			action: action.ignore
		});
		return button;
	}
	
	function getButtonNo(p){
		var button = new Button();
		button.setup({
			left: p.left,
			top: p.top,
			bottom: p.bottom,
			right: p.right,
			text: uiLang.no,
			action: action.no
		});
		return button;
	}
	
	function getButtonOk(p){
		var button = new Button();
		button.setup({
			left: p.left,
			top: p.top,
			bottom: p.bottom,
			right: p.right,
			text: uiLang.ok,
			action: action.ok
		});
		return button;
	}
	
	function getButtonRetry(p){
		var button = new Button();
		button.setup({
			left: p.left,
			top: p.top,
			bottom: p.bottom,
			right: p.right,
			text: uiLang.retry,
			action: action.retry
		});
		return button;
	}
	
	function getButtonYes(p){
		var button = new Button();
		button.setup({
			left: p.left,
			top: p.top,
			bottom: p.bottom,
			right: p.right,
			text: uiLang.yes,
			action: action.yes
		});
		return button;
	}
	
};



/*
// Пример использования
var msgBox = new MessageBox();

msgBox.setup({
	text: 'Только владелец, имеет право создавать новый альбом.',
	caption: 'Внимание',
	icon: 'information',
	type: 'OK'
});


var myform = new Form();
myform.setup({
	caption: "Моя форма",
	width: 400,
	height: 300,
	icon: false,
	name: 'myform',
	moveable: true
});

var myform_bttOk = new Button();
myform_bttOk.setup({
	left: 0,
	top: 0,
	text: "Ок",
	action: function(){
		msgBox.show();
	}
});

myform.add(myform_bttOk);
myform.show();*/
