var hr_accordion_hor = {
    settings: { min_width: 1, max_width: 328, duration: 400 },
    build: function(accordionid) {
        var headers = $("#" + accordionid + " .acc_header");
        var tabs = $("#" + accordionid + " .tab");
        headers.each(function() {
            $(this).hover(function() {
                var tab = $(this).next();
                tabs.not(tab).animate({ width: hr_accordion_hor.settings.min_width + "px" }, { queue: false, duration: hr_accordion_hor.settings.duration });
                tab.animate({ width: hr_accordion_hor.settings.max_width + "px" }, { queue: false, duration: hr_accordion_hor.settings.duration });
            });
        });
    }
}

var hr_image_expander = {
    settings: { min_width: 160, min_height: 168, max_width: 170, max_height: 179, duration: 200 },
    bind: function(expanderclass) {
        var images = $("img." + expanderclass);
        images.each(function() {
            $(this).hover(function() {
                $(this).stop();
                $(this).animate({ width: hr_image_expander.settings.max_width, height: hr_image_expander.settings.max_height }, hr_image_expander.settings.duration, "linear");
            }, function() {
                $(this).stop();
                $(this).animate({ width: hr_image_expander.settings.min_width, height: hr_image_expander.settings.min_height }, hr_image_expander.settings.duration, "linear");
            });
        });
    }
}

var hr_menu_hor = {
    settings: { color_in: "#f0f0f6", color_out: "#ffffff" },
    build: function(menuid) {
        var mainmenu = $("#" + menuid + ">ul");
        var headers = mainmenu.find("ul").parent();
        headers.each(function() {
            var header = $(this);
            header.hover(function() {
                var target = header.children("ul:eq(0)");
                target.css("visibility", "visible").fadeIn(100);
                header.css("background-color", hr_menu.settings.color_in);
            }, function() {
                header.children("ul:eq(0)").fadeOut(100);
                header.css("background-color", hr_menu.settings.color_out);
            });
        });
    }
}

var hr_scroll = {
    scroll: function(scrollreference, scrollitem) {
        $(window).scroll(function() {
            var reference = $("#" + scrollreference);
            var item = $("#" + scrollitem);
            var scroll = $(this).scrollTop();
            var min = reference.offset().top;
            var max = (reference.height() + min) - item.height();
            if (scroll >= min && scroll <= max) {
                item.offset({ top: scroll });
            }
            else if (scroll < min) {
                item.offset({ top: min });
            }
            else if (scroll > max) {
                item.offset({ top: max });
            }
        });
    }
}

var hr_social = {
    print: function(printclass) {
        $("." + printclass).click(function(e) {
            e.preventDefault();
            window.print();
        });
    },
    bookmark: function(bookmarkclass) {
        $("." + bookmarkclass).click(function(e) {
            e.preventDefault();
            var title = $(this).attr("title");
            var url = $(this).attr("href");
            if (window.sidebar) { //mozilla firefox
                window.sidebar.addPanel(title, url, "");
            }
            else if (window.opera && window.print) { //opera
                // rel="sidebar"
                return true;
            }
            else if (window.ActiveXObject) {
                window.external.AddFavorite(url, title);
            }
            else {
                alert("Toets \'Ctrl + D\' (\'Command + D\' voor macs) om deze pagina aan uw bladwijzers toe te voegen. Klik eerst op OK.");
            }
        });

    }
}

var hr_email = {
    send: function(emailformid) {
        var form = $("#" + emailformid);
        var submit = $("#" + emailformid + "_submit");
        var status = $("#" + emailformid + " .form_status");
        var success = $("#" + emailformid + "_success");
        submit.click(function() {
            var error = false;
            var email = $("#" + emailformid + "_email").val();
            var body = $("#" + emailformid + "_body").val();
            var name = $("#" + emailformid + "_name").val();
            var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
            var message = "";
            if (name == "") {
                error = true;
                message += "- Vul eerst uw naam in.\n";
            }
            else if (email == "") {
                error = true;
                message += "- Vul eerst uw e-mailadres in.\n";
            }
            else if (!emailReg.test(email)) {
                error = true;
                message += "- Vul een geldig e-mailadres in.\n";
            }
            else if (body == "") {
                error = true;
                message += "- Vul eerst de te stellen vraag in.\n";
            }
            if (error == false) {
                $(this).hide();
                status.append("Even geduld...");
                $.post('ajax/contact.aspx', { body: body, email: email, name: name }, function(data) {
			form.slideUp("slow", function() {
				success.show(); 
			});
			window.location = 'http://www.hollandridderkerk.nl/contact-bedankt';
                });
            }
            else {
                alert(message);
            }
            return false;
        });
    }
}

var hr_offerte = {
    send: function(offerteformid) {
        var form = $("#" + offerteformid);
        var step1 = $("#" + offerteformid + "_1");
        var step2 = $("#" + offerteformid + "_2");
        var status = $("#" + offerteformid + " .form_status");
        var next = $("#" + offerteformid + "_next");
        var prev = $("#" + offerteformid + "_prev");
        var submit = $("#" + offerteformid + "_submit");
        var success = $("#" + offerteformid + "_success");
        step2.css("display", "none");
        next.click(function() {
            var error = false;
            var email = $("#" + offerteformid + "_email").val();
            var phone = $("#" + offerteformid + "_phone").val();
            var name = $("#" + offerteformid + "_name").val();
            var company = $("#" + offerteformid + "_company").val();
            var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
            var message = "";
            if (name == "") {
                error = true;
                message += "- Vul eerst uw naam in.\n";
            }
            else if (email == "") {
                error = true;
                message += "- Vul eerst uw e-mailadres in.\n";
            }
            else if (!emailReg.test(email)) {
                error = true;
                message += "- Vul een geldig e-mailadres in.\n";
            }
            else if (company == "") {
                error = true;
                message += "- Vul eerst uw bedrijfsnaam in.\n";
            }
            else if (phone == "") {
                error = true;
                message += "- Vul eerst uw telefoonnummer in.\n";
            }
            if (error == false) {
                step1.slideUp("slow", function() {
                    step2.slideDown("slow");
                });
            }
            else {
                alert(message);
            }
            return false;
        });
        prev.click(function() {
            step2.slideUp("slow", function() {
                step1.slideDown("slow");
            });
            return false;
        });
        submit.click(function() {
            var email = $("#" + offerteformid + "_email").val();
            var phone = $("#" + offerteformid + "_phone").val();
            var name = $("#" + offerteformid + "_name").val();
            var company = $("#" + offerteformid + "_company").val();
            var body = $("#" + offerteformid + "_body").val();
            var interest = $("#" + offerteformid + "_interest").val();
            var found = $("#" + offerteformid + "_found").val();
            var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
            $(this).hide();
            status.append("Even geduld...");
            $.post("ajax/email_offerte.aspx", { opmerking: body, email: email, phone: phone, name: name, company: company, interest: interest, found: found }, function(data) {
                //form.slideUp("slow", function() {
                //    success.show();
                //});
		window.location = 'http://www.hollandridderkerk.nl/offerte-bedankt';
            });
            return false;
        });
    }
}


hr_scroll.scroll("scrollreference", "scrollitem");
jQuery(document).ready(function($) {
    hr_accordion_hor.build("hr_accordion");
    hr_image_expander.bind("expander");
    hr_menu_hor.build("hr_menu");
    hr_social.print("social_print");
    hr_social.bookmark("social_bookmark");
    hr_email.send("email_form");
    hr_offerte.send("offerte_form");
});