﻿$(document).ready(function() {

    // Load plugins
    var pluginspath = "/_scripts/jQuery/plugins/";

    $("body").append("<script type='text/javascript' src='" + pluginspath + "plugin.ajaxform.js'></script>");
    $("body").append("<script type='text/javascript' src='" + pluginspath + "plugin.easing.js'></script>");
    $("body").append("<script type='text/javascript' src='" + pluginspath + "plugin.fancybox.js'></script>");
    $("body").append("<script type='text/javascript' src='" + pluginspath + "plugin.slider.js'></script>");
    $("body").append("<script type='text/javascript' src='" + pluginspath + "plugin.suckerfish.js'></script>");
    $("body").append("<script type='text/javascript' src='" + pluginspath + "plugin.carrousel.js'></script>");
    $("body").append("<script type='text/javascript' src='" + pluginspath + "plugin.inputs.js'></script>");

    // Suckerfish
    //$(".content-menu ul").Suckerfish();

    // Fancyboxing
    $(".productbox").fancybox({
        width: 800,
        height: 400,
        hideOnContentClick: true,
        hideOnOverlayClick: true
    });

    // Search
    $("input.search").live("keypress", function(e) {
        if (e.keyCode == 13) {
            __doPostBack("ctl00$SearchBoxr$SearchButton", "");
        }
    });

    // Banner
    $(".banner").Slider({
        element: "li"
    });

    // Forms
    $(".contactform .fields").Ajaxform({
        executionpath: "/Ajax/SendContactForm.aspx"
    });

    // Usertype portal links
    $(".selector").each(function() {
        var href = $(this).find("a").attr("href");

        $(this).click(function() {
            window.location = href;
        });
    });

    // History back
    $(".button-last").click(function() {
        history.back(1);
    });

    // Introsliders
    $(".selector").each(function() {
        var parent = $(this);

        $(this).hover(function() {
            $(parent).find(".bolpijl").each(function() {
                $(this).fadeOut(300);
            });

            $(parent).find(".intro").each(function() {
                $(this).animate({
                    height: 333
                }, 600);
            });
        }, function() {
            $(parent).find(".bolpijl").each(function() {
                $(this).fadeIn(300);
            });

            $(this).find(".intro").each(function() {
                $(this).animate({
                    height: 0
                }, 600);
            });
        });
    });

    // Carrousel
    $(".scroller").each(function() {
        if ($(this).find("li").length > 4) {
            var car = $(".scroller ul").carrousel({
                auto: false,
                api: true,
                continuous: false,
                dispNumber: 4,
                speed: 600,
                easing: "easeOutBounce"
            });

            $(".next").click(function() {
                car.next();
                return false;
            });

            $(".prev").click(function() {
                car.prev();
                return false;
            });

            $(".scroller").each(function() {
                var currentpos = $(".selected").find("div").attr("rel");
                var moveto = 0;

                if (currentpos != null) {
                    car.moveTo(parseInt(currentpos));
                }
            });
        }
        else {
            $(".next").remove();
            $(".prev").remove();
        }
    });

    // Search
    $("input.search").live("keypress", function(e) {
        if (e.keyCode == 13) {
            var usertype = $("input[name='ctl00$SearchBox$UserTypeForJquery']").val();
            e.preventDefault();
            //__doPostBack('ctl00$SearchBox$SearchButton', '');            
        }
    });

    // Input handling
    $("input.search").Inputs({
        defaultvalue: "Vul hier uw zoekterm in..."
    });

    // Only numeric values
    $(".otheramount input[type='text']").live("keyup", function(e) {
        var wholevalue = $(this).val();
        var length = wholevalue.length;
        var newvalue;

        var value = wholevalue.substring(length - 1)

        if (value == "0" || value == "1" || value == "2" || value == "3" || value == "4" || value == "5" || value == "6" || value == "7" || value == "8" || value == "9") {
            newvalue = wholevalue;
        }
        else {
            newvalue = wholevalue.substring(0, length - 1);
            $(this).val(newvalue);
        }
    });

    // iPhone style passinput
    $("__.password-jquery").each(function() {
        var left = 0;
        var index = 0;
        var parent = $(this);
        var mask = "<div class='obscure'></div>";
        var masked = "<div class='masked'></div>";

        $(this).find("input").keyup(function(e) {
            if (e.keyCode == 8) {
                $(parent).find(".obscure:eq(" + (index - 1) + ")").remove();

                index--;
                left = left - 10;
            }
            else {
                $(parent).append(mask);
                $(parent).append(masked);
                $(parent).find(".masked").css("left", left + "px").append($(this).val()).delay(700).fadeOut().remove();
                $(parent).find(".obscure:eq(" + index + ")").css("left", left + "px").hide().delay(600).fadeIn(300);

                index++;
                left = left + 10;
            }
        });
    });


    // Canvassing
    $(".canvaswrapper").each(function() {
        var ctx = $(this).find(".canvas")[0].getContext("2d");

        ctx.beginPath();
        ctx.arc(75, 75, 10, 0, Math.PI * 2, true);
        ctx.closePath();
        ctx.fill();
    });

});

