﻿$(document).ready(function () {

    var cookieTrips = getCookie("bewaarde-reizen");
    var cookieToken = getCookie("Token");

    if (cookieTrips != undefined && cookieTrips != "") {
        var tripCount = cookieTrips.split('},{');
        $("#top_buttons_saved_trips span").text(tripCount.length);
    }

    if (cookieToken == undefined || cookieToken == "") {
        $("#top_buttons_saved_booking").hide();
    }
   
    /////////////
    // Pop-ups //
    /////////////

    $("#call_me_back, .call_me_back_big").colorbox({ iframe: true, titleHtml: "<h1>Bel mij terug</h1>", innerWidth: 600, innerHeight: 340, opacity: 0.7, close: "", speed: 650, scrolling: false, href: "/Pages/Content/Popup/CallBack/Default.aspx" });
    $(".video_link, #video_click_area div").colorbox({ iframe: true, titleHtml: "<h1>Het nieuwe Peter Langhout Reizen</h1>", innerWidth: 480, innerHeight: 390, opacity: 0.7, close: "", speed: 650, scrolling: false, href: "/Pages/Content/Popup/Video/Default.aspx" });


    $(".popup_destination, .btn_map").click(function () {

        var name = "Informatie " + $(this).text();
        var href = $(this).attr("href");
        var scrolling = true;

        if (!$(this).hasClass("btn_map")) {

            if ($(this).hasClass("catrelated")) {
                var category = href.substring(href.search('#') + 1, href.replace("#", "").search('#') + 1);
                href = href.replace("#" + category + "#", "");
            }

            href = href + "popup/?category=" + category;

            if ($(this).hasClass("acco_pics"))
                name = $(this).attr("title");

            if ($(this).hasClass("icon_plus")) {
                href = $(this).next().attr("href") + "popup/";
                name = $(this).next().attr("title");
            }
        }
        else {
            name = $(this).attr("title");
            scrolling = false;
        }

        $(this).colorbox({
            open: true,
            titleHtml: "<h1>" + name + "</h1>",
            iframe: true,
            innerWidth: 750,
            innerHeight: 500,
            opacity: 0.7,
            close: "",
            speed: 650,
            scrolling: scrolling,
            href: href
        });

        return false;
    });

    $('#sidebar_lastminutes a').colorbox({
        iframe: true,
        titleHtml: "<h1>Lastminutes ontvangen</h1>",
        iframe: true,
        innerWidth: 620,
        innerHeight: 450,
        opacity: 0.7,
        close: "",
        speed: 650,
        href: "/Pages/Travel/LastMinutes/Popup.aspx"
    });

    $('.icon_info').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - ",
        fade: 0
    });

    ////////////////////
    // href helpers   //
    ////////////////////

    $("#callus").click(function () {
        window.location = "/contact/contactgegevens/"
    });

    $("#header_bottom .menu_btn_content").click(function () {
        if ($("a", this).attr("href") != undefined) {
            window.location = $("a", this).attr("href");
        }
    });

    $("#show_results").click(function () {
        window.location = "/zoeken/"
    });

    $(" #sidebar_newsletter").click(function () {
        window.location = '/nieuwsbrief/';
    });

    $(" #sidebar_brochure").click(function () {
        window.location = '/brochure-aanvragen/';
    });

    $(".category").click(function () {
        window.location = $("a", this).attr("href");
    });

    $(".offers .offer").click(function () {
        window.location = $("a", this).attr("href");
    });

    $("#header_bottom .menu_btn_content").hover(
        function () {
            $("a", this).addClass("red");
        },
        function () {
            $("a", this).removeClass("red");
        }
    );


    /////////////
    // Forms   //
    /////////////

    $(".clickcheck").click(function () {

    });


    ////////////////
    // Searchbar //
    ///////////////

    var description = "Type hier uw zoekterm of reiscode...";

    $("[name='search']").focus(function () {
        if (description == $(this).val()) {
            $(this).val('');
        }
    });

    $("[name='search']").blur(function () {
        if ($(this).val() == '') {
            $(this).val(description);
        }
    });

    $(".btn_search").click(function () {
        doQuerySearch();
    });

    $('#q').keyup(function (e) {
        if (e.keyCode == 13) {
            doQuerySearch();
        }
    });

    function doQuerySearch() {
        if ($("#q").val() == description)
            window.location = "/zoeken/";
        else
            window.location = "/zoeken/?q=" + $("#q").val();
    }

    if ($("#hfQuery").val() != "" && $("#hfQuery").val() != undefined)
        $("#q").val($("#hfQuery").val());


    /////////////////////
    // Service overlay //
    /////////////////////

    if (location.hash == "#faq")
        toggleService($("<a href='/klantenservice/vraag-en-antwoord/algemeen/'>"));

    $("#top_buttons_service, .close_overlay, .bottom_buttons_service, .footer_button_service, .anvr-link").click(function () {
        toggleService($(this));
        return false;
    });


    ////////////////////////////////
    // Press                      //
    ////////////////////////////////
    $(".press_archive").click(function () {

        if ($("#press_archives").is(":visible")) {

            $("#press_archives").fadeOut();
        }
        else {
            $("#press_archives_loading").fadeIn(function () {
                $("#press_archives").delay(600).fadeIn();
                $("#press_archives_loading").delay(400).fadeOut();
            });
        }

        return false;
    });

    ////////////////////////////////
    // Category                   //
    ////////////////////////////////
    var description2 = "vul uw bedrag in";

    $("[name='surprise']").focus(function () {
        if (description2 == $(this).val()) {
            $(this).val('');
        }
    });

    $("[name='surprise']").blur(function () {
        if ($(this).val() == '') {
            $(this).val(description2);
        }
    });

    ////////////////////////////////
    // Info tabs                  //
    ////////////////////////////////
    if (window.location.hash == "#/informatie/") {
        $(".tab1_content").hide();
        $(".tab2_content").show();
        $(".tab1").removeClass("active");
        $(".tab2").addClass("active");
        $(".divSearch, .item_header").hide();
    }

    $(".tab2, .read_more_destination").click(function () {
        $(".tab1_content, .divSearch, .item_header").hide();
        $(".tab2_content").fadeIn(500);
        $(".tab1").removeClass("active");
        $(".tab2").addClass("active");
        window.location.hash = "/informatie/";
        return false;
    });

    $(".tab1").click(function () {
        $(".tab2_content").hide();
        $(".tab1_content, .divSearch, .item_header").fadeIn(500);
        $(".tab2").removeClass("active");
        $(".tab1").addClass("active");
        window.location.hash = "/";
        return false;
    });


    ////////////////////////////////
    // jGrowl defaults            //
    ////////////////////////////////
    $.jGrowl.defaults.position = 'center';
});




