/*
 * jQuery web&co rotator
 */
(function($) {
$.widget('ui.tourfinder', {

	feed : null,
	_tour: '*',
	_date: '*',
	_time: '*',
	_lang: '*',
	
	_requestTour: 'all',
	_requestDate: 'all',
	_requestTime: 'all',
	_requestLang: 'all',
		
	options: {
		test:'blub'
	},
	
	_create: function() {
		this._getFeed();
	},
	
	_getFeed: function() {
		var self = this;
		
		this._requestTour = (null == unescape(window.location.pathname).match(/(tour)\/([a-zA-z0-9\-\: ]*)/) ? 'all' : unescape(window.location.pathname).match(/(tour)\/([a-zA-z0-9\-\: ]*)/)[2]);
		this._requestDate = (null == unescape(window.location.pathname).match(/(date)\/([a-zA-z0-9\-\: ]*)/) ? 'all' : unescape(window.location.pathname).match(/(date)\/([a-zA-z0-9\-\: ]*)/)[2]);
		this._requestTime = (null == unescape(window.location.pathname).match(/(time)\/([a-zA-z0-9\-\: ]*)/) ? 'all' : unescape(window.location.pathname).match(/(time)\/([a-zA-z0-9\-\: ]*)/)[2]);
		this._requestLang = (null == unescape(window.location.pathname).match(/(lang)\/([a-zA-z0-9\-\: ]*)/) ? 'all' : unescape(window.location.pathname).match(/(lang)\/([a-zA-z0-9\-\: ]*)/)[2]);
	
		
		$.ajax({
		    type: "POST",
		    url: "/?eID=tour_list&tx_vsstour_pi2[tour]=" + self._requestTour,
		    contentType: "application/json; charset=utf-8",
		    dataType: "json",
		    success: function(json) {
				self.feed = json;
				self._initForm();
				self._process();
				 
			},
		    error: function (XMLHttpRequest, textStatus, errorThrown) {
		        $("#error").html(XMLHttpRequest.responseText);
		    }
		});
	},
	
	_initForm:function() {
		var self = this;
		
		// init select tour
		var el_tour = $(this.element).find('.tour');
		self._tour = ('all' == el_tour.val()) ? '*' : '?(@.tour == "'+el_tour.val()+'")';
		
		//init select date
		var el_date = $(this.element).find('.date');
		var dates = jsonPath(this.feed, "$.tours[*].dates[*].date'");
		dates = this._unique(dates).sort();
		for(i=0;i<dates.length;i++) {
			var selected = (this._requestDate == dates[i]) ? 'selected="selected"' : '';
			var d = $.format.date(dates[i]+' 00:00:00', 'dd.MM.yyyy');
			var tmp = $('<option value="'+dates[i]+'" '+selected+' class="value">'+d+'</option>');
			el_date.append(tmp);
		}
		
		//init select time
		var el_time = $(this.element).find('.time');
		var time = jsonPath(this.feed, "$.tours[*].dates[*].times[*].time'");
		time = this._unique(time).sort();
		for(i=0;i<time.length;i++) {
			var selected = (this._requestTime == time[i]) ? 'selected="selected"' : '';
			var tmp = $('<option value="'+time[i]+'" class="value" '+selected+'>'+time[i]+'</option>');
			el_time.append(tmp);
		}
		
		//init select language
		var el_lang = $(this.element).find('.lang');
		self._lang = ('all' == el_lang.val()) ? '*' : '?(@.lang == "'+el_lang.val()+'")';
//		var lang = jsonPath(this.feed, "$.tours[*].dates[*].times[*].langs[*].lang'");
//		lang = this._unique(lang).sort();
//		
//		for(i=0;i<lang.length;i++) {
//			var label = '';
//			switch(lang[i]) {
//				case 0: label = 'English'; break;
//				case 1: label = 'German'; break;
//				case 2: label = 'French'; break;
//				case 3: label = 'Italian'; break;
//				case 4: label = 'Japanese'; break;
//				case 5: label = 'Russian'; break;
//				case 6: label = 'Spanish'; break;
//				default: label = 'English';
//			}
//			var selected = (this._requestLang == lang[i]) ? 'selected="selected"' : '';
//			var tmp = $('<option value="'+lang[i]+'" class="value" '+selected+'>'+label+'</option>');
//			el_lang.append(tmp);
//		}
		
		el_tour.change(function() {
			self._tour = ('all' == $(this).val()) ? '*' : '?(@.tour == "'+$(this).val()+'")';
			self._process();
		});
		
		el_date.change(function() {
			self._date = ('all' == $(this).val()) ? '*' : '?(@.date == "'+$(this).val()+'")';
			self._process();
		});
		
		el_time.change(function() {
			self._time = ('all' == $(this).val()) ? '*' : '?(@.time == "'+$(this).val()+'")';
			self._process();
		});

		el_lang.change(function() {
			self._lang = ('all' == $(this).val()) ? '*' : '?(@.lang == "'+$(this).val()+'")';
			self._process();
		});
		
		
	},
	
	_process:function() {
		
//		this['_'+type] = ('all' == $(this).val()) ? '*' : '?(@.'+type+' == "'+$(this).val()+'")';
		
		var tour = jsonPath(this.feed, '$.tours['+this._tour+'].tour' );
		var date = jsonPath(this.feed, '$.tours['+this._tour+'].dates['+this._date+'].date');
		var time = jsonPath(this.feed, '$.tours['+this._tour+'].dates['+this._date+'].times['+this._time+'].time');
		var lang = jsonPath(this.feed, '$.tours['+this._tour+'].dates['+this._date+'].times['+this._time+'].langs['+this._lang+'].lang');
		
		tour = this._unique(tour).sort();
		date = this._unique(date).sort();
		time = this._unique(time).sort();
		lang = this._unique(lang).sort();
		
		this._setField('.tour', tour, 'any tour');
		this._setField('.date', date, 'any date');
		this._setField('.time', time, 'any time');
		this._setField('.lang', lang, 'any lang');
		
		$('select.tour,select.date,select.time,select.lang').not('.hoponhopoff select.tour').jqTransSelectUpdate();
		
	},
	
	_setField: function(selector, arr, label) {
		var el= $(this.element).find(selector);
		var current = el.find('option:selected');
		el.find('option.value').hide().removeClass('on');
		for(i=0;i<arr.length;i++) {
			var option = el.find('option[value = '+arr[i]+']');
			option.show()
			option.addClass('on');
		}
		current.attr("selected", true);

	},
	
	_unique: function(a) {
		 temp = new Array();
		 for(i=0;i<a.length;i++){
		  if(!this._contains(temp, a[i])){
		   temp.length+=1;
		   temp[temp.length-1]=a[i];
		  }
		 }
		 return temp;
	},
	
	_contains: function(a, e) {
		for(j=0;j<a.length;j++)if(a[j]==e)return true;
		return false;
	}
	
	


});

})(jQuery);
