var Highlight = Class.create({
	initialize: function(container, options){
		this.container  = $(container);
		this.options 	= Object.extend({
			thumbsStyle:				'#highlight_list li',
			pagesStyle:					'#highlight_pages li',
			nextPageStyle:				'#highlight_next',
			previousPageStyle:			'#highlight_prev',
			listStyle:					'#highlight_list',
			perPage:					1,
			listWidth:					485
		}, options);
		
		this.list		= this.container.down(this.options.listStyle);		
		this.page		= parseInt(this.container.down('.selected').previousSiblings().length / this.options.perPage);		
		this.lastPage	= this.container.select(this.options.pagesStyle).length - 1;
		
		this.container
			.delegate(this.options.pagesStyle, 'click', this.selectPage.curry(this))
			.delegate(this.options.nextPageStyle, 'click', this.selectPrevNextPage.bind(this, 1))
			.delegate(this.options.previousPageStyle, 'click', this.selectPrevNextPage.bind(this, -1))
		;
		
		this.setTimer();
	},
	selectPage: function(gallery, e){
		e.stop();
		gallery.gotoPage(+this.previousSiblings().length);
	},
	gotoPage: function(page){
		if (page < 0) page = this.lastPage;
		if (page > this.lastPage) page = 0;
		
		var thumb = this.container.down(this.options.thumbsStyle, page * this.options.perPage);
		if (thumb)  this.select(thumb);
	},
	selectPrevNextPage: function(dir){
		this.gotoPage(this.page + dir);
	},
	select: function(thumb){
		var number		= thumb.previousSiblings().length;
		
		this.page	 	= parseInt(number / this.options.perPage);
		this.current 	= thumb;
				
		this.container.select(this.options.pagesStyle + '.selected').invoke('removeClassName', 'selected');
		this.container.down(this.options.pagesStyle, this.page).addClassName('selected');
		this.list.morph({ left: (-1 * this.page * this.options.listWidth) + 'px' });
		this.setTimer(); 
	},
	setTimer: function(){
		this.timer && clearTimeout(this.timer);
		this.timer = setTimeout(this.selectPrevNextPage.bind(this, 1), 3000);
	}
});

var IndexGally = Class.create({
	initialize: function(container, options){
		this.container  = $(container);
		this.image			= $('index_gallery_img');
		this.options 		= Object.extend({
			thumbsStyle:		'#index_gallery_list a',
			nextPageStyle:		'.img_but_top',
			previousPageStyle:	'.img_but_bot',
			nextImageStyle:		'#index_gallery_next',
			previousImageStyle:	'#index_gallery_prev',
			listStyle:			'#index_gallery_list ul',
			perPage:			15,
			listHeight:			9
		}, options);
		
		this.list		= this.container.down(this.options.listStyle);
		this.current	= this.container.down(this.options.thumbsStyle);
		this.page		= 0;
		
		this.container
			.delegate(this.options.thumbsStyle, 'click', this.selectThumb.curry(this))
			.delegate(this.options.nextImageStyle, 'click', this.selectPrevNext.bind(this, 'next', 'previous'))
			.delegate(this.options.previousImageStyle, 'click', this.selectPrevNext.bind(this, 'previous', 'next'))
			.delegate(this.options.nextPageStyle, 'click', this.selectPrevNextPage.bind(this, -1))
			.delegate(this.options.previousPageStyle, 'click', this.selectPrevNextPage.bind(this, 1))
		;
	},
	selectThumb: function(gallery, e){
		e.stop();
		gallery.select(this);
	},
	selectPage: function(gallery, e){
		e.stop();
		gallery.gotoPage(+this.innerHTML - 1);
	},
	gotoPage: function(page){
		if (page < 0 && page > this.lastPage) return;
		
		var thumb = this.container.down(this.options.thumbsStyle, page * this.options.perPage);
		if (thumb)  this.select(thumb);
	},
	selectPrevNext: function(first, last){
		var li = this.current.up('li'); 
		this.select( (li[first]('li') || li[last + 'Siblings']().last()).down('a') );
	},
	selectPrevNextPage: function(dir){
		this.gotoPage(this.page + dir);
	},
	select: function(thumb){
		var number		= thumb.up('li').previousSiblings().length;
		
		this.page	 	= parseInt(number / this.options.perPage);
		this.current 	= thumb;
		
		this.container.select(this.options.thumbsStyle + '.selected').invoke('removeClassName', 'selected');	
		this.container.select(this.options.nextPageStyle).invoke(this.page == 0 ? 'addClassName' : 'removeClassName', 'disabled');
		this.container.select(this.options.previousPageStyle).invoke(this.page == this.lastPage ? 'addClassName' : 'removeClassName', 'disabled');
		
		this.list.morph({ top: (-1 * this.page * this.options.listHeight) + 'px' });
		
		this.transformImage(thumb.href, thumb.getAttribute('data-href'), thumb.title);
		
		thumb.addClassName('selected');
	},
	transformImage: function(src, href, title){
		this.image.style.backgroundImage = 'none';
		this.image.addClassName('loading');
		this.image.setOpacity(1);
		CD3.ImageLoader(src, this.changeImage.bind(this));
		this.image.href = href;
		this.image.title = title;
	},
	changeImage: function(loader){
		this.image.removeClassName('loading');
		this.image.style.backgroundImage = 'url(' + loader.src + ')';
	}
});

CD3.Behaviors({
	'body': {
		'focus:in': {
			'input[type=text]': function(){
				if (this.getValue() == this.getAttribute('title')) this.setValue('');
			}
		},
		'focus:out': {
			'input[type=text]': function(){
				if (this.getValue().length == 0) this.setValue(this.getAttribute('title') || '');
			}
		}
	},
	'#text_size': function(){
		new CD3.FontSwitcher(this, 'content', {
			reset:	'.reset',
			plus:	'.plus',
			minus:	'.minus'
		});
	},
	'#controls':function(controls){
	
		var head = $('slideme');
		head.style.width = head.select('li').size() * 115 + 'px' ;
		
		new CD3.Slider( head, {
			prev: 		controls.down('.up_arrow'), 
			next: 		controls.down('.down_arrow'),
			scrollBy:	345,
			scrollType: 'horizontal'

		});
		head.delegate('a', 'click', function(e){
			e.stop();
			$('gallery_holder').style.backgroundImage = 'url('+ this.href +')';
			head.down('.selected').removeClassName('selected');
			this.addClassName('selected');
		});
	
	},
	'#highlight': Highlight,
	'#index_gallery': IndexGally,
	'#related_products_title:click': function(){
		this.addClassName('selected');
		$('product_enquiry_fields').style.display = 'none';
		$('related_products_list').style.display = 'block';
		$('product_enquiry_title').removeClassName('selected');
	},
	'#product_enquiry_title:click': function(){
		this.addClassName('selected');
		$('related_products_list').style.display = 'none';
		$('product_enquiry_fields').style.display = 'block';
		$('related_products_title').removeClassName('selected');
	},
	'#categories_menu h3:click': function(){
		var item = this.up('li');
		
		if (item.className == 'selected') {
			item.removeClassName('selected');
		} else {
			if (item.up('ul').down('.selected'))
				item.up('ul').down('.selected').removeClassName('selected');
			item.addClassName('selected');
		}
	}
});
