function X200_TAB(name,func,del_dflt){
	this.name=name;
	this.tab='#'+name+' ul';
	this.delDefault=(X200_TAB.arguments.length>2 && del_dflt);
	this.running=0;
	this.atabs=new Array();
	this.atabs[0]='0';

	_obj={ fx: { opacity: 'toggle', duration: 'fast' }, cache: true, spinner: Drupal.t('loading') }
	if(X200_TAB.arguments.length>1 && func) _obj.show=func;
	_obj.load=x200_tabs_loaded;
	$(this.tab).tabs(_obj);
	
	if(typeof X200_TAB._initialized=='undefined'){
		X200_TAB.prototype.add=function(href,tabname,id){
			_t=this._getTabForId(id);
			if(_t>-1){
				$(this.tab).tabs('select',_t);
				return;
			}
			this.running++;
			_l=this.atabs.length;
			this.atabs[_l]=id;
//			this.aobjs[_l]=new Object;
			$(this.tab).tabs('add',href, tabname+'<div class="x200_tabs_sup" onclick="return '+this.name+'_js.remove('+id+')">x</div>');
			$(this.tab).tabs('select',_l);
		}
		X200_TAB.prototype.addauto=function(addr){
			_t=this.running+1;
			this.add(addr+_t,_t+1,_t);
			return _t;
		}
		X200_TAB.prototype.remove=function(id){
			_t=this._getTabForId(id);
			if(_t<0) return;
			x200_srch_obj.remove(_t);
			this.atabs.splice(_t,1);
			$(this.tab).tabs('remove',_t);
			if(this.delDefault) x200_module_delDefault(id);
			return false;
		}
		X200_TAB.prototype.selected=function(){
			return $(this.tab).data('selected.tabs');
		}
		X200_TAB.prototype.selectedId=function(){
			return this.atabs[this.selected()];
		}
		X200_TAB.prototype._getTabForId=function(id){
			for(_i=0;_i<this.atabs.length;_i++){
				if(this.atabs[_i]==id) return _i;
			}
			return -1;
		}
	}
	X200_TAB.__initialized=true;
}
function x200_tabs_loaded(){
	$(".x200_CLS_corner").corner("5px");
}
function x200_tabs_initialized(){
	return !(typeof x200_tabs_list1_js=='undefined');
}
function x200_tabs_setup(){
	if(Drupal.settings.prefs['lst_view']!=0) return;
	x200_tabs_list0_js=new X200_TAB('x200_tabs_list0',function(){if(!x200_tabs_initialized())return; x200_module_showDefault(x200_tabs_list0_js.selectedId())},true);
	x200_tabs_list1_js=new X200_TAB('x200_tabs_list1',function(){if(!x200_tabs_initialized())return; /*x200_module_refreshTab(x200_tabs_list1_js.selected()); */if(!x200_tabs_list1_js.selected()) x200_module_refreshDefault(x200_tabs_list0_js.selectedId())});

	x200_module_addDefault(0,1);
	x200_module_setDefault(0);
	x200_module_showDefault(0);
}
function x200_tabs_openOffer(href) {
	x200_srch_openOffer(href)
}

function X200_SOBJ(){
	this.aobjs=new Array();
	this.aobjs[0]=null;
	this.new_search=0;
	this.switching=0;
	
	if(typeof X200_SOBJ._initialized=='undefined'){
		X200_SOBJ.prototype.set=function(t,obj){
			this.aobjs[t]=obj;
		}
		X200_SOBJ.prototype.setCurrent=function(obj){
			t = (Drupal.settings.prefs['lst_view']==0?x200_tabs_list0_js.selected():0);
			this.aobjs[t]=obj;
		}
		X200_SOBJ.prototype.get=function(t){
			return this.aobjs[t];
		}
		X200_SOBJ.prototype.getCurrent=function(){
			t = (Drupal.settings.prefs['lst_view']==0?x200_tabs_list0_js.selected():0);
			return this.aobjs[t];
		}

		X200_SOBJ.prototype.remove=function(t){
			this.aobjs.splice(t,1);
		}
	}
	X200_SOBJ.__initialized=true;
}

x200_srch_init_obj=new Object;
$(function() {
	x200_srch_obj = new X200_SOBJ();
	x200_srch_obj.set(0,x200_srch_init_obj);
	x200_srch_set_offer_handler(this);
	x200_tabs_setup();	
	$('#x200_LST_prf_dtl ul').tabs({ fx: { opacity: 'toggle', duration: 'fast' }, cache: true, cookie: { name:'prefs', expires: 30 } });
})

