$(document).ready(function() {
	$(".thickbox").append("<span class='magnify'></span>");

  // make some images movable in their container (Amtrak)
	makeDraggable();

	// care about the gallery
	if ($("div#gallery").length) { 
		$.getScript("/js/jquery.galleria.js");
	}
	
	// the content’s width can be smaller without the adserver banner
	if (!$("#adserver1").length) {
		$("#wrapper").width(983);
	}

	// care about the shortlists
	if ($('#shortlistcontainer').length) {
		var params = $('#shortlistcontainer p a').attr('href');
		params = params.split('?')[1]
		var shortlist = 'tourlist.php?ajax=1&contentonly=1&' + params;
		var morelink = '<p class="morelink"><a href="reisen.php?' + params + '">Alle Angebote anzeigen</a></p>'
		$('#shortlistcontainer').load(shortlist, function() {
			$('#tourlist li:last').css({border: 'none'})
			$('#shortlistcontainer').append(morelink);
		});
	}

	// care about the boxoffers
	if ($('#boxoffers').length) {
		scope = $('#boxoffers').text();
		var boxoffer = '/main/boxoffers.php?ajax=1&scope=' + scope;
		$('#boxoffers').load(boxoffer, function() {
			boxofferLink();
			$('#boxoffers').fadeIn(1500, boxofferTurnPage);
		});
	}
});

// functions needed for the boxoffer functionality
function boxofferLink() {
	$('#boxofferpager span[class!="announce"]').click(function() {
		var number = parseInt($(this).html());
		boxofferLinkAction(number, scope);
	});
}
function boxofferLinkAction(number, scope) {
	stopTurnPage();
	var boxoffer = '/main/boxoffers.php?ajax=1&number=' + number + '&scope=' + scope;
	$('#boxoffers').fadeTo(500, 0.1, function() {
		$('#boxoffers').load(boxoffer, function() {
			boxofferLink();
			$('#boxoffers').fadeTo(500, 1, boxofferTurnPage);
		});
	});
}
function boxofferTurnPage() {
	var forward = parseInt($('#boxofferpager .forwardlink').html());
	aktiv = window.setTimeout("boxofferLinkAction(" + forward + ", '" + scope + "')", 10000);
}
function stopTurnPage() {
	window.clearTimeout(aktiv);
}

// make some images movable in their container (Amtrak)
function makeDraggable() {
	var image = $(".dragimg img");
	var imgwidth = image.width();
	var imgheight = image.height();
	if (imgwidth == 0 || imgheight == 0) {
		aktiv = window.setTimeout("makeDraggable()", 200);
		return;
	}
	if (imgwidth < 696) {
		$(".dragimg").width(imgwidth);
	}
	if (imgheight < 394) {
		$(".dragimg").height(imgheight);
	}
	if (imgheight > $(".dragimg").innerHeight() || imgwidth > $(".dragimg").innerWidth()) {
		image.draggable()
		                 .css({'cursor': 'move', 'text-align': 'center'});
		$(".dragimg").before('<p style="width: ' + Math.min(imgwidth + 6, 702) + 'px; margin: 0 auto 1em; "><strong>Verschieben Sie den Kartenausschnitt.</strong> Um den Kartenausschnitt zu verschieben, klicken und halten Sie die linke Maustaste gedrückt und verschieben Sie die Karte in die gewünschte Richtung.</p>');
	}
}
