﻿var BingSearch = new Class({
	search : '',
	filter : '',
	offsetNum : 0,
	newPage : 0,
	serviceUri : '',
	AppId : '',
	ajaxUri : '',
	newPage : 0,
	searchType : '',
	resultPerPage : 10,	
	searchFilter : new Hash({0:'', 1:' language:de',2:' language:de loc:CH',3:' language:de loc:DE',4:' language:de loc:AT',5:' language:en',6:' language:en loc:CH',7:' language:en loc:DE', 8:' language:en loc:AT', 9: ' language:en loc:US'}),
	

	initialize: function(ajaxUri, serviceUri, appId, searchType) {

	this.ajaxUri = ajaxUri;
	this.serviceUri = serviceUri;
	this.AppId = appId;
	this.searchType = searchType;
	this.setResultsPerPage(this.searchType);
	},
	
	setResultsPerPage: function(searchType){
		switch(searchType) {
		case 'Image':
			this.resultPerPage = 16;
			break;
	
		case 'Web':
			this.resultPerPage = 10;
			break;
	
		case 'Video':
			this.resultPerPage = 16;
			break;
	
		case 'News':
			this.resultPerPage = 10;
			break;
	
		default:
			this.resultPerPage = 10;
		break;
		}
	},
	
	setSearchType: function(type, el){
		this.searchType = type;
		this.setResultsPerPage(this.searchType);
		if($('searchType')){$('searchType').set('value',type);}
		this.actLink(el);
		this.newPage = 0;
		this.searchBing();
		this.setRadiobuttons('form2');
	},
	
	actLink: function(el){
		var liArr = $$('#search_nav', 'a.act');
		liArr.each(function(s){$(s).removeProperty('class');});
		$(el).setProperty('class', 'act');
	},
	
	previousPage: function(page){
		this.newPage = page -1;
		this.offsetNum = (this.newPage-1)*this.resultPerPage;
		this.searchBing();
	},
	
	nextPage: function(page){
		var page = page;
		this.newPage = page;
		this.offsetNum = (page -1)*this.resultPerPage;
		this.searchBing();
	},
	
	pageMove: function(pageNum){
		this.newPage = pageNum;
		this.offsetNum = (pageNum-1)*this.resultPerPage;
		this.searchBing();
	},
	
	relatedSearch: function(searchTerm){
		$('topSearch').set('value',searchTerm);
		$('baseSearch').set('value',searchTerm);
		this.searchBing(0);
	},
	
	searchTop: function(){
		this.offsetNum = 0;
		this.newPage = 0;
		this.search 	= "&query=" + encodeURIComponent($('topSearch').get('value'));
		this.filter = $$('[name="radio"]:checked').get('value');
		$('baseSearch').set('value',$('topSearch').get('value'));
		this.setRadiobuttons('form2');
		this.handleSearch();
	},
	
	searchBase: function(){
		this.offsetNum = 0
		this.newPage = 0;
		this.search = "&query=" + encodeURIComponent($('baseSearch').get('value'));
		this.filter = $$('[name="radio_base"]:checked').get('value');
		$('topSearch').set('value', $('baseSearch').get('value'));
		this.setRadiobuttons('form1');
		this.handleSearch();
	},
	
	setRadiobuttons: function(formId){
		var filter = this.filter;
		$$('#'+formId+' [type="radio"]').each( function(el){ if(el.value == filter) {el.set('checked', 'checked')}});
	},
	
	searchBing: function(){
		this.search = "&query=" + encodeURIComponent($('topSearch').get('value'));
		this.filter = $$('[name="radio"]:checked').get('value');
		this.handleSearch();
	},
	
	handleSearch: function() {
		var source = '';
		var options = '';
		var offset	= 0;
		var disableLocationDetection = '';
	
		switch(this.searchType) {
		case 'Image':
			source = 'Image';
			options = options + '&Image.Count=16';
			options = options + '&Image.Offset='+this.offsetNum;
			options = options + '&Adult=Moderate';
			options = options + '&Options=EnableHighlighting';
			break;
	
		case 'Web':
			source = 'Web';
			options = options + '&Web.Count=10';
			options = options + '&Web.Offset='+this.offsetNum;
			options = options + '&Adult=Moderate';
			options = options + '&Options=EnableHighlighting';
	
			break;
	
		case 'Video':
			source = 'Video';
			options = options + '&Video.Count=16';
			options = options + '&Video.Offset='+this.offsetNum;
			options = options + '&Video.SortBy=Relevance';
			options = options + '&Adult=Moderate';
			break;
	
		case 'News':
			source = 'News';
			options = options + '&News.Offset='+this.offsetNum;
			options = options + '&News.SortBy=Relevance';
			options = options + '&Options=EnableHighlighting';
			break;
	
		default:
			source = 'Web';
			options = options + '&Web.Count=10';
			options = options + '&Web.Offset='+this.offsetNum;
			options = options + '&Adult=Moderate';
			options = options + '&Options=EnableHighlighting';
			break;
		}
		var sourceType	= '&Sources='+source+'+RelatedSearch+Spell';
		var fullUri = this.serviceUri + this.AppId + this.search + this.searchFilter.get(parseInt(this.filter)) + sourceType + options ;
		new Request.JSONP({url: fullUri}).send();
	},
	
	handleResult: function(result){
		$('topSearchButton').set('src', '/static/img/circle-loader.gif');
		$('baseSearchButton').set('src', '/static/img/circle-loader.gif');
		new Request.JSON({
			method: 'post',
			url: ajaxUri,
			data: 'string='+encodeURIComponent(JSON.encode(result))+'&page='+this.newPage+'&searchFilter='+this.searchFilter.get(parseInt(this.filter)),
			onComplete: function(pageResults){
				$('searchResults').set('html', pageResults.results);
				$('topSearchButton').set('src', '/static/img/icon_search.gif');
				$('baseSearchButton').set('src', '/static/img/icon_search.gif');
				//$('searchAds').set('html', pageResults.ads);
				scroll(0,0);
				ReMooz.assign($$('a[rel=image]'), {
					'origin': 'img',
					'shadow': 'onOpenEnd', // fx is faster because shadow appears after resize animation
					'resizeFactor': 0.8, // resize to maximum 80% of screen size
					'cutOut': false, // don't hide the original
					'opacityResize': 0.4, // opaque resize
					'dragging': false, // disable dragging
					'centered': false // resize to center of the screen, not relative to the source element
				});
			}
		}).send();
		document.title = $('topSearch').get('value')+" - MyGuide";
	},
	
	initImageJs: function(){
		
		ReMooz.assign($$('a[rel=image]'), {
			'origin': 'img',
			'shadow': 'onOpenEnd', // fx is faster because shadow appears after resize animation
			'resizeFactor': 0.8, // resize to maximum 80% of screen size
			'cutOut': false, // don't hide the original
			'opacityResize': 0.4, // opaque resize
			'dragging': false, // disable dragging
			'centered': false // resize to center of the screen, not relative to the source element
		});
	}
});