


/* Easings
-------------------------------------------------------------------------------------------------- */
$.easing.easeInOutExpo = function (x, t, b, c, d) {
	if (t==0) return b;
	if (t==d) return b+c;
	if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
	return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
};
$.easing.easeOutQuint = function(x,t,b,c,d){
	return c*((t=t/d-1)*t*t*t*t + 1) + b;
};

/* Binding clicks for navs + animations
-------------------------------------------------------------------------------------------------- */
var levelOpen = 'level1';
var t;
var r = false;
var view = $(window);
var isIE67 = false;
if($('html').is('.iefix')) { 
	isIE67 = true; 
}

$('#inner').click(function(e){

	if(!r){
	
		r = true;
		setTimeout(function(){
			r = false;
		}, 800);
	
		if($(e.target).parents('#nav01').length || $(e.target).parents('#level2').length){
				
				var _this = $(e.target);
				
				if(_this.get(0).tagName == 'A'){
					var _link = _this;
					var elem = _this.attr('href').split('#')[1];
				}
				else{
					var _link = _this.parents('a');
					var elem = _this.parents('a').attr('href').split('#')[1];
				}
				
				if(view.scrollTop() == 0){
					setAbsolute();
				}
				
				// Level 01 nav
				if(_this.parents('#nav01').length){
					
					// Open nav01:
					switch(levelOpen){
						case 'level1':
							levelOpen = 'level2';
							_link.addClass('on');
							$('#container').stop().animate({ 'left':600 }, {duration: 400, easing: 'easeOutQuint', complete: function(){
								$('#'+elem).stop().fadeIn(200, function(){
									forceShow($(this));
									$('#wrap').height('auto');	
									// ---- setFixed ------
									setFixed();		
									// ---- /setFixed ------
									//r = false;
								});
							}});
							// ---- show image ------
							pauseCycle();
							// ---- /show image ------
							return false;
							break;
						case 'level2':
							if(!_link.hasClass('on')){
								var fn = function(){
									$('#nav01 a').removeClass('on');
									_link.addClass('on');
									$('.nav02:visible').stop().fadeOut(200, function(){
										$('#'+elem).stop().fadeIn(200, function(){
											forceShow($(this));
											// ---- setFixed ------
											setFixed();		
											// ---- /setFixed ------
										});
									});
								}
								scrollToTop(fn);
								
							}
							else{
								levelOpen = 'level1';
								$('#nav01 a').removeClass('on');
								$('#wrap').height('100%');
								
								var fn = function(){
									setAbsolute();
									$('#container').stop().animate({ 'left': -0 }, {duration: 400, easing: 'easeOutQuint', complete: function(){
										$('#'+elem).hide();	
									}});
								}
								scrollToTop(fn);
								
								// ---- show image ------
								resetCycle();
								// ---- /show image ------
								
							}
							return false;
							break;
						default:
							if(!_link.hasClass('on') && levelOpen == 'restore'){
								
								Cufon.replace('#level2 strong', {color: '#bcbcb9'});
								
								$('.article:visible, .nav02:visible').hide();
								$('#nav01 a, #level2 a').removeClass('on');
								$('#wrap').height('auto');
								_link.addClass('on');
								$('#container').stop().animate({
									'left':-200
								}, 200, function(){
									$('#'+elem).fadeIn(200);
								});
								$('#level2').stop().animate({
									'margin-left':-400
								}, 200).find('.image').show();
								levelOpen = 'level2';
								
								// ---- show image ------
								pauseCycle();
								// ---- /show image ------
							}
							else if(!_link.hasClass('on')){
								
								Cufon.replace('#level2 strong', {color: '#bcbcb9'});
								
								var fn = function(){
									levelOpen = 'level2';
									$('#nav01 a, #level2 a').removeClass('on');
									_link.addClass('on');
									$('.article:visible').fadeOut(200, function(){
										//$('#wrap').height('100%');
										$('#level2').animate({
											'margin-left':-300
										}, 200);
										$('#container').stop().animate({
											'left':342
										}, 200, function(){
											$('.nav02:visible').stop().fadeOut(200, function(){
												$(this).find('.image').show();
												$('#'+elem).fadeIn(200, function(){
													forceShow($(this));
													// ---- setFixed ------
													setFixed();		
													// ---- /setFixed ------
												});
											});
										});											  
									});
								}
								scrollToTop(fn);
							}
							else if(levelOpen == 'restore'){
								levelOpen = 'level3';
								$('#wrap').height('auto');
								$('#container').stop().animate({
									'left':191
								}, {duration: 400, easing: 'easeOutQuint', complete: setFixed });
								
								// ---- show image ------
								pauseCycle();
								// Check what section it was by looking at nav02 for 'on' class
								var resume = $('.nav02').find('.on').attr('href');
								resume = resume.split('#')[1];
								if(hoverable){
									swapImage(resume);
								}
								// ---- /show image ------
							}
							else{
								checkRestoreScroll();
								$('#container').stop().animate({
									'left':788
								}, {duration: 400, easing: 'easeOutQuint',  complete: function(){
									$('#wrap').height('100%');
									$('#nav01').css('top', 'auto');
									setFixed();	
								}});
		
								levelOpen = 'restore';
								
								// ---- show image ------
								resetCycle();
								// ---- /show image ------
							}
							return false;
							
					}
				}
				else{
					
					// Clicking on _any_ level 2 will pause cycle, even if already paused.
					pauseCycle();
					
					// Level 02 nav
					switch(levelOpen){
						case 'level2':
							if(!_link.attr('href').match(/http/)){
								var fn = function(){
									levelOpen = 'level3';
									_link.addClass('on');
									$('.nav02:visible').find('.image').fadeOut(200, function(){
										$('#level2').stop().animate({ 'margin-left':-191 }, {duration: 400, easing: 'easeOutQuint'});
										$('#container').stop().animate({ 'left':191 }, {duration: 400, easing: 'easeOutQuint', complete: function(){
											
											$('#'+elem).stop().fadeIn(200, function(){
												$('#wrap').height('auto');
												// ---- setFixed ------
												setFixed();		
												// ---- /setFixed ------
												if($('html').is('.ie6')){
													$('#footer').css('position','relative');
												}
												//r = false;
											});
											
											// ---- show image ------
											// NB: elem = id
											if(hoverable){
												swapImage(elem);
											}
											// ---- /show image ------
											
										}});
									});
								}
								scrollToTop(fn);
								
								if(_link.parents('li').hasClass('cfn')){
									if(_link.parents('#dutyFreeRetail').length){
										var title = $(this).children('strong');
										Cufon.replace(title, {color: '#b8231a'});
									}
									else{
										var _title = _link.children('strong');
										Cufon.replace(_title, {color: '#525342'});
									}
								}
								return false;
							}
							break;
							
						default:
							
							if(!_link.hasClass('on')){
								if(!_link.attr('href').match(/http/)){
									var fn = function(){
										$('#level2 a').removeClass('on');
										_link.addClass('on');
										Cufon.replace('#level2 strong');
										$('.article:visible').fadeOut(200, function(){
											$('#'+elem).fadeIn(200);
											// ---- setFixed ------
											setFixed();		
											// ---- /setFixed ------
											//r = false;
											// ---- show image ------
											// NB: elem = id
											if(hoverable){
												swapImage(elem);
											}
											// ---- /show image ------
										});
									}
									scrollToTop(fn);
									return false;
								}
							}
							else{
								//r = false;
								return false;
							}
							break;
					}
				}
			
		} // end: if($(e.target))
	
	}
	else{
		return false;
	}// end: if(!r)
	
});// end: click(fn);


