/**
 *
 */

var trevarno = {
	
	init: function() {
		
		trevarno.init_navigation();
		trevarno.init_product_category_items_hover();
		
		deliveryvan.init();
		
		$('.toggle-categories').click(function() {
			$('.categories').slideToggle('normal');
			$(this).blur();
			return false;
		});
		
		trevarno.slideshow();

		$('#eventaddressisdifferent').change(function() {
	
			if( $(this).is(':checked') ) {
				$('.event-address').slideDown('normal');
			} else {
				$('.event-address').slideUp('normal');
			}
			
		});
		
		trevarno.gallery();

		$('.social-icons a').hover(function() {
											
			$(this).find('img').stop().animate({
				marginTop: 0
			}, 'fast');
			
		}, function() {

			$(this).find('img').stop().animate({
				marginTop: '16px'
			}, 'fast');

		}).click(function(e) {
			e.preventDefault();
			$(this).blur();
			window.open( $(this).attr('href') );
		});

	}
	
	,init_navigation: function() {
		
		$('ul.navigation a').click(function() {
			var href = $(this).attr('href');
			
			$(this).blur();
			
			if( $(this).parent().find('ul').length ) {
				
				if( $(this).attr('rel') == 'directlink' ) {
					return true;
				}
				
				var ul = $(this).parent().find('ul:first');
				
				if( ul.is(':hidden') ) {
					$(this).addClass('selected');
					ul.slideDown('normal');
				} else {
					$(this).removeClass('selected');
					ul.slideUp('normal');
				}
				
				return false;
				
			}
			
			if( href == '#' ) {
				return false;
			}
			
			return true;
		});
		
	}
	
	,init_product_category_items_hover: function() {
		
		$('.categories .item').hover(function() {
			$(this).find('.image a span').addClass('hover');
		}, function() {
			$(this).find('.image a span').removeClass('hover');
		});
		
	}

	,slideshow: function() {
		
		var len = $('.page-heading-image img').length;
		
		if( len > 1 ) {
			$(window).load(function() {
				$('.page-heading-image').cycle({
					fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
				});
			});
		}
		
	}
	
	,gallery: function() {
		$(".gallery").yoxview({dataFolder:"/assets/js/yoxview/data/", lang:"en"});
	}

};

/** Delivery van methods **/
var deliveryvan = {

	init: function() {
		
		deliveryvan.init_addtobuttons();
		deliveryvan.init_updatebuttons();
		
		$('.deliveryitemsform').live('submit', function() {
			$('.update-deliveryvan').trigger('click');
			return false;
		});

		$('.item-form').live('submit', function() {
			$(this).find('.add-to-delivery-van-button').trigger('click');
			return false;
		});
		
	}
	
	,init_addtobuttons: function() {
		
		$('.add-to-delivery-van-button').click(function() {
			
			deliveryvan.show_vanoverlay();
			
			var itemid      = $(this).parents('.item-form:first').find('input.itemid').val();
			var attributeid = $(this).parents('.item-form:first').find('select.attributeid').val();
			var quantity    = $(this).parents('.item-form:first').find('input.quantity').val();
			var reminder    = $(this).parents('.item-form:first').find('input.remindernote').val();
			
			if(itemid && (quantity > 0)) {
				
				$.ajax({
					 url: '/checkout/additem.ajax.php'
					,data: 'itemid='+itemid+'&attributeid='+attributeid+'&quantity='+quantity
					,type: 'POST'
					,success: function(data) {
						$('.delivery-van-items').load('/checkout/loaditems.ajax.php', function() {
							deliveryvan.hide_vanoverlay( reminder );
						});
					}
				});
				
			}
			
			return false;
			
		});

	}
	
	,init_updatebuttons: function() {

		$('.update-deliveryvan').live('click', function() {
			
			deliveryvan.show_vanoverlay();
			
			var serialized = $(this).parents('form:first').serialize();

			$.ajax({
				 url: '/checkout/updateitems.ajax.php'
				,data: serialized
				,type: 'POST'
				,success: function(data) {
					$('.delivery-van-items').load('/checkout/loaditems.ajax.php', function() {
						deliveryvan.hide_vanoverlay();
					});
				}
			});

			return false;
			
		});

	}
	
	,show_vanoverlay: function() {
		
		$('.overlay').css('opacity', 0).show().fadeTo('fast', 0.8);
		
	}

	,hide_vanoverlay: function( reminder ) {
		
		$('.overlay').fadeOut('fast', function() {
			if( reminder ) {
				alert( reminder );
			}
		});
		
	}
	
	,validateCheckoutForm: function( form ) {
		
		if( form.emailaddress.value == '' ) {
			alert('Please enter your email address to continue');
			form.emailaddress.focus();
			return false;
		}
		
		if( form.firstname.value == '' ) {
			alert('Please enter your first name to continue');
			form.firstname.focus();
			return false;
		}
		if( form.lastname.value == '' ) {
			alert('Please enter your last name to continue');
			form.lastname.focus();
			return false;
		}
		if( form.phone.value == '' ) {
			alert('Please enter your phone number to continue');
			form.phone.focus();
			return false;
		} else {
			var phoneNo = form.phone.value;
			
			var phoneRE = /^\d\d\d\d\d \d\d\d\d\d\d$/;  
			if (phoneNo.match(phoneRE)) {  
			} else {  
				alert( "The phone number entered is invalid!\nFormat example: 01209 831333" );  
				form.phone.focus();
				return false;  
			}  
		}
		if( form.address1.value == '' ) {
			alert('Please enter the first line of your address to continue');
			form.address1.focus();
			return false;
		}
		if( form.town.value == '' ) {
			alert('Please enter your town address to continue');
			form.town.focus();
			return false;
		}
		if( form.county.value == '' ) {
			alert('Please enter your county address to continue');
			form.county.focus();
			return false;
		}
		if( form.postcode.value == '' ) {
			alert('Please enter your postcode to continue');
			form.postcode.focus();
			return false;
		}
		
		return true;
		
	}

};

var siteforms = {

	validateForm: function( form ) {

		if( form.emailaddress.value == '' ) {
			alert('Please enter your email address to continue');
			form.emailaddress.focus();
			return false;
		}
		
		if( form.firstname.value == '' ) {
			alert('Please enter your first name to continue');
			form.firstname.focus();
			return false;
		}
		if( form.lastname.value == '' ) {
			alert('Please enter your last name to continue');
			form.lastname.focus();
			return false;
		}
		if( form.phone.value == '' ) {
			alert('Please enter your phone number to continue');
			form.phone.focus();
			return false;
		} else {
			var phoneNo = form.phone.value;
			
			var phoneRE = /^\d\d\d\d\d \d\d\d\d\d\d$/;  
			if (phoneNo.match(phoneRE)) {  
			} else {  
				alert( "The phone number entered is invalid!\nFormat example: 01209 831333" );  
				form.phone.focus();
				return false;  
			}  
		}

		if( form.address1.value == '' ) {
			alert('Please enter the first line of your address to continue');
			form.address1.focus();
			return false;
		}
		if( form.town.value == '' ) {
			alert('Please enter your town address to continue');
			form.town.focus();
			return false;
		}
		if( form.county.value == '' ) {
			alert('Please enter your county address to continue');
			form.county.focus();
			return false;
		}
		if( form.postcode.value == '' ) {
			alert('Please enter your postcode to continue');
			form.postcode.focus();
			return false;
		}
		
		return true;
		
	}

};

$(document).ready(trevarno.init);
