/* javascript for cellarmarque.co.uk by stuart sillitoe / pulse8 2007 */



// on dom load
$(function()
{
	
	// set up country id onchange - will populate regions dropdown
	$('#country_id, #producer_country_id').change(function()
	{
		populateRegions($(this).fieldValue(), $('option:selected', this).text());
	});
	
	$('#country_id_search').change(function(){
		if($(this).val() != ''){
			
			$('#region_id_search').attr('disabled','disabled').html('<option value="">Loading...</option>');
			$.get('p/populateRegionOptions.php?country_id='+$(this).val(),function(strHtml){
				$('#region_id_search').html(strHtml).attr('disabled','');
			});
		
		}else{
			$('#region_id_search').html('<optgroup label="Please select a country"><option value="">All Regions</option></optgroup>');
		}
	});
	
	/*
	// add wine table row hovers
	$('#winetable tr').mouseover(function()
	{
		$('td', this).addClass('rowHover');
	}).mouseout(function()
	{
		$('td', this).removeClass('rowHover');
	});
	*/
	
	
	// alternate floats on the new wines page
	$('.newWineBox:odd').css('float','right').after('<div class="clear"></div>');
	

	// assign a copy address function to this lovely button
	$('#copyAddressToDeliveryButton').click(function()
	{
		copyAddressToDeliveryAddress();
	});
	
	var searchDefault = 'Enter your keyword search here';
	
	$('#txtKeyword').focus(function(){
		if($(this).val() == searchDefault){
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val() == ''){
			$(this).val(searchDefault);
		}
	});
	
	// open external links in new window
	$('a[href^=http]').each(function()
	{
		$(this).attr('target', '_blank');
	});
	

	$('a.lightbox').lightBox();
	
	
	setTimeout(function(){
		$('object').css('z-index','1');
	},1000);
	
	
	
	$('#slideshow').cycle({
		speed:		2000, 
		timeout:	6000, 
		width:		368, 
		height:		276, 
		fit:		true, 
		fx: 		'fade' 
	});
	
	
	
	
});


















// functions

// populate regions from coutry id
function populateRegions(country_id, country_name)
{
	$('#regionSelectWrapper')
		.html('<span class="loading">Loading ' + country_name + ' Regions</span>')
		.Pulsate(150,2)
		.load('../p/populateRegions.php?country_id=' + country_id);
}












function copyAddressToDeliveryAddress()
{
	// jquery is completely mental. lovin it like this.
	$('input, select', '#customerRegistrationForm_normalAddress').each(function()
	{
		var delEquiv = $(this).attr('name').replace(/cust_/, 'cust_del_');
		$('#' + delEquiv).attr('value', $(this).attr('value'));
	});
}














function agreedTerms()
{
	if ( $('#cust_terms').val() == 1 )
	{
		return true;
	}
	else
	{
		alert('You must agree to the terms and conditions before continuing.');
		return false;
	}
}
function showTerms()
{
	window.open('_terms.htm', 'terms', "resizable=1, location=1,status=1,scrollbars=1,width=600,height=460");
}







