/* ONLOAD */
$(function(){
	
	/* HIDE SUBMIT */
	$('#client-filter').change(function(){
		Utils.submitForm(this);
	}).find('option').click(function(){
		Utils.submitForm(this);
	});
	
	/* PNG FIX */
	$('document').pngFix({blankgif: imgPath + '/pixel.gif'});
	
	/* MENU NAVIGAZIONE HEADER */
	$('.headermenu').hover(
		function(){
			var obj = $(this),
				objParent = obj.parent(),
				container = obj.next('.header-submenu'),
				containerW = container.outerWidth(),
				leftPosition = obj.position().left,
				centerOffset = Math.round(objParent.outerWidth() / 2),
				subMenu = container.find('ul'),
				subMenuW = subMenu.hiddenWidth(true),
				subMenuP = leftPosition + centerOffset - Math.round(subMenuW / 2),
				subMenuTotalSize = subMenuP + subMenuW;
			
			if(subMenuTotalSize >= containerW) subMenuP -= subMenuTotalSize - containerW + 10;
			if(subMenuP < 0) {
				subMenuP = 0;
				subMenu.width(containerW);
			}
			
			subMenu.css('left', subMenuP);
			Utils.toggleMenu(container, true);
		},
		function(){
			var obj = $(this),
				container = obj.next('.header-submenu');
				
			Utils.toggleMenu(container, false);
		}
	).next('.header-submenu').hover(
		function(){ Utils.toggleMenu($(this), true); },
		function(){ Utils.toggleMenu($(this), false); }
	);
	
	/* SLIDESHOW */
	$('#slideshow').slideshow();
	
	/* LIGHTBOX */
	$('.gallery-thumbnail').lightBox({
		txtImage: 'Immagine',
		txtOf: 'di',
		containerMinWidth: 230
	});
	
	/* ANALYTICS */
	$('#ga-controller').load(function(){
		var page = this.src.split('?p=')[1];
		
		_gaq.push(['_trackEvent', 'Contatto', 'Form', page]);
		
		if(window.console) window.console.log(page);
	});
	
});


/* UTILITY */
var Utils = {
	chek: false,
	toggleMenu: function(obj, show) {
		var inst = this,
			parent = obj.parent();
		if(show) {
			parent.addClass('active').prev().addClass('prevActive');
			if(inst.check) {
				$('.header-submenu').hide();
				obj.show();
			} else {
				obj.slideDown('normal');
			}
			inst.check = true;
		} else {
			parent.removeClass('active').prev().removeClass('prevActive');
			inst.timeControlMenu(obj);
		}
	},
	timeControlMenu: function(obj) {
		var inst = this;
		timeOut = setTimeout(function(){
			if($('#header-nav').find('.active').length == 0) {
				obj.slideUp('normal');
				clearTimeout(timeOut);
				inst.check = false;
			}
		}, 200);
	},
	submitForm: function(element) {
		var form = element.form;
		form.submit();
	}
};

/* SLIDER */
jQuery.fn.slideshow = function() {
	var obj = this,
		imgList = obj.find('img'),
		lastImg = obj.find('img:last'),
		totalW = 0,
		slider = null,
		slideInterval = '';
		
	function init() {
		obj.addClass('slide-active');
		
		getTotatlW();
	}
	
	function getTotatlW() {
		if(window.XMLHttpRequest && document.all) {
			imgList.each(function(){
				totalW += this.width;
			});
			
			wrapping(totalW);
		} else {
			lastImg.load(function(){
				imgList.each(function(){
					totalW += this.width;
				});
				
				wrapping(totalW);
			});
		}
	}
	
	function wrapping() {
		imgList.wrapAll('<div class="slider"/>');
		slider = obj.find('.slider').append('<div class="clear"></div>').width(totalW);
		
		sliding();
	}
	
	function sliding() {
		slideInterval = setInterval(function(){
			var firstImg = slider.find('img:first'),
				lastImg = slider.find('img:last');
			
			slider.animate({
				left: '-=' + firstImg.width()
			}, 1000, function(){
				firstImg.insertAfter(lastImg);
				$(this).css('left', 0);
			});
		}, 3500);
	}
	
	init();
	
	return this;
}

/* HIDDEN ELEMENTS DIMENSION HACK */
jQuery.fn.hiddenWidth = function(outer){
	var obj = this,
		parent = obj.parent(),
		width = 0;
		
		parent.css({
			'display': 'block',
			'visibility': 'hidden',
			'position': 'absolute',
			'left': '-1000em'
		});
		width = outer? obj.outerWidth() : obj.width();
		parent.removeAttr('style');
	
	return width;
}
