var selectedImage;
var selectedItem;
var browserTimer;
var firstItem = true;
var animating = false;
		curPos = 0;

var numThumbs = 0;
var thumbWidth = 0;

$(document).ready(function()
{
	maxBG();
	initGallery();
	installation();
	
	$('#sname').focus(function()
	{	
		if($(this).attr('value') == 'Name') $(this).attr('value','');
	});
	$('#sname').blur(function()
	{	
		if($(this).attr('value') == '') $(this).attr('value','Name');
	});
	
	$('#semail').focus(function()
	{	
		if($(this).attr('value') == 'Email') $(this).attr('value','');
	});
	$('#semail').blur(function()
	{	
		if($(this).attr('value') == '') $(this).attr('value','Email');
	});
});

var installation = function()
{
	if($('#browser .thumb').length)
	{
		//$('#thumbs').css('width',($('#browser .thumb').length * 95) + 10);
		
		maxClick = 0;
		curPos = 0;
		numThumbs = $('#browser .thumb').length;
		thumbWidth = (numThumbs * 95) + 10;
		scrollMeBy = 0;
		initGalleryButtons();
		
		//$('#wrapper').hide();
		
		$("#installation-loading").spinner({
			height: 48,
			width: 48,
			speed: 50,
			image: '/images/spotlight_spinner_big.png'
		});
		
		selectedDesc = $('.gallery-desc').first();
		layoutInstallation();
		initInstallationButtons();
		$('#browser .thumb').first().find('a').click();
		$('#installation-next').fadeIn();
		$(window).resize(function()
		{
			layoutInstallation();
		});
		var count = 0;
		$('.thumb').each(function()
		{
			//$(this).data('isClicked',false);
			var left = 10 + (95 * count);
			$(this).css({
				'position':'absolute',
				'top':'20px',
				'left': left+'px'
			});
			count++;
			$(this).hover(function()
			{
				if(!$(this).hasClass('selected')) $(this).addClass('over');
			},function()
			{
				$(this).removeClass('over');
			});
		});
		
		$('#browser').mouseenter(function()
		{
			clearTimeout(browserTimer);
		});
		
		$('#browser').mouseleave(function()
		{
			browserTimer = setTimeout('hideBrowser()',1000);
		});
		
		// follow mouse so browser displays when it enters the bottom area
		
		$(document).mousemove(function(e)
		{
			var minY = $(document).height() - 145;
			var p = e.pageY;
			if(p > minY) {
				$('#browser').slideDown(300);
			}
		});
		
		$('#installation-next').hover(function()
		{
			$(this).find('img').attr('src','/images/installation-next.png');
		},function()
		{
			$(this).find('img').attr('src','/images/installation-next-over.png');
		});
			
		$('#installation-prev').hover(function()
		{
			$(this).find('img').attr('src','/images/installation-prev.png');
		},function()
		{
			$(this).find('img').attr('src','/images/installation-prev-over.png');
		});
		
		$('#installation-close').hover(function()
		{
			$(this).find('img').attr('src','/images/installation-close.png');
		},function()
		{
			$(this).find('img').attr('src','/images/installation-close-over.png');
		});
		
	

	}
}

var hideBrowser = function()
{
	$('#browser').slideUp(300);
}

var initInstallationButtons = function()
{
	$('#installation-next').click(function()
	{
		$('#installation-prev').fadeIn();
		selectedItem.next('.thumb').find('a').click();
	});
	$('#installation-prev').click(function()
	{
		$('#installation-next').fadeIn();
		selectedItem.prev('.thumb').find('a').click();
	});
}

var layoutInstallation = function()
{
	var h = $(window).height();
	var w = $(window).width();
	var top = h / 2;
	var left =  w / 2;
	$('#installation-prev,#installation-next').css('top',top);
	$('#installation-loading').css('top',top);
	$('#installation-loading').css('left',left);
	$('#installation,#installation-current').css('height',h);
	$('#installation,#installation-current').css('width',w);
	
	// layout thumbnail browser
	
	if(w < thumbWidth)
	{
		
		var diff = Math.ceil((thumbWidth - w) / 95);
		var n = (numThumbs - diff) * 95;
		maxClick = diff;
		scrollMeBy = $('#browser .thumb').length - diff;
		$('#thumbs').css('width',n);
		$('.next').fadeIn();
		var p = (w - n)/2;
	}
	else {
		$('#thumbs').css('width',thumbWidth);
		$('.next').fadeOut();
		$('.prev').fadeOut();
		scrollMeBy = 0;
		maxClick = $('#browser .thumb').length;
		var p = (w - thumbWidth)/2;
		if(curPos > 0)
		{
			var dist = curPos * 95;
			//move thumbnails back
			$('#browser .thumb').animate({
				left: '+=' + dist
			},300);
			curPos = 0;
		}
	}
	
	
	$('#thumbs').css('left',p);
	$('.next').css('right',p + 7);
	$('.prev').css('left',p + 8);
	
}

