// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// Slide the the elements contained in the specified tag in a infinite-loop carousel every xxx seconds.
// Stop and re-activate the animation onmouseover/out the tag area
var Carousel = {
	active: 0,
	init: function(options) {
		this.options = options || {};
		this.timing = this.options.timing || 5000;
		this.carousel = this.options.carousel || '#news_carousel';
		this.table = this.options.table || '#news_table';
		this.element = this.options.element || '.news_el';
		this.width = 295;
		this.maxNbr = $(this.table + ' > ' + this.element).length;
		this.last_left = (this.maxNbr-1)*this.width;
		if ($(this.carousel).length > 0 && this.maxNbr > 1) {
			this.adjustCss();
			this.animate();
			this.manageHovering();
		}
	},
	adjustCss: function() {
		$(this.table).css('width', this.width * this.maxNbr);
	},
	animate: function() {
		var that = this;
		this.intervalId = setInterval(function()  {
			if (that.active === (that.maxNbr-1)) {
				that.slideTo(0, 300);
			} else {
				that.slideTo(that.active+1);
			}
		}, this.timing);
	},
	stop: function() {
		clearInterval(this.intervalId);
	},
	slideTo: function(n, d) {
		var that = this;
		this.active = n;
		var d = d || 700;
		var left = this.active*this.width*(-1);
		$(this.table).animate({ 'left': left, duration: d});
	},
	manageHovering: function() {
		var that = this;
		$(this.table).mouseover(function() {
			that.stop();
		});
		$(this.table).mouseout(function() {
			that.animate();
		});
	}
};

// Fade In images aleternatively and let the claim appear once the image is displayed
var FadingImages = {
	active: 0,
	intervalId: null,
	init: function(options) {
		this.options = options || {};
		this.timing = this.options.timing || 6000;
		this.imagesTable = this.options.imagesTable || '#fading_pics_table';
		this.box = this.options.box || '#shadow';
		this.pic = this.options.pic || '.fading_pic';
		this.claim = this.options.claim || '.claim';
		this.width = 954;
		this.maxNbr = $(this.pic).length;
		if ($(this.imagesTable).length > 0 && this.maxNbr > 1) {
			this.start();
			this.animate();
			this.manageImages();
		}
	},
	showCurClaim: function() {
		$(this.claim + ':eq(' + this.active + ')').fadeIn(1000);
	},
	start: function() {
		$(this.claim).hide();
		$(this.pic).hide();
		$(this.pic + ':eq(' + this.active + ')').show();
		this.showCurClaim();
	},
	animate: function() {
		var that = this;
		this.intervalId = setInterval(function()  {
			if (that.active === (that.maxNbr-1)) {
				that.active = 0;
			} else {
				that.active = that.active+1;
			}
			that.showPic();
		}, this.timing);
	},
	stop: function() {
		clearInterval(this.intervalId);
	},
	hidePic: function(n, fn) {
		var that = this;
		if ($(this.claim).length > 0) {
			$(this.claim + ':eq(' + n + ')').fadeOut(1000, function() {
				$(that.pic + ':eq(' + n + ')').delay(200).fadeOut(2000);
				fn();
			});
		} else {
			$(that.pic + ':eq(' + n + ')').delay(200).fadeOut(2000);
			fn();
		}
	},
	showPic: function() {
		var that = this,
			prev;
		prev = this.active === 0 ? (this.maxNbr-1) : (this.active-1);
		this.hidePic(prev, function() {
			$(that.pic + ':eq(' + that.active + ')').fadeIn(2000, function() {
				that.showCurClaim();
			});
		});
	},
	manageImages: function() {
		var that = this;
		$(this.box).mouseover(function() {
			that.stop();
		});
		$(this.box).mouseout(function() {
			that.animate();
		});
	}
};

