var timelineSlide = true;
	
$(document).ready(function() {
	// Local scroll
	//$.localScroll({duration:2000});
	
	$('.desaturate').desaturate();

	$(document.body).bind('click',function(e) {
	showingParent = $(this).find('#wrap-map');
	if(showingParent.length > 0) {
	    var parentElement = showingParent[0];
	    if(!$.contains(parentElement,e.target)) {
			$('.map a.active').text('mappa');
			$('.map a.active').removeClass('active');
			$('#wrap-map').removeClass('active');
	    }
	  }
	});

	if($('#wrap-showsinfo #news-blog-btm').length)
		verticalSlider($('#wrap-showsinfo #news-blog-btm'), $('.shows-news .up'), $('.shows-news .down'));
	$('#timeline .change.bg, #wrap-showsinfo .change-bg').css({ 'background-color': '#'+$('#shows-list .central .show-color').text() });
	$('.change-border').css({ 'border-color': '#'+$('#shows-list .central .show-color').text() });
		
	//setup timeline
	var showsList = $('#shows-list');
	var showsNumber = showsList.children().length;
	var showsBeforeCurrent = $('#shows-list li.central').prevAll().length;
	var currentWidth = $('#shows-list li.central').outerWidth();
	var lateralWidth = $('#shows-list li.lateral:first').outerWidth();
	showsList.children().not('#shows-list .central').css({ opacity: 0.3 });
	$('#shows-list .central .month.mini').addClass('no-display');
	if(showsBeforeCurrent == 0)
		marginLeft = - lateralWidth + (lateralWidth / 4);
	else if(showsBeforeCurrent > 1)
		marginLeft = lateralWidth * (showsBeforeCurrent -1) + (lateralWidth / 4);
	else
		marginLeft = lateralWidth / 4;
	
	showsList.css({ width: currentWidth + (lateralWidth * showsNumber)/*, marginLeft: - (lateralWidth + (lateralWidth/4))*/ });
	showsList.css({ marginLeft: - marginLeft });
	
	//$('#shows-list .wrap-show *:not(img, .abig, .buttons .link, .slogan *)').addClass('no-display');
	//$('#shows-list .central div.buttons, #shows-list .central .slogan, #shows-list .central .show-logo, .show-logo img, #shows-list .central .current-highlight').removeClass('no-display');
	$('#shows-list .to-hide').addClass('no-display');
	$('#shows-list .central .to-hide').removeClass('no-display');
	
	$('#shows-list .lateral .show-logo').css({ marginTop: 45 });
	
	clickNext($('.arrow.next, #shows-list .lateral.right:first, .lateral.right:first .abig'),showsList,lateralWidth);
	clickPrev($('.arrow.prev, #shows-list .lateral.left:last, .lateral.left:first .abig'),showsList,lateralWidth);

	//opacity labels
	$('#search-form, #login-form li, #search-maison-showroom, #search-showroom').each(function() {
	    
	    $(this).mouseover(function() {
	    	$(this).children().first().animate({ 'opacity': 0.5 }, 200);
	    });
	    
	    $(this).mouseleave(function() {
	    	$(this).children().first().animate({ 'opacity': 1 }, 200);
	    });
	});
	
	//setta la larghezza del "view-all", per avere la linea al filo del titolo
	$('.view-all').each(function() {
		parentWidth = $(this).parent().width()
		previousWidth = $(this).prev().width();
		$(this).css({ 'width': parentWidth - previousWidth - 25 })
	});
		
	//form top
	validate($('form:not(#contacts-form, #fpform, #jobcv-form, form#accredits-form)'), "bottomRight", true);
	validate($('form#contacts-form'), "topRight", false);
	validate($('form#fpform'), "topRight", false);
	validate($('form#jobcv-form'), "topRight", false);
	validate($('form#accredits-form'), "topRight", false);
	
	lockify();
//	validate($('form.ajax'));
	dropdown();
	addLoader($('#subnavigator').find('a'),1);
	addLoader($('.ajax-link'),0);
	
	$('#show-horizontal-nav').css({ backgroundColor: '#'+$('#shows-list .central .show-color').text() });
	
	$('#close-showsinfo .close').click(function(e) {
		e.preventDefault();
		$('#close-showsinfo').animate({ opacity: 0 });
		$('#wrap-showsinfo .wrap-show').animate({ opacity: 0 }, 400, function() {
			$('#wrap-showsinfo').animate({ height: 0 }, 400, function() {
				$('#close-showsinfo').addClass('no-display');
			    $('#wrap-showsinfo .width-max:first').removeClass('open');
			});
		});
	});
});


