var Search = (function(){
	var module, from, resultsBox, buttonsBox, paramsBox, filterForm, menuBox, pagesBox, start, offset, urlparams = [];
	var act, act_type, act_id, act_forms_id;
	var frmSearch, msgBox, ajax = new newAjax();
	
	function initialization(){
		
		frmSearch = new Form();
		msgBox = new MessageBox();
		
		module = jWps('#srh_module')[0].value;
		from = jWps('#srh_from')[0].value;
		resultsBox = jWps('#srh_results');
		buttonsBox = jWps('#srh_buttons');
		filtersBox = jWps('#srh_filters');
		filterForm = jWps('#filterForm')[0];
		menuBox = jWps('.srh_menu');
		pagesBox = jWps('.srh_pages');
		start = jWps('#srh_start')[0];
		offset = jWps('#srh_offset')[0];
	}
	
	ajax.setup({
		onStart: function(){
			
		},
		onComplete: function(data, text){
			if(data.error > 0){
				msgBox.setup({
					caption: srhLang['error'],
					text: data.error_msg,
					icon: 'error',
					type: 'OK'
				});
				msgBox.show();
			}else{
				if(act == 'groupInvite' || act == 'groupDel' || act == 'groupBan' || act == 'groupExit' || act == 'groupEnter' || act == 'userAddFriend' || act == 'userDelFriend' || act == 'man' || act == 'woman'){
					var msg = jWps('#msg' + act_id);
					msg.html(data.msg);
					msg[0].style.display = 'block';
				}
				if(act == 'getParams'){
					if(act_type == 'forms'){
						if(data.html){
							jWps('#formsparams').html(data.html);
						}
					}
				}
				if(act == 'userMessage'){
					frmSearch.hide();
					msgBox.setup({
						width: 350,
						caption: 'Message sended',
						text: srhLang['messagesending'],
						icon: 'information',
						type: 'OK'
					});
					msgBox.show();
				}
			}
		},
		onError: function(text){
			alert(srhLang['errorconnecting']);
		}
	});
	
	var filter = {
		'toggle': function(e){
			jWps(e).parent().toggleClass('on','off');
		},
		'submit': function(){
			filterForm.submit();
		},
		'start': function(st){
			start.value = st;
			filter.submit();
		},
		'clearFormsParams': function(){
			jWps('#formsparams').html('');
		},
		'getParams': function(type, id){
			act = 'getParams';
			act_type = type;
			
			if(act_type == 'forms'){
				if(act_forms_id === undefined && from == 'account') act_forms_id = jWps('#sformsid')[0].value;
				if(act_forms_id == id) return;
				if(!id){
					jWps('#formsparams').html('');
					return;
				}else{
					act_forms_id = id;
					ajax.post({'module':module, 'act':act, 'from':from, 'type':type, 'id':id});
				}
			}
		},
		'clearEventDay': function(){
			jWps('[name="sday"]')[0].value = '';
		}
	}
	
	var group = {
		'del': function(gid, uid){
			act = 'groupDel';
			act_id = uid;
			ajax.post({'module':module, 'act':act, 'from':from, 'gid':gid, 'uid':uid});
		},
		'ban': function(gid, uid){
			act = 'groupBan';
			act_id = uid;
			ajax.post({'module':module, 'act':act, 'from':from, 'gid':gid, 'uid':uid});
		}
	}
	
	var user = {
		'man': function(uid){
			act_id = uid;
			act = 'man';
			ajax.post({'module':module, 'act':'man', 'uid':uid});
		},
		'woman': function(uid){
			act_id = uid;
			act = 'woman';
			ajax.post({'module':module, 'act':'woman', 'uid':uid});
		},
		'message': function(uid, fio){
			frmSearch.setup({
				width: 400,
				caption: srhLang['message'],
				name: 'frmGallery',
				minButton: false,
				maxButton: false
			});
			frmSearch.show();
			frmSearch.body('<div style="padding: 15px 10px;"><span style="color: #666">To:</span> <a href="id'+uid+'" target="blank">'+fio+'</a></div><div style="padding: 0 10px;"><span style="color: #666">'+srhLang['message']+':</span><br /><textarea id="msgtext" style="width: 100%; height: 100px;"></textarea></div><div style="height: 70px;"><a class="msg_history" href="index.php?module=account&act=messages&pm='+uid+'">'+srhLang['messaghistory']+'</a></div>');
			
			var btnSend = new Button();
			var btnCancel = new Button();
			
			btnSend.setup({
				text: srhLang.send,
				bottom: 15,
				left: 160,
				action: function(){
					act = 'userMessage';
					var msgtext = jWps('#msgtext', frmSearch.getObject())[0].value;
					ajax.post({'module':module, 'from':from, 'act':act, 'text':msgtext, 'uid':uid});
				}
			});
			btnCancel.setup({
				text: srhLang.cancel,
				bottom: 15,
				left: 256,
				action: function(){ frmSearch.hide(); }
			});
			frmSearch.add(btnSend);
			frmSearch.add(btnCancel);
		},
		'addfriend': function(uid){
			act = 'userAddFriend';
			act_id = uid;
			ajax.post({'module':module, 'from':from, 'act':act, 'uid':uid});
		},
		'delfriend': function(uid){
			act = 'userDelFriend';
			act_id = uid;
			ajax.post({'module':module, 'from':from, 'act':act, 'uid':uid});
		}
	}
	
	return{
		'init': function(){
			initialization();
		},
		'filter':{
			'toggle': function(e){
				filter.toggle(e);
			},
			'submit': function(){
				filter.submit();
			},
			'start': function(st){
				filter.start(st);
			},
			'getParams': function(type, id){
				filter.getParams(type, id);
			},
			'clearFormsParams': function(){
				filter.clearFormsParams();
			},
			'clearEventDay': function(){
				filter.clearEventDay();
			},
			'getCatForum': function(e){
				var cat = jWps('#scat')[0];
				if(cat.length > 1){
					for(var i=0; i < cat.length; i++){
						if(cat[i].selected == true){
							cat = cat[i].value;
							break;
						}
					}
				}
				if(cat > 0){
					ajax.post({'module':'search', 'act':'getCatForum', 'cat':cat}, function(data){
						jWps('#search_select_forum').html(data.html);
					});
				}else{
					jWps('#search_select_forum').html('');
				}
			},
			'offset': function(value){
				offset.value = value;
				filter.submit();
				Search.url.addparam('offset', 2);
			}
		},
		'update': function(data){
			if(data.items){
				var html = '';
				if(typeof data.items === 'object'){
					for(var i=0; i<data.items.length; i++){
						html += jWps.tpl_compare(data.tpl, data.items[i]);
					}
				}else{
					html = jWps.htmlspecialchars_decode(data.items);
				}
				resultsBox.html(html);
				menuBox.html(data.menu);
				pagesBox.html( jWps.htmlspecialchars_decode(data.pages) );
			}
		},
		'group':{
			'invite': function(gid, uid){
				ajax.post({'module':'community', 'act':'invite', 'gid':gid, 'uid':uid}, function(data){
					if(data.message){
						jWps('#msg' + uid).html(data.message).show();
					}
				});
			},
			'del': function(gid, uid){
				group.del(gid, uid);
			},
			'ban': function(gid, uid){
				group.ban(gid, uid);
			},
			'exit': function(id, e){
				msgBox.setup({
					width: 400,
					caption: srhLang[(from == 'groups')?'msgGroupExitCaption':'msgEventExitCaption'],
					text: srhLang[(from == 'groups')?'msgGroupExitText':'msgEventExitText'],
					icon: 'question',
					type: 'YESNO',
					action:{
						no: function(){ msgBox.hide(); },
						yes: function(){
							ajax.post({'module':'community', 'act':'exit', 'id':id}, function(data){
								if(data.message){
									jWps('#msg' + id).html(data.message).show();
									e.innerHTML = data.enter;
									e.onclick = function(){ Search.group.enter(id, this); }
								}
							});
							msgBox.hide();
						}
					}
				});
				msgBox.show();
			},
			'enter': function(id, e){
				ajax.post({'module':'community', 'act':'enter', 'id':id}, function(data){
					if(data.message){
						jWps('#msg' + id).html(data.message).show();
						e.innerHTML = data.exit;
						e.onclick = function(){ Search.group.exit(id, this); }
					}
				});
			}
		},
		'user':{
			'man': function(uid){
				user.man(uid);
			},
			'woman': function(uid){
				user.woman(uid);
			},
			'message':function(id){
				var ajax = new Ajax();
				ajax.post({'module':'account','act':'message','id':id}, function(data){
					if(data.status == 0){
						var msgBox = new MessageBox();
						msgBox.setup({
							width: 400,
							caption: data.caption,
							text: data.text,
							icon: 'information',
							type: 'OK'
						});
						msgBox.show();
					}else if(data.status == 1){
						var msgForm = new Form();
						msgForm.setup({
							width: 400,
							caption: data.caption,
							name: 'msgForm',
							body: data.body,
							minButton: false,
							maxButton: false
						});
						msgForm.show();
						jWps('#btnmsgsend').bind('click', function(){
							var msgtext = jWps('#messagetext');
							if(msgtext[0].value == msgtext[0].placeholder || msgtext[0].value == ''){
								return;
							}else{
								ajax.post({'module':'account','act':'message','id':id,'text':msgtext[0].value,'accept':1}, function(data){
									msgForm.hide();
									if(data.caption && data.text){
										var msgBox = new MessageBox();
										msgBox.setup({
											width: 400,
											caption: data.caption,
											text: data.text,
											icon: 'information',
											type: 'OK'
										});
										msgBox.show();
									}
								});
							}
						});
						jWps('#btnmsgcancel').bind('click', function(){
							msgForm.hide();
						});
					}			
				});
			},
			'friend': function(id, e){
				var ajax = new Ajax();
				ajax.post({'module':'account', 'act':'friend', 'id':id}, function(data){
					if(data.caption && data.text){
						var msgBox = new MessageBox();
						msgBox.setup({
							width: 350,
							caption: data.caption,
							text: data.text,
							icon: 'question',
							type: 'YESNO',
							action:{
								no: function(){ msgBox.hide(); },
								yes: function(){
									ajax.post({'module':'account', 'act':'friend', 'id':id, 'accept':1}, function(data){
										msgBox.hide();
										if(data.caption && data.text){
											msgBox.setup({
												caption: data.caption,
												text: data.text,
												icon: 'information',
												type: 'OK'
											});
											msgBox.show();
											if(e) e.innerHTML = data.title;
										}
									});
								}
							}
						});
						msgBox.show();
					}
				});
			}
		},
		'url':{
			'addparam': function(param, type){
				var value = '';
				if(type == 0){ // radio
					value = jWps('[name="'+param+'"]');
					if(value.length == 1){
						value = value[0].value;
					}else{
						for(var i=0; i<value.length; i++){
							if(value[i].checked){
								value = value[i].value;
								break;
							}
						}
					}
				}
				if(type == 1){ // select
					var select = jWps('[name="'+param+'"]')[0];
					if(select.length > 1){
						for(var i=0; i < select.length; i++){
							if(select[i].selected == true){
								value = select[i].value;
							}
						}
					}
				}
				if(type == 2){ // text, textarea
					value = jWps('[name="'+param+'"]')[0].value;
				}
				if(type == 3){ // checkbox
					if(jWps('#'+param+'')[0] === undefined){
						var elem = jWps('[name="'+param+'"]')[0];
						if(elem.checked){ value = elem.value; }
					}else{
						value = jWps('#'+param+'')[0].checked;
						if(value) value = 1;
						else value = 0;
					}
				}
				
				// ”îðìèðóåì ìàññèâ ñóùåñòâóþùèõ URL hash ïàðàìåòðîâ
				var hash = location.hash;
				hash = hash.split('#')[1];
				if(hash){
					hash = hash.split('&');
					for(var i=0; i<hash.length; i++){
						urlparams[i] = ['param','value'];
						var pv = hash[i].split('=');
						urlparams[i]['param'] = pv[0];
						urlparams[i]['value'] = pv[1];
					}
				}
			
				var notparam = 1, newhash = '';
				for(var i=0; i<urlparams.length; i++){
					if(urlparams[i]['param'] == param){
						urlparams[i]['value'] = value;
						notparam = 0;
						break;
					}
				}
				if(notparam == 1){
					urlparams[i] = ['param','value'];
					urlparams[i]['param'] = param;
					urlparams[i]['value'] = value;
				}
				for(var i=0; i<urlparams.length; i++){
					if(newhash == ""){
						newhash = urlparams[i]["param"] + "=" + urlparams[i]["value"];
					}else{
						newhash += "&" + urlparams[i]["param"] + "=" + urlparams[i]["value"];
					}			
				}
				location.hash = newhash;
			}
		}
	}
})();
