$(document).ready(function(){
    positionFooter();
    
    if($('#recent').height()){
        $('#recent').height(Math.max($('#recent').height(), $('#highest').height()));
        $('#recent').height(Math.max($('#recent').height(), $('#latest').height()));
        $('#highest').height($('#recent').height());
        $('#latest').height($('#recent').height());
    }
    
    if($('#slideshow1').offset()){
        $('#slideshow1').cycle();
        $('#slideshow2').cycle();
    }
});

$(window).load(function(){
    if($('#slideshow1').offset()){
        $('#slideshow1').cycle({
            before: onBefore
        });
        $('#slideshow2').cycle({
            before: onBefore
        });
    }
});

function onBefore(curr,next,opts) {
    var $slide = $(next);
    var w = $slide.outerWidth();
    var h = $slide.outerHeight();
    $slide.css({
        marginTop: ($('#slide').height() - h) / 2,
        marginLeft: ($('#slide').width() - w) / 2
    });
};


function positionFooter(){
    $('#right').height(Math.max($('#left').height(), Math.max($('#right').height(), $('#content').height())));
    contentpos = $('#left').offset();
    $('#footer').css('top', ($('#right').height() + contentpos.top + 10) + 'px');
    //$('#footer').css('left', contentpos.left + 'px');
    $('#footer').css('display', 'block');
}

function newCaptcha(){
    if($('#captchacode').html()){
        $.get(basePath + 'ajax/captcha.php', function(data){
            $('#captchacode').html(data);
        }, 'html');
    }
    else
        alert('Deze pagina bevat geen code.');
}

function showDialog(title, message){
    $('#dialogtitle').html(title);
    $('#dialogmessage').html(message);
    $('#dialog').css('left', ($('#companylist').width() - $('#dialog').width()) / 2);
    $('#dialog').css('top', ($(window).scrollTop() + 100));
    $('#dialog').css('display', 'block');
}

function validateForm(id){
    $('#' + id).validate({
        errorClass: 'error',
        errorPlacement: function(error, element){
            //
        },
        highlight: function(element, errorClass){
            $('#' + element.name).addClass(errorClass);
        },
        invalidHandler: function(form, validator){
            var errors = validator.numberOfInvalids();
            if(errors){
                $('#error').show();
            }
            else
                $('#error').hide();
        },
        rules: {
            code: {
                required: true,
                equalTo: '#code1'
            }
        },
        submitHandler: function(form){
            if($('#' + id).children("input[name='subscribe']").val() == 1)
                var url = basePath + 'ajax/subscribe.php';
            else if($('#' + id).children("input[name='type']").val() == 'tellafriend')
                var url = basePath + 'ajax/tellafriend.php';
            else if($('#' + id).children("input[name='type']").val() == 'review')
                var url = basePath + 'ajax/review.php';
            else
                var url = basePath + 'ajax/formsubmit.php';
            
            $('#' + id).ajaxSubmit({
                dataType: 'json',
                success: function(response, statusText){
                    showDialog(response.title, response.message);
                },
                type: 'post',
                url: url
            });
        },
        unhighlight: function(element, errorClass){
            $('#' + element.name).removeClass(errorClass);
        }
    });
}

function updateSelection(type, id){
    $.get(basePath + 'ajax/myselection.php', {type: type, id: id}, function(data){
        showDialog(data.title, data.message);
        if(data.hide){
            $('#vermelding' + data.hide).hide();
        }
        $('#aantalinselectie').html('<strong>' + data.selectioncount + '</strong>');
        if($('#aantalinselectie1').offset()){
            $('#aantalinselectie1').html('<strong>' + data.selectioncount + '</strong>');
        }
    }, 'json');
}

function addFriend(){
    var d = new Date();
    var id = d.getTime();
    $('#addfriend').before('<tr class="' + id + '"><td>Naam vriend</td><td colspan="2"><input type="text" class="text" name="vriendnaam[]" value="" style="width: 100%" /></td></tr><tr class="' + id + '"><td>E-mailadres vriend<br /> <a href="#" onclick="removeClass(\'' + id + '\'); return false;">verwijderen</a></td><td colspan="2"><input type="text" class="text" name="vriendemail[]" value="" style="width: 100%" /></td></tr>');
    positionFooter();
}

function addPhoto(){
    var d = new Date();
    var id = d.getTime();
    $('#addphoto').before('<tr id="' + id + '"><td colspan="4"><input type="file" class="text" name="foto[]" value="" style="height: 25px;" /> <a href="#" onclick="removeElement(\'' + id + '\'); return false;">verwijderen</a></td></tr>');
    positionFooter();
    updateTotal();
}

function removeElement(id){
    $('#' + id).remove();
    positionFooter();
    updateTotal();
}

function removeClass(id){
    $('.' + id).remove();
    positionFooter();
    updateTotal();
}

function addKeyword(){
    var d = new Date();
    var id = d.getTime();
    $('#addkeyword').before('<tr id="' + id + '"><td colspan="4"><input type="text" class="text" name="trefwoord[]" value="" style="width: 18%;" />&nbsp;<input type="text" class="text" name="trefwoord[]" value="" style="width: 18%;" />&nbsp;<input type="text" class="text" name="trefwoord[]" value="" style="width: 18%;" />&nbsp;<input type="text" class="text" name="trefwoord[]" value="" style="width: 18%;" />&nbsp;<input type="text" class="text" name="trefwoord[]" value="" style="width: 18%;" /><br /><a href="#" onclick="removeElement(\'' + id + '\'); return false;">verwijderen</a></td></tr>');
    positionFooter();
    updateTotal();
}

function addCategory(){
    var d = new Date();
    var id = d.getTime();
    $('#addcategory').before('<tr id="' + id + '"><td>Extra rubriek</td><td colspan="3"><select name="rubriekid[]"><option value=""></option>' + categoryList + '</select> <a href="#" onclick="removeElement(\'' + id + '\'); return false;">verwijderen</a></td></tr>');
    positionFooter();
    updateTotal();
}

function updateTotal(){
    var form = '#infoform';
    var total = 0;
    var countKeyword = 0;
    var countCategory = 0;
    
    if($('#type').val() == 1){
        total += 40;
    }
        
    $("input[name^='trefwoord'][name!='trefwoord']").each(function(){
        countKeyword += 1;
    });
    total += 10 * (Math.ceil(countKeyword / 5) - 1);
    
    var i = 1;
    $("select[name^='rubriekid']").each(function(){
        if(i == 2){
            if($(this).children('option:selected').val() != ''){
                if($('#type').val() == 0){
                    countCategory += 1;
                }
            }
        }
        else
            countCategory += 1;
            
        i++;
    });
    total += 10 * (countCategory - 1);
        
    $(form).children("input[name='total']").val(total);
    $('#totaalbedrag').html(total);
    
    if(total == 0){
        $('#akkoord').html('Ik ga akkoord met vermelding van bovenstaande gegevens op Feestaanbieders.nl.');
        $('#isfactuuradres').hide();
        $('#factuur').hide();
        $('#factuur1').hide();
    }
    else{
        $('#akkoord').html('Ik ga akkoord met vermelding van bovenstaande gegevens op Feestaanbieders.nl en ontvang daarvoor een factuur met bovenstaand bedrag.');
        $('#isfactuuradres').show();
        toggleInvoice($("input[name='isfactuuradres']"));
    }
}

function toggleInvoice(element){
    if(element.is(':checked')){
        $('#factuur').hide();
        $('#factuur1').hide();
    }
    else{
        $('#factuur').show();
        $('#factuur1').show();
    }
    
    positionFooter();
}
