$(document).ready(function(){	
	// Cufon.replace('#header p strong, #main-feature h1, #feature-items h2 a, #news h2, #mainarea h1, #mainarea h2, #mainarea h3, #mainarea h4, #callto p, #sidenav li a.depth_2 span, #sidenav a, #nav ul li a', { fontFamily: 'cufoned-myriad' });
	
	$('#slideshow').after('<div id="slideshowNav">').cycle({
		timeout: 6000,
		pager: '#slideshowNav'
	});
	
	var images = [];
	$('#product_slideshow').after('<ul id="product_slideshow_nav">').cycle({
		timeout: 20000,
		pager: '#product_slideshow_nav',
		pagerAnchorBuilder: function(idx, slide) {
			var src = slide.style.backgroundImage.replace("url(\"", '').replace("\")", '').replace("url(", '').replace(")", '');
			
			return '<li><a href="#"><img src="' + src + '" width="0" height="0" /></a></li>';
		}
	}).each(function() {
		$('#product_slideshow_nav img').each(function() {
			this.check_dims = function() {
				var img = this;
				if(img) {
					var max_width = 40, max_height = 23,
						image_width = img.naturalWidth, image_height = img.naturalHeight,
						ratio = image_width / image_height,
						max_ratio = max_width / max_height,
						new_width, new_height,
						padding_top = 0;
					
					if(ratio > max_ratio) { //width dominant axis
						if(image_width > max_width) {
							new_width = max_width;
							new_height = Math.round((max_width / image_width) * image_height);
						} else {
							new_width = image_width;
							new_height = image_height;
						}
					} else {
						if(image_height > max_height) {
							new_height = max_height;
							new_width = Math.round((max_height / image_height) * image_width);
						} else {
							new_width = image_width;
							new_height = image_height;
						}
					}
					
					if(new_width == max_width - 1) new_width = max_width;
					if(new_height == max_height - 1) new_height = max_height;
					
					if(new_height < max_height) {
						$(img).css('padding-top', parseInt((max_height - new_height) / 2) + 'px');
						//style = ' style="padding-top: ' + parseInt((max_height - new_height) / 2) + 'px;"';
					}
					
					img.height = new_height;
					img.width = new_width;
				}
			}
			
			$(this).bind('load', function() {
				this.check_dims();
			});
				
			//this.check_dims();
		});
	});
	
	
	$('#copy_across_link').click(function() {
		$('#billing_address input').each(function() {
			$('#' + this.id.replace('_billing_', '_shipping_'))[0].value = this.value;
		});
		
		return false;
	});
	
	
	$('#main_slideshow').cycle();
	
	if($('#section_products_dropdowns_area')[0]) {
		$('#section_products_dropdowns_product_id').change(function() {
			$('#section_products_dropdowns_images li').hide();
			$('#section_products_dropdowns_image_' + $(this).val()).show();
		});
		
		$('#section_products_dropdowns_product_id').change();
	}
	
	$('.date input, input.date').datepicker({ dateFormat: 'dd/mm/yy', gotoCurrent: true });
	
	$('.show_hide').click(function() {
		$($(this).attr('href')).slideToggle();
		return false;
	});
	
	$('.show_hide').each(function() {
		$($(this).attr('href')).hide();
	});
	
	$('#contact_us_about_this_form_holder').hide();
	
	$('#contact_us_about_this_link').click(function() {
		if($('#contact_us_about_this_form_holder').is(':visible')) {
			$('#contact_us_about_this_form_holder').stop().slideUp();
			$(this).find('span').html('&#9654;');
		} else {
			$('#contact_us_about_this_form_holder').stop().slideDown();
			$(this).find('span').html('&#9660;');
		}
		return false;
	});
	
	if(document.location.href.indexOf('#contact_us_about_this') > -1)
		$('#contact_us_about_this_link').click();
	
	common_init({ no_init_slides: true });
});
