var RRCart;

function closeNotification(){
	return $(this).closest('.product-notification-container, .notification-e').fadeOut();
}

jQuery(function($){


	if ($.browser.msie) $('html').addClass('ie');

	RRCart = {

		button:  $('#cart-box'),
		updateContent: function(content){
			if (RRCart.content)
				RRCart.content.replaceWith(content);

			RRCart.content = content;
			RRCart.button.click(function(e){
				e.preventDefault();
				content.toggle();
			});
		}
	};

	
	RRCart.updateContent($('#cart-box + div'));
	
	var planet = {
		button:  $('#planet'),
		content: $('#planet + div')
	};

	planet.button.click(function(e){
		e.preventDefault();
		planet.content.toggle();
	});
	
	
	
	// close notification
	$('.cm-notification-close').click(closeNotification);
	
	
	// populate contact form
	$('.aside-form')
		.find('input[name = "Prodotto"]').val($('.l-side h1').text())

		.end().submit(function(e){
			e.preventDefault();
			var self = $(this);

			var empty = self.find('input:visible, select, textarea').filter(function(){
				return !$(this).val();
			});

			if (empty.length) return alert('Completa tutti i campi.');

			$.post(self.attr('action'), self.serialize(), function(){
				self.find('input:submit').replaceWith($('<span>').text('Richiesta inoltrata.').css('float', 'right'));
			});
		});


	// profile form validation
	var profileForm = $('#profile_form');
	if (!profileForm) return;

	var fields = null;
	profileForm.submit(function(e){
		if (!fields) fields = profileForm.find('.cm-required + input');

		fields.css('border', 'none');
		var empty = fields.filter(function(){ return !this.disabled && !this.value; });
		if (!empty.length) return;

		e.preventDefault();
		empty.css('border', '1px solid #e00');
		$("html:not(:animated),body:not(:animated)")
     	   .animate({scrollTop: empty.first().offset().top -25}, 1100);
	});
	
	
	
	// orders date picker
	$( "#f_date, #t_date" ).datepicker({
		dateFormat: 'dd/mm/yy'
	});
});

jQuery.fn.extend({
	switchAvailability: function(flag, hide)
	{
		if (hide != true && hide != false) {
			hide = true;
		}

		if (flag == false || flag == true) {
			if (flag == false) {
				$(':input:not(.cm-skip-avail-switch)', this).attr('disabled', '');
				$(':input:not(.cm-skip-avail-switch)', this).removeClass('disabled');
				if (hide) {
					this.show();
				}
			} else {
				$(':input', this).attr('disabled', 'disabled');
				$(':input', this).addClass('disabled');
				if (hide) {
					this.hide();
				}
			}
		} else {
			$(':input', this).each(function(){
				var self = $(this);
				self.attr('disabled', !self.attr('disabled'));
			});
			if (hide) {
				this.toggle();
			}
		}
		if (typeof(control_buttons_container) != 'undefined' && control_buttons_container.length) {
			jQuery.buttonsPlaceholderToggle();
		}
	}
});



jQuery.datepicker.regional['it'] = {
        clearText: 'Svuota', clearStatus: '',
            clearStatus: 'Cancella la data corrente',
            closeText: 'Chiudi', closeStatus: '',
            closeStatus: 'Chiudi senza cambiamenti',
            prevText: '&lt;Prec', prevStatus: '',
            prevStatus: 'Mostra il mese precedente',
            nextText: 'Succ&gt;', nextStatus: '',
            nextStatus: 'Mostra il mese successivo',
            currentText: 'Oggi', currentStatus: '',
            currentStatus: 'Mostra il mese corrente',
            monthNames:
['Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno',
            'Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'],
            monthNamesShort: ['Gen','Feb','Mar','Apr','Mag','Giu',
            'Lug','Ago','Set','Ott','Nov','Dic'],
            monthStatus: 'Mostra un altro mese',
            yearStatus: 'Mostra un altro anno',
            weekHeader: 'Sm', weekStatus: '',
            weekStatus: 'Settimana dell\'anno',
            dayNames:
['Domenica','Luned&#236','Marted&#236','Mercoled&#236','Gioved&#236','Venerd&#236','Sabato'],
            dayNamesShort: ['Dom','Lun','Mar','Mer','Gio','Ven','Sab'],
            dayNamesMin: ['Do','Lu','Ma','Me','Gio','Ve','Sa'],
            dayStatus: 'Imposta DD come primo giorno della settimana',
            dateStatus: 'Seleziona DD, M d',
            dateFormat: 'dd/mm/yy', firstDay: 1,
            initStatus: 'Seleziona una data',
            isRTL: false
    };
jQuery.datepicker.setDefaults($.datepicker.regional['it']); 

