var featureSlider = {
	init: function(){
		if( !$('#feature').length )
			return false; 
		featureSlider.initSlide();
	},
	initSlide: function() {
		$('#feature').siteFeature({
			autoPlay: true,
			autoPlayInterval: 3500
		});
	}
}
var tabs = false;
var productsTab = {
	init: function() {
		var $product_accordion = $('.product-accordion');
		if( !$product_accordion.length )
			return false;
		$product_accordion.tabs('.product-accordion .product-accordion-pane', {
			tabs: '.product-accordion-title',
			effect: 'slide',
			initialIndex: 0
		});
		var self = this;
		tabs = $product_accordion.data('tabs');
		// display live total
		$product_accordion.find('.convention_choice').live('change', function() {
			self.calculateTotal(self.checkConvention());
		});
		$('input[name="produit_id[]"]').live('change', function() {
			self.calculateTotal(self.checkConvention());
		});
	},
	calculateTotal: function(convention) {
		var total = 0;
		$('#product-form').find('input[name="produit_id[]"]').each(function() {
			var $this = $(this),
				$parent = $this.parent('li');
			if($this.is(':checked')) {
				if(convention)
					total += parseInt($parent.find('.price-convention').text());
				else
					total += parseInt($parent.find('.price-full').text());
			}
		});
		$('.product-total-number').find('span').text(total);
	},
	checkConvention: function() {
		var convention = false;
		if( $('#convention_yes').is(':checked') )
			convention = true;
		else
			convention = false;
		return convention;
	}
}
var conventionChecker = {
	init: function() {
		var $check = $('#convention_yes');
		if( !$check.length )
			return false;
		this.changePrice();
	},
	changePrice: function() {
		var $choice = $('.convention_choice'),
			$price_full = $('.price-full'),
			$price_conv = $('.price-convention');
		$price_conv.hide();
		$choice.live('change', function() {
			var $checked = $(this).attr('value');
			if( $checked == 1 ) {
				$price_full.hide();
				$price_conv.show();
			} else {
				$price_conv.hide();
				$price_full.show();
			}
			if(tabs !== false) {
				setTimeout(function(){
					tabs.next();
				}, 100);
			}
		});
	}
}
var oldConventionChecker = {
	init: function() {
		var $check = $('#convention');
		if( !$check.length )
			return false;
		this.changePrice();
	},
	changePrice: function() {
		var $choice = $('#convention'),
			$price_full = $('.price-full'),
			$price_conv = $('.price-convention');
		$price_conv.hide();
		$choice.live('change', function() {
			
			if( $(this).is(':checked') ) {
				$price_full.hide();
				$price_conv.show();
			} else {
				$price_conv.hide();
				$price_full.show();
			}
			if(tabs !== false) {
				setTimeout(function(){
					tabs.next();
				}, 100);
			}
		});
	}
}

jQuery.fn.anchorAnimate = function(settings) {
 	settings = jQuery.extend({
		speed : 1100
	}, settings);	
	
	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")
			
			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({scrollTop: destination}, settings.speed, function() {
				window.location.hash = elementClick
			});
		  	return false;
		})
	})
}
$(document).ready(function() {
	productsTab.init();
	// site feature
	featureSlider.init();
	conventionChecker.init();
	oldConventionChecker.init();
	// smooth scroll
	$(".smooth-scroll").anchorAnimate({
		speed: 800
	});
	
	// enlarge search field on focus
	$("#search-field").focus(function() {
		$(this).animate({
			width: "250px"
		 }, 500 );
	}).blur(function() {
		$(this).animate({
			width: "150px"
		}, 500);
	});


	
	// display code group field on checked
	/*
	var code_group = $('.code-group-checked');
	if ( code_group.length == 1 ) {
		code_group.click( function() {
			if ( code_group.is(':checked') ) {
				$('#code-group-box').slideDown();
			}
		});
		code_group.blur( function() {
			if ( code_group.is(':checked') ) {
				return false;
			} else {
				$('#code-group-box').slideUp();
			}
		});
	}*/

	// display code group field on checked
	var code_group = $('.code-group-checked');
	if ( code_group.length == 1 ) {
		code_group.click( function() {
			if ( code_group.is(':checked') ) {
				$('#code-group-box').slideDown();
			}
		});
		code_group.change( function() {
			if ( code_group.attr('checked') != true ) {
				$('#code-group-box').slideUp();
			}
		});
	}
	
	// clear search field on focus
	$("#search-field").focus(function() {
	    if( this.value == this.defaultValue ) {
	    this.value = "";
	    }
	}).blur(function() {
	    if( !this.value.length ) {
	    this.value = this.defaultValue;
	    }
	});

	
	// check form fields
	var form = $('#checkout2');
	if ( form.length == 1 ) {
		form.each(function() {
			$('.required').blur(function() {
				var value = $(this).attr('value');
				if(!value) {
					$(this).addClass('empty').nextAll('small').fadeIn();
					$(this).next('span').removeClass('field-on').addClass('field-off');
				} else {
					$(this).removeClass('empty').nextAll('small').slideUp();
					$(this).next('span').removeClass('field-off').addClass('field-on');
				}
			});
		});
		form.submit(function() {
			var ok = true;
			$('.required').each(function() {
				var value = $(this).attr('value');
				if (!value) {
					$(this).addClass('empty').nextAll('small').fadeIn();
					$(this).next('span').removeClass('field-on').addClass('field-off');
					ok = false; 
				}
			});
			return ok;
		});
	}
	
	// google map
	var memberpage = $('#member-page');
	if ( memberpage.length == 1 ) {
		var $name = $('h1').text();
		var $place = $('address').html();
		initialize( $name, $place);
	}
	var ish_institut = $('#ish-institut').parent();
	var ish_formation = $('#ish-formation');
	if( !ish_formation.is(':checked') )
		ish_institut.hide();
	
	ish_formation.change(function() {
		if( $(this).is(':checked') )
			$('#ish-institut').parent().fadeIn('fast');
		else
			$('#ish-institut').parent().fadeOut('fast');
	});
});
function initialize( name, place ) {
	geocoder = new google.maps.Geocoder();
	if (geocoder) {
		var address = document.getElementById("adresse").value;
		geocoder.geocode( {'address': address}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				
				var latlng = results[0].geometry.location;
				
				var mapOptions = {
					zoom: 6,
					center: latlng,
					mapTypeControl: false,
				    mapTypeControlOptions: {
				    	style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
				    	position: google.maps.ControlPosition.BOTTOM_RIGHT	
				    },
				    
					mapTypeId: google.maps.MapTypeId.ROADMAP
				}
				map = new google.maps.Map(document.getElementById("member-map"), mapOptions);

				var windowContent = '<div id="map-info-window"><span class="info-window-name">'+name+'</span></div>';
				
				var infowindow = new google.maps.InfoWindow({
					content: windowContent,
					maxWidth: 350
				});
				var marker = new google.maps.Marker({
					position: latlng, 
					map: map
				});
				infowindow.open(map,marker);

				google.maps.event.addListener(marker, 'click', function() {
					infowindow.open(map,marker);
				});
			} else {
				alert("Impossible de trouver l'adresse pour les raisons suivantes : " + status);
			}
		});
	}
}
