var disabled = false;
var lastChanged = '#bg1';
var duration = 800;
var toshow_url;
var toshow_img;
var showed_img = 0;
var requestedAction;
var loader = $({});
var section_disabled = false;
var section_disabled_timer, disabled_timer;

(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		if(jQuery.browser.msie && jQuery.browser.version < 9)
			//$(this).children('div').fadeIn(speed);
			$(this).children('div').each(function() { if ($(this).attr('id') != "benvenuti_img") $(this).fadeIn(speed); })
		$(this).fadeIn(speed, function() {
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		if(jQuery.browser.msie && jQuery.browser.version < 9)
			//$(this).children('div').fadeOut(speed);
			$(this).children('div').each(function() { if ($(this).attr('id') != "benvenuti_img") $(this).fadeOut(speed); })
		$(this).fadeOut(speed, function() {
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);

function hide_show_benvenuti(force_hide, user)
{
	if (user)
		changed_by_user = true;

	if (hidden_benvenuti && !force_hide)
	{
		$('#benvenuti_img').show();
		$('#benvenuti').animate ( { height: '178px', marginTop: '322px' }, 300, function() {
			$('#info').css('background', 'url("'+infomuseo_hide+'")');
			hidden_benvenuti = false;
		});
	}
	else
	{
		$('#benvenuti').animate ( { height: '74px', marginTop: '426px' }, 300, function() {
			$('#info').css('background', 'url("'+infomuseo_show+'")');
			$('#benvenuti_img').hide();
			hidden_benvenuti = true;
		});
	}
}

function load_bg(action, bg)
{
	if (!disabled)
	{
	    disabled = true;
	    setTimeout(function() { disabled = false; }, 1000);
		requestedAction = action;

		objImage = new Image();

		if (bg != -2)
		{
			if (bg == -1)
			{
				if (action == 'next' || action == 'up')
				{
					toshow_img = showed_img+1;
					if (toshow_img >= images.length)
						toshow_img = 0;
				}
				else if (action == 'prev' || action == 'down')
				{
					toshow_img = showed_img-1;
					if (toshow_img < 0)
						toshow_img = images.length-1;
				}
			}
			else
				toshow_img = bg;
			toshow_url = images[toshow_img];
			var image = [ toshow_url ];
			loader.imageLoader({
				images: image,
				async: true,
				complete: function(e, ui) {
					var toChange = lastChanged == '#bg1' ? '#bg2' : '#bg1';

					$(toChange).css('background-color', 'transparent');
					$(toChange).css('background-image', 'url("'+toshow_url+'")');
					if (requestedAction == 'next')
						next();
					else if (requestedAction == 'prev')
						prev();
					if (requestedAction == 'up')
						up();
					else if (requestedAction == 'down')
						down();
					showed_img = toshow_img;
				}
			});
		}
		else
		{
			var toChange = lastChanged == '#bg1' ? '#bg2' : '#bg1';
					
			$(toChange).css('background-image', 'none');
			$(toChange).css('background-color', '#dad5cb');

			if (requestedAction == 'next')
				next();
			else if (requestedAction == 'prev')
				prev();
			if (requestedAction == 'up')
				up();
			else if (requestedAction == 'down')
				down();

			showed_img = 0;
		}
	}
}

function next()
{
	var toChange = lastChanged == '#bg1' ? '#bg2' : '#bg1';

	$('#bg1').css('top', '0px');
	$('#bg2').css('top', '0px');
	$('#bg1').css('position', 'relative');
	$('#bg2').css('position', 'relative');

	$(lastChanged).css('float', 'left');
	$(toChange).css('float', 'right');

	$('#contenitore_bg').animate ( { marginLeft: '-840px' }, duration, function() {
			if ($('#bg1').css('float') == 'right')
			{
				$('#bg1').css('float', 'left');
				$('#bg2').css('float', 'right');
			}
			else
			{
				$('#bg1').css('float', 'right');
				$('#bg2').css('float', 'left');
			}

			$('#contenitore_bg').css('margin-left', '0px');
		}
	);

	lastChanged = toChange;
}

function prev()
{
	var toChange = lastChanged == '#bg1' ? '#bg2' : '#bg1';

	$('#bg1').css('top', '0px');
	$('#bg2').css('top', '0px');
	$('#bg1').css('position', 'relative');
	$('#bg2').css('position', 'relative');

	$(lastChanged).css('float', 'right');
	$(toChange).css('float', 'left');

	$('#contenitore_bg').css('margin-left', '-840px');

		$('#contenitore_bg').animate ( { marginLeft: '0px' }, duration);

	lastChanged = toChange;
}

function up() {
	var toChange = lastChanged == '#bg1' ? '#bg2' : '#bg1';
	
	$('#bg1').css('position', 'absolute');
	$('#bg2').css('position', 'absolute');
		
	$(lastChanged).css('top', '-500px');
	$(toChange).css('top', '0px');

	$('#contenitore_bg').css('margin-top', '500px');
	$('#contenitore_bg').animate ( { marginTop: '0px' }, duration);
		
	lastChanged = toChange;
}

function down() {
	var toChange = lastChanged == '#bg1' ? '#bg2' : '#bg1';
	
	$('#bg1').css('position', 'absolute');
	$('#bg2').css('position', 'absolute');
		
	$(lastChanged).css('top', '0px');
	$(toChange).css('top', '-500px');

	$('#contenitore_bg').css('margin-top', '0px');
	$('#contenitore_bg').animate ( { marginTop: '500px' }, duration, function() {
			if ($('#bg1').css('top') == '-500px')
			{
				$('#bg1').css('top', '0px');
				$('#bg2').css('top', '500px');
			}
			else
			{
				$('#bg1').css('top', '500px');
				$('#bg2').css('top', '0px');
			}
			$('#contenitore_bg').css('margin-top', '0px');
		}
	);
		
	lastChanged = toChange;
}

function change_section(section, entry_page)
{
    if (!section_disabled && !disabled && lastSection != section)
    {
		var last_section = lastSection;
        section_disabled = true;
        setTimeout (function() { section_disabled = false }, 1600);
		
		if (!changed_by_user && (lastSection == 0 || section == 0))
			hide_show_benvenuti(false, false);
		
		if (lastSection == 4)
			$('#shop_info_button').hide();
		
		if (lastSection == 0)
			$('#loghi').customFadeOut(duration);
		if (section == 0)
			$('#loghi').customFadeIn(duration);

		if (section != 5 && lastSection != 5  && lastSection != 6)
		{
			if (showed_img != default_sections_bg[section])
			{
				if (section == 4)
				{
					$('#shop_info_button').show();
					load_bg('up', default_sections_bg[section]);
					wasUp = true;
				}
				else
				{
					if (wasUp)
					{
						load_bg('down', default_sections_bg[section]);
						wasUp = false;
					}
					else
						load_bg('next', default_sections_bg[section]);
				}
			}
			else
			{
				if (section == 4)
				{
					wasUp = true;
					$('#shop_info_button').show();
				}
				else
					wasUp = false;
			}		
		
			$('#titolo_sezione').customFadeOut(400, function() {				
				$('#titolo_sezione').html(sections[section]);
				$('#titolo_sezione').customFadeIn(duration);
			});
			$('#contenuto_scrollbar .overview_bg').customFadeOut(400, function() {
				$('#contenuto_scrollbar .overview').html(sections_html[section]);
				$('#contenuto_scrollbar .overview_bg').customFadeIn(duration, function() { $('#contenuto_scrollbar').tinyscrollbar_update(); });
			});
			$('#news').hide();

			lastSection = section;
		}
		else if (section != 5 && lastSection == 5)
		{
			$('#news').animate({ height: 570+'px'}, duration);
			$('#contenitore, #cornice').animate({ height: 640+'px'}, duration);

			setTimeout (function() {
				$('#contenuto_scrollbar .overview').html(sections_html[section]);
				$('#titolo_sezione').html(sections[section]);
				$('#contenuto').customFadeIn(duration, function() { $('#contenuto_scrollbar').tinyscrollbar_update(); });
				$('#benvenuti').customFadeIn(duration);
				$('#freccia_dx').customFadeIn(duration);
				$('#freccia_sx').customFadeIn(duration);
				$('#bottom').customFadeIn(duration);
			
				$('#news').hide();
				load_bg('down', default_sections_bg[section]);
				if (section != 4)
					wasUp = false;
				else
					$('#shop_info_button').show();

				lastSection = section;
			}, duration);
		}
		else if (section != 5 && lastSection == 6)
		{
			$('#contenuto_scrollbar .overview').html(sections_html[section]);
			$('#titolo_sezione').html(sections[section]);
			$('#contenuto').show();
			$('#contenuto_scrollbar').tinyscrollbar_update();
			$('#benvenuti').show();
			$('#freccia_dx').show();
			$('#freccia_sx').show();
			$('#bottom').show();
			$('#news').animate({ height: 570+'px'}, duration);
			$('#contenitore, #cornice').animate({ height: 640+'px'}, duration);

			$('#news').hide();

			load_bg('down', default_sections_bg[section]);
			if (section != 4)
				wasUp = false;

			lastSection = section;
		}
		else
		{
			$('#news').customFadeOut(duration);
			$('#contenuto').customFadeOut(duration);
			$('#benvenuti').customFadeOut(duration);
			$('#freccia_dx').customFadeOut(duration);
			$('#freccia_sx').customFadeOut(duration);
			$('#freccia_dx').customFadeOut(duration);
			$('#bottom').customFadeOut(duration);

			load_bg('up', -2);
			wasUp = true;
			
			var page = entry_page;
			if (page == 0)
				page = 1;

			setTimeout (function() {
				$.ajax
				({
					url: base_path+"/page.php",
					type: 'POST',
					data: 'page=' + page,
					dataType: 'html',
					success: function (html)
					{
						$('#contenuto_news').tinyscrollbar({ sizethumb: 0, size: 0 });
						$('#contenuto_news .overview').html(html);
						$('#news').customFadeIn(duration, function()
						{
							var window_height = $(window).height();
							$('#contenuto_news .scrollbar_line').css('height', window_height-150);
							if (lastSection != 6)
							{
								$('#contenitore, #cornice').animate({ height: '100%'}, duration);
								$('#news').animate({ height: window_height+'px'}, duration, function() {
									$('#contenuto_news').tinyscrollbar({ sizethumb: 30, size: window_height-170 });
								});
							}
							else
								$('#contenuto_news').tinyscrollbar({ sizethumb: 30, size: window_height-170 });

							lastSection = section;
						});
					},
					error: function ()
					{
						$('#contenuto_news').tinyscrollbar({ sizethumb: 0, size: 0 });
						$('#news .overview').html('<br /><br /><br /><h2 style="text-align:center;">Impossibile eseguire la richiesta al server, riprovare in seguito.</h2>');
						$('#news').customFadeIn(duration);

						lastSection = section;
					}
				});
			}, duration);
		}

		if (last_section != 6 && section != 6)
		{
			$('#section'+last_section).css('border-bottom', 'none');
			$('#section'+last_section).css('cursor', 'pointer');
			if (section == 4 || section == 5)
				$('#section'+section).css('border-bottom', '1px solid #a69e96');
			else
				$('#section'+section).css('border-bottom', '1px solid #252012');
			$('#section'+section).css('cursor', 'default');
		}
    }
}