loadFull = function(src,lnk)
{
	if(!$(lnk).parent().data('isClicked'))
	{
		
		$("#installation-loading").show();
	}
	var i = new Image();
	$(i).load(function()
	{
		$(lnk).parent().data('isClicked',true);
		$("#installation-loading").hide();
		$('#browser .thumb').removeClass('selected');
		$(lnk).parent().addClass('selected');
		selectedItem = $(lnk).parent();
		if(!selectedItem.next('.thumb').length) $('#installation-next').fadeOut();
		if(!selectedItem.prev('.thumb').length) $('#installation-prev').fadeOut();
		var ht = '<img src="' + src + '" alt="" />';
		if(firstItem)
		{
			$('#installation-current img').maxImage({
				isBackground:true,
				verticalAlign: 'top',
  				zIndex: -10,
  				overflow: 'hidden'
			});
			firstItem = false;
			$('#browser').slideDown(300);
			browserTimer = setTimeout('hideBrowser()',3000);
		}
		$('#installation-current img').attr('src',src);
		
		var id = $(lnk).attr('id').replace('link','');
		var desc = $('#desc' + id);
		selectedDesc.hide();
		desc.show();
		selectedDesc = desc;
		//$('#installation-desc').fadeIn(100);
	});
	i.src = src;
}

subscribe = function()
{
	$('#subscribe .subscribe-button').html('Please wait');
	var f = $('#subscribe form').serialize();
	$.post('/ajax/subscribe',f,function(data)
	{
		$('#subscribe').fadeOut(300,function()
		{
			$(this).html(data);
			$(this).fadeIn(300);
		});
	});
	return false;
}

maxBG = function()
{
	if($('#full-image').length)
	{
		$('#full-image img').maxImage({
			isBackground:true,
			verticalAlign: 'top',
  			zIndex: -10,
  			overflow: 'hidden'
		});
	}
}

initGalleryButtons = function()
{
	if($('.thumb').length > 5)
	{
		/*
		$('.thumb').each(function()
		{
			if($(this).find('img').length) maxClick++;
		});
		*/
		maxClick = $('.thumb').length - scrollMeBy - 1;
		// show next/prev buttons
		$('.next').fadeIn();
		// add browsing functionality
		$('.next').click(function()
		{
			if(animating) return false;
			$('.prev').fadeIn();
			animating = true;
			var move = curPos + scrollMeBy; 
			move = move > (maxClick) ? (maxClick) : move;
			moveMeBy = 95 * (move - curPos);
			$('.thumb').animate({
				left: '-='+moveMeBy
			},300,function(){ animating = false; });
			curPos=move;
			if(curPos == (maxClick))	$(this).fadeOut(300);
		});
		$('.prev').click(function()
		{
			if(animating) return false;
			$('.next').fadeIn();
			animating = true;
			// calculate next move
			var move = curPos - scrollMeBy;
			move = move < 0 ? 0 : move;
			moveMeBy = 95 * (curPos - move);
			$('.thumb').animate({
				left: '+=' + moveMeBy
			},300,function(){ animating = false; });
			curPos=move;
			if(curPos == 0)	$(this).fadeOut(300);
		});
		
		$('.next').hover(function()
		{
			$(this).find('img').attr('src','/images/next-over.png');
		},function()
		{
			$(this).find('img').attr('src','/images/next.png');
		});
		
		$('.prev').hover(function()
		{
			$(this).find('img').attr('src','/images/prev-over.png');
		},function()
		{
			$(this).find('img').attr('src','/images/prev.png');
		});
	}
}

initGallery = function()
{
	if($('#right .thumb').length)
	{
		scrollMeBy = 4;
		maxClick = -5;
		selectedImage = $('.image .item').first();
		selectedDesc = $('.gallery-desc').first();
		var count = 0;
		$('.thumb').each(function()
		{
			if($(this).find('a').length)
			{
				$(this).hover(function()
				{
					if(!$(this).hasClass('selected')) $(this).addClass('over');
				},function()
				{
					$(this).removeClass('over');
				});
				
				$(this).find('a').click(function()
				{
					if(animating) return false;
					var off = parseInt($(this).parent().css('left'));
					var n = off / 95;
					newPos = curPos + n;
					var id = $(this).attr('id').replace('link','');
					var img = $('#image' + id);
					var desc = $('#desc' + id);
					selectedImage.fadeOut();
					img.fadeIn(300,function()
					{
						selectedImage = $(this);
					});
					selectedDesc.fadeOut(300,function()
					{
						selectedDesc = desc;
					});
					desc.fadeIn(300);
					$('.thumb').removeClass('selected');
					$(this).parent().addClass('selected');
					animating = true;
					if(newPos <= maxClick) {
						curPos = newPos;
						$('.thumb').animate({
							left: '-=' + off
						},300,function(){ animating = false; });
					} else {
						setTimeout(function(){ animating = false; },500);
					}
					if(curPos > 0) $('.prev').fadeIn();
					else $('.prev').fadeOut();
					
					if(curPos < (maxClick)) $('.next').fadeIn();
					else $('.next').fadeOut();
				});
			}
			var left = 95 * count;
			$(this).css({
				'position':'absolute',
				'top':'0px',
				'left': left+'px'
			});
			count++;
		});
		
		initGalleryButtons();
		
		if($('.image .item').length > 1)
		{
			$('.image .item').click(function()
			{
				if(!animating) $('.thumb.selected').next().find('a').click();
			});			
		}
		
	}
}
