function page_init(){
	curpage = 1;
	$('div.scroller','#text_wrapper')
	.jScrollPane({scrollbarWidth:8, scrollbarMargin:30});
	
	var thumbs = $('a.thumb');

	function formatTitle(title, currentArray, currentIndex, currentOpts) {
	    return '<div>' + (title && title.length ? '<b>' + title + '</b>' : '' ) + (currentIndex + 1) + ' di ' + currentArray.length + '</div>';
	}

	thumbs.fancybox({
		'titlePosition' : 'inside',
		'titleFormat'	: formatTitle
	});	

$(".tip7")
	if((thumbs.length) > 4){
		page = Math.ceil(thumbs.length/4);
		
		show_pics_page(curpage);
		
		html='<p><a href="#prev" class="pager"><img src="img/prev.gif" class="middle" /></a>';
		for(i=1;i<=page;i++){
			if(i==1) 
				html+='<a href="#'+i+'" class="pager bold">'+i+'</a>';
			else
				html+='<a href="#'+i+'" class="pager">'+i+'</a>';
		}
		html+='<a href="#next" class="pager"><img src="img/next.gif" class="middle" /></a></p>';
		$('div.foto_bg').append(html);
				
	}
	
}

function show_pics_page(curpage){
	$('a.thumb').each(function(i){
											
		if(( (i+1) < ((curpage-1)*4)+1) || ((i+1) > ((curpage-1)*4)+4)){
			$(this).hide();							
		}else{
			$(this).show();							
		}
	});
}		


function preloadCallback() {
	$("#mContainer").remove();
	$('div.fo').fadeOut();
	$('div.progressContainer').animate({top:h+'px',opacity:0},3000,function(){
		$('div#logoloader').fadeIn('slow',function(){
				$(this).animate({opacity:1},2000,function(){
					$(this).remove();
					update_window(713);

					$('#container').removeClass('center');

					var page_name = self.document.location.hash.substring(1);
					if(page_name.length == 0) page_name = 'azienda';

					$('#content')
						.removeAttr('class')
						.addClass(page_name)
						.hide()
						.fadeIn('slow',
							function(){
								
								if(jQuery.support.opacity){
									$(this).html('<div id="target" class="hidden"></div><div id="news_scroller_bg" class="hidden"></div><div id="news_scroller" class="hidden">loading...</div>');										
								}else{
									$(this).html('<div id="target"></div><div id="news_scroller_bg"></div><div id="news_scroller">loading...</div>');	
								}								
								

								$('#target')
								.load(page_name+'.php',function(){
						
									if(jQuery.support.opacity){
										$('#target,#news_scroller_bg,#news_scroller').fadeIn('slow',
										function(){
											setup();
										})
									}else{
										setup();	
									}								
																		
									page_init();
									
									$('div#news_scroller').load('news.php',function(){
										$('div#news_scroller marquee').marquee()
										.mouseover(function () {
								            $(this).trigger('stop');
								        })
								        .mouseout(function () {
								            $(this).trigger('start');
								        })
									});	
									
									//Paginazione
									$('a.pager').live('click',function(e){
										e.preventDefault();
										if($(this).hasClass('bold')) return false;
										
										
										
										var t=$(this).attr('href').split('#')
										if(t[1].length == 0) return false;
										var href= t[1];

										if(href == 'next'){ 
											if(curpage < page)curpage++;
										}else if(href == 'prev'){ 
												if(curpage > 1)curpage--;
											} else {
												curpage = href;
											}
										
										$('a.pager.bold').removeClass('bold');
										$('a.pager:eq('+curpage+')').addClass('bold');
										
										show_pics_page(curpage);
										return false;
									})
										
								});
								
								
							});	
				})
				
		});
	});
}

function setup(){
	$('.menu ul li a')
	.live('mouseenter',function(){
		if($(this).hasClass('active')) return false;
		$(this).animate({'padding-left':'15px'});				
	})
	.live('mouseleave',function(){
		if($(this).hasClass('active')) return false;
		$(this).animate({'padding-left':'10px'});
	})
	.live('click',function(e){
		e.preventDefault();
		if($(this).hasClass('active')) return false;
		var t = $(this).attr('href');
		load_page(t);
		return false;
	})
	
}

function load_page(t){
	t=t.split('#')
	if(t[1].length == 0) return false;

	$('#content')
	.after('<div id="content_old" class="hidden '+t[1]+'" style="width:'+w+'px;height:'+h+'px"></div>');
	
		
	$('#content_old')	
	.fadeIn('slow',function(){
		$('#content')
		.removeAttr('class')
		.addClass(t[1]);
		
		$('#target')
		.load(t[1]+'.php',function(){
			
			page_init();
			
			$('#content_old').fadeOut('slow',function(){
				$(this).remove();
				$('div#text_wrapper,div#text_wrapper table td.menu ul li a').css('filter','none');
			});
			
			
			
				
		});			
	});
	return false;	
}

function incrementProgress() {
	progress++;
	var p = -1*progress/imageArray.length*100;
	$('div.progressBar').stop().animate({'left':p+'%'});
	if(progress>=imageArray.length) {
		preloadCallback();
	}
}

function preloadImages() {
	mHTML = "";
	rowTrack=0;
	for(i=0;i<imageArray.length;i++) {
		mHTML+='<img onload="incrementProgress();" class="mImg" width=1 height=1 src=' + imageArray[i] + '>'
		
		rowTrack++;
	}
	$("#mContainer").html(mHTML);
}
	
	
	
function start_loading(){	
	$('div#content').addClass('loader').html('<div style="height:100px;display:block;overflow:hidden"><div class="fo"><p>loading</p><p><img src="img/loader.gif" /></p></div></div><div class="progressContainer"><div class="progressBar"></div></div>').append('<div id="mContainer"></div><div id="logoloader"></div>');
	
	imageArray=new Array('img/text_bg.png','img/logo_loader.jpg','img/bg_azienda.jpg','img/bg_degustazione.jpg','img/bg_dovesiamo.jpg','img/bg_vini.jpg','img/bottiglia_vini.png','img/bg_schedavino.jpg','img/bg_enogastronomia.jpg','img/bottiglia_enogastronomia.png','img/bg_turismo.jpg','img/bg_arte.jpg','img/bg_scheda_tecnica.jpg','img/verdicchio_fogliano.jpg','img/verdicchio_passito.jpg','img/verdicchio_senex.jpg','img/rosso_fogliano.jpg','img/villa_castiglioni.jpg','img/piangifame.jpg','img/grappa.jpg','img/distillato.jpg','img/schede/verdicchio_passito.jpg','img/schede/verdicchio_senex.jpg','img/schede/rosso_fogliano.jpg','img/schede/villa_castiglioni.jpg','img/schede/piangifame.jpg');
							
	setTimeout("preloadImages()",1500);
}


function update_window(minh){
	w = parseInt($(window).width());		
	h = parseInt($(window).height());
	if((w<950) || (w>950)) w=950;
	if((typeof(minh)!=undefined)&&(h<minh)) h=minh;
	$('#container').addClass('center').css({width:w+'px',height:h+'px',position:'relative',ovwerflow:'hidden'});
}

var w,h,progress=0,curpage,page;
$(document).ready(function(){
		
	$(document).bind("contextmenu",function(e){
    	return false;
    });
	update_window();
	$(window).resize(function() {
		update_window(713);	
	})	
	start_loading();
})