var local = {
	
	init: function(){

		if( gf.isPage('dove-siamo', 1) ){
			$('div.google').gMap(
			{
				xy			: '45.666031,9.744062',
				tipImage	: '/wp-content/themes/ghezzi/style/img/icons/tlg_tip.png',
				mapType		: 'satellite'
			});
		}
		
		$('li.page-item-6 > a')
			.click(
				function()
				{
					location.href = '/prodotti/tendaggi/';
					return false;
				}
			);
		
		// creo un piccolo effetto fade all'apertura del sito
		local.showSite();
		
		// fix delle png per ie6
		gf.fixPng('.fixPng');
		
		// animazione sul menu principale del sito
		local.menuHover();

	},
	
	// fade all'apertura del sito
	showSite: function(){
		$('div.load').fadeTo(1500, 0, function(){
			$(this).remove();
		});
		local.onLoad();
	},
	
	// tutto quello che succede una volta finito il fade-in
	onLoad: function(){
		
		// pagina contatti
		if( gf.isPage('contatti', 1) ){

			$('#google-map').gMap(
			{
				xy			: '45.666031,9.744062',
				tipImage	: '/wp-content/themes/ghezzi/style/img/icons/tlg_tip.png',
				mapType		: 'satellite'
			});

			setTimeout(function()
			{
				$('div.form, div.col-sx, div.col-dx').fadeIn(1000);
			}, 600);
			
			
			gf.validate(
			{
				form		: 'form[name=contatti]',
				fields		: [ '#nome', '#cognome', '#email', '#messaggio' ],
				onSuccess 	: function()
				{
				
					$.post(
						'/wp-content/themes/ghezzi/ajax/mail.php',
						{nome: $( '#nome' ).val(), cognome: $( '#cognome' ).val(), email: $( '#email' ).val(), msg: $( '#messaggio' ).val()}
					);
					
					$('div.over-black').fadeTo(400, 0.6, function(){
						$('div.message').show().animate({width: '400px', height: '40px'}, 500, function(){
							$('div.message .msg').slideDown(function(){
								$('div.timer').animate({width: '400px'}, 5000, function(){
									location.href = '/';
								});
							});
						});
					});
					
					return false;
				}
			});

		}
		
		// pagina promozioni
		if( gf.isPage('promozioni', 1) ){
			$('div#side ul li')
				.hover(
					function(){
						$(this).css('background-position', 'left 0');
					}, function(){
						$(this).css('background-position', 'left -34px');
					}
				);
		}
		
		// pagina prodotti
		if( gf.isPage('prodotti', 1) || gf.isPage('prodotto', 1) ){
			// Animazione iniziale
			$( '.novita' ).fadeTo( 1100, 1 );
			/*
			$('div.novita img').animate({left: '0'}, 1100, function(){
				$('div.novita-2010').fadeIn('slow');
			});
			*/
			// Evidenzio il pulsante prodotti di gamma
			$('li.page-item-6').addClass('current_page_item');
			
			// Attivo l'hover sul menu delle sub categorie
			$('ul#gamma li a')
				.hover(
					function(){
						$(this).children('h5').stop().animate({color: '#983c00'}, 700);
						$(this).children('img:not(.hidden, .on)').stop().fadeTo(700, 0);
					}, function(){
						$(this).children('img:not(.hidden)').stop().fadeTo(200, 1);
						$(this).children('h5:not(.attivo)').stop().animate({color: '#8f8f8f'}, 200);
					}
				)
				/** 
				 * disattivo il caricamento ajax delle sotto-categorie
				 */
				.click(
					function(){
						// Ricavo qualche informazione sulla linea
						var title 	= $(this).children('h5').html(),
							catId	= $(this).attr('rel'),
							link	= $(this).attr('href'),
							$box	= $('.box-dettaglio');
							
						// Se è già stata selezionata una linea devo svuotare il contenitore
						if( $('ul.linee').html() != '' ){
						
							$(this).parent().siblings().children('a').children('h5').animate({color: '#8f8f8f'}, 200).removeClass('attivo');
							$(this).parent().siblings().children('a').children('img:not(.on)').fadeTo(300, 1);
						
							$('ul.linee').html('');
							$box.children('div.content').animate({height: '0'}, 100, function(){
								$(this).children('.box-linee').css({'height':'0'});
							});
						}
						
						// Attivo il pulsante cliccato
						$(this).children('img:not(.on)').removeAttr('style').addClass('hidden');
						$(this).children('h5').addClass('attivo');
						
						// Mostro il box è faccio una piccola animazione
						$box.fadeIn();

						// ricevo dalla pagina le sotto-categorie
    					$.getJSON('/wp-content/themes/ghezzi/json/subcategories.php', {catId : catId}, function(data) {
    						for(i=0; i<data.length; i++){
    							//gf.debug(data[i].nicename);
    							var theLi 	= '<li class="hidden">'
    										+ '<a href="'+link+'/'+data[i].nicename+'" class="'+data[i].nicename+'" rel="'+data[i].cat_id+'">'
    											+ '<h5>'+data[i].cat_name+'</h5>'
    											+ data[i].image_off
    											+ data[i].image_on
    										+ '</a>'
    							'</li>';
    							$('ul.linee').append(theLi);
    						}

							$('ul.linee > li')
								.each(
									function(i){
										var time = 400 * (i+1);
										$(this).delay(time).fadeIn(500);
									}
								);


    						local.subCategoriesAnimation();
    					});

						$box.children('div.content').animate({height: '212px'}, 1000, function(){
							$(this).children('.box-linee').css({'height':'212px'});
						});
						$box.children('.bar').children('h4').html(title);

						return false;
					}
				);
			
			local.arrowNavigation();	
		
			$('#colori li a').click(
				function()
				{
					if( $( this ).closest( 'ul' ).hasClass( 'static' ) )
						return false;
					
					var color = $(this).attr('rel');
					var slug = $( '#current-slug' ).val();
					
					var b1, b2, b3, b4;
					
					b1 = $('li#bastone01');
					b2 = $('li#bastone02');
					b3 = $('li#bastone03');
					b4 = $('li#bastone04');
					
					local.cambiaColore( slug, color, '01', b1 );
					local.cambiaColore( slug, color, '02', b2 );
					local.cambiaColore( slug, color, '03', b3 );
					local.cambiaColore( slug, color, '04', b4 );
					
					return false;
				}
			);
		
		}
		
		if( gf.isHome(1) ){
			local.homePage();			
		}
	},
	
	cambiaColore: function( slug, colore, n, el )
	{
		
		var src = $( '#current-path' ).val();
		
		el.children( 'span' ).html('');
		var img = new Image();
		$(img).load(function () {
			$(this).hide();
			el.addClass('loading').children( 'span' ).append( this );
			$(this).fadeIn();
        }).attr('src', src +'/'+colore+'_'+n+'.jpg');

	},
	
	// mostro il div nero che copre il sito
	hideSomething: function(el, opacity){
		el.removeAttr('style').fadeTo(500, opacity, function(){
			$(this).next().fadeIn(function(){
				$(this)
					.click(
						function(){
							$(this).fadeOut(200, function(){
								$(this).prev().fadeOut();
								$('ul.prodotti').remove();
							});
							return false;
						}
					);
			});
		});		
	},

	// animazione all'hover sulle linee
	subCategoriesAnimation: function(){
		$('div.box-linee > ul > li > a')
			.hover(
				function(){
					$(this).children('h5').stop().animate({color: '#983c00'}, 700);
					$(this).children('img:not(.hidden, .on)').stop().fadeTo(700, 0);
				}, function(){
					$(this).children('img:not(.hidden)').stop().fadeTo(200, 1);
					$(this).children('h5:not(.attivo)').stop().animate({color: '#8f8f8f'}, 200);
				}
			)
	},
	
	// sposta il contenitore su e giù
	arrowNavigation: function(){
		$('div.arrows a')
	    	.hover(
	    		function(){
	    			var direction 	= $(this).attr('class'), // su o giù
	    				$ul		  	= $('ul.linee'), // ul container
	    				contaHeight	= $ul.parent().height(), // altezza del contenitore dell'ul
	    				liNumbers 	= Math.ceil(($ul.children('li').size() / 2)), // Quantità di li / 2 (2 per riga)
	    				liHeight  	= $ul.children('li').height(), // height di ogni li
	    				liMarginB	= parseInt($ul.children('li').css('margin-bottom'), 10), // margin bottom
	    				liMarginT	= parseInt($ul.children('li').css('margin-top'), 10), // margin top
	    				theMargin	= ((liHeight + liMarginB + liMarginT) * liNumbers) - contaHeight, // margine totale da spostare
	    				animation 	= (direction == 'up')? {marginTop : '0'} : {marginTop : '-'+theMargin+'px'},
	    				aniTime		= 300 * liNumbers;
	    			
	    			$('ul.linee').animate(animation, aniTime);
	    		}, function(){
	    			$('ul.linee').stop();
	    		}
	    	);
	},
	
	// animations for the home page
	homePage: function(){
		$('div.novita img').animate({left: '0'}, 1100, function(){
			$('div.novita-2010').fadeIn('slow');
			$('div.linea-binario').fadeIn('slow');
		});
	
		$('div.menu ul li:first-child').addClass('current_page_item');
		
		$('ul#main-categories li a')
			.hover(
				function(){
					$(this).children('h2').stop().animate({color: '#983c00'}, 700);
					$(this).children('img').stop().fadeTo(700, 0);
				}, function(){
					$(this).children('img').stop().fadeTo(200, 1);
					$(this).children('h2').stop().animate({color: '#8f8f8f'}, 200);
				}
			);
	},
	
	// menu animation
	menuHover: function(){
		/*
		$('div.menu ul li:not(.current_page_item) a')
			.hover(
				function(){
					$(this).parent().stop()
						.css({'background-color':'#772f00'})
						.animate({backgroundColor: '#983c00'}, 500);
				}, function(){
					$(this).parent().stop().removeAttr('style');
				}
			);
			*/
	}
	
}

jQuery(document).ready(function($) {
	local.init();
	
	$( '#select-filter' ).change(function()
	{
		var value = $( this ).children( ':selected' ).val();
		
		if( value ){
			window.location.hash = '#diametro-' + value;
		}
	});
	
	$( '.go-back' ).click(function()
	{
		window.history.back();
		return false;
	});
	
	$( '.opt .details' ).colorbox(
	{
		opacity		: 0.4,
		width		: 700,
		initialHeight : 500,
		innerHeight : 500,
		height		: 500,
		close		: 'chiudi'
	});
	
});
