SearchAdvanced =Class.create();

SearchAdvanced.prototype = {

    initialize: function( objConfig )
	{
		this.sFieldId		= objConfig.sInputId;
		this.idOpenEl		= objConfig.openEl;
		this.idCloseEl		= objConfig.closeEl;
		this.idOpenRel		= objConfig.openRel;
		this.idCloseRel		= objConfig.closeRel;
		this.advFieldId		= objConfig.advFieldId;
		this.advBlockId		= objConfig.advBlock;
		this.advItemsRel	= objConfig.advItemsRel;
		this.sFormSearchId	= objConfig.formId;
		this.imgFiltersBlockId = objConfig.imgFiltersBlockId;
		this.relImgFilters	= objConfig.imgFilters;
		
		this.sAdvencedIndex = (objConfig.defAdvancedSearchIndex)?objConfig.defAdvancedSearchIndex:1;
		this.aItemsLinks	= new Array();
		this.__sCurrentItem	= objConfig.sCurrentItem;

		
		Event.observe( document,'dom:loaded', this.init.bindAsEventListener(this) );
	},
	
	init: function()
	{
		if( !$(this.sFieldId) ){
			alert('Input field not identify');
		} else {
			this.setCatchers();
		}
	},
	
	setCatchers: function()
	{
		this.setOpenFormCatcher();
		this.setCloseFormCatcher();
		this.setAdvancedItemsRel();

		this.setImgFiltersListActions();

		this.setOpenRelatedSearches();
		this.setCloseRelatedSearches();
		
		this.setAdvancedTermsCatcher();
	},
	
	setImgFiltersListActions: function()
	{
	    var aItemsLinks = $$('a[rel="' + this.relImgFilters + '"]');

	    aItemsLinks.invoke('observe', 'click', this.actionFiltersList.bindAsEventListener(this));
	},
	
	setOpenFormCatcher: function()
	{
		var openEl = $(this.idOpenEl);
		openEl.onclick = function(){
			return false;
		}
		Event.observe(openEl,'click',this.openAdvancedForm.bindAsEventListener(this) );
	},
	
	setCloseFormCatcher: function()
	{
		var closeEl = $(this.idCloseEl);
		closeEl.onclick = function()
		{
			return false;
		}
		Event.observe(closeEl,'click',this.closeAdvancedForm.bindAsEventListener(this) );
	},
	
	setAdvancedItemsRel: function()
	{
		this.aItemsLinks = $(this.advBlockId).getElementsBySelector('a[rel="'+this.advItemsRel+'"]');
		for(var i=0; i< this.aItemsLinks.length; i++ )
		{
			this.aItemsLinks[i].onclick = function()
			{
				return false;
			}
			Event.observe(this.aItemsLinks[i],'click',this.setAdvancedFormItem.bindAsEventListener(this) );
		}
	},

	setOpenRelatedSearches: function()
	{
	    if ($(this.idOpenRel))
	    {
			var openRel = $(this.idOpenRel);
			openRel.onclick = function(){
				return false;
			}
			Event.observe(openRel, 'click', this.openRelatedSearches.bindAsEventListener(this) );
	    }
	},

	setCloseRelatedSearches: function()
	{
	    if ($(this.idOpenRel))
	    {
			var closeRel = $(this.idCloseRel);
			closeRel.onclick = function()
			{
				return false;
			}
			Event.observe(closeRel, 'click', this.closeRelatedSearches.bindAsEventListener(this));
	    }
	},

	openAdvancedForm: function(objEvent)
	{
		$(this.idOpenEl).hide();
		$(this.advBlockId).show();
	},

	closeAdvancedForm: function(objEvent)
	{
		$(this.idOpenEl).show();
		$(this.advBlockId).hide();
		$(this.advFieldId).value = 0;
	},

	actionFiltersList: function(objEvent)
	{
	    var el = Event.element(objEvent);

		if($(el.id + '_list'))
		{
		    if($(el.id + '_list').getStyle('display') == 'none')
		    {
		        $(el.id + '_list').show();
		    }
		    else
		    {
		        $(el.id + '_list').hide();
		    }
		}
	},
	
	setAdvancedFormItem: function(objEvent)
	{
		if (Object.isElement(objEvent)) {
			var objHref = $(objEvent);
		}
		else {
			var objHref = Event.findElement(objEvent, 'a');
		}	
		if( !objHref){
			return false;
		}
		
		var sHrefName = objHref.name;
		objHref.hide();
		$(sHrefName+'_span').show();
		switch( sHrefName )
		{
			case 'adv_site_domain':
				this.sAdvencedIndex	= '2';
			break;

			case 'adv_country':
				this.sAdvencedIndex	= '3';
			break;

			case 'adv_lang':
				this.sAdvencedIndex='4';
			break;
			
			case 'adv_dimensions':
				this.sAdvencedIndex='5';
			break;

			case 'adv_search_terms':
			default:
				this.sAdvencedIndex='1';
			break;
			
		}
		
		for(var i=0;i<this.aItemsLinks.length; i++ )
		{
			if(this.aItemsLinks[i]!=objHref && this.aItemsLinks[i].style.display=='none'){
				var tmpName  = this.aItemsLinks[i].name;
				
				this.aItemsLinks[i].show();
				$(tmpName+'_span').hide();
				$(tmpName+'_block').hide();
			}
		}
		$(sHrefName+'_block').show();
		this.__setCurrentItem(sHrefName);
	},

	openRelatedSearches: function(objEvent)
	{
		$('RelatedSearchesPrev').hide();
		$('RelatedSearchesAll').show();
	},

	closeRelatedSearches: function(objEvent)
	{
		$('RelatedSearchesAll').hide();
		$('RelatedSearchesPrev').show();
	},

	setAdvancedTermsCatcher: function()
	{
		if (typeof CurrentTypeEngine != "undefined") 
		{
			
			if(CurrentTypeEngine == 'yahoo')
			{
				this.setAdvancedTermsCatcherYahoo();
			}
			else
			{
				this.setAdvancedTermsCatcherLive();
			}
		}
		else
		{	
			this.setAdvancedTermsCatcherLive();
		}	
	},
	
	setAdvancedTermsCatcherLive:function()
	{
		Event.observe($('adv_search_terms_button'), 'click', this.__setAdvancedTerms.bindAsEventListener(this));
		Event.observe($('adv_site_domain_button'), 'click', this.__setSiteDomainTerms.bindAsEventListener(this));
		Event.observe($('adv_country_button'), 'click', this.__setCountryTerms.bindAsEventListener(this));
		Event.observe($('adv_lang_button'), 'click', this.__setLanguageTerms.bindAsEventListener(this));
		Event.observe($('adv_dimensions_button'), 'click', this.__setDimensionsTerms.bindAsEventListener(this));
	},
	
	setAdvancedTermsCatcherYahoo:function()
	{
		Event.observe($('adv_search_terms_button'), 'click', this.__setAdvancedTerms.bindAsEventListener(this));
		Event.observe($('adv_site_domain_button'), 'click', this.__setSiteDomainTermsYahoo.bindAsEventListener(this));
		Event.observe($('adv_country_button'), 'click', this.__setCountryTermsYahoo.bindAsEventListener(this));
		Event.observe($('adv_lang_button'), 'click', this.__setLanguageTermsYahoo.bindAsEventListener(this));
		Event.observe($('adv_dimensions_button'), 'click', this.__setDimensionsTermsYahoo.bindAsEventListener(this));
	},
	clearAdvancedTermsCatcher:function()
	{
		[$('adv_search_terms_button'),
		 $('adv_site_domain_button'),
		 $('adv_country_button'),
		 $('adv_lang_button'),
		 $('adv_dimensions_button')].each(function(el){el.stopObserving('click');});
	},
	__setAdvancedTerms: function( objEvent )
	{
		var b			= $(this.sFieldId);
		var f			= $('adv_search_sel');
		var e			= $('adv_search_input');
		
		if(b&&f&&e){
			if(b.value!=0)
				b.value+=" ";
		
			if(f.value=="phrase")
				b.value+='"'+e.value+'"';
			else if(f.value=="links")
				b.value+="links:("+e.value+")";
			else if(f.value=="title")
				b.value+="title:("+e.value+")";
			else if(f.value=="all")
				b.value+=e.value;
			else if(f.value=="none")
				b.value+="-("+e.value+")";
			else{
				var c=e.value.split(" "),d="";
				b.value+="(";
				for(var a=0;a<c.length;a++){
					if(a>0)
						if(d!="")
							b.value+=" "+c[a];
						else 
							b.value+=" OR "+c[a];
					else 
						b.value+=c[a];
					if(d!=""){
						if(c[a].substr(c[a].length-1)==d)
							d=""
					}else if(c[a].indexOf("'")==0||c[a].indexOf('"')==0){
						if(c[a].indexOf("'")==0)
							d="'";
						else d='"';
					
						var h=false;
						for(var g=a;g<c.length;g++)
							if(c[g].substr(c[g].length-1)==d){
								h=true;break
							}
							if(!h||g==a)d=""
					}//end else if(c[a].indexOf("'")==0||c[a].indexOf('"')==0)
					}//end for(var a=0;a<c.length;a++)
					
					b.value+=")"
				}//end else (str #196)
				
				e.value="";
				this.__submitForm();
			}//end if(b&&f&&e){
	},
	
	
	__setSiteDomainTerms: function(objEvent)
	{
		var a=this.__getCheckBoxValue("site_domain");
		if(a){
			if(a=="0"){
				this.addTerms("adv_site_domain_input","-site:");
			}
			else{ 
				this.appendWithOr("adv_site_domain_input","site:")
			}
		}
		
	},
	__setSiteDomainTermsYahoo:function(objEvent)
	{
		this.YahooAddVars('adv_site_domain_input', 'yahoo_search_domain');
	},
	__setCountryTerms: function(objEvent)
	{
		this.appendWithOr('adv_country_input','loc:');	
	},
	__setCountryTermsYahoo:function(objEvent)
	{
		this.YahooAddVars('adv_country_input', 'yahoo_search_country');
	},
	__setLanguageTerms:function(objEvent)
	{
		this.appendWithOr('adv_lang_input','language:');
	},
	__setLanguageTermsYahoo:function(objEvent)
	{
		this.YahooAddVars('adv_lang_input', 'yahoo_search_language');
	},
    __setDimensionsTerms: function(objEvent)
	{
		this.appendWithOr('adv_dimensions_input','dimensions:');	
	},
	__setDimensionsTermsYahoo:function(objEvent)
	{
		this.YahooAddVars('adv_dimensions_input', 'yahoo_search_dimension');
	},
	
	YahooAddVars:function(source, destination)
	{
		if($(destination))
		{
			$(destination).value = $(source).getValue(); 
		}
		this.__submitForm();
	},
	
	/**
	 * sTerms
	 */
	 //c(d,c)
	addTerms: function(d,c)
	{
		var b= $(this.sFieldId);
		var a= $(d);
		if(b&&a){
			if(b.value.length!=0)
				b.value+=" ";
			
			b.value+=c+a.value;
			if(a.type=="text")
				a.value="";
		this.__submitForm();
		}
	},
	
	appendWithOr: function(h,e)
	{
		var f= $(this.sFieldId);
		var d= $(h);
		
		if(f&&d)
		{
			var b=f.value,a=b.indexOf(e);
			while(a>=0){
				if(a>0&&b.substr(a-1,1)=="-"){
					a=b.indexOf(e,a+1);
					continue
				}
				
				if(a>0&&b.substr(a-1,1)=="("){
					f.value=b.substr(0,a)+e+d.value+" OR "+b.substr(a);
					if(d.type=="text")
						d.value="";return
				}
				
			var g=b.indexOf(" ",a);
			if(g>=0)
				f.value=b.substr(0,a)+"("+e+d.value+" OR "+b.substr(a,g)+")"+b.substr(g);
			else 
				f.value=b.substr(0,a)+"("+e+d.value+" OR "+b.substr(a)+")";
				
			if(d.type=="text")
				d.value="";
			return
			
			}
			this.addTerms(h,e);
		}
		
	},
	
	//h()
	__getCheckBoxValue:function(chName){
		var a=document.getElementsByName(chName);
		if(!a)return null;
		for(var b=0;b<a.length;b++)
			if(a[b].checked)
				return a[b].value;
			return null
	},
	
	__submitForm:function()
	{
			var b=document.getElementById(this.sFormSearchId);
			$(this.advFieldId).value = this.sAdvencedIndex;
			b.submit();
		/*
		$(this.sFormSearchId).request({
		  onComplete: function(){ 
		  	
		  }
		})
		*/ 		
	},
	
	__setCurrentItem: function(name)
	{
		this.sCurrentItem = name;
	},
	
	__getCurrentItem:function()
	{
		return this.__sCurrentItem;
	}
	
}

function in_array(mixed_value, hash)
{
	for(var i=0;i<hash.length;i++){	
		if(hash[i]==mixed_value){
			return true;
		}	
	}
	return false;
}