/**
 * @author Jakub Kozicki
 */
var SchoolSearch = Class.create();

SchoolSearch.prototype = {
	initialize: function(accordion, failureUrl){
		this.accordion = accordion;
		this.failureUrl = failureUrl;
		
		this.state = {id:'', name: ''};;
		this.province = {id:'', name: ''};;
		this.city = {id:'', name: ''};;
		this.schoolType = {id:'', name: ''};
		this.school = {id:'', name: ''};
		
		this.loadWaiting = false;
		this.steps = ['state', 'province', 'city', 'school-type', 'school'];
		
		this.steps.each(function(step) {
			var opc = $('opc-' + step + '-progress');
			var a = opc.getElementsByTagName('a')[0];
			Event.observe(a, 'click', function() {
				this.accordion.openSection("opc-" + step);
				this.resetData(step);
				this.reloadProgressBlock();
				
				for(var i = this.steps.indexOf(step) + 1; i < this.steps.length; i++) {
					this.accordion.closeAndClearSectionByStepName(this.steps[i]);
				}
				
				return false;
			}.bind(this));
		}.bind(this)
		);
		
		this.accordion.disallowAccessToNextSections = true;
	},
	
	ajaxFailure: function(){
		location.href = this.failureUrl;
	},
	
	resetData: function(step) {
		switch(step) {
			case 'state' :
				this.state = {id:'', name: ''};
			case 'province' :
				this.province = {id:'', name: ''};
			case 'city' :
				this.city = {id:'', name: ''};
			case 'school-type' :
				this.schoolType = {id:'', name: ''};
			case 'school' :
				this.school = {id:'', name: ''};
			default :
				break;
		}
	},
	
	reloadProgressBlock: function() {
		
		this.steps.each(function(step) {
			
			var opc = $('opc-' + step + '-progress');
			var span = opc.getElementsByTagName('span')[0];
			
			var text = "";
			
			switch(step) {
				case 'state' :
					text = this.state.name;
					break;
				case 'province' :
					text = this.province.name;
					break;
				case 'city' :
					text = this.city.name;
					break;
				case 'school-type' :
					text = this.schoolType.name;
					break;
				case 'school' :
					text = this.school.name;
					break;
				default :
				break;
			}
			
			if (text.length) {
				span.innerHTML = text;
				opc.show();
			}
			else {
				opc.hide();
			}
		}.bind(this));
	},
	
	hideProgressBlock: function(section) {
		var opc;
		for(i = this.steps.indexOf(section); i < this.steps.length; i++) {
			opc = $('opc-' + this.steps[i] + '-progress');
			opc.hide();
		}
	},
	
	setLoadWaiting: function(step) {
		if (step) {
			if (this.loadWaiting) {
				this.setLoadWaiting(false);
			}
			// $(step+'-buttons-container').setStyle({opacity:.5});
			//$('school-step-'+step).hide();
			Element.show(step+'-please-wait');
		} else {
			if (this.loadWaiting) {
				// $(this.loadWaiting+'-buttons-container').setStyle({opacity:1});
				Element.hide(this.loadWaiting+'-please-wait');
				//$('school-step-'+this.loadWaiting).show();
			}
		}
		this.loadWaiting = step;
	},
	
	gotoSection: function(section)
	{
		this.hideProgressBlock(section);
		section = $('opc-'+section);
		section.addClassName('allow');
		this.accordion.openSection(section);
	},
	
	setState: function() {
		this.reloadProgressBlock();
		this.gotoSection('province');
	},
	
	setProvince: function() {
		this.reloadProgressBlock();
		this.gotoSection('city');
	},
	
	setCity: function() {
		this.reloadProgressBlock();
		this.gotoSection('school-type');
	},
	
	setSchoolType: function() {
		this.reloadProgressBlock();
		this.gotoSection('school');
	},

	back: function(){
		if (this.loadWaiting) return;
		this.accordion.openPrevSection(true);
	}
}


