jQuery.fn.scroller = function(params){
	var p = params || {
		next:"nextImage",
		prev:"prevImage",
		images:"images",
		presentation:"presentation",
		numbers:"imageNumbers",
		
		frame:"viewerFrame",
		width:100,
		child:"a",
		duration:1000
	};
	var images = $("#"+p.images);
	
	var presentationFrame = $("#"+p.presentation);
	var presentationFrameContext = presentationFrame.context;
	
	var imageNumbers = $("." + p.numbers);
	
	presentationFrameContext.currentImage = 0;
	presentationFrameContext.imagesCount = images.children().length;

	// show and highlight the first image
	images.find(':first').clone().appendTo(presentationFrame);
	imageNumbers.find(':first').addClass('selected');

	// hide the images
	images.children().hide();
	
	// add the scrolling functions
	var scrollTo = function(index) {
		if (presentationFrameContext.scrolling || presentationFrameContext.currentImage == index || isNaN(index)) return;
		
		presentationFrameContext.scrolling = true;

		var currentIndexExpression = ':nth-child(' + (presentationFrameContext.currentImage + 1) + ')';
		var newIndexExpression = ':nth-child(' + (index + 1) + ')';

		imageNumbers.find(currentIndexExpression).removeClass('selected');
		imageNumbers.find(newIndexExpression).addClass('selected');

		var newImage = images.find(newIndexExpression).clone();
		
		newImage.appendTo(presentationFrame);
		presentationFrameContext.currentImage = index;
		
		setTimeout('removeOldImage("' + p.presentation + '")', 350);
		newImage.fadeIn(400, function() {
			presentationFrameContext.scrolling = false;
		});
	}
	
	var scrollNext = function() {
		var index = (presentationFrameContext.currentImage + 1) % presentationFrameContext.imagesCount;
		scrollTo(index);
	}
	var scrollPrevious = function() {
		var index = (presentationFrameContext.currentImage + (presentationFrameContext.imagesCount - 1)) % presentationFrameContext.imagesCount;
		scrollTo(index);
	}
	
	$('#'+p.next).click(scrollNext);
	$('#'+p.prev).click(scrollPrevious);
	
	imageNumbers.children().click(function() {
		scrollTo($(this).parent().children().index(this));
	});
	
	presentationFrame.click(scrollNext);
}

function removeOldImage(presentationFrameId) {
	var presentationFrame = $("#" + presentationFrameId);
	var oldImage = presentationFrame.find(':first');
	oldImage.fadeOut(150, function() {
		$(this).remove();
	});
}

$(function() {
	$(".content").scroller( {
		next : "nextImage",
		prev : "previousImage",
		images : "images",
		presentation : "imageViewer",
		numbers: "imageNumbers",
		width : 700,
		child : "li",
		duration : 1100
	});
	
	$(".overlay").parent().mouseover(function() {
		$(this).find('.overlay').stop().css('visibility', 'visible').animate( {
			opacity : 1
		}, 200);
	}).mouseout(function() {
		$(this).find('.overlay').stop().animate( {
			opacity : 0
		}, 200, null,
				function(i) {$(this).css('visibility', 'hidden');}
		);
	});
	
	$(".open").click(
		function() {
			$(this).nextAll('.full').slideToggle(1000);
		}
	);
	
	if (typeof mapping != 'undefined') {
		processHighlighting('.groups', '.matrix');
	}
});

function processHighlighting(menu, matrix) {
	$(menu).find('[id]').mouseover(function() {
		highlight($(this).attr('id'), true);
	}).mouseout(function() {
		highlight($(this).attr('id'), false);
	});
	$(matrix).find('[id]').mouseover(function() {
		highlight($(this).attr('id'), true);
	}).mouseout(function() {
		highlight($(this).attr('id'), false);
	});
}

function highlight(id, enable) {
	var m = mapping[id.substring(2)];
	if (m) {
		var link = $('#' + m['link']);
		var image = $('#' + m['image']);
		if (enable) {
			link.addClass('highlighted');
			image.find('.highlighted').stop().css('visibility', 'visible').animate( { opacity : 1 }, 200);
		} else {
			link.removeClass('highlighted');
			image.find('.highlighted').stop().animate( { opacity : 0 }, 200, null,
				function(i) {$(this).css('visibility', 'hidden');}
			);
		}
	}
}

function isDefined(object, variable) {
	if (typeof object == 'undefined') return false;
	return (typeof(object[variable]) == "undefined")? false : true;
}

function loadArchiveContent(url) {
	var archiveContent = $('#archiveContent');
	var archiveList = $('#archiveList');
	
	if ($('.archive').hasClass('archiveOpen')) {
		// hide archive
		archiveContent.find('table').hide("slow");
		archiveList.find('ul').slideUp("slow");
		
		$('.archiveOpen').removeClass('archiveOpen').addClass('archiveClosed');
		
		archiveContent.context.archiveLoaded = false;
	}
	else {
		// load archive
		$.getJSON(url, null, function(data, status) {
			if (status == 'success') {
				var archiveContentHtml = '<table style="display:none" class="matrix">';
				var archiveListHtml = '<ul style="display:none" class="products">';
				$.each(data.rows, function(i, row) {
					if (typeof (row) == 'undefined') return;
					archiveContentHtml += '<tr>';
					$.each(row.columns, function(i, item) {
						if (typeof (item) == 'undefined') return;
						var itemHtml = '<td';
						if (isDefined(item, 'columns'))
							itemHtml += ' colspan="' + item.columns + '"'; 
						if (isDefined(item, 'rows'))
							itemHtml += ' rowspan="' + item.rows + '"'; 
						
						itemHtml += ' id="i_' + item.id + '"><a href="' + item.href + '"><img class="absolute" src="' + item.image + '"/>';
						if (item.highlighted)
							itemHtml += '<img class="highlighted relative" src="' + item.highlighted + '"/>';
							
						itemHtml += '</a></td>';
						archiveContentHtml += itemHtml;
						archiveListHtml += '<li id="l_' +item.id + '"><a href="' + item.href + '">' + item.label + '</a></li>';
						
						mapping[item.id] = {link : 'l_' + item.id, image : 'i_' + item.id};
					});
					archiveContentHtml += '</tr>';
				});
				archiveContentHtml += '</table>';
				archiveListHtml += '</ul>';
				
				archiveContent.html(archiveContentHtml);
				
				archiveList.html(archiveListHtml);
				
				processHighlighting('#archiveList', '#archiveContent');
				
				archiveContent.find('table').show("slow");
				archiveList.find('ul').slideDown("slow");
				
				$('.archiveClosed').removeClass('archiveClosed').addClass('archiveOpen');
				
				archiveContent.context.archiveLoaded = true;
			}
			else {
				alert(status);
			}
		});
	}
}