////////////////////////////////
// Service overlay            //
////////////////////////////////
function toggleService(objectSender) {

    scrollToID('popupcb');

    var url = "/klantenservice/reisinformatie/algemeen/";

    if (objectSender != undefined) {
        if (objectSender.attr("href") != '' && objectSender.attr("href") != undefined) {
            url = objectSender.attr("href");
        }        
    }        

    if ($("#service_wrap").is(':visible')) {
        $("#olDimmer").fadeTo(800, 0, function () { $(this).hide(); $("#olLoadedContent").empty(); });        
    }
    else {               
        $("#olDimmer").fadeTo(600, 0.7);
        $("#olLoadingOverlay").show();
        $("#olLoadedContent").html("<iframe scrolling=\"no\" frameborder=\"0\" src=\"" + url + "\" name=\"olFrame\" width=\"100%\" height=\"100%\"></iframe>");               
    }

    $("#service_wrap").slideToggle(750, function () {

        if ($(this).is(':visible')) {
            $(".top_buttons_service_overlay").fadeIn(400);
            $("#olLoadingOverlay").fadeOut(500, function () {

            });
        }        
        $(".top_buttons_service_overlay").click(function () {
            $(this).slideUp(400);
            $(this).hide();
        });

    });

}

function scrollToID(id) {
    document.getElementById(id).scrollIntoView(true);   
}


////////////////////////////////
// Pop-ups called from frames //
////////////////////////////////
function popup_callback() {
    $("#popupcb").colorbox({ iframe: true, titleHtml: "<h1>Bel mij terug</h1>", innerWidth: 600, innerHeight: 340, opacity: 0.7, close: "", speed: 650, scrolling: false, href: "/Pages/Content/Popup/CallBack/Default.aspx", open: true });
}
function popup_lastminutes() {    
    $('#popupcb').colorbox({
        iframe: true,
        titleHtml: "<h1>Lastminutes ontvangen</h1>",
        iframe: true,
        innerWidth: 620,
        innerHeight: 450,
        opacity: 0.7,
        close: "",
        speed: 650,
        href: "/Pages/Travel/LastMinutes/Popup.aspx",
        open: true 
    });    
}


function addClickFunction(id) {
    
    {
        var b = document.getElementById(id);
        if (b && typeof (b.click) == 'undefined') b.click = function () {
            
            {
                var result = true; if (b.onclick) result = b.onclick();
                if (typeof (result) == 'undefined' || result) { { eval(b.getAttribute('href')); } }
            } 
        } 
    }
}

function trim(str) {
    return str.replace(/^\s+|\s+$/g, "");
}

function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}

function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString() + "; path=/");
    document.cookie = c_name + "=" + c_value;
}

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

(function ($) {
    $.fn.filterContent = function() {
        $('#landsuggest .sbdd_title, #plaatssuggest .sbdd_sub').each(function() {
            var element = $(this);
            var text = element.text();
            
            text = text.replace(/(__c)?,\d+/g, '');
            element.text(text);
        });
    };
})(jQuery);
