// JavaScript Document
function anim_sbuttons()
{
	if(jQuery('#seasons').length > 0)
	{
		jQuery('#seasons a .sbutton').mouseenter(function()
			{
				if(!jQuery(this).hasClass('active'))
				{
					jQuery(this).stop();
					jQuery(this).animate({top: '0px'}, 250);
				}
			});
		jQuery('#seasons a .sbutton').mouseleave(function()
			{
				if(!jQuery(this).hasClass('active'))
				{
					jQuery(this).stop();
					jQuery(this).animate({top: '-85px'}, 250);
				}
			});
	}
}
function anim_htabs()
{
	if(jQuery('#htabs').length > 0)
	{
		jQuery('#htabs').mouseenter(function()
			{
				if(!jQuery(this).hasClass('active'))
				{
					jQuery(this).stop();
					jQuery('.htabs_c').css('z-index', 201);
					jQuery(this).animate({marginTop: '0px'}, 250);
				}
			});
		jQuery('#htabs').mouseleave(function()
			{
				if(!jQuery(this).hasClass('active'))
				{
					jQuery(this).stop();
					jQuery(this).animate({marginTop: '146px'}, 250, function() { jQuery('.htabs_c').css('z-index', 91); } );
				}
			});
	}
}

Array.prototype.search = function(o)
{
	for(var i = 0; i < this.length; i++)
		if(this[i] == o)
			return i;
	return -1;
}

var busy = false;
function slider_navto(indx)
{
	if(busy) return;
	busy = true;
	setTimeout('busy=false;', 5000);
	
	indx = ids.search(indx);
	
	//alert(request_url+'&req=image');
	if(jQuery('#head_image #img_'+ids[indx]).length == 0)
	{
		// Download the image
		jQuery.ajax({ url: request_url+'&req=image&img_id='+ids[indx] }).success(
			function(data)
			{
				jQuery('#head_image img.current').fadeOut('slow');
				jQuery('#head_image img.current').removeClass('current');
				jQuery('#head_image').append(data);
				jQuery('#head_image #img_'+ids[indx]).fadeIn('slow');
				jQuery('#head_image #img_'+ids[indx]).addClass('current');
			});
	}
	else
	{
		// We allready have the image
		jQuery('#head_image img.current').fadeOut('slow');
		jQuery('#head_image img.current').removeClass('current');
		jQuery('#head_image #img_'+ids[indx]).fadeIn('slow');
		jQuery('#head_image #img_'+ids[indx]).addClass('current');
	}
	// Update hotspots
	jQuery.ajax({ url: request_url+'&req=hotspots&img_id='+ids[indx] }).success(
		function(data)
		{
			if(jQuery('#head_i .hotspot').length > 0)
			{
				jQuery('#head_i .hotspot').fadeOut('slow', function(){
					jQuery('#head_i .hotspot').remove(); 
					jQuery('#head_i').append(data);
					init_hotspots(); // Update mouse events on hotspots
					busy = false;
				});
			}
			else
			{
				jQuery('#head_i').append(data);
				init_hotspots(); // Update mouse events on hotspots
				busy = false;
			}
		});
	// Update navigation
	jQuery('#slider .dot.current').removeClass('current');
	jQuery('#dot_'+ids[indx]).addClass('current');
}

function init_slider()
{
	if((typeof(ids) == 'undefined') || (typeof(images) == 'undefined')) return;
	
	jQuery('#head_i').append('<div class="slider" id="slider"></div>');
	for(i=0; i < ids.length; i++)
	{
		if(ids[i] != selection)
			jQuery('#slider').append('<div class="dot" id="dot_'+ids[i]+'" onclick="slider_navto('+ids[i]+');"></div>');
		else
			jQuery('#slider').append('<div class="dot current" id="dot_'+ids[i]+'" onclick="slider_navto('+ids[i]+');"></div>');
	}
}

/* --------------------------- Hotspots ---------------------------------- */
function triggerClick(e)
{
	jQuery(e).children('a:first').trigger('click');
}

var htimer = 0;
var hotspot_e = null;

function init_hotspots()
{
	jQuery('.hotspot').mouseenter(function()
	{
		if(jQuery(this).hasClass('h360')) return;
		
		hotspot_e = this;
		htimer = setTimeout('triggerClick(hotspot_e)', 500);
	});
	jQuery('.hotspot').mouseleave(function()
	{
		clearTimeout(htimer);
	});
}


jQuery(document).ready(function()
{
	anim_sbuttons();
	anim_htabs();
	init_slider();
	init_hotspots();
	
	// Preload head images
	for(i = 0; i < images.length; i++)
	{
		jQuery('#head_image').append('<img id="img_'+ids[i]+'" src="'+images[i]+'" alt="'+alt_text+'" style="display: none;" />');
	}
});
