$(document).ready(function() {
	
	// hide dialog windows
	$('#dialog').hide();
	$('.jqmClose').show();
	
	// calculate width of wall container
	var calculateWallWidth = function() {
		var wall = $('#wall');
		$(wall).css('width', $(document).width());
	}
	calculateWallWidth();
	
	// enable application menu
	$('.dropdown').each(function() {
		$(this).parent().eq(0).hover(function () {
			var current = $('.dropdown:eq(0)', this);
			current.slideDown(100);
		}, function () {
			var current = $('.dropdown:eq(0)', this);
			current.fadeOut(200);
		});
	});
	
	// do the drag
	var drag_options = {
		which: 0,  
		distance: 10,  
		not: ":input, a"
	};
	var wall = $('#wall');
	var lastOffset = 0;
	$(wall).bind("drag", drag_options, function(event) {
		var pos = $(wall).offset();
		var offset = event.offsetX;
		var direction = (lastOffset < offset) ? 'left' : 'right';
		var doDrag = true;
		// if wall is on the left, we cannot drag it further to the left
		if(pos.left >= 0 && direction == 'left') { doDrag = false; }
		// if wall is on the right, we cannot drag it further to the right
		if(($(window).width() - ($(wall).width() + pos.left)) >= 0 && direction == 'right') { doDrag = false; }
		// if everything is ok, allow the drag
		if(doDrag) {
			$(wall).stop(true).animate({ left: offset + "px" }, 0, function() {
				// re-position if too far on the right
				if($(wall).offset().left > 0) {
					$(wall).css('left', 0);
				}
				// re-position if too far on the left
				if(($(window).width() - ($(wall).width() + $(wall).offset().left)) > 0) {
					$(wall).css('left', $(window).width() - $(wall).width());
				}
			});
			lastOffset = offset;
		}
	});
	// we want some nice cursors while dragging
	$(wall).bind('mousedown', function() {
		$(this).css('cursor', '-moz-grabbing');
	}).bind('mouseup', function() {
		$(this).css('cursor', 'move');
	});
	// we need a function to pan to client coords
	var panTo = function(x, y) {
		$(wall).stop(true).animate({ left: x + "px" }, 1000, function() {
			// finished panning
		});
	}
	// direct selection of client image
	$('.pan_to').click(function() {
		var x = $('a', this).attr('rel');
		panTo(-1 * x);
		return false;
	});
	
	// disable a click on "clients"
	$('.no-link').click(function() {
		return false;
	});
	
	// handle dialog windows
	$('.dialog').click(function() {
		$('#dialog').jqmHide();
		$('#dialog-title span').text('');
		$('#dialog-content').empty();
	});
	$('#dialog').jqDrag('.drag-handle').jqm({
		trigger: '.dialog',
		overlay: 0,
		onShow: function(h) {
			var url = $(h.t).attr('href') + '?format=html';
			h.w.fadeIn(500, function() {
				$('#dialog-title span').text($(h.t).attr('rel'));
				h.w.find('#dialog-wrapper').addClass('loading');
				$('#dialog-content').load(url, {}, function() {
					h.w.find('#dialog-wrapper').removeClass('loading');
					setTimeout(function() {
						$('#news-wrapper').jScrollPane(); // we want a scrollbar for news container
					}, 100);
				});
			}); 
		},
		onHide: function(h) {
			h.w.hide(); //fadeOut(500, function() {}); 
		} 
	});
	// lets pretend someone clicked the faq item on startup
	$('a#help-link').click();
	
	// add tooltip behavior to description markers
	$('.marker-type-info a.marker-link, .marker-type-video a.marker-link, ').each(function() {
		var animatingTooltip = false;
		var tooltip = $(this).parent().find('.tooltip');
		$(this).hover(function() {
			if(!animatingTooltip) {
				animatingTooltip = true;
				$(tooltip).fadeIn('fast', function() {
					animatingTooltip = false;
				});
			}
		}, function() {
			$(tooltip).fadeOut('fast', function() {
				if(!animatingTooltip) {
					animatingTooltip = true;
					$(tooltip).fadeOut('fast', function() {
						animatingTooltip = false;
					});
				}
			});
		});
	});
	
});