// JavaScript Document
var News = new Class({
	Extends: Page,
	
	messageObj: null,
	
	setVideo: null,
	setPhotoGallery: null,
	setPhotoList: null,
	setNewsList: null,
	setVideoLinks: null,
	setNewsSignupForm: null,
	setVideoSnippet: null,
	
	initialize: function(pageState) {
		this.parent('news', pageState);
		
		this.messageObj = new Message();
		
		this.setVideo = this.setBlock.pass(['abBanner', 'video'], this);
		this.setPhotoGallery = this.setBlock.pass(['abBanner', 'photoGallery'], this);
		this.setPhotoList = this.setBlock.pass(['divPhotoList', 'photoList'], this);
		this.setNewsList = this.setBlock.pass(['divNewsList', 'newsList'], this);
		this.setVideoLinks = this.setBlock.pass(['divVideoLinks', 'videoLinks'], this);
		this.setNewsSignupForm = this.setBlock.pass(['divVideoLinks', 'newsSignupForm'], this);
		this.setVideoSnippet = this.setBlock.pass(['divVideoSnippet', 'videoSnippet', false], this);
	},
	
	requestVideo: function(id) {
		return {
			server: 'NewsServer',
			handler: 'video',
			params: [id]
		};
	},
	requestPhotoGallery: function(id) {
		return {
			server: 'NewsServer',
			handler: 'photoGallery',
			params: [id]
		};
	},
	requestNewsList: function(featureType, id, artistId, filter) {
		artistId = $defined(artistId) ? artistId : null;
		filter = $defined(filter) ? filter : null;
		return {
			server: 'NewsServer',
			handler: 'newsList',
			params: [featureType, id, artistId, filter]
		};
	},
	requestVideoLinks: function(id) {
		return {
			server: 'NewsServer',
			handler: 'videoLinks',
			params: [id]
		};
	},
	requestNewsSignupForm: function() {
		return {
			server: 'NewsServer',
			handler: 'newsSignupForm',
			params: []
		};
	},
	requestPersistNewsSignup: function(formValues) {
		return {
			server: 'NewsServer',
			handler: 'persistNewsSignup',
			params: [formValues]
		};
	},
	requestVideoSnippet: function(id) {
		return {
			server: 'NewsServer',
			handler: 'videoSnippet',
			params: [id]
		};
	},
	
	persistNewsSignup: function(form) {
		if (Validation.validateForm(form)) {
			this.addRequest(this.requestPersistNewsSignup(form.getFormValues()));
			this.sendRequests();
			form.reset();
		} else {
			this.messageObj.message('The form contains validation errors. Please correct and resend.', true);
		}
	},
	selectVideo: function(id) {
		this.pageState.featureType = 'video';
		this.pageState.id = id;
		
		this.addRequest(this.requestVideo(id));
		this.addRequest(this.requestVideoLinks(id));
		this.addRequest(this.requestVideoSnippet(id));
		this.addRequest(this.requestNewsList('video', id, this.pageState.artistId, this.pageState.filter));
		
		this.sendRequests();
	},
	selectPhotoGallery: function(id) {
		this.pageState.featureType = 'photoGallery';
		this.pageState.id = id;
		
		this.addRequest(this.requestPhotoGallery(id));
		this.addRequest(this.requestNewsSignupForm());
		this.addRequest(this.requestNewsList('photoGallery', id, this.pageState.artistId, this.pageState.filter));
		
		this.sendRequests();
	},
	
	attachScrollbar: function() {
		var content = $('abPhotoListHomeScroll');
		var scrollbar = $('area');
		var handle = $('knob');
		var steps = content.getScrollSize().x - content.getSize().x;
		
		var slider = new Slider(scrollbar, handle, {	
			steps: steps,
			mode: 'horizontal',
			onChange: function(step){
				var x = step;
				var y = 0;
				content.scrollTo(x,y);
			}
		}).set(0);
	},
	attachNewsListScrollbar: function() {
		var offSet = 30;
		if ($$('.newsList .scroll')[0].getStyle('height') == '350px') offSet = 180;
		var content = $$('.newsList .left .scroll')[0];
		var scrollbar = $('newsListArea');
		var handle = $('newsListKnob');
		//var steps = content.getScrollSize().y - content.getSize().y;
		var steps = content.getScrollSize().y + offSet;
		
		var slider = new Slider(scrollbar, handle, {	
			'steps': steps,
			mode: 'vertical',
			onChange: function(step){
				var x = 0;
				var y = step;
				content.scrollTo(x,y);
			}
		});
		slider.set(0);
	},
	
	afterContentUpdate: function(responseClass) {
		switch (responseClass) {
			case 'photoList':
				this.attachScrollbar();
				break;
			case 'newsList':
				this.attachNewsListScrollbar();
				break;
		}
	}
});
