jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

$(document).ready(function () {

	$("a[href='#']").click(function(e){e.preventDefault()});

	$("a[rel='external']").each(function(){
		$(this).attr("target","_blank");
	});
	
	if($('#homeProductsWrapper ul').length) {
		$('#homeProductsWrapper ul').carouFredSel({
			visibleItems: 1,
			auto: {
				play: true,
				duration: 1500
			},
			scroll: {
				duration: 1500
			},
			prev: {
				button: '#prev'
			},
			next: {
				button: '#next'
			},
			pagination: {
				container: '#pagination'
			}		
		});	
	}

	if($('#recipesWrapper ul').length) {	
		$('#recipesWrapper ul').carouFredSel({
			visibleItems: 1,
			auto: {
				play: false
			},
			scroll: {
				duration: 1000
			},
			prev: {
				button: '#prevRecipe'
			},
			next: {
				button: '#nextRecipe'
			}
		});	
	}

	$('#country_list, #partner_country_list, .uniformSel').uniform();

	jQuery.validator.addMethod("USAOnly", function(value, element) {
		var country = $("#countryId");
		if(country.length && country.val()=='20172'){
			if(element.value!=null && typeof element.value != 'undefined' && element.value.length>0){
				return true;
			}else{
				return false;
			}
		}else{
			return true;
		}
	}, "This field is required.");


	
	$("#contact_form").validate({
		rules: {
			name: 'required',
			surname: 'required',
			city: 'required',			
			country_id: 'required',
			state: {USAOnly: true},	
			zipcode: 'required',								
			phone: 'required',
			fax: 'required',						
			email: {
				required: true,
				email: true
			},
			email_confirm: {
				required: true,
				email: true				
			},			
			note: 'required',			
			privacy: 'required',
			kaptcha: 'required'
		}
	});
	
	$("#login_form").validate({
		rules: {
			username: 'required',
			password: 'required'					    			    			    			    			
		}
	});	

	if (!($('#firstLev li.active .secondLev').length)) {
		$('#secondNav').addClass('oneLev');
	}
	
	if ($('#secondNav').length == 0) {
		$('#header').addClass('pushDown');
	}
	
	if ($('.secondLev').length == 0) {
		$('#secondNav').addClass('oneLev');
	}
	
	if($('#firstLev li.active .secondLev').length) {
		$('#firstLev li.active').addClass('nobd');
	}
	
	$('#firstLev li a').each(function() {
		$(this).bind({
			click: function (e) {
				if($(this).siblings('.secondLev').length) {
	 				e.preventDefault();
					$('#secondNav').removeClass('oneLev');
					$('#firstLev li.subActive').removeClass('subActive');
					$('#firstLev .secondLev.active').removeClass('active');
					$(this).parent().addClass('subActive');
					$(this).siblings('.secondLev').addClass('active');
					$('#firstLev li.nobd').removeClass('nobd');
				}
			}
		});
	});
	
	$('#productList p, #openList').bind('click', function (e) {
		e.preventDefault();
		if ($('#openList').hasClass('closeList')) {
			$('#productList ul').slideUp(300);
			$('#openList').removeClass('closeList');		
		} else {
			$('#openList').addClass('closeList');
			$('#productList ul').slideDown(300);			
		}
	});
	
	var singleProductWidth = $('.product').width(); 
	var allProductWidth = singleProductWidth * $('.product').length;  
	
	$('#slider').width(allProductWidth);

	if($('.colorbox').length) {
		$('.colorbox').colorbox({
			onComplete: function () {
				initialize();
			}
		});
	}
	
	if($('#select_country').length) {
		$(document).keydown(function(e){
			switch (e.keyCode) {
				case 13:
					$('#select_country button').click();
					break;
			}   
		});
	}	

	if($.browser.msie && (parseInt($.browser.version, 10) < 8)) {
		$('#productList ul li:last-child').addClass('last-child');
	}
	
	$('#slider .product a img').bind({
		mouseenter: function () {
			$(this).animate({
				width: '320px',
				height: '270px',
				top: '0',
				left: '0'				
			}, 100);
		},
		mouseleave: function () {
			$(this).animate({
				width: '300px',
				height: '250px',
				top: '10px',
				left: '10px'
			}, 100);
		}		
	});
	
	$('a.nord').bind({
		mouseenter: function () { $(this).closest('ul').addClass('nordItalia'); },
		mouseleave: function () { $(this).closest('ul').removeClass('nordItalia'); }		
	});
	
	$('a.centro').bind({
		mouseenter: function () { $(this).closest('ul').addClass('centroItalia'); },
		mouseleave: function () { $(this).closest('ul').removeClass('centroItalia'); }		
	});
	
	$('a.sud').bind({
		mouseenter: function () { $(this).closest('ul').addClass('sudItalia'); },
		mouseleave: function () { $(this).closest('ul').removeClass('sudItalia'); }		
	});		

});

function initialize() {

	var latlng = new google.maps.LatLng(40.861148,14.283342);

	var myOptions = {
		zoom: 14,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		navigationControl: true,
		navigationControlOptions: {
			style: google.maps.NavigationControlStyle.ZOOM_PAN
		},
		mapTypeControl: false,
		scaleControl: true
	};

	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

	var marker = new google.maps.Marker({
		position: latlng,
		map: map,
		title:"Centro Direzionale"
	});

	marker.setMap(map);
	
	var contentString = '<div id="infowindow"><p><strong>Bellaitalia</strong><br />Centro direzionale Isola E2 - 80143 Napoli, Italy</p></div>';

	var infowindow = new google.maps.InfoWindow({
	    content: contentString
	});

	if(!($.browser.msie && (parseInt($.browser.version, 10) < 7))) {	
		infowindow.open(map,marker);
	}
	
}

