var slidey;
var id;

function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
    }
}

$(document).ready(function () {
    
    

    /* ADVANCED SEARCH */

    if ($(".ddlCountry").prop("selectedIndex") == 0 && $(".ddlMonth").prop("selectedIndex") == 0 && $(".ddlYear").prop("selectedIndex") == 0 && $(".ddlDurationMin").prop("selectedIndex") == 0 && $(".ddlDurationMax").prop("selectedIndex") == 0 && !isNumber($(".txtPriceFrom").val()) && !isNumber($(".txtPriceTo").val())) {
        $('.advanced-contents').hide();
        $('.advanced-contents_in').css('opacity',0)
    }

    $('.advanced-link').click(function toggleSlider() {
        if ($(this).prev().is(":visible")) {
            $(this).prev().children('.advanced-contents_in').animate(
            {
                opacity: "0"
            },
            300,
            function () {
                $(this).parent().slideUp();
            }
        );
            $('.advanced-link').removeClass('on');
        }
        else {
            $(this).prev().slideDown(300, function () {
                $(this).children('.advanced-contents_in').animate(
                {
                    opacity: "1"
                },
                300
            );
                $('.advanced-link').addClass('on');
            });
        }
        return false;
    });



    /* EXPANDABLE AREA SHOW/HIDE */

    $('.expandable-header').click(function toggleSlider() {
        if ($(this).next().is(":visible")) {
            $(this).next().children('.expandable-contents_in').animate(
            {
                opacity: "0"
            },
            300,
            function () {
                $(this).parent().slideUp();
            }
        );
        }
        else {
            $(this).next().slideDown(300, function () {
                $(this).children('.expandable-contents_in').animate(
                {
                    opacity: "1"
                },
                300
            );
            });
        }
        return false;
    });



    /* INFO PAGE TABS - SET DEFAULT STATE */

    $('.tabs_content.info .expandable-contents').hide();
    $('.tabs_content.info .expandable-contents_in').fadeTo(0, 0);



    /* HOLIDAY LIST ITEM - SLIDESHOW */

    $(".HL_item").each(function () {
        $(this).hover(function () {
            slidey = $(this).find(".slideshow");
//            if ($(slidey).children("img.active").hasClass("first")) {
//                $(slidey).children("img.active").removeClass("first");
//                id = setInterval("slideSwitch()", 500);
//            } else {
                id = setInterval("slideSwitch()", 750);
//            }

        }, function () { clearInterval(id); });
    });

    $('#trip-slideshow').cycle({
        fx: 'fade',
        timeout: '7000',
        pager: '#trip-nav',
        pagerAnchorBuilder: function (idx, slide) {
            // return selector string for existing anchor
            return '#trip-nav li:eq(' + idx + ') a';
        }
    });
});

function slideSwitch() {
    var $active = $(slidey).children('img.active');
    if ($active.length == 0) $active = $(slidey).children('img:last');

    var $next = $active.next().length ? $active.next()
                : $(slidey).children('img:first');

    $active.addClass('last-active');

    $next.css({ opacity: 0.0 })
                .addClass('active')
                .animate({ opacity: 1.0 }, 0, function () {
                    $active.removeClass('active last-active');
                });
            }



 /* TAB FUNCTIONS */

$(document).ready(function () {
    $('#tabs div.tabs_content').hide();
    $('#tabs div:first').show();
    $('#tabs ul li:first').addClass('on');

    $('#tabs ul li a').click(function () {
        $('#tabs ul li').removeClass('on');
        $(this).parent().addClass('on');
        var currentTab = $(this).attr('href');
        $('#tabs div.tabs_content').hide();
        $(currentTab).show();
        return false;
    });
});

$(document).ready(function () {
    var toggleStatus = [];
    var contents = [];
    $("#tab-wrapper").children().each(function (i) {
        contents[i] = $(this);
    });

    $(".tab-top-wrapper").children().each(function (i) {
        toggleStatus[i] = false;
        jQuery(this).click(function () {
            if (toggleStatus[i] == false) {
                for (j = 0; j < toggleStatus.length; j++) {
                    toggleStatus[j] = false;
                    $(contents[j]).hide();
                    $(".tab-top-wrapper").children().removeClass("current");
                }

                $(this).addClass("current");
                toggleStatus[i] = true;
                $(contents[i]).show();
            }
        });
    });

    var innerToggleStatus = [];
    var innerContents = [];
    $("#inner-tab-wrapper").children().each(function (i) {
        innerContents[i] = $(this);
    });

    $(".inner-tab-top-wrapper").children().each(function (i) {
        innerToggleStatus[i] = false;
        jQuery(this).click(function () {
            if (innerToggleStatus[i] == false) {
                for (j = 0; j < innerToggleStatus.length; j++) {
                    innerToggleStatus[j] = false;
                    $(innerContents[j]).hide();
                    $(".inner-tab-top-wrapper").children().removeClass("current");
                }

                $(this).addClass("current");
                innerToggleStatus[i] = true;
                $(innerContents[i]).show();
            }
        });
    });

    var accommToggleStatus = [];
    var accommContents = [];
    $("#accomm-tab-wrapper").children().each(function (i) {
        accommContents[i] = $(this);
    });

    $(".accomm-tab-top-wrapper").children().each(function (i) {
        accommToggleStatus[i] = false;
        jQuery(this).click(function () {
            if (accommToggleStatus[i] == false) {
                for (j = 0; j < accommToggleStatus.length; j++) {
                    accommToggleStatus[j] = false;
                    $(accommContents[j]).hide();
                    $(".accomm-tab-top-wrapper").children().removeClass("current");
                }

                $(this).addClass("current");
                accommToggleStatus[i] = true;
                $(accommContents[i]).show();
            }
        });
    });
});