function x200_srch_popupSearch(new_search) {
	if(Drupal.settings.prefs['lst_view']!=0) new_search=0;
	buttons = new Object;
	buttons[Drupal.t('Search')]=1;
	buttons[Drupal.t('Cancel')]=0;
	obj=new Object;
	if(!new_search) obj=x200_srch_obj.getCurrent();
	else x200_srch_obj.new_search=new_search;
	if(obj['order']) x200_srch_obj['order']=obj['order'];
	$.prompt(x200_cmn_imp_get_close_panel()+'<div id="x200_srch_popup" class="x200_srch_popup"><div class=\"x200_CLS_wait\">'+Drupal.t('Please wait')+'</div></div>',{ buttons:buttons, prefix:'x200_imp', show:'fadeIn', promptspeed: 'slow', opacity: 0.85, top: '10%', submit: x200_srch_prompt, zIndex: 99999});
	$("#x200_srch_popup").load(x200_cmn_get_app_path()+'x200/offers/search',obj,x200_srch_initSearch);
}
function x200_srch_prompt(v,m){
	new_search=0;
	if(Drupal.settings.prefs['lst_view']==0) new_search=x200_srch_obj.new_search;
	x200_srch_obj.new_search=0;
	if(!v) return true;
	if(new_search) x200_tabs_list0_js.addauto('#x200_tabs_srch');
	if(x200_srch_prompt.arguments.length>2 && x200_srch_prompt.arguments[2]) obj=x200_srch_prompt.arguments[2];
	else {
		obj=x200_srch_getValues();
		if(x200_srch_obj['order']) obj['order']=x200_srch_obj['order'];
	}
	x200_srch_obj.setCurrent(obj);
	if(Drupal.settings.prefs['lst_view']==0){
		t=x200_tabs_list0_js.selectedId();
		x200_module_addDefault(t);
		x200_module_showDefault(t);
	}
	x200_srch_handler();
	return true;
}
function x200_srch_object(obj){
	x200_srch_prompt(true,null,obj);
}
function x200_srch_initSearch(){
	Drupal.behaviors.autocomplete(document.getElementById('x200_frm_srch'));
	if(x200_srch_obj.new_search) return;
	x200_srch_setValues(x200_srch_obj.getCurrent());
}
function x200_srch_handler(){
	switching=x200_srch_obj.switching;
	x200_srch_obj.switching=0;
	t=(Drupal.settings.prefs['lst_view']==0 && !switching?x200_tabs_list0_js.selectedId():0);
	obj=switching?x200_srch_obj.get(0):x200_srch_obj.getCurrent();
	page=x200_srch_handler.arguments.length>0?x200_srch_handler.arguments[0]:1;
	obj['page']=(page>1?page:1);
	obj['tab']=t;
	obj['list_view']=Drupal.settings.prefs['lst_view'];
	if(obj['order']=='undefined') delete obj['order'];
	if(!(typeof Drupal.settings.user_id=='undefined')) obj['id_user']=Drupal.settings.user_id;
	landiv='#x200_LST_SRCH';
	lanfun='listing_main';
	if(obj['list_view']==0 && !switching){
		landiv='#x200_tabs_srch'+t;
		lanfun='listing_panel';
	}
	$(landiv).html('<div class=\"x200_CLS_wait\">'+Drupal.t('Please wait')+'</div>');
	$(landiv).load(x200_cmn_get_app_path()+'x200/offers/'+lanfun,obj,function(){
		x200_srch_set_offer_handler(this);
		x200_cmn_make_corner();
		if(Drupal.settings.prefs['lst_view']==0) {
			x200_srch_set_offer_handler(this);
			if(switching) x200_tabs_setup();
			else x200_module_setDefault(t);
		}
	});
}
function x200_srch_set_offer_handler(scope){
	$('a.x200_viewtab',scope).click(function(){
		x200_tabs_list1_js.add(this.href, this.getAttribute('tabname'), this.getAttribute('tabid'));
		return false;
	});
	$('a.x200_viewoff',scope).click(function(){
		x200_srch_openOffer(this.href);
		return false;
	});
}
function x200_srch_sort(srt){
	obj=x200_srch_obj.getCurrent();
	obj['order']=srt;
	x200_srch_obj.setCurrent(obj);
	x200_srch_handler();
}
function x200_srch_exc(fld){
	if(!fld) return;
	obj=x200_srch_obj.getCurrent();
	delete obj[fld];
	x200_srch_obj.setCurrent(obj);
	x200_srch_handler();
}
function x200_srch_inc(fld){
	if(!fld) return;
	obj=x200_srch_obj.getCurrent();
	val=obj[fld];
	obj2=new Object;
	obj2[fld]=val;
	if(obj['order']) obj2['order']=obj['order'];
	x200_srch_obj.setCurrent(obj2);
	x200_srch_handler();
}
function x200_srch_tabbed(id,ttl){
	if(Drupal.settings.prefs['lst_view']!=0) {
		alert(Drupal.t('Change view to TABS first'));
		return;
	}
	x200_tabs_list1_js.add('/?idoffertab='+id,ttl,id);
}