/* init */
jQuery(document).ready(function() {
	frontpromo.init();
	frontcarousel.init();
	carousel2.init();
	$('.box-offers .ph .scrollarea').tinyscrollbar({size:192});
	bookbox.init();
			if ($.browser.msie) {
			$(".box-content").each(function() {
				this.fireEvent('onmove');
			});
		}

});



/* promo on frontpage */
var frontpromo = {
	gallery: {},

	init: function() {
		
		// only circle navigation if minimum amount of items
		var wrap = "circular";

		if ($("#frontpromo-nav").length > 0) {
			/* hide navigation arrows if few items */
			if ($("#frontpromo-nav .items li").length < 5) {
				$("#frontpromo-nav .frontpromo-prev").hide();
				$("#frontpromo-nav .frontpromo-next").hide();
				wrap = null;
			}
			
			/* hide navigation if only one item */
			if ($("#frontpromo-nav .items li").length < 2) {
				$("#frontpromo-nav").hide();
			}

			/* init gallery */
			LBi_gallery.frontpromo = $('#frontpromo').LBi_gallery({
				type: "fadebg",
				dir: "ltr",
				width:461,
				height:282,
				speed: 500,
				delay: 3500,
				cb_startAnim: frontpromo.callback,
				autostart: true,
				fancyboxtitle: frontpromo.fancyboxtitle
			});
		
			
			/* init navigation */
			frontpromo.gallery = jQuery("#frontpromo-nav .items").jcarousel({
				scroll: 1,
				vertical: true,
				wrap: wrap,
				itemFallbackDimension: 52,
				initCallback: frontpromo.carousel_initCallback,
				itemVisibleOutCallback: { onAfterAnimation:frontpromo.hideOutside },
				itemVisibleInCallback: { onBeforeAnimation:frontpromo.showInside },
				buttonNextHTML: null,
				buttonPrevHTML: null
			});
		}

	},
	
	fancyboxtitle: function(cb) {
		return '<a href="'+LBi_gallery.frontpromo.getLink(cb["cur-nr"])+'" onclick="showWaitLayer();" class="btn2 big center"><span>'+LBi_gallery.frontpromo.getHeadline(cb["cur-nr"])+'</span></a>';
	},
	
	carousel_initCallback: function(carousel) {
		jQuery('#frontpromo-nav .frontpromo-next').bind('click', function() {
			carousel.next();
			frontpromo.fixListheight();
			frontpromo.stopAutoScroll();
			return false;
		});

		jQuery('#frontpromo-nav .frontpromo-prev').bind('click', function() {
			carousel.prev();
			frontpromo.fixListheight();
			frontpromo.stopAutoScroll();
			return false;
		});

		$("#frontpromo-nav .items li").eq(0).addClass("sel");

		$("#frontpromo-nav .items li a").each(function() {
		
			$(this).click(function(e) {
				$("#frontpromo-nav .items li.sel").removeClass("sel");
				$(this).parent("li").addClass("sel");
				//e.stopPropagation();
			});
		
			$(this).hover(function() {
				$(this).parent("li").addClass("over");
			}, function() {
				$(this).parent("li").removeClass("over");
			
			});
		});


	},


	change: function(item) {	

		$("#frontpromo-nav .items li.sel").removeClass("sel");
		$("#frontpromo-nav .items li a.elm_" + item).parent("li").addClass("sel");	
	},

	
	// hide elements not in visible range
	hideOutside: function(carousel, item) {
		$(item).css("visibility","hidden");
	},

	// show elements in visible range
	showInside: function(carousel, item, idx) {
		$(item).css("visibility","visible");
	},

	
	callback: function(cb) {
	
		if ($("#frontpromo").data("autoscroll") === undefined) {
			$("#frontpromo").data("autoscroll", true);
		}
		if ((frontpromo.gallery.data('jcarousel').last == cb["cur-nr"]-1 || $("#frontpromo").data("scrolling")) && $("#frontpromo").data("autoscroll")) {
			frontpromo.gallery.data('jcarousel').next();
			frontpromo.fixListheight();
			$("#frontpromo").data("scrolling", true);
		}
	//	alert(cb["next-ref"]);
		frontpromo.change(cb["next-ref"]);
	},
	
	showitem: function(item) {
		frontpromo.change(item);
	},
	
	user: function(item, obj) {

		frontpromo.stopAutoScroll();
		frontpromo.showitem(item);
		LBi_gallery.frontpromo.gotoAndStop(item);
		
	},

	stopAutoScroll: function() {
		LBi_gallery.frontpromo.stopIt();
		$("#frontpromo").data("autoscroll", false);
	},
	
	// fix list height which mess up due to negative margin and position
	fixListheight: function() {
		var h = 0;
		$("#frontpromo-nav ul li").each(function() {
			h+= $(this).outerHeight();
		});
		$("#frontpromo-nav ul").css("height",h);
	}
};