// Manage the pictures slideshow by clicking on the next or previus button
var Slideshow = {
	active: 0,
	init: function(options) {
		this.options = options || {};
		this.slideshow = this.options.slideshow || '#slideshow';
		this.maxNbr = $(this.slideshow + ' > img').length;
		if ($(this.slideshow).length > 0 && this.maxNbr > 1) {
			this.showActive();
			this.manageSlideshow();
		}
	},
	manageSlideshow: function() {
		var that = this;
		$(this.slideshow + ' > span > a.prev').click(function() {
			that.active = that.active === 0 ? (that.maxNbr-1) : (that.active-1);
			that.showActive();
		});
		$(this.slideshow + ' > span > a.next').click(function() {
			that.active = that.active === (that.maxNbr-1) ? 0 : (that.active+1);
			that.showActive();
		});
	},
	showActive: function() {
		$(this.slideshow + ' > img').hide();
		$(this.slideshow + ' > img:eq(' + this.active + ')').fadeIn(1000);
	}
};

// Manage the sub menu opening
var Menu = {
	init: function(options) {
		this.options = options || {};
		this.menuClass = this.options.menuClass || 'menu_overlay';
		this.menuLinkClass = this.options.menuLinkClass || 'open_menu';
		if ($('.' + this.menuLinkClass).length > 0) {
			this.manageMenu();
		}
	},
	manageMenu: function() {
		var that = this;
		$('.' + this.menuLinkClass).click(function() {
			$('.' + that.menuClass).toggle();
			return false;
		});
	}
};

// Manage the accordion
var Accordion = {
	init: function(options) {
		this.options = options || {};
		this.accordionId = this.options.accordionId || 'accordion';
		if ($('#' + this.accordionId).length > 0) {
			this.manageAccordion();
		}
	},
	manageAccordion: function() {
		var that = this;
		$('#' + this.accordionId).accordion({
			autoHeight: false,
			header: 'h3',
			active: -1,
			collapsible: true
		});
	}
};

// Load the GMAP mashup and lazily add it to the page
var Gmap = {
	map:  [],
	init: function(options) {
		this.options = options || {};
		this.canvasIds = this.options.canvasIds || ['canvas_1', 'canvas_2'];
		this.latLongs = this.options.latLongs || [
			[44.56901, 10.803748],
			[39.960444,-0.243231]
		];
		this.startZoom = this.options.startZoom || 14;
		if ($('#' + this.canvasIds[0]).length > 0) {
			this.initMap();
		}
	},
	initMap: function() {
		var i = 0,
			gLatLong,
			gOptions,
			gMap;
		for (i = this.canvasIds.length; i--;) {
			gLatLong = new google.maps.LatLng(this.latLongs[i][0], this.latLongs[i][1]);
			gOptions = {
				zoom: this.startZoom,
				center: gLatLong,
				mapTypeId: google.maps.MapTypeId.SATELLITE
			};
			gMap = new google.maps.Map(document.getElementById(this.canvasIds[i]), gOptions);
			new google.maps.Marker({
				position: gLatLong,
				map: gMap
			});
		}
	}
};

var Placeholder = {
	init: function(options) {
		this.options = options || {};
		this.placeholderId = this.options.placeholderId || 'search_input';
		this.placeHolderLabel = this.options.placeHolderLabel || 'RICERCA PRODOTTI';
		if ($('#' + this.placeholderId).length > 0) {
			this.manageLabel();
		}
	},
	manageLabel: function() {
		var that = this,
			label;
		$('#' + this.placeholderId)
			.focus(function() {
				if ($(this).val() === that.placeHolderLabel) {
					$(this).val('');
				}
			})
			.blur(function() {
				if ($(this).val().match(/^\s*$/)) {
					$(this).val(that.placeHolderLabel);
				}
			});
	}
};

// Load all of the routines when window loading is ended
$(document).ready(function() {
	Carousel.init();
	FadingImages.init();
	Slideshow.init();
	Menu.init();
	Accordion.init();
	Gmap.init();
	Placeholder.init();
});