var switchPhotoVideo = function() {
	$('.button.video, .button.photo').click(function(e) {
		e.preventDefault();
		if($(this).hasClass('active'))
			return false;
		else
		{
			$('.button.active').removeClass('active');
			$(this).addClass('active');
			if($('.button.active').hasClass('video'))
				$('div.paginator.mini').addClass('no-display');
			else
				$('div.paginator.mini').removeClass('no-display');
				
			notVisible = $('.wrap-media div.no-display');
			$('.wrap-media div').addClass('no-display');
			notVisible.removeClass('no-display');
		}
	});
}

var lockify = function() {
	$('.lock').click(function(e) {
		e.preventDefault();
		$('.lock').each(function() {
			id = $(this).find('a:first').attr('href');
		});
		id = $(this).find('a:first').attr('href');
		if($(this).hasClass('open'))
		{
			$(this).removeClass('open');
			$(id).animate({ opacity: 0 }, 300, function() {
				$(id).parent().animate({ height: 0, marginTop: -6 }, 400);
			});
		}
		else
		{
			$(this).addClass('open');
			idHeight = $(id).outerHeight(true);
			$(id).parent().animate({ height: idHeight, marginTop: 0 }, 400, function() {
				$(id).animate({ opacity: 1 }, 300, function() {
					if (jQuery.browser.msie)  
						$(this).css({ filter: 0 });
				});
			});
		}
	});
}

var validate = function(el, position, label) {
	if(position)
		el.validationEngine({ scroll: false, promptPosition: position });
	else
		el.validationEngine({ scroll: false });
	if(label)
	{
		el.find('label[for=search-input]').click(function(e) {
			$(this).next().trigger('focus');
		});
		//focus & blur input text
		el.find('.input-txt').each(function() { 
			if(!$(this).val())
			{
		    	$(this).focus(function() {
				    $(this).prev().addClass('no-display'); 
				});
				
		    	$(this).blur(function() {
		    	    if($(this).val() == '')
		    	    	$(this).prev().removeClass('no-display'); 
				});
			}
			else
				$(this).prev().addClass('no-display');
		});
	}
}

var dropdown = function() {
	$("body").find('.dropdown').focus(function() {
		$(this).css({ 'z-index': 0, 'opacity': 1 });
		$(this).next().css({ 'z-index': 10 });
	});
	$("body").find('.dropdown').change(function() {
		$(this).css({ 'z-index': 10, 'opacity': 0 });
		$(this).next().css({ 'z-index': 0 });
		value = $(this).find('option[value='+$(this).val()+']').text();
		$(this).next().find('.text').text(value);
	});
	$(document).click(function() {
		$("body").find('select.dropdown').each(function() {
			$(this).css({ 'z-index': 10, 'opacity': 0 });
			$(this).next().css({ 'z-index': 0 });
		});
	});
}

var clickNext = function(el,showsList,lateralWidth) {
	el.click(function(e) {
		e.preventDefault();
		trackEvent('Calendario','Slideshow','Next'); 
		if(timelineSlide)
		{
	    	//ferma le animazioni
	    	timelineSlide = false;
			//rimuove tutto dentro wrap-show e lascia visibile solo l'immagine
			//$('#shows-list .central div.wrap-show *:not(.big, .abig)').addClass('no-display');
			$('#shows-list .central .to-hide, #shows-list .central .slogan').addClass('no-display');
	    	//toglie la classe no-display alla freccia prev
	    	if($('.arrow.prev').hasClass('no-display'))	
	    		$('.arrow.prev').removeClass('no-display');
	    	next = $('#shows-list .central').next();
	    	if(!next.next().position())
	    		$('.arrow.next').addClass('no-display');
	    	//chiude events-wrapper (calendario eventi giornaliero)
	    	if($('#events-wrapper').hasClass('open'))
	    	{
	    		if($('#wrap-showsinfo .width-max:first').hasClass('open'))
	    		{
	    			$('#close-showsinfo').animate({ opacity: 0 });
	    			$('#wrap-showsinfo .wrap-show').animate({ opacity: 0 }, 400, function() {
	    			    $('#wrap-showsinfo').animate({ height: 0 }, 400, function() {
	    			    	$('#close-showsinfo').addClass('no-display');
	    			    	$('#wrap-showsinfo .width-max:first').removeClass('open');
	    			    	$('#wrap-showsinfo .wrap-show').addClass('no-display');
	    			    });
	    			});
	    		}
	    		$('#events-wrapper').removeClass('open');
	    		$('#events-wrapper').animate({ opacity: 0 }, 300, function() {
	    			$(this).find('div:first').addClass('hide');
	    			$('#current-month .active').removeClass('active');
	    			$('#events-wrapper').animate({ 'min-height': 0 }, 300, function() {
	    				changeEvent('right',showsList,next,lateralWidth);
	    			});
	    		});
	    	}
	    	else if($('#wrap-showsinfo .width-max:first').hasClass('open'))
	    	{
	    		$('#close-showsinfo').animate({ opacity: 0 });
	    		$('#wrap-showsinfo .wrap-show').animate({ opacity: 0 }, 400, function() {
	    		    $('#wrap-showsinfo').animate({ height: 0 }, 400, function() {
	    		    	$('#close-showsinfo').addClass('no-display');
	    		    	changeEvent('right',showsList,next,lateralWidth);
	    		    	$('#wrap-showsinfo .width-max:first').removeClass('open');
	    		    	$('#wrap-showsinfo .wrap-show').addClass('no-display');
	    		    });
	    		});
	    	}
	    	else
	    		changeEvent('right',showsList,next,lateralWidth);
	    }
	});
}

