$(function() {
    // FIX IMG CLICK WITHIN A
    $(".liveBoardOverlay").click(function() {
        var a = $(this).parent().find(".liveBoardImg a img").closest("a");
        if (a.attr("href") != "")
            window.open(a.attr("href"));

    });
    //$(".zoom a").lightBox();
    
    $('.nav ul').dropDownMenu2({ linkClass: "a, span.drop", timer: 300, parentMO: 'parent-hover', childMO: 'child-hover1' });
    
    // overzicht keuze dropdown
    $('.dropdown ul').dropDownMenu({ linkClass: "span", timer: 300, parentMO: 'parent-hover', childMO: 'child-hover1' });
    $('.dropdown li ul li').click(function() {
            $(this).parent().parent().parent().find("span").html($(this).html());
        }
    );

    // Photo gallery pirobox
    $().piroBox({
        my_speed: 600,
        bg_alpha: 0.5,
        radius: 4,
        scrollImage: true,
        pirobox_next: 'piro_next',
        pirobox_prev: 'piro_prev',
        close_all: '.piro_close',
        slideShow: 'slideshow',
        slideSpeed: 4
    });
});

$(document).ready(function() {
    $("#aspnetForm").validate({
                        errorPlacement: function(error, element) {
                            error.appendTo(element.parent("div").next("div"));
                        }, 
                        rules:
                        {                                
                            jclc_password_check: { equalTo: "#jclc_password" }
                        }
    });
});

function doCountDown(date, start, el)
{
    $(el).countdown({ 
        until: date,
        serverSync: function(){return start;},
        labels: ['J', 'M', 'W', 'D', 'u', 'm', 's'],
        labels1: ['J', 'M', 'W', 'D', 'u', 'm', 's'],
        format: 'HMS',
        layout: '{hn} {hl} {mn} {ml} {sn} {sl}',
        onExpiry: function() { setTimeout('$("#aspnetForm").submit();', 1000);}
    });
}

function showDialog()
{
	$("<div />")	.addClass("dialog-shadow")
					.css({
							   	position	: 'absolute', 
								width 		: '100%', 
								left		: 0,
								top			: 0,
								height 		: $("body").innerHeight(),
								background	: '#000000',
								'z-index'		: 200,
								opacity : 0.5
								})
					.appendTo("body");

	$d = $(".dialog");
									
	$d.data("html", $d.html());
	
	$r = $("<div />").addClass("dialog-row").appendTo($d);
	
	// Button bevestig
	$("<span />").addClass("dialog-confirm").appendTo($r);
	
	// Button cancel
	$("<span />").addClass("dialog-cancel").appendTo($r);
	
	// Close dialog
	$("<div />").addClass("dialog-close").appendTo($d).click(closeDialog);
	
	$dt = $("<div />").addClass("dialog-top");
	$db = $("<div />").addClass("dialog-bottom");
	$d.wrap($db).wrap($dt);
	
	$d.show();
}
function closeDialog(){
	$html = $(".dialog").data("html");
	$classes = $(".dialog").attr("class"); // IVM type
	$(".dialog-bottom").remove();
	$("<div />").attr("class", $classes).html($html).appendTo($("body")).hide();
	$(".dialog-shadow").remove();
	
	// Controleren of hij officieel andere HTML had.
	// Zo ja terug plaatsen
	if($(".originalHTML").length > 0)
	{
	    $(".dialog").html($(".originalHTML").html()).attr("class", $(".originalHTML").attr("classes"));
	}
}

/**
*	Dialog buttons afhandelen
**/
$(function(){
	// BOD PLAATSEN CLICK
    $(".bod .dialog-confirm").live("click", function() {
                                                    $("#confirm").val("conf");
	                                                $('#aspnetForm').submit();
												});
	$(".bod .dialog-cancel").live('click', closeDialog);

	// VOORWAARDEN CLICK
	$(".voorwaarden .dialog-confirm").live("click", function() {
	                                                $("#voorwaarden").attr('checked', true);
	                                                closeDialog();
	});
	// VOORWAARDEN CLEAR
	$(".voorwaarden .dialog-cancel").live('click', function() {
	                                                $("#voorwaarden").attr('checked', false);
	                                                closeDialog();
	});
	
	// TELL A FRIEND CLICK
	$(".tellafriend .dialog-confirm").live("click", function(){
	                                                            if ($('#aspnetForm').valid()) {
	                                                                    $('#aspnetForm').submit();                                                        	    
	                                                            }
															 });
	
	// TELL A FRIEND CLEAR
	$(".tellafriend .dialog-cancel").live('click', function(){
	                                                            document.aspnetForm.reset();
															});
});

function placeBod() {
    // De originele HTML tijdelijk opslaan.
    // Deze wordt terug geplaatst als de dialog wordt gesloten
    if($(".originalHTML").length == 0)
        $("<div />").addClass("originalHTML").hide().html($(".dialog").html()).data("classes", $(".dialog").attr("class")).appendTo($("body"));
    
    // Start placeBod()
    $laatstBod = parseFloat($("#laatsteBod").val());
    $prijs = parseFloat($("#prijs").val());
    if ($prijs < $laatstBod) {
        alert("Uw bod is te laag");
        return;
    }

    $html = $(".origial").html();
    $html = $html.replace(/{prijs}/g, $prijs);
    $(".dialog").attr("class", "dialog bod");
    $(".dialog").html($html);
    showDialog();
}

function tellAFriend() {
    showDialog();
}

function showVoorwaarden() {
    showDialog();
}

function validNumber(id) {
    $("#" + id).val($("#" + id).val().replace(/[^0-9]/g, ""));
}

function submitForm() {
    if ($('#aspnetForm').valid()) {
        //var rekening = $("#jclc_accountnumber").val()
        //if (elf_proef(rekening)) {
            $('#aspnetForm').submit();
        //}
        //else {
        //    $("#jclc_accountnumber").parent("div").next("div").html($("#jclc_accountnumber").attr("title"));
        //}
    }
}

function submitLogin(redirect) {
    if (redirect == 'ja') {
        document.aspnetForm.action = "/";
    }
    document.aspnetForm.submit();
}

function uitloggen() {
    $("#mode").val("uitloggen");
    $('#aspnetForm').submit();
}

function passwordLost() {
    if ($("#jclc_emailfirst_ombouw").val() != "") {
        $("#mode").val("lostpassword");
        $('#aspnetForm').submit();
    }
    else {
        alert("vul uw email adres in \n klik daarna op wachtwoord vergeten \n uw wachtwoord wordt dan toegezonden per email");
    }
}

function postItem(id, value) {
    $("#" + id).val(value);
    $('#aspnetForm').submit();
}

function elf_proef(bankrekeningnummer) {
    // verwijder alle tekens die geen cijfers zijn
    bankrekeningnummer = bankrekeningnummer.replace(/\D/, "");
    aantal_tekens = bankrekeningnummer.length;
    var som = 0;
    // loop door de 9 cijfers met de 11 proef formule
    for (i = 1; i < 10; i++) {
        getal = bankrekeningnummer.charAt(i - 1);
        som += getal * (10 - i);
    }
    // geef resultaat van check terug
    if (som % 11 == 0 && aantal_tekens == 9) {
        return true
    } else {
        if (aantal_tekens < 9) {
            return true 
        }
        else {
            return false
        }
    }
}

