$(document).ready(function() {
	$("#maincontainer").css('width','951px');

	if ($("#portnav").length>0) {
	
		function loadImage(src) {
			$('#portfoto').empty().addClass('loading');
			block = true;
			
			var patt=new RegExp("^[0-9]+\.[a-zA-Z]{3,4}$");
			if (patt.test(src)===true) {
				src = PATH_TO_PIX+'img.php?src='+src+'&w=634&h=634&t=rs';
				var img = new Image();
				$(img)
					.load(function () {
						$(this).hide();
						$('#portfoto').removeClass('loading').append(this);
						$(this).fadeIn();
						block = false;
					})
					.error(function () {})
					.attr('src', src);
			} else {
				videosrc = decodeVideoName(src,520,358);
				$('#portfoto').removeClass('loading').append(this);
				$('#portfoto').html(videosrc);
				block = false;
			}
			
		}
		
		loadImage($('#t'+curId+' a').attr('id'));

		function selItem(cid) {
			if (!block) {
				curId = cid;
				$('.num').removeClass('sel');
				$('#t'+curId+' a').addClass('sel');
				loadImage($('#t'+curId+' a').attr('id'));
				$('.legenda').html($('#t'+curId+' a').attr('alt'));
			}
		}

		var maxId = parseInt($('.num:last').parent().attr('id').substr(1));
		$('#t1 a').addClass('sel');
		
		$(".num").click(function () {
			selItem($(this).parent().attr('id').substr(1));
		});

		$().mousemove(function(e) {
			var offset = $('#portfoto').offset();
			var min_x = offset.left;
			var max_x = min_x + $('#portfoto').width();
			var min_y = offset.top;
			var max_y = min_y + $('#portfoto').height();
			var midPoint = min_x + ((max_x-min_x)/2);
			if (!block && e.pageX>min_x && e.pageX<max_x && e.pageY>min_y && e.pageY<max_y) {
				if (e.pageX>midPoint) {
					$('#seta_dir').show();
					$('#seta_esq').hide();
					curSide = 'dir';
				} else {
					$('#seta_esq').show();
					$('#seta_dir').hide();
					curSide = 'esq';
				}
			} else {
				$('#seta_esq').hide();
				$('#seta_dir').hide();
				curSide = '';
			}
		});

		$('#portfoto, #seta_esq, #seta_dir').click(function () {
			if (!block) {
				if (curSide === 'dir') {
					if (curId<maxId) {
						curId++;
					} else {
						curId = 1;
					}
					selItem(curId);
				} else if(curSide === 'esq') {
					if (curId>1) {
						curId--;
					} else {
						curId = maxId;
					}
					selItem(curId);
				}
			}
		});

	}
});

var curId = 1;
var curSide = '';
var block = false;