var clickPrev = function(el,showsList,lateralWidth) {
	el.click(function(e) {	
		e.preventDefault();
		trackEvent('Calendario','Slideshow','Prev'); 
		//console.log("trackEvent('Calendario','Slideshow','Prev');");
		if(timelineSlide)
		{
	    	//ferma le animazioni
	    	timelineSlide = false;
			//rimuove tutto dentro wrap-show e lascia visibile solo l'immagine
			$('#shows-list .central .to-hide, #shows-list .central .slogan').addClass('no-display');
			//toglie la classe no-display alla freccia prev
			if($('.arrow.next').hasClass('no-display'))	
				$('.arrow.next').removeClass('no-display');
			prev = $('#shows-list .central').prev();
			if(!prev.prev().position())
				$('.arrow.prev').addClass('no-display');
			//chiude events-wrapper (calendario eventi giornaliero)
			if($('#events-wrapper').hasClass('open'))
			{
				if($('#wrap-showsinfo .width-max:first').hasClass('open'))
	    		{
	    			$('#close-showsinfo').animate({ opacity: 0 });
	    			$('#wrap-showsinfo .wrap-show').animate({ opacity: 0 }, 400, function() {
	    			    $('#wrap-showsinfo').animate({ height: 0 }, 400, function() {
	    			    	$('#close-showsinfo').addClass('no-display');
	    			    	$('#wrap-showsinfo .width-max:first').removeClass('open');
	    			    	$('#wrap-showsinfo .wrap-show').addClass('no-display');
	    			    });
	    			});
	    		}
				$('#events-wrapper').removeClass('open');
				$('#events-wrapper').animate({ opacity: 0 }, 300, function() {
					$(this).find('div:first').addClass('hide');
					$('#current-month .active').removeClass('active');
					$('#events-wrapper').animate({ 'min-height': 0 }, 300, function() {
						changeEvent('left',showsList,prev,lateralWidth);
				    	$('#wrap-showsinfo .width-max:first').removeClass('open');
					});
				});
			}
			else if($('#wrap-showsinfo .width-max:first').hasClass('open'))
			{
				$('#close-showsinfo').animate({ opacity: 0 });
				$('#wrap-showsinfo .wrap-show').animate({ opacity: 0 }, 400, function() {
				    $('#wrap-showsinfo').animate({ height: 0 }, 400, function() {
				    	$('#close-showsinfo').addClass('no-display');
				    	changeEvent('left',showsList,prev,lateralWidth);
				    	$('#wrap-showsinfo .width-max:first').removeClass('open');
				    	$('#wrap-showsinfo .wrap-show').addClass('no-display');
				    });
				});
			}
			else
				changeEvent('left',showsList,prev,lateralWidth);
		}
	});
}

