HOMISSMESearchController = new Class({

	initialize: function(version) {
		this.name = 'Search';
		this.self = this;
		this.Frame = $('frame_Search');

		this.resultsNum = -1;

		this.view = {
			title : $('Search_Title'),
			backingLayer : $('Search_Backing_layer'),
			searchclose : $('Search_Close_Button'),
			searchStatus : $('Search_Status'),
			searchFormArea : $('Search_Form_MainArea'),
			searchInput: $('Search_Form_TextField'),
			submitbutton : $('Search_Submit_Button'),
			hide : function(how, args) {
				var views = Scene_Search.view;
				if (args == undefined) var args = [];
				for (v in views) {
					if (args.some(function(item, index) {
						return item == v;
					})) continue;
					if (typeOf(views[v]) != "function" && views[v] != null) views[v].view(how);
					if (typeOf(views[v]) == "function" && v != 'hide' && v != 'show') {
						var els = views[v]().view(how);
					}
				}
			},

			show : function(how, args) {
				var views = Scene_Search.view;
				if (args == undefined) var args = [];
				for (v in views) {
					if (args.some(function(item, index) {
						return item == v;
					})) continue;
					if (typeOf(views[v]) != "function" && views[v] != null) views[v].view(how);
					if (typeOf(views[v]) == "function" && v != 'hide' && v != 'show') {
						var els = views[v]().view(how);
					}
				}
			}
		};

		this.timers = [];
		this.timer = '';
		this.tCount = 0;

		// Morphs
		this.containerFX = new Fx.Morph(this.Frame, {
			link: 'cancel'
		});

		this.searchFormAreaMorph = new Fx.Morph('Search_Form_MainArea', {
			link: 'cancel'
		});

		this.view.submitbutton.set('morph', {
			duration: 500,
			link:'cancel',
			transition: Fx.Transitions.Quart.easeOut
		});

		this.view.searchInput.set('morph', {
			duration: 500,
			link:'cancel',
			transition: Fx.Transitions.Quart.easeOut
		});

		this.Frame.set('view', {
			duration: 500,
			link:'cancel',
			'transition': Fx.Transitions.Quart.easeOut
		});

		$('Search_Form_TextField').addEvent('keyup', function(event){
			this.user_searchfield_keyboardPress(event);
		}.bind(this));

		$('Search_Form_TextField').addEvent('focus', function(event) {
			this.value = '';
			this.set('class', 'goactive');
		});
	},

	PresentAtLaunch : function(fromTag) {
	},

	PresentScene : function() {
		//console.log("HOMISSMESearchController.PresentScene()");

		// hide preloader
		spinner.hide();

		// TODO: view method isn't cancelling repeat calls

		// show frame and fade in
		$('frame_Search').show();
		this.containerFX.start({
			'opacity': 1.0
		});

		// TODO: use morph instead
		this.view.show('in', ['searchStatus','submitbutton']);

		// reset search value
//		this.view.searchInput.value = 'Enter Keyword';

		this.view.searchInput.value = '';

		// TODO: check for hml5 compat with modernizr
		this.view.searchInput.set('class', 'inactive');
		this.view.searchInput.set('placeholder', 'Enter Keyword');      // HTML 5

		// animate in search box
		this.searchFormAreaMorph.start({
			'opacity': [0,1],
			'top': [(window.getSize().y * .5), (window.getSize().y / 2) - (170 * .5)]
		}).chain(function(){
			// set focus to the input textfield
			document.getElementById('Search_Form_TextField').focus();
		});
	},

	user_searchfield_keyboardPress : function(e) {
		clearTimeout(presstimeout);
		presstimeout = setTimeout('Scene_Search.processKeyPresses()', 500);

		var event = e || window.event;
		var keycode = event.code;

		Scene_Search.processTextFieldExtent();


		// submit search result on keypress enter IF we have at least 1 search result
		if (event.key == "enter") {
			if (this.resultsNum > 0) {
				clearTimeout(presstimeout);

//				this.processKeyPresses();
 			
				 Director.searchHandler();
			}
		}
	},

	processTextFieldExtent : function() {
		//CHECK FOR STRING LENGTH
		var searchstringlen = Scene_Search.view.searchInput.value.length;
		var characterWidth = 7;
		var startanimationExtent = 15;
		var extraWidth = ((searchstringlen - startanimationExtent) * characterWidth);
		var strboxwidth = 300;

		if (searchstringlen > startanimationExtent && searchstringlen < 80) {
			Scene_Search.view.searchInput.morph({'width': strboxwidth + extraWidth,'margin-left':-(extraWidth * .5) });
		}

		if (searchstringlen < startanimationExtent) {
			Scene_Search.view.searchInput.morph({
				'width': 300,
				'margin-left':0
			});
		}
	},

	processKeyPresses : function() {
		//console.log(this.view.searchInput.value);
		clearTimeout(presstimeout);

		// TODO if empty search by tag to bring up latest posts
		Scene_Wall.fetchPostsBySearchString(this.view.searchInput.value);

		//spinner.show(); * CA * may place back in future//

		presstimeout = null;
		pressArray.empty();
		pressArray = [];
		special_keyPhrase.empty();
		special_keyPhrase = ['l','u','v'];
	},

	wallControllerDidChangeWall : function(resultnum) {
//		console.log("HOMISSMESearchController.wallControllerDidChangeWall");

		Scene_Wall.clearPosts();

		this.resultsNum = resultnum;
		spinner.hide();
		Scene_Search.view.searchStatus.set('html', 'Results: (' + resultnum + ')');
		Scene_Search.view.searchStatus.view('in').wait(1000).chain(function() {
			this.element.view(0);
		});
	},

	/* OVERRIDE METHODS*/
	directorDidRequestResize : function() {
		// reposition on resize
		this.searchFormAreaMorph.start({
			'top': (window.getSize().y / 2) - (170 * .5)
		});
	},

	directorDidChangeVersionControl : function(version) {

	},

	closeScene : function(fromScene) {

		//called from eventcontroller (clicking home button)
		for (timer in this.timers) {
			clearTimeout(timer);
		}

		// clear search status
		this.view.searchStatus.empty();

		// animate out of view
		this.searchFormAreaMorph.start({
			'top': [(window.getSize().y / 2) - (170 * .5), (window.getSize().y * .5)]
		});

		// fade out frame
		this.containerFX.start({
			'opacity': 0.0
		}).chain(function() {
			this.Frame.hide();
		}.bind(this));
	}
});



 