// State
var State = Class.create();
State.prototype = {
	initialize: function(saveUrl){
		this.saveUrl = saveUrl;
		this.onSave = this.nextStep.bindAsEventListener(this);
		this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
		this.onClick = this.click.bindAsEventListener(this);
		this.parse();
	},
	
	parse: function() {
		var links = $('school-state-html-load').getElementsBySelector('a');
		
		links.each(function(a) {
			Event.observe(a, 'click', this.onClick);
			
		}.bind(this));
	},
	
	click: function(event) {
		var a = event.element();
		var stateId = a.readAttribute('href').match(/\S+state\/([0-9]+)\/\S*/)[1];
		this.save(stateId);
		event.stop();
	},

	save: function(id){
		
		if (schoolSearch.loadWaiting != false) return;
	   	
		var sParams = new String();
		sParams = addPostParam(sParams, "state", id);
		
		schoolSearch.setLoadWaiting('state');
		var request = new Ajax.Request(
			this.saveUrl,
			{
				method:'post',
				postBody: sParams,
				onComplete: this.onComplete,
				onSuccess: this.onSave,
				onFailure: schoolSearch.ajaxFailure.bind(schoolSearch)
			}
		);
	},

	resetLoadWaiting: function(){
		schoolSearch.setLoadWaiting(false);
	},

	nextStep: function(transport){
		
		var response = transport.responseText.evalJSON();
		
		if (response.redirect) {
			location.href = response.redirect;
			return;
		}
		
		if (response.provincesHtml) {
			var provincesHtml = $('school-province-html-load');
			provincesHtml.innerHTML = response.provincesHtml;
			provincesHtml.show();
			$('school-step-province').show();
			
			province.parse();
		}
		
		if (response.state) {
			schoolSearch.state = response.state;
		}
		schoolSearch.setState();
	}
}


// Province
var Province = Class.create();
Province.prototype = {
	initialize: function(saveUrl){
		this.saveUrl = saveUrl;
		this.onSave = this.nextStep.bindAsEventListener(this);
		this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
		this.onClick = this.click.bindAsEventListener(this);
		this.parse();
	},
	
	parse: function() {
		var links = $('school-province-html-load').getElementsBySelector('a');
		
		links.each(function(a) {
			Event.observe(a, 'click', this.onClick);
			
		}.bind(this));
	},
	
	click: function(event) {
		var a = event.element();
		var provinceId = a.readAttribute('href').match(/\S+province\/([0-9]+)\/\S*/)[1];
		this.save(provinceId);
		event.stop();
	},

	save: function(id){
		if (schoolSearch.loadWaiting!=false) return;
	   	
		var sParams = new String();
		sParams = addPostParam(sParams, "state", schoolSearch.state.id);
		sParams = addPostParam(sParams, "province", id);
		
		schoolSearch.setLoadWaiting('province');
		var request = new Ajax.Request(
			this.saveUrl,
			{
				method:'post',
				postBody: sParams,
				onComplete: this.onComplete,
				onSuccess: this.onSave,
				onFailure: schoolSearch.ajaxFailure.bind(schoolSearch)
			}
		);
	},

	resetLoadWaiting: function(){
		schoolSearch.setLoadWaiting(false);
	},

	nextStep: function(transport){
		
		var response = transport.responseText.evalJSON();
		
		if (response.redirect) {
			location.href = response.redirect;
			return;
		}
		
		if (response.citiesHtml) {
			var citiesHtml = $('school-city-html-load');
			citiesHtml.innerHTML = response.citiesHtml;
			citiesHtml.show();
			$('school-step-city').show();
			
			city.parse();
		}
		
		if (response.province) {
			schoolSearch.province = response.province;
		}
		schoolSearch.setProvince();
	}
}