var changeEvent = function(direction,showsList,el,lateralWidth) {
	//toglie freccina verso il basso 
	$('#shows-list .central div.wrap-show').removeClass('bg');
	//abbassa l'opacità della colonna centrale
	$('#shows-list .central').animate({ opacity: 0.3 }, 400);
	//spegne la barra del menu colorata e cambia il colore di sfondo
	$('#show-horizontal-nav').animate({ opacity: 0 }, 400, function() {
		color = '#'+$('#shows-list .central .show-color').text();
	    $(this).css({ backgroundColor: color });
	    $('#close-showsinfo').css({ 'border-color': color });
	    //$('.wrap-show .arw').css({ backgroundColor: color });
	});
	//opacità a 0 per il calendario principale
	$('#calendars').animate({ opacity: 0 }, 300);
	
	//rimpicciolisce logo centrale
	$('#shows-list .central .show-logo img').animate({ width: 200 }, 300);
	$('#shows-list .central .show-logo').animate({ marginTop: 45 }, 300);
	//rimpicciolisce l'immagine principale
	$('#shows-list .central img.big').animate({ width: 300, height: 145, marginTop: 42, marginLeft: 92 }, 300, function() {
		$('#shows-list .central .slogan').removeClass('no-display');
	    //toglie il margin-left e passa da big a mini (classe)
	    $(this).css({ marginLeft: 0 }).removeClass('big').addClass('mini');
	    if(direction == 'right')
	    {
	    	//sposta le classi dal central al nuovo central
	    	$('#shows-list .central').removeClass('central').addClass('lateral left');
	    	lateralMove = '-='+lateralWidth+'px';
	    	el.removeClass('lateral right').addClass('central');
	    	slogan = $('#shows-list .central .slogan');
	    }
	    else
	    {
	    	//sposta le classi dal central al nuovo central
	    	$('#shows-list .central').removeClass('central').addClass('lateral right');
	    	lateralMove = '+='+lateralWidth+'px';
	    	el.removeClass('lateral left').addClass('central');
	    	slogan = $('#shows-list .central .slogan');
	    }
	    $('#shows-list li').unbind();
	    clickNext($('#shows-list .central').next(),showsList,lateralWidth);
	    clickPrev($('#shows-list .central').prev(),showsList,lateralWidth);
	    //ora c'è il nuovo central, all'immagine ridà il margin-left
	    $('#shows-list .central img.mini').css({ marginLeft: 92 });
	    //crea menu sotto
	    createSubmenuTimeline();
	    //pesca il nuovo calendario corrente
	    updateCalendar();
	    //sposta tutta la lista
	    slogan.addClass('no-display');
	    showsList.animate({ marginLeft: lateralMove }, 300, function() {
	    	//riporta l'opacità di central a 1
	    	$('#shows-list .central').animate({ opacity: 1 }, 400);
			
	    	//ingrandisce logo di central
	    	$('#shows-list .central .show-logo img').animate({ width: 270, marginTop: 0 }, 400);
			$('#shows-list .central .show-logo').animate({ marginTop: 0 }, 300);
	    	//ingrandisce l'immagine di central
	    	$('#shows-list .central img.mini').animate({ width: 472, height: 231, marginTop: 0, marginLeft: 0 }, 400, function() {
	    		//riaccende tutto dentro wrap-show
	    		//$('#shows-list .central div.wrap-show').children().not('.show-color').removeClass('no-display');
	    		//$('#shows-list .central div.wrap-show .show-logo img').removeClass('no-display');
	    		$('#shows-list .central .to-hide').removeClass('no-display');
	    		slogan.removeClass('no-display');
	    		$('#shows-list .central div.wrap-show').addClass('bg');
	    		timelineSlide = true;
	    	});
	    	//assegna all'immagine la classe big, togliendo quella mini
	    	$('#shows-list .central img.mini').removeClass('mini').addClass('big');
	    	//riaccende la barra del menu colorata
	    	$('#show-horizontal-nav').animate({ opacity: 1 }, 400);
	    	$('#calendars').animate({ opacity: 1 }, 400);
	    	
	    });
	});
}

function initImage(obj,toggle)
{
/*
	var paircount = 0;
	obj.onload = null;
	var $newthis = $(obj);
	if ($.browser.msie)
	{
	  // You need this only if desaturate png with aplha channel
	  $newthis = $newthis.desaturateImgFix();
	}
	// class for easy switch between color/gray version
	$newthis.addClass("pair_" + ++paircount);
	var $cloned = $newthis.clone().attr('id', '');
	// reset onload event on cloned object
	$cloned.get(0).onload = null;
	// add cloned after original image, we will switch between
	// original and cloned later
	$cloned.insertAfter($newthis).addClass("color").hide();
	// desaturate original
	$newthis = $newthis.desaturate({'rgb': [0.4444, 0.4444, 0.4444]});
	if(toggle)
	{
        $newthis.bind("mouseenter mouseleave", desevent);
        $cloned.bind("mouseenter mouseleave", desevent);
	}
*/
};
     
var createSubmenuTimeline = function() {
	$('#show-horizontal-nav').html($('#shows-list .central .horizontal-nav').html());
   $('#show-horizontal-nav .no-display').removeClass('no-display');
}

