function Reis()
{
}

Reis.setup = function ()
{
	$(".booknow").bind("click", Reis.direct_booking_click);
	$(".user-rating").find("h5").bind("click", Reis.accommodation_rating_click);

	TabControl.registerInitEvent(function()
	{
	    var tabctrl = TabControl.instances.trip_tabcontrol;
	    if (tabctrl)
		    tabctrl.addListener("onswitch", Reis.tabswitch_onclick);
	});
	Reis.setupWeatherCarousel();
}

Reis.accommodation_rating_click = function ()
{
    $(this).parent().parent().toggleClass("user-rating-expand");
}

Reis.direct_booking_click = function ()
{
	var tabctrl = TabControl.instances.trip_tabcontrol;
	var priceCount = $("td.price").length;

	if (tabctrl && priceCount != 0)
	{
		var elem = document.getElementById("tab_prijzen");
		if (elem)
		{
			if (elem.offsetHeight == 0)
				tabctrl.showTab("prijzen");

			return false;
		}
	}
	return true;
}

Reis.weather_click = function ()
{
    var tabctrl = TabControl.instances.trip_tabcontrol;
    if (tabctrl)
	    tabctrl.showTab("weer");
	return false;
}

Reis.tabswitch_onclick = function (event)
{
	var item = event.data.item;
	Reis.toggleSubnavigation(item);
	Reis.toggleBookButton(item);
	
	if (item == "weer")
		Reis.setupWeatherCarousel();
}

Reis.toggleBookButton = function (item)
{
	if (item == "prijzen")
		$(".booknow").addClass("bookactive");
	else
		$(".booknow").removeClass("bookactive");
}

Reis.toggleSubnavigation = function (item)
{
	var elems = $(".linkboard");
	for (var i = 0; i < elems.length; i++)
	{
		if (elems[i].id == "content_navigation_" + item)
			$(elems[i]).show();
		else
			$(elems[i]).hide();
	}
}

Reis.setupWeatherCarousel = function ()
{
	if (Reis.weatherCarouselInitialized == 1)
		return;

    $(".weather_forecast").each(function() {
        var element = $(".weather-item", this);
        if (0 < element.length && Number(element.width()) != 0)
        {
            var itemWidth = Number(element.width()) 
                + Reis.getCssAttributePixelValue(element, "margin-right")
                + Reis.getCssAttributePixelValue(element, "padding-right")
                + Reis.getCssAttributePixelValue(element, "padding-left");

			var maxIndex = Math.max(0, element.length -4);
            var maxOffset = itemWidth * maxIndex;
            Reis.updateWeatherCarousel(this, 0, maxOffset);
            
            var data = { carousel: this, itemWidth: itemWidth, maxOffset: maxOffset };
            $("a.next, a.prev", this).bind("click", data, Reis.moveWeatherCarousel);
            
            Reis.weatherCarouselInitialized = 1;
        }
    });
}

Reis.moveWeatherCarousel = function (e)
{
    var container = $(".weather-carrousel", e.data.carousel);
    var element = $(".weather-item", container);
    var direction = ($(this).attr("class").match(/\bnext\b/) == "next") ? 1 : -1;
    
    var offset = -Reis.getCssAttributePixelValue(container, "left");
    offset += e.data.itemWidth * direction;
    offset = Math.max(0, Math.min(offset, e.data.maxOffset));
    Reis.updateWeatherCarousel(e.data.carousel, offset, e.data.maxOffset);

    return false;
}

Reis.updateWeatherCarousel = function (carousel, offset, maxOffset)
{
    $("a.prev", carousel).css("visibility", (offset <= 0) ? "hidden" : "");
    $("a.next", carousel).css("visibility", (offset >= maxOffset) ? "hidden" : "");
    $(".weather-carrousel", carousel).css("left", -offset + "px");
}

Reis.getCssAttributePixelValue = function(element, property)
{
    return Number($(element).css(property).match(/^(-?\d+)px$/) ? RegExp.$1 : 0);
}

$(document).ready(function() {
	setTimeout(Reis.setup, 1);
});
