$.ready(function() {
	$('.menucategory').addEvent('click', function() {
		this.parent().$('ul').item(0).apply(function() {
			var effect = new ElementEffect(new Effect_Slide);
			effect.run(this, 'show');
		});
	});
	
	//For product showcase, set up width
	if ($('.sidebar .slider').count() > 0) {
		$('.sidebar .slider').style.width = (120 * $('.sidebar .slider img').count()) + 'px';
	}
	
	var distvis = false;
	$('.close').addEvent('click', function() {
		var effect = new Effect_Slide;
		effect.element = $('.distributors article');
		effect.speed = 1;
		effect.step = 10;
		if (distvis) effect.hide();
		else effect.show();
		
		distvis = !distvis;
	});
	
	
	if ($('.contactwidget2form')) {
	$('.contactwidget2form').addEvent('mouseout', function() {
		window.collapse = true;
		clearTimeout(window.timeout);
		window.timeout = setTimeout(function() {
			if (window.collapse) {
					this.$('.contactwidget2form').style.display = 'none';
					window.collapse = false;
			}
		}, 3000);
	});
}
	
	$('.contactwidget2form').addEvent('mouseover', function() {
		window.collapse = false;
	});
	
	
	$('.contactwidget2form input').addEvent('focus', function() {
		window.collapse = false;
		if (this.type == 'text') {
			if (this.blanked) return;
			this.defaultValue = this.value;
			this.value = '';
			this.blanked = true;
		}
	});
	
	$('.contactwidget2form textarea').addEvent('focus', function() {
		window.collapse = false;
		if (this.blanked) return;
		this.defaultValue = this.value;
		this.value = '';
		this.blanked = true;
	});
	
	$('.contactwidget2form form').addEvent('submit', function(event) {
		
		var valid = true;
		
		if (!$('.contactwidget2form form input[name="name"]').blanked || $('.contactwidget2form form input[name="name"]').value == '') {
			valid = false;
			alert('Please enter your name');
		}
		else if (!$('.contactwidget2form form textarea').blanked || $('.contactwidget2form form textarea').value == '') {
			valid = false;
			alert('Please enter your question');
			
		} 
		
		var checked = false;
		$('.contactwidget2form form input[type="radio"]').apply(function(){
			if (this.checked && valid) {
				checked = true;
				if (this.value == 1) {
					if ($('.contactwidget2form form input[name="telephone"]').value == '' || !$('.contactwidget2form form input[name="telephone"]').blanked) {
						alert('Please supply a telephone number');
						valid = false;
					}
				}
				else if (this.value == 2) {
					if ($('.contactwidget2form form input[name="email"]').value == '' || !$('.contactwidget2form form input[name="email"]').blanked) {
						alert('Please supply an email address');
						valid = false;
					}
				}
			}
		});
		
		
		if (!checked && valid) {
			//check there's a name or phone number
			var phone = ($('.contactwidget2form form input[name="telephone"]').value == '' || !$('.contactwidget2form form input[name="telephone"]').blanked);
			
			var email = ($('.contactwidget2form form input[name="email"]').value == '' || !$('.contactwidget2form form input[name="email"]').blanked);
			
			if (phone && email) {
				alert('Please enter a phone number or email address');
				valid = false;
			}			
			
		}
		
		
		if (valid) {
			new Ajax({
				url: 'contactwidget/quickform2/' + getPage(),
				params: $('.contactwidget2form form'),
				callback: function() {
					alert('Thank you for your enquiry');
					$('.contactwidget2form').style.display = 'none';
					
					$('.contactwidget2form input').apply(function() {
						if (this.defaultValue) this.value = this.defaultValue;
					});
					
					$('.contactwidget2form textarea').apply(function() {
						if (this.defaultValue) this.value = this.defaultValue;
					});
					
				}
			});
		
		}
		
		$.killEvent(event);
		return false;
	});
	
	$('.distributors span,.close').addEvent('click', function() {		
		var effect = new Effect_Slide;
		effect.element = $('.distributors article');
		effect.speed = 1;
		effect.step = 10;
		if (distvis) effect.hide();
		else effect.show();
		
		distvis = !distvis;		
	});
	
	if ($('.contact2')) {
		$('.contact2').addEvent('mousedown', function() {
			this.$('.contactwidget2form').style.display = 'block';
			
		});
	}
});

var collapse = false;

function swapProductImage(el) {
	if ($('#mainimage').src != el.src.replace('thumbs/', '')) {
		$('#mainimage').setOpacity(0);
		$('#mainimage').src = el.src.replace('thumbs/', '');
	
	
		$('#mainimage').onload = function() {
			setTimeout(function() {
				$('#mainimage').fade();				
			}, 100);
		}
	}
}

function showcaseSlide(direction) {
	$('.sidebar .slider').move({direction: direction, amount: 120});
}

function selectDistributor(loc) {
	//window.location.href = name;
	 var b = document.getElementsByTagName('base');
	 if (b && b[0] && b[0].href) {
	   if (b[0].href.substr(b[0].href.length-1) == '/' && loc.charAt(0) == '/')
	     loc = loc.substr(1);
	   loc = b[0].href + loc;
	 }
	 location.href = loc;
}


function showJob(el) {
	var effect = new ElementEffect(new Effect_Slide);
	effect.effect.step = 15;
	effect.run($(el).parent().$('section'), 'show');
}


function slideDlCat(id) {
	var cat = $('.downloadcategory' + id);
	if (cat.style.display =='block') {
		cat.parent().$('.more').empty();
		cat.parent().$('.more').add('show');
	}
	else if (cat.style.display == 'none') {
		cat.parent().$('.more').empty();
		cat.parent().$('.more').add('hide');
	}
	
	cat.toggle({type: 'slide', speed: 1, step: 10});
}


function showGalleryCategory(name) {
	if (name == 0)  {
		$('.gallery > div').apply(function() {
			this.style.display = 'block';
		});
	}
	else {
		$('.gallery > div').apply(function() {
			this.style.display = 'none';
		});
		
		$('#' + name).style.display = 'block';
	}
}



function showGalleryPreview(id) {
	//$(el).$('img').style.display = 'block';
	$('.img' + id ).style.display='block';
}

function hideGalleryPreview(id) {
	$('.img' + id ).style.display = 'none';
}