var loadTimeline = function() {	
	$('#timeline #shows-loader').remove();
	createSubmenuTimeline();
	$('#timeline #wrap-shows').removeClass('hidden').animate({ opacity: 1 }, 500, function() {
		//nasconde le frecce se primo o ultimo
		if($('#shows-list .central').next().position())
			$('.arrow.next').removeClass('no-display');
		if($('#shows-list .central').prev().position())
			$('.arrow.prev').removeClass('no-display');
		$('#timeline').css({ height: 'auto', minHeight: 339 });
		$('#show-horizontal-nav').animate({ opacity: 1 }, 300);
	});
};
     
var showTimeline = function() {	
	$('#timeline #shows-loader').remove();
	createSubmenuTimeline();
	$('#timeline #wrap-shows').removeClass('hidden').css({ opacity: 1 });
		//nasconde le frecce se primo o ultimo
		if($('#shows-list .central').next().position())
			$('.arrow.next').removeClass('no-display');
		if($('#shows-list .central').prev().position())
			$('.arrow.prev').removeClass('no-display');
		$('#timeline').css({ height: 'auto', minHeight: 339 });
		$('#show-horizontal-nav').css({ opacity: 1 });
};

var addLoader = function(el,expanded) {
	el.click(function(e) {
		e.preventDefault();
		if(expanded)
		{
			$('#subnavigator .active').removeClass('active');
			$(this).parent().addClass('active');
		}
		$('#subnavigator-w').addClass('loader');
		
		href = $(this).attr('href');
		$('#content-above').animate({ opacity: 0 }, 200, function() {
			$('#hidden').animate({ height: 43, 'min-height': 43, padding: 0 }, 300, function() {
				window.location = href;
			})
		});
	});	
}

var verticalSlider = function(el,up,down) {
	maskHeight = el.parent().height();
	totalHeight = el.height();
//	console.log(totalHeight);
//	console.log(maskHeight);
	if(totalHeight < maskHeight)
	{
		up.addClass('no-display');
		down.addClass('no-display');
		return false;
	}
	var page = 1;
	scrollsNum = Math.ceil(totalHeight / maskHeight) * 2;
	scrollHeight = Math.ceil(totalHeight / scrollsNum);
	if(scrollsNum > 1)
	{
		up.click(function(e) {
			e.preventDefault();
			if(page != 1)
			{
				page -= 1;
				el.animate({ marginTop: '+='+scrollHeight }, 1000);
			}
		});
		down.click(function(e) {
			e.preventDefault();
			if(page < scrollsNum)
			{
				page += 1;
				el.animate({ marginTop: '-='+scrollHeight }, 1000);
			}
		});
	
	}
}

var desevent = function(event) {
  var classString = new String($(this).attr('class'));
  var pair = classString.match(/pair_\d+/);
  // first I try was $("."+pair).toggle() but IE switching very strange...
  $("."+pair).hide();
  if (event.type == 'mouseenter')
    $("."+pair).filter(".color").show();
  if (event.type == 'mouseleave')
    $("."+pair).filter(":not(.color)").show();
}

var ajLink = function(el, dest, html, loader, father, social, rassegna) {
	el.click(function(e) {
		a = $(this);
		e.preventDefault();
		url = $(this).attr('href');
		firstLi = $(this).parents('li:first');
		if(firstLi.hasClass('active'))
			return false;
		father.find('.active').removeClass('active');
		firstLi.addClass('active');
		loader.removeClass('no-display');
		$.get(url, function(data) {
			htmlLoaded = $(html, data);
			destination = html.replace(' > *','');
			dest.animate({ opacity: 0 }, 200, function() {
				$(destination).html(htmlLoaded);
				//check gallery
				if($('.photo-gallery').length)
				{
					$('.photo-gallery').cycle({ 
					    prev:   '.prevPhoto', 
					    next:   '.nextPhoto', 
						fx: 'scrollHorz',
						timeout: 5000,
						pause:   1,
					    after:   onAfterPhoto
					});
				}
				if(social)
				{
					//twitter
					twttr.widgets.load();
					//FB
					dest.find("#test").html('<fb:like href="http://www.cameramoda.it'+url+'" width="90"  layout="button_count"  send="true" font="arial">')
        			if (typeof FB  != "undefined"){
            			FB.XFBML.parse(dest.find("#test").get(0));
        			}

				}
				$(this).animate({ opacity: 1 }, 200, function() {
					loader.addClass('no-display');
				});
			});
		});
	});
}