/* Bind h1
-------------------------------------------------------------------------------------------------- 
$('h1').click(function(){
	if((levelOpen != 'level1') || !levelOpen.match(/restore/)){
		if(levelOpen == 'level2'){
			levelOpen = 'level1';
			$('#nav01 a').removeClass('on');
			$('.nav02').hide();
		}
		else{ // level3
			levelOpen = 'restore';
		}
		setAbsolute();
		checkRestoreScroll();
		$('#container').stop().animate({
			'left':790
		}, {duration: 400, easing: 'easeOutQuint',  complete: function(){
			$('#wrap').height('100%');
			$('#nav01').css('top', 'auto');
		}});
		
		// ---- show image ------
		resetCycle();
		// ---- /show image ------
	}
});
*/
/* Cufon calls
-------------------------------------------------------------------------------------------------- */
Cufon.replace('#footer', { hover:true });
Cufon.replace('#level2 .cfn strong, #level2 .arrow');
Cufon.replace('#nav01 ul li a p');
Cufon.replace('#testolavorazioni p');
Cufon.replace('#testoazienda p');
Cufon.replace('#testocontatti p');
Cufon.replace('#menuprodotti a p');
Cufon.replace('.paginazione a');
Cufon.replace('.dettaglioprodotto');
Cufon.replace('.registrati');
Cufon.replace('#contactForm');
Cufon.replace('#name_id');
Cufon.replace('.new-users');
Cufon.replace('.registered-users');
Cufon.replace('#creaaccount');
Cufon.replace('.page-title');
Cufon.replace('.group-select');
Cufon.replace('.fatturazione');
Cufon.replace('.sistemapagamento');
Cufon.replace('.button');
Cufon.replace('#shopping-cart-totals-table');
Cufon.replace('#formchangepswd');
Cufon.replace('.input-box');

