/*Rotationsettings for all offer rotations*/
var rotationTimeInSeconds = 5;
var intRotationCount = 0;
var intRotationItems = 0;
var rotationTimer;
var rotationLastNavigationEvent = "";
var selectLastNavigationEvent = "";
var selectedProductOfferIndex = 1;

$(document).ready(function () {
    init_rotation();
    prepareFormFieldsLabelInside();
    showProductOfferItem(selectedProductOfferIndex);

    $('.hotelList li:nth-child(odd)').addClass('colLeft');

    var ua = navigator.userAgent.toLowerCase();
    var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
    

    $('.offerNavigation a').mouseover(function () { rotationLastNavigationEvent = "mouseenter"; stopRotation(); showNewRotationItem(1 + $(this).parent().index()); }).mouseout(function () { if (rotationLastNavigationEvent != "mouseleave")/*because IE keeps firing event*/{ rotationLastNavigationEvent = "mouseleave"; intRotationCount--; if (intRotationCount < -1) intRotationCount = intRotationItems - 2; playRotation(); } });
    //ipad and iphone fix
    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
        $('.productOfferNavigation a').click(function () { selectLastNavigationEvent = "mouseenter"; showProductOfferItem(1 + $(this).parent().index()); });
        $('body').addClass('bodyIPhone');
    }
    else if (isAndroid) {
        $('body').addClass('bodyAndroid');
    }
    else {
        $('.productOfferNavigation a').mouseover(function () { selectLastNavigationEvent = "mouseenter"; showProductOfferItem(1 + $(this).parent().index()); });
    }

    $('.bookingSmall .btnExpandBooking').click(function () { $('.booking').removeClass('bookingSmall').addClass('bookingLarge bookingAlternateLarge') });
    $('.bookingSmall .btnMinimizeBooking').click(function () { $('.booking').removeClass('bookingLarge bookingAlternateLarge').addClass('bookingSmall') });
    $('.bookingSmall + .bookingBg, .bookingAlternateLarge + .bookingBg').hide();

    $(".sliderGalleryOuter .navNext").click(function () { $(".sliderGalleryOuter .btn-right").click() });
    $(".sliderGalleryOuter .navPrev").click(function () { $(".sliderGalleryOuter .btn-left").click() });

    var container = $('div.sliderGallery');
    var ul = $('ul', container);
    var itemsWidth = 240; //ul.innerWidth() - container.outerWidth();
    var intSliderWithInAll = $(".sliderGalleryOuter ul li").size() * itemsWidth;
    $(".sliderGalleryOuter ul").width(intSliderWithInAll);

    $('.slider', container).slider({
        min: 0,
        max: itemsWidth,
        handle: '.ui-slider-handle',
        stop: function (event, ui) { ul.animate({ 'left': ui.value * -1 }, 500); },
        slide: function (event, ui) { ul.css('left', ui.value * -1); }
    });

    $(".btn-left").click(function () {
        var elValue = $('.slider', container).slider('option', 'value');
        if (elValue > 0) {
            elValue = elValue - itemsWidth;
            if (elValue < 0) {
                elValue = 0;
            }
            $(".sliderGallery .slider").slider('value', elValue);
            $(".sliderGallery ul").animate({ 'left': elValue * -1 }, 500);
        }
    });

    $(".btn-right").click(function () {
        var elValue = $('.slider', container).slider('option', 'value');
        if (elValue < itemsWidth) {
            elValue = elValue + itemsWidth;
            if (elValue > itemsWidth) {
                elValue = itemsWidth;
            }
            $(".sliderGallery .slider").slider('value', elValue);
            $(".sliderGallery ul").animate({ 'left': elValue * -1 }, 500);
        }
    });

});

function init_rotation() {
    showNewRotationItem(intRotationCount);
    intRotationItems = $('.offerNavigation > li').size();
    if(intRotationItems == 1)
    {
			$('.offerNavigation').addClass('offerNavigation1Item');
			showNewRotationItem(1);
    }
    else
    {
	    playRotation();
	  }
}
function stopRotation() {
    clearTimeout(rotationTimer);
}
function playRotation() {
    intRotationCount++;
    if (intRotationCount > intRotationItems )
        intRotationCount = 1;

    showNewRotationItem(intRotationCount);
    rotationTimer = setTimeout(function () { playRotation() }, rotationTimeInSeconds * 1000);
}
function showNewRotationItem(argInt) {
    $('.offerNavigation li a.selected').removeClass('selected');
    var origItem = $('.offerNavigation li:nth-child(' + argInt + ') a:first-child');
    for (var i = argInt; i >= 1; i--) {
        var item = $('.offerNavigation li:nth-child(' + i + ') a:first-child');
        if (!item.hasClass('duplicated')) {
            item.addClass('selected');
            $('#offerHolderBox').html(origItem.next().clone());
            break;
        }
    }
}

function prepareFormFieldsLabelInside()
{
	$('textarea[title]').focus(function(){showHideInputValue($(this), 'focus')}).blur(function(){showHideInputValue($(this), 'blur')}).each(function(){showHideInputValue($(this), 'blur')});
	$('input[type=text][title]').focus(function () { showHideInputValue($(this), 'focus') }).blur(function () { showHideInputValue($(this), 'blur') }).each(function () { showHideInputValue($(this), 'blur') });
}

function showHideInputValue(argObj, argStrType)
{
	if(argStrType == 'focus' && $(argObj).val()==$(argObj).attr('title'))
		$(argObj).val('')
	else if($(argObj).val()=="")
		$(argObj).val($(argObj).attr('title'))
}

var _showProductOfferItem_displayProductText = null;
function showProductOfferItem(argInt) {
    $('.productOfferNavigation li a.selected').removeClass('selected');
    $('.productOfferNavigation li:nth-child(' + argInt + ') a:first').addClass('selected');
    $('#productOfferHolderBox').html($('.productOfferNavigation li a.selected').next().clone());

    if (_showProductOfferItem_displayProductText == null)
        _showProductOfferItem_displayProductText = $('input[name=displayingProduct]').length == 0; //cache result

    if (_showProductOfferItem_displayProductText) {
//        $('.productOfferTextUl li.showProductOffer').removeClass('showProductOffer').addClass('hideProductOffer');
        $('.productOfferTextUl li:nth-child(' + argInt + ')').removeClass('hideProductOffer').addClass('showProductOffer');
    }
}


