$(document).ready(function() {
    /* Testing
    $("a").hover(function() { 
    $("#bgImage").hide();
    $("body").css("background-color", "#ff0000");
    }, function() {
    $("#bgImage").show();
    $("body").css("background-color", "transparent");
    });
    */



    // Enable loggin
    $(document).logger({ enabled: false });

    // Reposition werkgroepFoto contents
    var werkgroepFotoContainer = $(".werkgroepFoto");
    var werkgroepFoto = $(".werkgroepFoto .foto");
    if (werkgroepFoto.width() > werkgroepFotoContainer.width()) {
        werkgroepFoto.css("margin-left", ((werkgroepFotoContainer.width() - werkgroepFoto.width()) / 2) + "px");
        werkgroepFoto.css("margin-top", ((werkgroepFotoContainer.height() - werkgroepFoto.height()) / 2) + "px");
    }

    // Replace png with gif on werkgroepFoto
    if ($.browser.msie && $.browser.version == "6.0") {
        $(".werkgroepFoto img.overlay").attr('src', 'images/werkgroepContactContainer.gif');
    }

    // Disable sIFR for generatedContent headers
    //$(".generatedContent h1, .generatedContent h2, .generatedContent h3, .generatedContent h4, .generatedContent h5, .generatedContent h6").addClass("sIFR-ignore");


    // Breaks all scripts.. Need to look at this
    // Autocomplete
    if ($(document).autocomplete) {
        if ($("#zoekopdracht").length > 0) {


            $("#zoekopdracht").autocomplete('/scripts/localdata.aspx', {

                width: 250,
                formatItem: function(row, position, results) {
                    var resultWord = "resultaten";
                    if (results < 2)
                        resultWord = "resultaat";

                    return row[0] + '<div class="ac_custom_amountResults">' + row[1] + ' ' + resultWord + '</div><div class="clearboth"></div>';
                },
                formatResult: function(row, position, results) {
                    return row[0];
                }

            });

            // Original
            //$("#zoekopdracht").autocomplete('scripts/localdata.aspx');
        }

        if ($("#productSearch #product_name").length > 0) {

            var e = $("#productSearch #product_type").val();
            var c = $("#productSearch #product_cat").val();

            var searchAutocomplete = $("#productSearch #product_name").autocomplete('/scripts/localdata.aspx?e=' + e + '&c=' + c, {

                width: 250,
                formatItem: function(row, position, results) {
                    var resultWord = "resultaten";
                    if (results < 2)
                        resultWord = "resultaat";

                    return row[0] + '<div class="ac_custom_amountResults">' + row[1] + ' ' + resultWord + '</div><div class="clearboth"></div>';
                },
                formatResult: function(row, position, results) {
                    return row[0];
                }

            });

            $("#productSearch #product_type").change(function() {

                var e = $("#productSearch #product_type").val();
                var c = $("#productSearch #product_cat").val();

                searchAutocomplete.setOptions({
                    url: '/scripts/localdata.aspx?e=' + e + '&c=' + c
                });

            });

        }

        if ($("#productSearch").length > 0) {
            // Need to add the localdata file on every page
            //$("#defaultSearchInput").autocomplete('scripts/localdata.aspx');

            $("#defaultSearchInput").autocomplete('/scripts/localdata.aspx', {

                width: 250,
                formatItem: function(row, position, results) {
                    var resultWord = "resultaten";
                    if (results < 2)
                        resultWord = "resultaat";

                    return row[0] + '<div class="ac_custom_amountResults">' + row[1] + ' ' + resultWord + '</div><div class="clearboth"></div>';
                },
                formatResult: function(row, position, results) {
                    return row[0];
                }

            });
        }
    }

    // Make an easy ajax call
    if ($(".ajaxcall").length > 0 && $(document).ajaxcall) {
        $(".ajaxcall").ajaxcall();
    }

    // Modal overlay
    $(".modal").hide();

    $(".modal form").submit(function() {
        return false;
    });

    $(".modal_trigger").unbind("click").click(function() {

        $($(this).attr("href")).overlay({
            expose: {
                color: '#333',
                loadSpeed: 200,
                opacity: 0.9
            },

            closeOnClick: false,

            api: true
        }).load();

        return false;

    });

    if ($(".modal_autostart").length > 0) {

        var clone = $(".modal").clone().addClass("clone").appendTo("body");
        $(".modal:not(.clone)").remove();

        $(".modal").addClass("modalStyle");
        $(".modal_autostart").overlay({
            expose: {
                color: '#333',
                loadSpeed: 200,
                opacity: 0.9
            },

            closeOnClick: false,

            api: true
        }).load();
    }

    // Google maps directions
    if ($("#directionsForm").length > 0) {

        $("#routeInText").hide();

        $("#directionsForm").submit(function() {
            $("#routeInText").show();
            setDirections($("#directionsForm #route_from").val(), $("#directionsForm #route_to").val(), "nl_NL");
            return false;
        });
    }

    // Afbeeldingen box 
    if ($('#afbeeldingenbox').length > 0) {
        $("#afbeeldingenbox").imagebox();
    }

    // Tabs carousel
    if ($(".tabsCarousel").length > 0) {
        $(".tabsCarousel").tabsCarousel();
    }

    // Algemene zoekfunctie
    if ($('#defaultSearchInput').length > 0) {
        var defaultSearchText = 'kennis zoeken...';
        $('#defaultSearchInput').Watermark(defaultSearchText, '#333');

        // Check if the default value has been changed
        $('#defaultSearchSubmit').unbind("click").click(function() {
            if ($('#defaultSearchInput').val() == defaultSearchText) {
                alert('Vul eerst een zoekterm in.');
            }
            else {
                return true;
            }
            return false;
        });
    }


    // Product zoekfunctie
    if ($('#productSearch').length > 0) {
        var pageTitle = $("h1").html();
        var productSearchText = pageTitle + ' zoeken...';
        $('#productSearch #product_name').Watermark(productSearchText, '#333');

        // Check if the default value has been changed
        $('#productSearch .submitPurple').unbind("click").click(function() {
            if ($('#productSearch #product_name').val() == productSearchText) {
                alert('Vul eerst een zoekterm in.');
            }
            else {
                return true;
            }
            return false;
        });
    }

    // Tabs
    if ($(".tabs").length > 0) {
        var tabs = $('.tabs').tabs();

        // Using a link to go to another tab
        $(".tabModifier").unbind("click").click(function() {

            var href = $(this).attr("href")
            var rel = $(this).attr("rel")
            if ($("form.usesValidation").length > 0) {
                jQuery(this).log($("form.usesValidation " + rel + " :input").valid());
                if ($("form.usesValidation").valid()) {
                    tabs.tabs('select', href);
                }
            }
            else {
                tabs.tabs('select', href);
            }

            return false;
        });

        $(".tabs_link").unbind("click").click(function() {
            tabs.tabs('select', $(this).attr("href"));
            //return false;
        });

    }


    // Show hide toggler
    if ($(".showhide").length > 0) {
        $(".showhide").showhide();
    }

    // Form active field by radio button

    // Setup
    $(".radioSwitch").each(function() {

        $(this).find("input:not(:radio), select").attr("disabled", "disabled");

        var activeRadio = $(this).find(":radio:checked");

        $("#" + $(activeRadio).attr("id") + "_input").removeAttr("disabled");

    });

    // On click radiobutton
    $(".radioSwitch input:radio").unbind("click").click(function() {

        $(this).parents(".radioSwitch").find("input:not(:radio), select").attr("disabled", "disabled");

        $("#" + $(this).attr("id") + "_input").removeAttr("disabled");

    });

    // Form show hide toggler

    // Via checkbox
    $(".showHideCheckbox").each(function() {
        var id = $(this).attr("id");
        if ($(this).attr("checked") && !$(this).hasClass("inverted")) {
            $(document).find("#" + id + "_target").hide();
        }
        else if ($(this).attr("checked") == false && !$(this).hasClass("inverted")) {
            $(document).find("#" + id + "_target").show();
        }
        else if ($(this).attr("checked") && $(this).hasClass("inverted")) {
            $(document).find("#" + id + "_target").show();
        }
        else if ($(this).attr("checked") == false && $(this).hasClass("inverted")) {
            $(document).find("#" + id + "_target").hide();
        }

    });
    $(".showHideCheckbox").unbind("click").click(function() {
        var id = $(this).attr("id");
        $(document).find("#" + id + "_target").toggle();
    });

    // Via radiobutton
    $(".showHideRadio input").each(function() {
        var target = $(this).val();
        if (!$(this).attr("checked"))
            $(document).find("#" + target).hide();
    });
    $(".showHideRadio input").unbind("click").click(function() {
        $(".showHideRadio input").each(function() {
            var target = $(this).val();
            $(document).find("#" + target).hide();
        });
        var target = $(this).val();
        $(document).find("#" + target).show();
    });

    // Add/remove buttons
    $(".removeList li .textEdit, .addList li .textEdit").hide();

    $(".removeList li, .addList li").hover(function() {
        $(this).addClass("hover");
        $(this).find(".iconEdit").hide();
        $(this).find(".textEdit").show();
    }, function() {
        $(this).removeClass("hover");
        $(this).find(".iconEdit").show();
        $(this).find(".textEdit").hide();
    });

    // Multi level list 
    $(".multiLevelList li").unbind("click").hover(function() {
        $(this).addClass("hover");
    }, function() {
        $(this).removeClass("hover");
    });

    $(".multiLevelList").find(".active").each(function(i, item) {
        $(item).parents(".active").removeClass("active").show();
        $(item).siblings("li").show();
    });

    // Folding list - hoeft niet te folden
    /*
    $(".foldingList li ul").hide();
	
	$(".foldingList li").unbind("click").hover(function() {
    $(this).addClass("hover");		
    }, function() {
    $(this).removeClass("hover");		
    });

	$(".foldingList li:not(.foldingList li li)").unbind("click").click(function() {
	
		$(".foldingList li ul").stop().slideUp(100);
    $(".foldingList li").removeClass("active");

		$(this).addClass("active");
    $(this).find("ul").stop().slideDown(100);		
		
		// REMOVE THIS
    return false;
    });
    */

    // Tooltips


    $(".properTooltip img[title]").tooltip({

        // place tooltip on the right edge
        position: "center right",
        // a little tweaking of the position
        offset: [-2, 10],
        // use the built-in fadeIn/fadeOut effect
        effect: "fade",
        // custom opacity setting
        opacity: 0.7
    });

    if ($(".tooltipRight_trigger").length > 0) {
        $(".tooltipRight_trigger").tooltip({
            tip: '.tooltipRight',
            position: "top right",
            offset: [63, 5],
            onShow: function() {
                var tooltipContent = this.getTrigger().find("img").attr("title").split(" - ");

                $(".tooltipRight .content strong").html(tooltipContent[0]);
                $(".tooltipRight .content p").html(tooltipContent[1]);
            }
        });

        // Hide browser tooltip		
        $(".tooltipRight_trigger").hover(function() {
            $(this).find("img").attr("title_temp", $(this).find("img").attr("title"));
            $(this).find("img").attr("alt_temp", $(this).find("img").attr("alt"));
            $(this).find("img").attr("title", "");
            $(this).find("img").attr("alt", "");
        }, function() {
            $(this).find("img").attr("title", $(this).find("img").attr("title_temp"));
            $(this).find("img").attr("alt", $(this).find("img").attr("alt_temp"));
        });
    }





    if ($(".tooltipInschrijven_trigger").length > 0) {

        $(".tooltipInschrijven_trigger").tooltip({
            tip: '.tooltipInschrijven',
            position: 'top center',
            offset: [-5, 0]
        });

        $(".tooltipInschrijven_trigger").hover(function() {

            // Add logic to fire only when no date is selected

            var index = $(".tooltipInschrijven_trigger").index(this);
            var api = $(".tooltipInschrijven_trigger").tooltip(index);
            api.show();

            $(this).find("img").attr("title_temp", $(this).find("img").attr("title"));
            $(this).find("img").attr("alt_temp", $(this).find("img").attr("alt"));
            $(this).find("img").attr("title", "");
            $(this).find("img").attr("alt", "");

        }, function() {
            var index = $(".tooltipInschrijven_trigger").index(this);
            var api = $(".tooltipInschrijven_trigger").tooltip(index);
            api.hide();

        });
    }

    // Address form
    $(".formAddress .formAddress_street").attr("disabled", "disabled").addClass("disabled");
    $(".formAddress .formAddress_city").attr("disabled", "disabled").addClass("disabled");
    $(".formAddress .formAddress_country").change(function() {
        if ($(this).val() == "NL") {
            $(this).parents(".formAddress").find(".formAddress_street").attr("disabled", "disabled").addClass("disabled");
            $(this).parents(".formAddress").find(".formAddress_city").attr("disabled", "disabled").addClass("disabled");
        } else {
            $(this).parents(".formAddress").find(".formAddress_street").removeAttr("disabled").removeClass("disabled");
            $(this).parents(".formAddress").find(".formAddress_city").removeAttr("disabled").removeClass("disabled");
        }
    });

    // Datepicker
    $.dpText = {
        TEXT_PREV_YEAR: 'Vorig jaar',
        TEXT_PREV_MONTH: 'Vorige maand',
        TEXT_NEXT_YEAR: 'Volgend jaar',
        TEXT_NEXT_MONTH: 'Volgende maand',
        TEXT_CLOSE: 'Sluiten',
        TEXT_CHOOSE_DATE: 'Kies datum'
    };

    // Load Date Picker
    $(".datepicker").datePicker({ clickInput: true, startDate: '01/01/1990' });

    // scroll 
    $("div#info_inhoud a[href$='#info_locatie']").click(function() {
        //also possible: window.scroll(0,0)
        window.setTimeout(function() {
            $('div#contentTop h1')[0].scrollIntoView(true);
        }, 250);
    });

    // Courses next tab
    //	$("#info_inhoud .availabilityOK").click(function() { 
    //		tabs.tabs('select', "info_locatie");
    //		var dates = $("#info_locatie li img[src='/images/availabilityOK.png']").parents("li");
    //		if(dates.length > 0) { 
    //			$(dates[0]).find(".hiddenContent").show(); 
    //			$(dates[0]).find(".toggleNextNode").addClass("active"); 
    //		}
    //});

    $(".firstOccurenceLink").click(function() {
        tabs.tabs('select', "info_locatie");
        $($(this).attr("href")).find(".hiddenContent").show();
        $($(this).attr("href")).find(".toggleNextNode").addClass("active");
        return false;
    });
    if ($('.tabbedBlock.selected').length > 0) {
        $('.tabbedBlock.unselected').addClass('ui-tabs-hide');
        $('.tabbedBlock.selected').removeClass('ui-tabs-hide');
        var ttselectedTabId = $('.tabbedBlock.selected').attr('id');
        if (ttselectedTabId != null) {
            $('.tabs').children().removeClass('ui-tabs-selected');
            $('#tab' + ttselectedTabId).addClass('ui-tabs-selected');
        }
    }
});

//Dummy
function doSifr() { }

function initDatePicker() {
    $(".datePicker2").datepicker();
}