/* Gaeca bubble
-------------------------------------------------------------------------------------------------- */
$(document).ready(function(){
	//create a bubble popup for each DOM element with class attribute as "text", "button" or "link" and LI, P, IMG elements.
	$('#menucornici').CreateBubblePopup({
		position : 'top',
		align	 : 'center',
		innerHtml: 'Cornici',
		innerHtmlStyle: {
							color:'#FFFFFF', 
							'text-align':'center'
						},								
		themeName: 	'all-black',
		themePath: 	'images/jquerybubblepopup-theme'
	});
});
$(document).ready(function(){
	//create a bubble popup for each DOM element with class attribute as "text", "button" or "link" and LI, P, IMG elements.
	$('#menucomplementi').CreateBubblePopup({
		position : 'top',
		align	 : 'center',
		innerHtml: 'Complementi di arredo',
		innerHtmlStyle: {
							color:'#FFFFFF', 
							'text-align':'center'
						},								
		themeName: 	'all-black',
		themePath: 	'images/jquerybubblepopup-theme'
	});
});
$(document).ready(function(){
	//create a bubble popup for each DOM element with class attribute as "text", "button" or "link" and LI, P, IMG elements.
	$('#menuconchiglie').CreateBubblePopup({
		position : 'top',
		align	 : 'center',
		innerHtml: 'Conchiglie',
		innerHtmlStyle: {
							color:'#FFFFFF', 
							'text-align':'center'
						},								
		themeName: 	'all-black',
		themePath: 	'images/jquerybubblepopup-theme'
	});
});
$(document).ready(function(){
	//create a bubble popup for each DOM element with class attribute as "text", "button" or "link" and LI, P, IMG elements.
	$('#menuvassoi').CreateBubblePopup({
		position : 'top',
		align	 : 'center',
		innerHtml: 'Vassoi',
		innerHtmlStyle: {
							color:'#FFFFFF', 
							'text-align':'center'
						},								
		themeName: 	'all-black',
		themePath: 	'images/jquerybubblepopup-theme'
	});
});
$(document).ready(function(){
	//create a bubble popup for each DOM element with class attribute as "text", "button" or "link" and LI, P, IMG elements.
	$('#menutavola').CreateBubblePopup({
		position : 'top',
		align	 : 'center',
		innerHtml: 'Tavola',
		innerHtmlStyle: {
							color:'#FFFFFF', 
							'text-align':'center'
						},								
		themeName: 	'all-black',
		themePath: 	'images/jquerybubblepopup-theme'
	});
});
$(document).ready(function(){
	//create a bubble popup for each DOM element with class attribute as "text", "button" or "link" and LI, P, IMG elements.
	$('#menulampade').CreateBubblePopup({
		position : 'top',
		align	 : 'center',
		innerHtml: 'Oggettistica',
		innerHtmlStyle: {
							color:'#FFFFFF', 
							'text-align':'center'
						},								
		themeName: 	'all-black',
		themePath: 	'images/jquerybubblepopup-theme'
	});
});


