// set right domain
(function() {
  var a = document.domain.match(/\.?(gaygirls|gay)\.(nl|eu)/);
  if(a != null)document.domain = a[1] + "." + a[2]
})();

var GAYSEARCH = new Class({
	
	mbFirstOpen: true,
	mbSearchType: 'region',
	maAge : null,
	
	initialize : function() {
		$('searchBoxContent').set('tween', {duration: 'fast'});
		$('searchBoxContent').fade('out');
		var o_this = this;
		
		this.moAge = new Slider($('ageSlider'), $('minKnob'), $('sliderBackground'), {
				start: 16, 
				end: 99,
				knobheight: 20,
				onChange: function(pos){
					$('ageSliderLabelLeft').set('html', pos.minpos + ' ' + $('searchYearText').get('text'));
					$('ageSliderLabelRight').set('html', pos.maxpos + ' ' + $('searchYearText').get('text'));
					this.maAge = pos;
				},
				onComplete: (function(pos){
					this.maAge = pos;
				}).bind(this)
			}, 
			$('maxKnob')
		).setMin(18).setMax(35);
		
		// adjust ages
		if ($('searchMinAge') && $('searchMaxAge')){
			this.moAge.setMin(parseInt($('searchMinAge').get('text'), 10));
			this.moAge.setMax(parseInt($('searchMaxAge').get('text'), 10));
		}
		
		$('searchCity').addEvent('click', function(){
			o_this.mbSearchType = 'city';
			$('searchCityInput').removeClass('hidden');
			$('searchStateInput').addClass('hidden');
		});
		
		$('searchState').addEvent('click', function(oEvent){
			o_this.mbSearchType = 'region';
			$('searchCityInput').addClass('hidden');
			$('searchStateInput').removeClass('hidden');
		});
		
		this.moComplete = new SimpleComplete($('searchCityInput'), {
			'url': '/ajax/search_cities_country',
			'fieldName': 'cityname'
		});
		
		$('searchCountryInput').addEvent('change', (this.initRegions).bind(o_this));
		
		$('searchBtn').addEvent('click', (this.submit).bind(o_this));
	},
	
	toggle : function() {
		if($('searchBoxFull').getStyle('height') == '0px') {
			$('searchBoxContent').set('tween', {duration: 'normal'});
			$('searchBoxContent').fade('in');
			$('searchBoxFull').tween('height', '176px');
			if (this.mbFirstOpen === true){
				this.mbFirstOpen = false;
				this.initCountries();
			}
		} else {
			$('searchBoxContent').set('tween', {duration: 'fast'});
			$('searchBoxContent').fade('out');
			$('searchBoxFull').tween('height', '0px');
		}
	},
	
	initRegions : function(){
		try{
			var s_country = $$('div#searchCountry select')[0].get('value');
		} catch (e){
			return false;
		}
		
		if (s_country.length === 0){
			return;
		}
		
		this.moComplete.SetExtraData({
			'country': s_country
		});
		
		var o_req = new Request.JSON({
			'url': '/ajax/get_regions',
			'method': 'post',
			'data': $H({
				'country': s_country
			}).toQueryString(),
			'onComplete': function(oResponse){
				$('searchStateInput').empty();
				
				if ($('searchRegionText')){
					var s_text = $('searchRegionText').get('text') + ' ...';
					var o_option = new Element('option', {
						'value': '',
						'label': s_text
					});
					o_option.set('text', s_text);
					o_option.inject($('searchStateInput'));
				}
				
				oResponse.each(function(oEl){
					var o_option = new Element('option', {
						'value': oEl.regionid,
						'label': oEl.region
					});
					o_option.set('text', oEl.region);
					o_option.inject($('searchStateInput'));
				});
			}
		});
		o_req.send();
	},
	
	initCountries: function(){
		try{
			var o_countries = $$('div#searchCountry select')[0];
		} catch (e){
			return false;
		}
		
		(new Request.JSON({
			'url': '/ajax/get_countries',
			'method': 'post',
			'data': $H({
				'escape': 'html'
			}).toQueryString(),
			'onComplete': (function(oResponse){
				if ($type(oResponse.each) === false){
					oResponse = $H(oResponse);
				}
				
				var s_selected = o_countries.get('value');
				o_countries.empty();
				
				if ($('searchCountryText')){
					var s_text = $('searchCountryText').get('text') + ' ...';
					var o_option = new Element('option', {
						'value': '',
						'label': s_text
					});
					o_option.set('text', s_text);
					o_option.inject(o_countries);
				}
				
				oResponse.each(function(oEl){
					var o_option = new Element('option', {
						'value': oEl.shortcode,
						'label': oEl.title
					});
					o_option.set('text', oEl.title);
					if (oEl.shortcode == s_selected){
						o_option.selected = true;
					}
					o_option.inject(o_countries);
				});
				
				this.initRegions();
			}).bind(this)
		})).send();
	},
	
	submit: function(oEvent){
		oEvent.stop();
		
		var a_data = $H({
			'minage': this.maAge.minpos,
			'maxage': this.maAge.maxpos,
			'photo': $('searchPhoto').checked,
			'online': $('searchOnline').checked
		});
		
		if ($('searchCountryInput').get('value').length > 0){
			a_data.country = $('searchCountryInput').get('value');
		}
		
		if (this.mbSearchType == 'region'){
			a_data.region = $('searchStateInput').get('value').trim();
		}
		
		if (this.mbSearchType == 'city'){
			a_data.cityname = $('searchCityInput').get('value').trim();
		}
		
		location = '/search.html?type=quick&' + a_data.toQueryString();
	}
});