/* wide carousel */
var frontcarousel = {
	carousel: {},

	init: function() {
		if ($("#frontcarousel").length > 0) {
		
			frontcarousel.start();
		}
	},
	
	start: function() {
		
			var autoscroll = 3;	// if set to 0 it will not autoscroll when initialized

			if (location.href.indexOf("solresor") != -1) {
				autoscroll = 0;
			}

			
			/* hide navigation arrows if few items */
			if ($("#frontcarousel .items li").length < 4) {
					$("#frontcarousel-prev").hide();
					$("#frontcarousel-next").hide();
					autoscroll = 0;
					
			}
		
			
			/* init carousel */
			frontcarousel.carousel = jQuery("#frontcarousel .items").jcarousel({
				scroll: 1,
				vertical: false,
				wrap: "circular",
				itemFallbackDimension: 52,
				initCallback: frontcarousel.carousel_initCallback,
				buttonNextHTML: null,
				buttonPrevHTML: null,
				auto:autoscroll
			});
			
	},
	
	carousel_initCallback: function(carousel) {
		jQuery('#frontcarousel-next').bind('click', function() {
			carousel.next();
			carousel.stopAuto();
			return false;
		});

		jQuery('#frontcarousel-prev').bind('click', function() {
			carousel.prev();
			carousel.stopAuto();
			return false;
		});

		$("#frontcarousel .items a").eq(0).addClass("sel");
	}
}



/* 2 col carousel */
var carousel2 = {
	carousel: {},

	init: function() {
		if ($("#carousel2").length > 0) {
			carousel2.start();
		}
	},
	
	start:function() {
		
		var elementHeight = 195; // default height
		
		/* hide navigation arrows if few items */
		if ($("#carousel2-nav .items li a").length < 2) {
			$("#carousel2-nav").hide();
			var elementHeight = 230;
		}

		/* init gallery */
		LBi_gallery.carousel2 = $('#carousel2').LBi_gallery({
			type: "slideshow",
			dir: "ltr",
			width:386,
			height:elementHeight,
			speed: 500,
			delay: 3500,
			cb_startAnim: carousel2.callback,
			autostart: false
		});

		
		$("#carousel2-nav .items li a").eq(0).addClass("sel");

	},
	
	callback: function(cb) {
		$("#carousel2-nav .items li a").removeClass("sel");

		
		$("#carousel2-nav .items li a").eq(cb["cur-nr"]-1).addClass("sel");
		/*
		switch (cb["ndir"]) {
			case 1:
				$("#carousel2-nav .items li a").eq(cb["next-nr"]-1).addClass("sel");
				break;
			case -1:
				$("#carousel2-nav .items li a").eq(cb["prev-nr"]-1).addClass("sel");
				break;
			default:
				$("#carousel2-nav .items li a").eq(cb["cur-nr"]-1).addClass("sel");
				break;
		}
		*/
	},
	
	user: function(dir) {
		if (dir == "next") {
			LBi_gallery.carousel2.slideNext();
		}

		if (dir == "prev") {
			LBi_gallery.carousel2.slidePrev();
		}

	},
	
	go: function(ref) {
	
		LBi_gallery.carousel2.gotoAndStop(ref);
	
	}
}
