/**
 *  FIXES (BROWSER, CMS, ...)
 */

jQuery(function ($) {
	// IE
		if ($.browser.msie) { // IE
			// vertical resize of the window did not reset the footers position (how ever, a horizontal resize after a vertical one did)
				var ieFixPage = $('#page');
				$(window).bind('resize', function () {
						ieFixPage.css('height', 'auto').css('height', '100%');
				});
		}
	// CMS
		if ($('#actualContent').length && !$('#gwt-jahiatoolbar').length) $('#page').unwrap();
});



/**
 *  PROCESSING AND FORMATTING DOM ELEMENTS
 */

jQuery(function ($) {
  // priceBox table on detailpage
  	$('table.priceBox th:first-child, table.priceBox tr td:first-child').addClass('first');
  // rich text areas
  	$('.richTextArea p:last-child').addClass('last');
  	$('.richTextArea table.row:last-child').addClass('last');
		// back to top buttons
			var img = $('.rtBackToTopArrow:first');
			var topLink = $('<a href="#top"></a>').append(img);
			$('#page.richtext #content .richTextArea a[name]').prepend(topLink.clone());
			$('#page.richtext #content .richTextArea:last').append($('<div class="lastRtBackToTopContainer" />').append(topLink.clone()));
	// page title
		var title = $('#headerInfo h1').text();
		if ($('#page.detailpage').length && title && title.length) document.title = title;
});



/**
 *  TOOLS
 */

jQuery(function ($) {
	$('#printButton').bind('click', function () {
		$(this).blur();
		$('#logoContainer img').each(function () {
			var imgStr = $(this).css('background-image').replace(/"/g, "").replace(/url\(/g, "").replace(/\)/g, "").replace(/_bright.png/g, "_dark.png");
			$(this).attr('src', imgStr).removeAttr('width').removeAttr('height').css('background-image', 'none');
		});
		window.print();
	});
	$('#shareButton').bind('click', function () {
		$(this).blur();
	});
});



/**
 *  MENU - DROPDOWN
 */

jQuery(function ($) {
	var fadeDuration = 300;
	var container = $('#headerMenuContainer');
  container.find('li:last').addClass('last');
	$('.mainMenuDropDown').each(function () {
		var me = $(this);
		me.parents('li:first').children('a:first').bind('mouseenter', function () {
			// me.width(container.width() - $(this).css('margin-right').replace("px", ""));
      me.height($('#header').height() - 30 - 129);
    	me.stop(true, true).fadeIn(fadeDuration);
		}).end().bind('mouseleave', function () {
			me.stop(true, true).fadeOut(fadeDuration);
		});
	});
});



/**
 *  DESTINATION SLIDER   (HOMEPAGE, DETAILPAGE)
 */

jQuery(function ($) {
	var slideDuration = 500;
	$('.destinationSlider').each(function () {
		var me = $(this);
		var num = $('.destinationDetail', me).length;
    var width = 486;
		var mover = $('.moverArea', me);
		var locked = false;
		// init
			if (num <= 2) $('.sliderNavigation', me).hide();
			if (num == 3) {
				$('.destinationDetail', me).clone().prependTo(mover);
        num *= 2;
			}
  		mover.width(num * width);
		// events
			$('.slideLeftArrow', me).bind('click', function () {
				$(this).blur();
				if (locked) return false;
        locked = true;
				$('.destinationDetail:last', me).prependTo(mover);
				$('.destinationDetail:last', me).prependTo(mover);
				mover.css("left", -width * 2);
				mover.animate({ left: 0 }, slideDuration, function () {
	        locked = false;
				});
				return false;
			});
			$('.slideRightArrow', me).bind('click', function () {
				$(this).blur();
				if (locked) return false;
        locked = true;
				mover.animate({ left: -width * 2 }, slideDuration, function () {
					$('.destinationDetail:first', me).appendTo(mover);
					$('.destinationDetail:first', me).appendTo(mover);
	        mover.css("left", 0);
	        locked = false;
				});
				return false;
			});
	});
});



/**
 *  IMAGE SLIDER   (DETAILPAGE)
 */

jQuery(function ($) {
	var slideDuration = 500;
	$('.destinationImageSlider').each(function () {
		var me = $(this);
		var mover = $('.moverArea', me);
		var num = $('img', mover).length;
		var width = 709;
		var locked = false;
		// init
			if (num <= 1) $('.sliderNavigation', me).hide();
			if (num == 2) {
				$('img', mover).clone().prependTo(mover);
        num *= 2;
			}
  		mover.width(num * width);
		// events
			$('.slideLeftArrow', me).bind('click', function () {
				$(this).blur();
				if (locked) return false;
        locked = true;
				$('img:last', mover).prependTo(mover);
				mover.css("left", -width);
				mover.animate({ left: 0 }, slideDuration, function () {
	        locked = false;
				});
				return false;
			});
			$('.slideRightArrow', me).bind('click', function () {
				$(this).blur();
				if (locked) return false;
        locked = true;
				mover.animate({ left: -width }, slideDuration, function () {
					$('img:first', mover).appendTo(mover);
	        mover.css("left", 0);
	        locked = false;
				});
				return false;
			});
	});
});