// JavaScript Document
var slideElementTimer = null;
var slideElementTimerInterval = 5000;
var slideElementTimerClickClear = true;
$(document).ready(function(){
	
	var minZ = 100;
	var maxZ = -50;
	$('#slideElement-image img').each(function(){
		var curZ = parseInt($(this).css('z-index'));
		if(curZ > maxZ)
		{
			maxZ = curZ;
		}
		if(curZ < minZ)
		{
			minZ = curZ;
		}
	});

	$('#slideElement-adList a').click(
		function()
		{
			if(slideElementTimerClickClear && slideElementTimer != null)
			{
				timerDie();
			}
			var $me = $(this);
			var rel = $me.attr('rel');
			var curRel = $('#slideElement-adList a.slideElement-current').attr('rel');
			if(rel == curRel)
			{
				return false;
			}
			var $myImg = $('#slideElement-image'+rel);	//do not animate any further, make sure completely visible
			var $curImg = $('#slideElement-image'+curRel);	//do not animate any further, make sure completely visible
			var newLink = $me.attr('href');
			var curZ = parseInt($curImg.css('z-index')); //get zindex of active
			var newZ = curZ - 1; //we will swap this one.  now cycle through other images and make sure their zindexes are below these two
			var cursorZ = newZ - 1;
			
			if(newLink == '#')
			{
				newLink = '';
			}

			$myImg.css('z-index', newZ);
			$('#slideElement-image img').each(function(){
				var id = $(this).attr('id');
				if(id != 'slideElement-image'+rel && id != 'slideElement-image'+curRel)
				{
					$(this).css('z-index', cursorZ);
					cursorZ - 1;
				}
			});
			
			if ($.browser.msie)
			{
				$('#slideElement-moreDetails').hide();
			}
			else
			{
				$('#slideElement-moreDetails').stop().fadeTo('slow', 0);
			}
			if($curImg.is(':animated'))
			{
				$curImg.stop().fadeTo('slow', 0,
					function()
					{
						if(newLink != '')
						{
							if ($.browser.msie)
							{
								$('#slideElement-moreDetails').show().attr('href', newLink);
							}
							else
							{
								$('#slideElement-moreDetails').stop().fadeTo('fast', 1).attr('href', newLink);
							}
						}
						$myImg.css('z-index', curZ).show();
						$curImg.css('z-index', newZ).show();//
						$('#slideElement-adList a.slideElement-current').removeClass('slideElement-current');
						$me.addClass('slideElement-current');
					}
				);
			}
			else
			{
				$curImg.fadeOut( 'slow',
					function()
					{
						if(newLink != '')
						{
							if ($.browser.msie)
							{
								$('#slideElement-moreDetails').stop().show().attr('href', newLink);
							}
							else
							{
								$('#slideElement-moreDetails').stop().fadeTo('fast', 1).attr('href', newLink);
							}
						}
						$myImg.css('z-index', curZ).show();
						$curImg.css('z-index', newZ).show();//
						$('#slideElement-adList a.slideElement-current').removeClass('slideElement-current');
						$me.addClass('slideElement-current');
					}
				);
			}
			$(this).get(0).blur();
			return false;
		}
	);
	timerGo();
});

function timerGo()
{
	slideElementTimer = setTimeout('slideNext(true);', slideElementTimerInterval);
}

function timerDie()
{
	clearTimeout(slideElementTimer);
	slideElementTimer = null;
}

function slideNext(timerProceed)
{
	timerProceed = timerProceed == null ? false : timerProceed;
	var rel = parseInt($('#slideElement-adList a.slideElement-current').attr('rel'));
	var nextRel = 0;
	if(!timerProceed && slideElementTimer != null)
	{
		slideElementTimerClickClear = true;
		timerDie();
	}
	else
	{
		slideElementTimerClickClear = false;
	}

	if(!$('#slideElement-adList a[rel='+rel+']').is(':last-child'))
	{
		nextRel = rel + 1;
	}
	$('#slideElement-adList a[rel='+nextRel+']').click();
	slideElementTimerClickClear = true;
	
	if(timerProceed)
	{
		timerGo();
	}
}

function slidePrevious()
{
	var rel = parseInt($('#slideElement-adList a.slideElement-current').attr('rel'));
	var nextRel = parseInt($('#slideElement-adList a:last-child').attr('rel'));
	if(!$('#slideElement-adList a[rel='+rel+']').is(':first-child'))
	{
		nextRel = rel - 1;
	}
	slideElementTimerClickClear = true;
	$('#slideElement-adList a[rel='+nextRel+']').click();
}

