$(document).ready(function() {
	// Domain base
	langbase = window.location.toString().substring(0, window.location.toString().lastIndexOf('/'));
	base = langbase.substring(0, langbase.lastIndexOf('/'));
	// Page position
	$('#page').css({position: 'relative'});
	// Menu click
	$('#menu ul a').click(function() {
		$(this).blur();
		var id = $(this).attr('id');
		var url = $(this).attr('href');
		$('#menu').animate({top: '-100px'}, {duration: 'slow', complete: function() {
			$('#menu #inner ul a').removeClass('current').css({'background-position': '0 0', 'margin': 0});
			$('#menu #inner ul #'+id).addClass('current').css('background-position', '-30px 0');
		}});
		$('.leftcover').slideDown('slow');
		$('.rightcover').slideDown('slow', function() {
			$.post(url+'/page', {}, function(data) {
				$('#content').html(data);
				prep_links();
				$('.leftcover').slideUp('slow');
				$('.rightcover').slideUp('slow');
				$('#menu').animate({top: 0}, {duration: 'slow', complete: function() {
					$('#menu #inner ul #'+id).css('background-position', '-30px 0');
				}});
			});
		});
		return false;
	});
	// Initial props for menu and background switch
	$('#menu ul a')
		//.css({backgroundPosition: '0 0'})
		.mouseover(function() {
			if(!$(this).hasClass('current'))
				$(this).animate({marginTop: '7px'}, {duration: 300});
		})
		.mouseout(function() {
			if(!$(this).hasClass('current'))
				$(this).animate({marginTop: '0'}, {duration: 300});
		});
	// Initial menu selection (based on url)
	$('#menu ul .current').css({backgroundPosition:'-30px 0'});
	$('#right').animate({scrollTop: 0}, 0);
	prep_links();
});
function triger_page(id) {$('#menu ul #'+id).trigger('click');}
function prep_links() {
	var title = '';
	// Domain base
	base = window.location.toString().substring(0, window.location.toString().lastIndexOf('/'));
	base = base.substring(0, base.lastIndexOf('/'));
	// Click on more info link => left side
	$('a[name=more]').click(function() {
		$('#left .content').html($(this).siblings('.hidden').html());
		$('#left').animate({scrollTop: '0px'}, 200, function() {
			prep_links();
		});
	});
	$('a[name=imgmore]').hover(
		function() {
			title = $(this).attr('title');
			$(this).attr('title', '');
			$(this).append('<div class="moreimg">'+title+'</div>');
			$('.moreimg').css({'width': $(this).find('img').width() + 'px', 'margin-top':($(this).find('img').height()/2-10), opacity: '0.5'});
			if($.browser.msie) {
				$('.moreimg').css('margin-left', '-'+$(this).find('img').width()+'px');
			}
		},
		function() {
			$(this).attr('title', title);
			$('.moreimg').remove();
		}
	);
	$('a[name=imgmore]').click(function() {
		$('#left .content').html($(this).parent().find('.hidden').html());
		$('#left').animate({scrollTop: '0px'}, 200, function() {
			prep_links();
		});
	});
	// Add scroll buttons on the left side
	if ($('#left .inner').height() > $('#left').height()) {
		$('#left').prepend('<div class="lscroll"><a href="javascript:void(0);" name="up"><img src="'+base+'/public/img/4_z1.gif" /></a><a href="javascript:void(0);" name="down"><img src="'+base+'/public/img/4_z2.gif" /></a></div>');
	}
	else $('.lscroll').remove();
	// Add scroll buttons on the right side
	if ($('#right .inner').height() > $('#right').height()) {
		$('#right').prepend('<div class="rscroll"><a href="javascript:void(0);" name="up"><img src="'+base+'/public/img/k1.gif" /></a><a href="javascript:void(0);" name="down"><img src="'+base+'/public/img/k2.gif" /></a></div>');
	}
	else $('.rscroll').remove();
	// On up scroll button press, scroll up
	$('a[name=up]').mousehold(60, function() {
		$(this).blur();
		$(this).parent().parent().stop().animate({scrollTop: '-=50px'}, 200);
	});
	// On down scroll button press, scroll down
	$('a[name=down]').mousehold(60, function() {
		$(this).blur();
		$(this).parent().parent().stop().animate({scrollTop: '+=50px'}, 200);
	});
	// Map links
	$('.map a').click(function() {
		var id = $(this).attr('id');
		$('.map .details').addClass('hidden');
		$('.map #lct'+id).removeClass('hidden');
	});
	$('.map a').hover(
		function() {
			title = $(this).attr('title');
			$(this).attr('title', '');
			$(this).after('<div class="over">'+title+'</div>');
			$('.over').css({'right': $(this).css('right'), 'top': ($(this).css('top').replace('px', '')-$('.over').height()-12+'px'), opacity: '0.7'});
		},
		function() {
			$(this).attr('title', title);
			$('.over').remove();
		}
	);
}
jQuery.fn.mousehold = function(timeout, f) {
	if (timeout && typeof timeout == 'function') {
		f = timeout;
		timeout = 100;
	}
	if (f && typeof f == 'function') {
		var timer = 0;
		var fireStep = 0;
		return this.each(function() {
			jQuery(this).mousedown(function() {
				fireStep = 1;
				var ctr = 0;
				var t = this;
				timer = setInterval(function() {
					ctr++;
					f.call(t, ctr);
					fireStep = 2;
				}, timeout);
			});

			clearMousehold = function() {
				clearInterval(timer);
				if (fireStep == 1) f.call(this, 1);
				fireStep = 0;
			}
			
			jQuery(this).mouseout(clearMousehold);
			jQuery(this).mouseup(clearMousehold);
		});
	}
}