// City
var City = Class.create();
City.prototype = {
	initialize: function(saveUrl, loadUrl) {
		this.saveUrl = saveUrl;
		this.loadUrl = loadUrl;
		
		this.onSaveCities = this.fill.bindAsEventListener(this);
		this.onSave = this.nextStep.bindAsEventListener(this);
		this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
		
		this.onClick = this.click.bindAsEventListener(this);
		this.onClickPaginator = this.clickPaginator.bindAsEventListener(this);
		this.parse();
	},
	
	parse: function() {
		var links = $('school-city-html-load').getElementsBySelector('a');
		
		links.each(function(a) {
			var city = a.readAttribute('href').match(/\S+city\/([0-9]+)\/\S*/);
			var cityLetter = a.readAttribute('href').match(/\S+city_letter\/(.+)\/\S*/);
			
			if(cityLetter != null || city != null) {
				if(city != null && city.length == 2)
					Event.observe(a, 'click', this.onClick);
				else if(cityLetter != null && cityLetter.length == 2)
					Event.observe(a, 'click', this.onClickPaginator);
			}
			
		}.bind(this));
	},
	
	click: function(event) {
		var a = event.element();
		var cityId = a.readAttribute('href').match(/\S+city\/([0-9]+)\/\S*/)[1];
		this.save(cityId);
		event.stop();
	},
	
	clickPaginator: function(event) {
		var a = event.element();
		var cityLetter = a.readAttribute('href').match(/\S+city_letter\/(.)\/\S*/)[1];
		var cityPage = a.readAttribute('href').match(/\S+city_page\/([0-9]+)\/\S*/);
		cityPage = cityPage != null && cityPage.length == 2 ? cityPage[1] : '1';
		this.setCities(cityLetter, cityPage);
		event.stop();
	},
	
	setCities: function(sLetter, iPage){
		if (sLetter && iPage) {
			
			var sParams = new String();
			sParams = addPostParam(sParams, "state", schoolSearch.state.id);
			sParams = addPostParam(sParams, "province", schoolSearch.province.id);
			sParams = addPostParam(sParams, "letter", sLetter);
			sParams = addPostParam(sParams, "page", iPage);
			
			schoolSearch.setLoadWaiting('city');
			request = new Ajax.Request(
				this.loadUrl,
				{
					method:'post',
					postBody: sParams,
					onComplete: this.onComplete,
					onSuccess: this.onSaveCities,
					onFailure: schoolSearch.ajaxFailure.bind(schoolSearch)
				}
			);
		}
	},
	
	fill: function(transport){
	   
	   var response = transport.responseText.evalJSON();
	   
	   if (response.redirect) {
			location.href = response.redirect;
			return;
		}
		
		if (response.citiesHtml) {
			$('school-city-html-load').innerHTML = response.citiesHtml;
			this.parse();
		}
	},
	
	save: function(id){
		if (schoolSearch.loadWaiting!=false) return;
		
		var sParams = new String();
		sParams = addPostParam(sParams, "state", schoolSearch.state.id);	
		sParams = addPostParam(sParams, "province", schoolSearch.province.id);	
		sParams = addPostParam(sParams, "city", id);
		
		schoolSearch.setLoadWaiting('city');
		var request = new Ajax.Request(
			this.saveUrl,
			{
				method: 'post',
				postBody: sParams,
				onComplete: this.onComplete,
				onSuccess: this.onSave,
				onFailure: schoolSearch.ajaxFailure.bind(schoolSearch)
			}
		);
	},

	resetLoadWaiting: function(transport){
		schoolSearch.setLoadWaiting(false);
	},

	nextStep: function(transport){
 
		var response = transport.responseText.evalJSON();
		
		if (response.redirect) {
			location.href = response.redirect;
			return;
		}
		if (response.schoolTypesHtml) {
			var schoolTypesHtml = $('school-school-type-html-load');
			schoolTypesHtml.innerHTML = response.schoolTypesHtml;
			schoolTypesHtml.show();
			$('school-step-school-type').show(); // Added by Kozicki Jakub [09.05.2009]
			
			schoolType.parse();
		}
		
		if (response.city) {
			schoolSearch.city = response.city;
		}
		
		schoolSearch.setCity();
	}
}

// School type
var SchoolType = Class.create();
SchoolType.prototype = {
	initialize: function(saveUrl){
		this.saveUrl = saveUrl;
		this.onSave = this.nextStep.bindAsEventListener(this);
		this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
		
		this.onClick = this.click.bindAsEventListener(this);
		this.parse();
	},
	
	parse: function() {
		var links = $('school-school-type-html-load').getElementsBySelector('a');
		
		links.each(function(a) {
			Event.observe(a, 'click', this.onClick);
			
		}.bind(this));
	},
	
	click: function(event) {
		var a = event.element();
		var schoolTypeId = a.readAttribute('href').match(/\S+school_type\/([0-9]+)\/\S*/)[1];
		this.save(schoolTypeId);
		event.stop();
	},

	save: function(id){
		if (schoolSearch.loadWaiting!=false) return;
	   	
		var sParams = new String();
		sParams = addPostParam(sParams, "state", schoolSearch.state.id);	
		sParams = addPostParam(sParams, "province", schoolSearch.province.id);	
		sParams = addPostParam(sParams, "city", schoolSearch.city.id);
		sParams = addPostParam(sParams, "school_type", id);
		
		schoolSearch.setLoadWaiting('school-type');
		var request = new Ajax.Request(
			this.saveUrl,
			{
				method:'post',
				postBody: sParams,
				onComplete: this.onComplete,
				onSuccess: this.onSave,
				onFailure: schoolSearch.ajaxFailure.bind(schoolSearch)
			}
		);
	},

	resetLoadWaiting: function(){
		schoolSearch.setLoadWaiting(false);
	},

	nextStep: function(transport){
		
		var response = transport.responseText.evalJSON();
		
		if (response.redirect) {
			location.href = response.redirect;
			return;
		}
		
		if (response.schoolsHtml) {
			var schoolsHtml = $('school-school-html-load');
			schoolsHtml.innerHTML = response.schoolsHtml;
			schoolsHtml.show();
			$('school-step-school').show(); // Added by Kozicki Jakub [09.05.2009]
			
			school.parse();
		}
		
		if (response.schoolType) {
			schoolSearch.schoolType = response.schoolType;
		}
		
		schoolSearch.setSchoolType();
	}
}

