﻿function initSnelMenu() {

    $('#snelmenu input').toggle();
    $('#snelmenu select').change(function() {
        $('#snelmenu form').submit();
    });

}

function initProductOverzicht() {

    $('#productfiltersc').change(
        function() {
			$('#productfilterzoek').hide();
            var intSCId = $('#productfiltersc').val();
            var intWebsiteId = $('#zoekWebsiteId').val();
            var intProductType = $('#zoekProductType').val();
            GetProductSoorten(intWebsiteId, strTaal, intSCId, intProductType);
            GetProductMerken(intWebsiteId, strTaal, intSCId, intProductType);
        }
    );
}

function initProductDetailImageGallery() {

    if ($('ul.productgallery li').length > 1) {
        var firstImageSrc = $('ul.productgallery li:first img').attr('src');
        jQuery(
            function($) {
                $('ul.productgallery').galleria({history: false});
            }
        );
        jQuery.historyCallback(firstImageSrc);
    }
    else if ($('ul.productgallery li').length = 1) {
        var strImageTag = $('ul.productgallery li').html();
        $('ul.productgallery').replaceWith('<div class="galleria_container"><div class="galleria_wrapper">' + strImageTag + '</div></div>')
    }
}

function GetProductSoorten(intWebsiteId, strTaal, intSiteCatId, intProductType) {
    $('#productfilterps').html('<option value="-1">...     </option>');
    $.get(
        '/ajax/productsoortenbysitcatid.aspx',
        {
        	w: intWebsiteId,
        	taal: strTaal,
        	sc: intSiteCatId,
        	type: intProductType
        },
        function(data) {
        	if (data.indexOf('-1') > 0) {
        		$('#productfilterzoek').hide();
        	}
        	else {
        		if ($('#productfilterpm').length == 0) $('#productfilterzoek').show();
        	}
        	$('#productfilterps').html(data);
        }
    );
}

function GetProductMerken(intWebsiteId, strTaal, intSiteCatId, intProductType) {
    if ($('#productfilterpm').length > 0) {
            $('#productfilterpm').html('<option value="-1">...     </option>');
            $.get(
            '/ajax/productmerkenbysitcatid.aspx',
            {
                w: intWebsiteId,
                taal: strTaal,
                sc: intSiteCatId,
                type: intProductType
            },
            function(data) {
                if (data.indexOf('-1') > 0) {
                    $('#productfilterzoek').hide();
                }
                else {
                    $('#productfilterzoek').show();
                }
                $('#productfilterpm').html(data);
            }
        );
    }
}

function initBackButtons() {
    $('.back').click(function() { history.go(-1); });
}

function initPrintButtons() {
    $('.print').click(function() { window.print(); return false; });
}

function initFormValidation() {

    var strErrorMessageVerplicht = 'verplicht veld'
    var strErrorMessageEmail = 'geen geldig e-mailadres'

    var validator = $('#frmContact').validate({
    messages: {
        Naam: { required: strErrorMessageVerplicht },
        Bedrijfsnaam: { required: strErrorMessageVerplicht },
        Telefoonnummer: { required: strErrorMessageVerplicht },
        Emailadres: { required: strErrorMessageVerplicht, email: strErrorMessageEmail },
        VraagOpmerking: { required: strErrorMessageVerplicht }
        } 
    });
    $('#frmTellAFriend').validate({
        messages: {
            txtUwNaam: { required: strErrorMessageVerplicht },
            txtUwEmailadres: { required: strErrorMessageVerplicht, email: strErrorMessageEmail },
            txtKennisNaam: { required: strErrorMessageVerplicht },
            txtKennisEmailadres: { required: strErrorMessageVerplicht, email: strErrorMessageEmail }
        }
    });
}

function showContentFlash(pId, pPath, pVersion, pWidth, pHeight) {

    var flashvars = {};
    var params = {};
    params.wmode = "transparent";

    swfobject.embedSWF(pPath, pId, pWidth, pHeight, pVersion, "expressInstall.swf", flashvars, params);

}

function initSearch() {

    var strAllesSelecteren = 'alles selecteren';
    var strAllesDeselecteren = 'alles deselecteren';

    if (strTaal == 'de') {
    	strAllesSelecteren = 'alle auswählen';
    	strAllesDeselecteren = 'auswahl aufheben';
    }

    $('.searchBtn').click(function() {
        if ($(this).attr('value') == strAllesSelecteren) {
            var strCheckBoxClass = '.searchChk' + $(this).attr('rel');
            $(strCheckBoxClass).each(function() {
                $(this).attr('checked', true);
            });
            $(this).attr('value', strAllesDeselecteren)
        } else {
            var strCheckBoxClass = '.searchChk' + $(this).attr('rel');
            $(strCheckBoxClass).each(function() {
                $(this).attr('checked', false);
            });
            $(this).attr('value', strAllesSelecteren)
        }
    });
}

$(document).ready(function() {

    initSnelMenu();
    initProductOverzicht();
    initProductDetailImageGallery();
    initBackButtons();
    initPrintButtons();
    initFormValidation();
    initSearch();

});

