var numimgs = 0;
var mainwidth = 0;
var activeimg = 0;
var pos = "";
var initwidth = 0;
var initheight = 0;
$(document).ready(function() {
	$('ul.menu li a').mouseover(function(){
		$(this).addClass('over');
	});
	$('ul.menu li a').mouseout(function(){
		$(this).removeClass('over');
	});

	mainwidth = $(document).width();

	$('#thirdfourth img').each(function(){
		$(this).attr('id', 'img_'+numimgs);
		$(this).css('left', mainwidth+'px');
		$(this).addClass('movingimg');
		$(this).attr('lbl', $(this).attr('title'));
		$(this).attr('title', '');
		$(this).css('zIndex', '100'+numimgs);
		numimgs++;
	});

	var pos = $('#thirdfourth').offset();

	if(pos && initwidth > 0){
		$('#img_0').css('left', pos.left);
		$('#img_0').css('opacity', '1');

		$('#clickaction').css('top', pos.top);
		$('#clickaction').css('left', pos.left-20);
		$('#clickaction').width(initwidth+40);
		$('#clickaction').height(initheight);
		
		$('#prev').css('left', pos.left-16);
		$('#prev').css('top', (initwidth/2));
		$('#next').css('top', (initwidth/2));
		$('#next').css('left', pos.left+initwidth+10);
		
	}
	
	$('#clickaction').click(function(clickpos){
		var pos = $(this).offset();
		var inipos = new Array(pos.top, pos.left+20);
		var endpos = new Array((pos.top+$(this).width()), (pos.left+$(this).height()));
		var middlepoint = ($(this).width() / 2) + pos.left;

		$('#label').hide();
		$('#next').hide();
		$('#prev').hide();
		$(this).hide();

		var imgout = 'img_'+activeimg;

		if(clickpos.pageX >= middlepoint){
			if(activeimg < (numimgs-1))
				activeimg++;
			else
				activeimg = 0;
			var imgin = 'img_'+activeimg;

			$('#'+imgout).animate({
				left: '0px',
				opacity: 0
				}, 'fast', 'swing', function(){
					$(this).css('left', mainwidth+'px');
					$('#'+imgin).animate({
						left: (pos.left+20)+'px',
						opacity: 1
						}, 'fast', 'swing', function(){
							var thispos = $(this).offset();
							$('#label').css('left', thispos.left);

							$('#clickaction').css('top', thispos.top);

							$('#clickaction').css('left', thispos.left-20);

							$('#clickaction').width($(this).width()+40);

							$('#clickaction').height($(this).height());

							$('#clickaction').show();
							
							thispos = $('#clickaction').offset();
							$('#label').html($('#img_'+activeimg).attr('lbl'));
							newtop = thispos.top+$('#clickaction').height()+10;
							$('#label').width(($('#clickaction').width()-50));
							$('#label').css('left', thispos.left+20);
							$('#label').css('top', newtop);
							$('#label').fadeIn();
							$('#next').css('left', thispos.left+$('#clickaction').width()-10);

							$('#next').fadeIn();
							$('#prev').fadeIn();
					});
			});
		} else{
			if(activeimg > 0)
				activeimg--;
			else
				activeimg = (numimgs-1);
			var imgin = 'img_'+activeimg;
			$('#'+imgout).animate({
				left: mainwidth+'px',
				opacity: 0
				}, 'fast', 'swing', function(){
					$('#'+imgin).css('left', '0px');
					$('#'+imgin).animate({
						left: (pos.left+20)+'px',
						opacity: 1
						}, 'fast', 'swing', function(){
							var thispos = $(this).offset();
							$('#label').css('left', thispos.left);
							$('#clickaction').css('top', thispos.top);
							$('#clickaction').css('left', thispos.left-20);
							$('#clickaction').width($(this).width()+40);
							$('#clickaction').height($(this).height());
							$('#clickaction').show();

							thispos = $('#clickaction').offset();
							$('#label').html($('#img_'+activeimg).attr('lbl'));
							newtop = thispos.top+$('#clickaction').height()+10;
							$('#label').width(($('#clickaction').width()-50));
							$('#label').css('left', thispos.left+20);
							$('#label').css('top', newtop);
							$('#label').fadeIn();
							$('#next').css('left', thispos.left+$('#clickaction').width()-10);

							$('#next').fadeIn();
							$('#prev').fadeIn();
					});
			});
		}
	});

	$('#mail').attr('href', 'mailto:'+$('#mail').attr('href').replace('[at]', '@'));
});