// School
var School = Class.create();
School.prototype = {
	initialize: function(saveUrl, loadUrl){
		this.saveUrl = saveUrl;
		this.loadUrl = loadUrl;
		
		this.onSaveSchools = this.fill.bindAsEventListener(this);
		this.onSave = this.nextStep.bindAsEventListener(this);
		this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
		
		this.onClick = this.click.bindAsEventListener(this);
		this.onClickPaginator = this.clickPaginator.bindAsEventListener(this);
		this.parse();
	},
	
	parse: function() {
		var links = $('school-school-html-load').getElementsBySelector('a');
		
		links.each(function(a) {
			
			var schoolLetter = a.readAttribute('href').match(/\S+school_letter\/(.+)\/\S*/);
			var school = a.readAttribute('href').match(/\S+school\/([0-9]+)-show.html/);
			
			if(schoolLetter != null || school != null) {
				if(schoolLetter != null && schoolLetter.length == 2)
					Event.observe(a, 'click', this.onClickPaginator);
				else if(school != null && school.length == 2)
					Event.observe(a, 'click', this.onClick);
			}
		}.bind(this));
	},
	
	click: function(event) {
		var a = event.element();
		var schoolId = a.readAttribute('href').match(/\S+school\/([0-9]+)-show.html/)[1];
		this.save(schoolId);
		event.stop();
	},
	
	clickPaginator: function(event) {
		var a = event.element();
		var schoolLetter = a.readAttribute('href').match(/\S+school_letter\/(.+)\/\S*/)[1];
		var schoolPage = a.readAttribute('href').match(/\S+school_page\/([0-9]+)\/\S*/);
		schoolPage = schoolPage != null && schoolPage.length == 2 ? schoolPage[1] : '1';
		this.setSchools(schoolLetter, schoolPage);
		event.stop();
	},

	setSchools: function(sLetter, iPage){
		if (iPage) {
			
			var sParams = new String();
			sParams = addPostParam(sParams, "state", schoolSearch.state.id);	
			sParams = addPostParam(sParams, "province", schoolSearch.province.id);	
			sParams = addPostParam(sParams, "city", schoolSearch.city.id);
			sParams = addPostParam(sParams, "school_type", schoolSearch.schoolType.id);
			sParams = addPostParam(sParams, "letter", sLetter);
			sParams = addPostParam(sParams, "page", iPage);
			
			schoolSearch.setLoadWaiting('school');
			request = new Ajax.Request(
				this.loadUrl,
				{
					method:'post',
					postBody: sParams,
					onComplete: this.onComplete,
					onSuccess: this.onSaveSchools,
					onFailure: schoolSearch.ajaxFailure.bind(schoolSearch)
				}
			);
		}
	},
	
	fill: function(transport){
	   
	   var response = transport.responseText.evalJSON();
	   
	   if (response.redirect) {
			location.href = response.redirect;
			return;
		}
		
		if (response.schoolsHtml) {
			$('school-school-html-load').innerHTML = response.schoolsHtml;
			
			this.parse();
		}
	},
	
	save: function(id){
		if (schoolSearch.loadWaiting!=false) return;
		
		var sParams = new String();
		sParams = addPostParam(sParams, "state", schoolSearch.state.id);	
		sParams = addPostParam(sParams, "province", schoolSearch.province.id);	
		sParams = addPostParam(sParams, "city", schoolSearch.city.id);
		sParams = addPostParam(sParams, "school_type", schoolSearch.schoolType.id);
		sParams = addPostParam(sParams, "school", id)
		
		schoolSearch.setLoadWaiting('school');
		var request = new Ajax.Request(
			this.saveUrl,
			{
				method: 'post',
				postBody: sParams,
				onComplete: this.onComplete,
				onSuccess: this.onSave,
				onFailure: schoolSearch.ajaxFailure.bind(schoolSearch)
			}
		);
	},

	resetLoadWaiting: function(transport){
		schoolSearch.setLoadWaiting(false);
	},

	nextStep: function(transport){
 
		var response = transport.responseText.evalJSON();
		
		if (response.redirect) {
			location.href = response.redirect;
			return;
		}
		
		if (response.school) {
			schoolSearch.school = response.school;
		}
		
		schoolSearch.setCity();
	}
}