/* Window load
-------------------------------------------------------------------------------------------------- */
$(window).load(function(){
	setContentHeight();	
});

/* Window resizing
-------------------------------------------------------------------------------------------------- */
view.resize(function(){
	setContentHeight();
	
	if(!$('html').is('.iefix')){
	
		if(levelOpen != 'level1'){
			//$('#wrap').height('100%');
			view.css('overflow','hidden');
			// get scrollTop, then assign to absolute
			var viewTop = view.scrollTop();
			setAbsolute();
			$('#nav01').css({'top':viewTop});
		}
		
		// while it's resizing, if trigger bound, remove it.
		if(this.resizeTO) { clearTimeout(this.resizeTO); }
		// yet, bind it again, in case resizing stops.
		this.resizeTO = setTimeout(function() {
			$(this).trigger('resizeEnd');
		}, 0);
	}
});

/* Bind resizeEnd actions
-------------------------------------------------------------------------------------------------- */
view.bind('resizeEnd', function() {
	if(levelOpen != 'level1'){
		view.css('overflow','visible');
		
		// needs another way for calc position
		var xpos = $('#container').offset().left;
		$('#nav01').css({
			'position':'fixed',
			'top': 'auto',
			'left': xpos
		});
	}
});

/* Set content height
-------------------------------------------------------------------------------------------------- */
function setContentHeight(){
	var windowHeight = view.height();
	if(windowHeight - 50 > 768){
		windowHeight -= 102;
	}
	if($('html').is('.ie6')){
		$('#content').css('height', windowHeight + 'px');	
	}
	else{
		$('#content').css('min-height', windowHeight + 'px');	
	}
}




function pauseCycle(){
	$('#homeSet').cycle('pause');
}

function resetCycle(){
	$('#subSet img').fadeOut(400);
	$('#homeSet:hidden').css('height', 823).fadeIn(400).cycle('resume');
}

// Force show
function forceShow(elem){
	if(elem.css('opacity') < 1){
		elem.css('opacity',1);
	}
}

// Set Fixed
function setFixed(){
	if(!isIE67){
		var xpos = $('#container').offset().left;
		$('#nav01').css({'position':'fixed', 'left': xpos});
	}
}

// Set Absolute
function setAbsolute(){
	if(!isIE67){
		$('#nav01').css({'position':'absolute', 'left': 'auto'});
	}
}

// Scroll To Top
function scrollToTop(fn){
	var viewTop = view.scrollTop();
	if(viewTop > 0){
		$.smoothScroll({
			scrollTarget: '#wrap',
			speed: 400,
			easing: 'easeOutQuint',
			afterScroll: function(){
				setAbsolute();
				fn();
			}
		});	
	}
	else{
		fn();
	}
}

// Check restore scroll
function checkRestoreScroll(){
	if(view.scrollTop() > 0){
		if(!isIE67){
			$('#nav01').css({
				'position': 'absolute',
				'left': 'auto',
				'top': view.scrollTop()
			});
		}
		return true;
	}
}
