﻿var AppUrl = "http://ketabeavval.ir";
//var AppUrl = "http://localhost/ketabeavval";
var searchMenu = new Class({
	options: {
		show: 0,
		static: false,
		searchString:'',
		inDirectory:false,
		
		onShow: function(element, index) {
			element.removeClass('hover');			
			element.addClass('active');
			this.searchform.setStyle('background-color', this.tabs[index].bgcolor);
			this.searchterm.setHTML(this.tabs[index].searchterm);
			this.examples.setHTML(this.tabs[index].examples);
			
			if(index==1){
				if(typeof(Map_Lang)=="undefined"){Map_Lang="fa"}
				if(Map_Lang=="en"){
					this.searchterm.setHTML(this.tabs[index].searchterm2);
					this.examples.setHTML(this.tabs[index].examples2);
					this.submitButton.setStyle('background','url('+AppUrl+'/images/search_btn_en.gif)');
					this.searchInput.setStyle('direction','ltr');
					this.searchInput.removeClass('fa');
					
					//Make Search form LTR
					this.searchterm.setStyle('float','left');
					this.searchterm.setStyle('direction','ltr');
					this.examples.setStyle('direction','ltr');
					$('inputbox').setStyle('float','left');
					$('formbutton').setStyle('float','left');
					//this.submitButton.style.background="url(../images/search_btn.gif) no-repeat 0 0;";
				}else{
				
					//Make Search form RTL
					this.searchterm.setStyle('float','right');
					this.searchterm.setStyle('direction','rtl');
					this.examples.setStyle('direction','rtl');
					$('inputbox').setStyle('float','right');
					$('formbutton').setStyle('float','right');
				
					this.submitButton.setStyle('background','url('+AppUrl+'/images/search_btn.gif)');
				}
			}else{
			
					//Make Search form RTL
					this.searchterm.setStyle('float','right');
					this.searchterm.setStyle('direction','rtl');
					this.examples.setStyle('direction','rtl');
					$('inputbox').setStyle('float','right');
					$('formbutton').setStyle('float','right');
			
				this.submitButton.setStyle('background','url('+AppUrl+'/images/search_btn.gif)');
			}
			this.submitButton.setStyle('background-repeat','no-repeat');
			
			if (index == 0 && this.options.inDirectory) {
				this.examples.empty();
				var radios ='<input id="inDir" name="directory" type="radio" value="inDir"  />'+
							'<label for="inDir">در اینجا</label>&nbsp;&nbsp;'+
							'<input id="inAll" name="directory" type="radio" value="inAll" checked />'+
							'<label for="inAll">در کل</label>&nbsp;&nbsp;';	
				this.directoryRadios.setHTML(radios);
			}
			if(index==0){
				stradios =	'<input checked id="searchtype_all" name="searchtype" type="radio" value="all" />&nbsp;'+
							'<label for="searchtype_all">در کل</label>&nbsp;'+
							'<input id="searchtype_directory" name="searchtype" type="radio" value="directory" />&nbsp;'+
							'<label for="searchtype_directory">نوع شغل</label>&nbsp;'+
							'<input id="searchtype_name" name="searchtype" type="radio" value="name" />&nbsp;'+
							'<label for="searchtype_name">نام واحد شغلی</label>&nbsp;'+
							'<input id="searchtype_code" name="searchtype" type="radio" value="code" />&nbsp;'+
							'<label for="searchtype_code">کد اشتراک</label>&nbsp;&nbsp;';
							
				this.searchterm.setHTML(stradios);
				var chk = this.directoryRadios.innerHTML;
				chk += '<input style="display:none" id="Chk_inAdvers" name="inAdvers" type="checkbox" value="inAdvers" />'+
						'<label  style="display:none" for="Chk_inAdvers">در آگهی ها</label>';
				this.directoryRadios.setHTML(chk);
					
			}
			if (this.tabs[index].advSearchURL !== null || this.tabs[index].helpURL !== null) {
				var advString = this.tabs[index].advSearchURL !== null ? "<li><a href=\""+this.tabs[index].advSearchURL+"\">جستجوی پیشرفته</li>" : '';
				var helpString = this.tabs[index].helpURL !== null ? "<li><a href=\""+this.tabs[index].helpURL+"\">راهنمای جستجو</li>" : '';
				var list = "<ul>"+advString+helpString+"</ul>";
				this.formextras.setHTML(list);	
			}
			if($('searchtype_code')!= null){
				switch(document.getElementById('txtsearchtype') ? $('txtsearchtype').getProperty('value'):''){
					case 'c':
						$('searchtype_code').setProperty('checked',true);
						break;
					case 'n':
						$('searchtype_name').setProperty('checked',true);
						break;
					case 'd':
						$('searchtype_directory').setProperty('checked',true);
						break;
					case 'a':
						$('searchtype_all').setProperty('checked',true);
						break;
					default :
						$('searchtype_all').setProperty('checked',true);
						break;
					}
			}
			if($('txtsearchinadver')!= null){
				if($('txtsearchinadver').getProperty('value')=='1'){
						$('Chk_inAdvers').setProperty('checked',true);
					}
			}
		}
		,
		
		getstype:function(){
			stradios ='<input id="searchtype_code" name="searchtype" type="radio" value="code"  />'+
							'<label for="searchtype_code">کد اشتراک</label>&nbsp;&nbsp;'+
							'<input id="searchtype_name" name="searchtype" type="radio" value="name" />'+
							'<label for="searchtype_name">نام واحد شغلی</label>'+
							'<input id="searchtype_directory" name="searchtype" type="radio" value="directory" />'+
							'<label for="searchtype_directory">نوع شغل</label>'+
							'<input checked id="searchtype_all" name="searchtype" type="radio" value="all" />'+
							'<label for="searchtype_all">در کل</label>';
			return stradios;
		},
		onHide: function(element, index) {
			element.removeClass('active');
			this.formextras.empty();
			this.directoryRadios.empty();
		}
	},
//	directorySearchUrl:'http://www.ketabeavval.ir/DirectorySearch/?q=',
	directorySearchUrl:AppUrl+'/DirectorySearch/?q=',
	
	/*
	Tab Properties
	**************/
	tabs: {
			0:{
				toggler: 'مشاغل تهران',
//				url: 'http://www.ketabeavval.ir/Search/?q=',
				url: AppUrl+'/Search/?q=',
				bgcolor: '#FDECC6',
				searchterm: 'نام واحد شغلی / نوع شغل:',
				examples: 'نمونه: کتاب اول، دکتر فرزام، تاکسی سرویس، ...',
//				advSearchURL: 'http://www.ketabeavval.ir/AdvancedSearch',
				advSearchURL: AppUrl+'/AdvancedSearch',
//				helpURL: 'http://www.ketabeavval.ir/Help/jobsSearchBox.aspx'
				helpURL: null				
				},
			1:{
				toggler: 'معابر تهران',
//				url: 'http://www.ketabeavval.ir/map/?q=',
				url: AppUrl+'/map/?q=',
				url2: AppUrl+'/enmap/?q=',				
				bgcolor: '#DEECFF',				
				searchterm: 'نام معبر یا خیابان:',
				searchterm2:'Pathway name:',
				examples: 'نمونه: خیابان طالقانی، رسالت، زعفرانیه، ...',
				examples2: 'Example: taleghani street, resalat, zaaferaniye, ...',
				advSearchURL: null,
				helpURL: null
				},

			2:{
				toggler: 'بلاگ کتاب اول',
//				url: 'http://blog.avval.ir/?s=',
				url: 'http://blog.avval.ir/?s=',				
				bgcolor: '#EFEFEF',				
				searchterm: 'واژه در بلاگ کتاب اول:',
				examples: null,
				advSearchURL: null,
				helpURL: null
				},
			3:{
				toggler: 'اطلاعات عمومی تهران',
//				url: 'http://www.ketabeavval.ir/SearchInfo/?q=',
				url: AppUrl+'/SearchInfo/?q=',				
				bgcolor: '#E5F9DA',				
				searchterm: 'اماکن تفریحی / خدمات اداری:',
				examples: 'نمونه: تاتر شهر، گواهینامه، صدور شناسنامه، ...',
				advSearchURL: null,
//				helpURL: 'http://www.ketabeavval.ir/Help/uInfoSearchBox.aspx'
				helpURL: null
				},
			4:{
				toggler: 'وب سایتهای فارسی',
//				url: 'http://www.ketabeavval.ir/Links/?c=siteList&q=',
				url: AppUrl+'/Links/?c=siteList&q=',				
				bgcolor: '#FEEEFF',				
				searchterm: 'نام وب سایت / موضوع:',
				examples: 'نمونه: بلاگ کتاب اول، کامپیوتر، ...',
				advSearchURL: null,
				helpURL: null
				}
		   },
	   
	
	/*
	hello, how r u?	:)
	************/
	initialize: function(options) {
		this.setOptions(options);
		["menu","searchform","searchInput","directoryRadios","submitButton","searchterm","examples","formextras"].each(function(el){
			this[el] = $(el);
			}, this);
		this.selected = null;
		this.build();
	},
	
	build: function() {
		this.entries = [];
		this.searchInput.value = document.getElementById('searchString') ? $('searchString').getProperty('value') : '';
//		this.searchInput.value = this.options.searchString;
		this.menu.getElements('li a').each(function(el, index) {
			var li = el.getParent();
			this.entries.push(li);
			li.addEvent('mouseenter', function(){if (!this.hasClass('active')) this.addClass('hover')});
			li.addEvent('mouseleave', function(){this.removeClass('hover')});			

			if(!this.options.static) {
				new Element('a', {
						'class': 'toggle',
						'href':'#',
						events: {click: this.onClick.bindWithEvent(this, index)}
						}).setHTML(this.tabs[index].toggler).injectAfter(el);
			}
			else {
				new Element('a', {
						'class': 'toggle',
						'href':'#',
						events: {click: this.redirect.bindWithEvent(this, index)}
						}).setHTML(this.tabs[index].toggler).injectAfter(el);				
			}
			
		}, this);
		if (this.entries.length) this.select(this.options.show);
		this.submitButton.addEvent('click', this.redirect.bindWithEvent(this).bind(this));
		this.searchInput.addEvent('keyup', this.onEnterPressed.bindWithEvent(this).bind(this));
	},
	
	onClick: function(evt, index) {
		evt.stop();
		this.select(index);
	},
	redirect: function(evt, index){
		evt.stop();
		
		stype='c';
		if(this.selected==0){
			if($('searchtype_name').getProperty('checked')==true)
				stype='n';
				
			if($('searchtype_directory').getProperty('checked')==true)
				stype='d';
				
			if($('searchtype_all').getProperty('checked')==true)
				stype='a';
		
			search_inAdvers='0';
			if($('Chk_inAdvers').getProperty('checked')==true)
				search_inAdvers='1';
		}	
		
		if(index >= 0) {
			if (this.selected != index)	location.href=this.tabs[index].url+this.searchInput.value;
		}
		else {
			if (this.selected == 0 && this.options.inDirectory && $('inDir').getProperty('checked')==true) {
				var dirId = document.getElementById('dirId') ? $('dirId').getProperty('value') : null;
				var actId = document.getElementById('actId') ? $('actId').getProperty('value') : null;
				if (actId == '') actId = null;	
				var redirectTo = this.directorySearchUrl+this.searchInput.value+(dirId !== null?'&c='+dirId:'')+(actId !== null?'&a='+actId:'')+(this.selected==0?'&st='+stype:'');
				location.href = redirectTo;
			}
			else if(this.selected==1)
			{
				/*&if(Map_Lang=="fa"){
						location.href=this.tabs[this.selected].url+this.searchInput.value+'&lng='+$('chlang').Lang;
				}else{
					location.href=this.tabs[this.selected].url2+this.searchInput.value+'&lng='+$('chlang').Lang;
				}*/
				if(this.HaveFarsiChar(this.searchInput.value)){
					location.href=this.tabs[this.selected].url+this.searchInput.value+'&lng=Fa';
				}else{
					location.href=this.tabs[this.selected].url2+this.searchInput.value+'&lng=En';
				}
			}else{
				//
					location.href=this.tabs[this.selected].url+this.searchInput.value+(this.selected==0?'&st='+stype+'&adv='+search_inAdvers:'');
			}
		}
   },
   HaveFarsiChar: function(sText)
	{
		if(Map_Lang=="fa")
			return true;
		return false;
		var FaChars= "آابپتثجچحخدذرزژسشصضطظعغفقکگلمنوهیيك";
		var IsNumber=true;
		var Char;
		 
		for (i = 0; i < sText.length && IsNumber == true; i++) 
		{ 
			Char = sText.charAt(i); 
			if (FaChars.indexOf(Char) > -1) 
			{
     			return true;
			}
		}
		return false;
	},
   onEnterPressed: function(event) {
	    event = new Event(event);
	    if (event.key == 'enter') {
	    
	    	stype='c';
			if(this.selected==0){
				if($('searchtype_name').getProperty('checked')==true)
					stype='n';
					
				if($('searchtype_directory').getProperty('checked')==true)
					stype='d';
					
				if($('searchtype_all').getProperty('checked')==true)
					stype='a';
			
				search_inAdvers='0';
				if($('Chk_inAdvers').getProperty('checked')==true)
					search_inAdvers='1';
			}	
	    
			if (this.selected == 0 && this.options.inDirectory && $('inDir').getProperty('checked')==true) {
				var dirId = document.getElementById('dirId') ? $('dirId').getProperty('value') : null;
				var actId = document.getElementById('actId') ? $('actId').getProperty('value') : null;
				if (actId == '') actId = null;				
				var redirectTo = this.directorySearchUrl+this.searchInput.value+(dirId !== null?'&c='+dirId:'')+(actId !== null?'&a='+actId:'');
				location.href = redirectTo;
			}
			//else location.href=this.tabs[this.selected].url+this.searchInput.value;
				
			
			else if(this.selected==1)
			{		
				/*if(Map_Lang=="fa"){
						location.href=this.tabs[this.selected].url+this.searchInput.value+'&lng='+$('chlang').Lang;
					}else{
						location.href=this.tabs[this.selected].url2+this.searchInput.value+'&lng='+$('chlang').Lang;
					}*/
				if(this.HaveFarsiChar(this.searchInput.value)){
					location.href=this.tabs[this.selected].url+this.searchInput.value+'&lng=Fa';
				}else{
					location.href=this.tabs[this.selected].url2+this.searchInput.value+'&lng=En';
				}
			}else{
				//
					location.href=this.tabs[this.selected].url+this.searchInput.value+(this.selected==0?'&st='+stype+'&adv='+search_inAdvers:'');
			}
		}
   },
	select: function(index) {
		if (this.selected === index || !this.entries[index]) return this;
		var entry = this.entries[index];
		var params = [entry, index];
		if (this.selected !== null) {
			var current = this.entries[this.selected];
			params.concat([entry, this.selected]);
			this.fireEvent('onHide', [current, this.selected]);
		}
		this.fireEvent('onShow', params);
		this.selected = index;
		return this;
	}
});
searchMenu.implement(new Events, new Options);