var GAYEU = new Class({
	DOMReady : function() {
		// get static url
		if ($('staticURL') && $('serverURL')){
			this.s_static_url = $('staticURL').get('text').trim();
			this.s_server_url = $('serverURL').get('text').trim();
		} else {
			this.s_static_url = '';
			this.s_server_url = '';
		}
		
		// Logo
		if ($('imgLogo')){
			$('imgLogo').addEvent('click', function(){
				location = '/';
			}).setStyle('cursor', 'pointer');
		}
		
		// spotlight
		var o_spotlight = new Spotlight('spotlightContainer', {
			'url': '/json/spotlight.json'
		});
		
		this.initLanguageSwitch();
		this.initSearchBox();
		
		// handle form post
		if ($('pollForm')){
			$('pollForm').addEvent('submit', function(){var a=false;$A(this.poll).each(function(b){if(b.get("checked")===true)a=true});return a});
		}
 	},
 	
	initSearchBox : function() {
		var o_this = this;
		this.moSearch = new GAYSEARCH();
		
		// Search box
		$('searchBox').addEvent('click', function() {
			o_this.moSearch.toggle();
		});
		
		try {
			$('searchBoxFull').set('tween', {duration: 'normal', transition: Fx.Transitions.Expo.easeOut});
		} catch(oErr) {
			//alert(oErr);
		}
	},
	
	initLanguageSwitch : function() {
		if ($('languageBox')){
			$('languageBox').addEvent('change', function(oEvent){
				if (oEvent.target.value.trim().length === 0){
					return;
				}
				
				var s_language = oEvent.target.value;
				(new Request({
					'url': '/ajax/set_language/',
					'method': 'post',
					'data': 'language=' + s_language,
					'onComplete': function(sResponse){
						if (s_language == 'EN'){
							document.location = '/profiles/';
						} else if (sResponse == '.'){
							document.location.reload(true);
						}
					}
				})).send();
			});
		}
	}
});


/**
 * Create new class instance
 */
var moGAYEU = new GAYEU();


/**
 * DOM Ready callback
 *
 * @access  public
 * @param   event
 * @return  void
 */
window.addEvent('domready', function(oEvent) {
	moGAYEU.DOMReady();
});