var countyList, categoryList;

$(document).ready(function(){
	$('.signupForm .email').focus(function(){
		if($(this).val() == 'Enter your email address'){
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val() == ''){
			$(this).val('Enter your email address');
		}
	});
	
	$('.signupForm .name').focus(function(){
		if($(this).val() == 'Enter your name'){
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val() == ''){
			$(this).val('Enter your name');
		}
	});
	
	$('.signupForm .company').focus(function(){
		if($(this).val() == 'Enter your business name'){
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val() == ''){
			$(this).val('Enter your business name');
		}
	});
	
	$('.countyListInputs').hide();
	$('.categoryListInputs').hide();
	
	$('.countyList').click(function(){
		$('.countyListInputs').fadeIn();
		$('.categoryListInputs').fadeOut();
	});
	
	$('.categoryList').click(function(){
		$('.categoryListInputs').fadeIn();
		$('.countyListInputs').fadeOut();
	});
	
	
	$('.countyListClose').click(function(){
		updateCountyList();
		$('.countyListInputs').fadeOut();
	});
	
	$('.categoryListClose').click(function(){
		updateCategoryList();
		$('.categoryListInputs').fadeOut();
	});
	
	function updateCountyList(){
		countyList = '';
		$('.countyListInputs p label input').each(function(){
			if($(this).attr('checked')){
				countyList += $(this).parent().find('span').text() + '<br />';
			}
		});
		if(countyList == ''){
			countyList = 'All Counties';
		}
		$('.countyList').html(countyList);
	}
	
	function updateCategoryList(){
		categoryList = '';
		$('.categoryListInputs p label input').each(function(){
			if($(this).attr('checked')){
				categoryList += $(this).parent().find('span').text() + '<br />';
			}
		});
		if(categoryList == ''){
			categoryList = 'All Wedding Suppliers';
		}
		$('.categoryList').html(categoryList);
	}
	
	$('.gallery .large').cycle({
		fx: 'fade',
		timeout: 5000,
		speed: 'slow',
		pager: '.gallery .thumbs',
		pagerAnchorBuilder: function(idx, slide) { 
			if((idx + 1) % 4==0){
				return '<a style="margin-right:0px;" href="javascript:;"><img src="' + slide.src + '" width="72" /></a>'; 
			}else{
				return '<a href="javascript:;"><img src="' + slide.src + '" width="72" /></a>'; 
			}
		} 
	});
	
	$('.fairsAttending .fairs').cycle({
		fx: 'scrollUp',
		timeout: 0,
		speed: 750,
		next: '.fairsAttending .next'
	});
	
	$('#searchField').Postcoder({
		searchType: "PREMISE_LIST",
		searchURL: "postcoder/websoap.php",
		searchButton : "searchButton",
		outputMap: {
			addressLines: ["address1", "address2", "address3"],
			postTown: "town",
			county: "county"
		},
		debug: false
	});
	
	$('input').each(function(){
		if($(this).attr('type') == 'checkbox'){
			$(this).click(function(){
				$(this).attr('force_check', 'no');
			});
		}
	});
});

function clearText(theField)
{
	if (theField.defaultValue == theField.value)
		theField.value = '';
}

function addText(theField)
{
	if (theField.value == '')
		theField.value = theField .defaultValue;
}

function checkAll(obj, input_name){
	switch(obj.checked){
		case true:
			$('input').each(function(){
				if($(this).attr('type') == 'checkbox'){
					if($(this).attr('name').indexOf('interest') != -1){
						if($(this).attr('checked') != 'checked'){
							$(this).attr('force_check', 'yes');
						}
						$(this).attr('checked', 'checked');
					}
				}
			});
		break;
		default:
			$('input').each(function(){
				if($(this).attr('type') == 'checkbox'){
					if($(this).attr('name').indexOf('interest') != -1){
						if($(this).attr('force_check') == 'yes'){
							$(this).attr('checked', false);
						}
					}
				}
			});
		break;
	}
}
