var pathExpressInstallFile = escape("/lib/flash/expressInstall.swf");
var mediaPopupCallerId;

function initPopups() {

    $.fn.nyroModal.settings.regexImg = '[^\.]\.(jpg|jpeg|png|tiff|gif|bmp)\s*$|image.aspx';
    $.fn.nyroModal.settings.minWidth = 0;
    $.fn.nyroModal.settings.minHeight = 0;

}
function initEuropeMapFlash() {

if($("#europeMap").length > 0) {            
        $('#europeMap').flashembed(
        {
            src: 'lib/flash/EuropeMap.swf',
            version: [9],
            width: '472px',
            height: '650px',
            expressInstall: pathExpressInstallFile,
            wmode: 'transparent',
            scale: 'noscale'
        }, { xmlPath: '/xml/names.ashx',mapHasPlayedBefore: kaartIsAlEerderAfgespeeld })
    }
}
function initHomepageFlash() {
    $('#homepageflash').flashembed(
    {
        src: 'lib/flash/twentysevennames.swf',
        version: [9],
        width: '999px',
        height: '600px',
        expressInstall: pathExpressInstallFile,
        wmode: 'transparent',
        scale: 'noscale'
    }, { xmlPath: '/xml/names.ashx' })
}

function initContentFlash() {
    $('.contentflash').each(
        function() {
            var strId = $(this).attr('id');
            var strParams = $(this).find('a').attr('rel').split('|');

            $('#' + strId).flashembed(
            {
                src: strParams[0],
                version: [strParams[1]],
                width: strParams[2],
                height: strParams[3],
                expressInstall: pathExpressInstallFile,
                wmode: 'transparent',
                scale: 'noscale'
            })
        }
    );
}

function initValidationMember() {
    if ($('#frmMemberInformation').length > 0) {
        $('#frmMemberInformation').validate({
            rules: {
                txtPassword: { required: true },
                txtVerifyPassword: { equalTo: '#txtPassword' },
                lstCountry: { min: 1 }
            },
            messages: {
                lstCountry: { min: 'This field is required.' }
            }
        });
    }
}

function initValidationCase() {
    if ($('#frmCaseInformation').length > 0) {
        jQuery.validator.addMethod("defaultInvalid", function(value, element) {
            switch (element.value) {
                case "00000000-0000-0000-0000-000000000000":
                    if (element.name == "00000000-0000-0000-0000-000000000000")
                        return false;
                    break;
                default:
                    return true;
                    break;
            }
        });

        $('#frmCaseInformation').validate({
            messages: { hiddenMedia1: 'This field is required.' }
        });

        if ($('#ddlClient').length > 0) {
            $('#ddlClient').change(function() {
                //alert($('#ddlClient option:selected').text());
                if ($('#ddlClient').val().length > 0) {
                    $('#txtClient').val($('#ddlClient option:selected').text());
                }
            });
        }

    }
}

function initValidationClient() {
    if ($('#frmClientInformation').length > 0) {

        jQuery.validator.addMethod("defaultInvalid", function(value, element) {
            switch (element.value) {
                case "00000000-0000-0000-0000-000000000000":
                    if (element.name == "00000000-0000-0000-0000-000000000000")
                        return false;
                    break;
                default:
                    return true;
                    break;
            }
        });

        $('#frmClientInformation').validate({
            messages: { hiddenLogo: 'This field is required.' }
        });
    }
}

function initValidationNewsletter() {
    if ($('#frmNewsletterInformation').length > 0) {

        $('#frmNewsletterInformation').validate({
            messages: { hiddenLogo: 'This field is required.' }
        });

        $('#tabs a').bind('click', function() {


            $('#tabs a').removeClass('active');
            $(this).addClass('active');

            $('#frmNewsletterInformation tr').hide();
            $('tr.' + $(this).attr('rel')).show();
            $('tr.buttons').show();

        });
        
    }
}


function initValidationLogin() {
    if ($('#frmMemberLogin').length > 0) {
        $('#frmMemberLogin').validate();
    }
}

function initValidationContact() {
    if ($('#frmContact').length > 0) {
        $('#frmContact').validate();
    }
}

function initAddMediaPopup() {

    if ($('#frmAddMediaFile').length > 0) {
        $('#btnAddMedia').bind("click", function() {
            ajaxFileUpload();
        });
    }

    if ($('#frmAddMediaGadget').length > 0) {
        $('#btnAddGadget').bind("click", function() {
            var strGadgetCode = $('#txtGadgetcode').val();
            $.getJSON("/ajax/addmediagadget.aspx", { gadget: strGadgetCode },
                function(data, textStatus) {
                    $('#' + mediaPopupCallerId).val(data.msg);
                    $('#' + mediaPopupCallerId.replace('hidden', 'span')).html(data.gadgetcode);
                }
            );
        });
    }

    if ($('#MediaWizard').length > 0) {
        $('#btnWizardFile').bind("click", function() {
            $('#MediaWizardStart').hide(500);
            $('#MediaWizardFile').show(500);
        });
        $('#btnBackMedia').bind("click", function() {
            $('#MediaWizardFile').hide(500);
            $('#MediaWizardStart').show(500);
        });
        $('#btnWizardGadget').bind("click", function() {
            $('#MediaWizardStart').hide(500);
            $('#MediaWizardGadget').show(500);
        });
        $('#btnBackGadget').bind("click", function() {
            $('#MediaWizardGadget').hide(500);
            $('#MediaWizardStart').show(500);
        });
    }
}

function ajaxFileUpload() {
    $("#loading")
    .ajaxStart(function() {
        $(this).show();
    })
    .ajaxComplete(function() {
        $(this).hide();
    });
    $.ajaxFileUpload
		(
			{
			    url: 'ajax/addmediafile.aspx',
			    secureuri: false,
			    fileElementId: 'fileMedia',
			    dataType: 'json',
			    beforeSend: function() {
			        $("#loading").show();
			    },
			    complete: function() {
			        $("#loading").hide();
			    },
			    success: function(data, status) {
			        if (typeof (data.error) != 'undefined') {
			            if (data.error != '') {
			                $('#spanUploadError').html(data.error);
			            } else {
			                var strMediaGuid = data.msg;

			                $('#' + mediaPopupCallerId).val(strMediaGuid);
			                $.get("/ajax/getmediahtml.aspx", { id: strMediaGuid },
			                    function(data, textStatus) {
			                        $('#' + mediaPopupCallerId.replace('hidden', 'span')).html(data);
			                    }
			                );
			            }
			        }
			    },
			    error: function(data, status, e) {
			        alert(e);
			    }
			}
		)
    return false;
}

function initForms() {

    $('a.addmedia').nyroModal({
        bgColor: '#000',
        width: 400,
        height: 200,
        endShowContent: initAddMediaPopup,
        galleryLinks: null
    });

    if ($('a.addmedia').length > 0) {
        $('a.addmedia').bind("click", function() {
            mediaPopupCallerId = $(this).attr('rel');
        });
    }

    if ($('input.cancel').length > 0) {
        $('input.cancel').bind("click", function() {
            document.location = document.referrer;
        });
    }

    if ($('a.clearmedia').length > 0) {
        $('a.clearmedia').bind("click", function() {
            if (confirm('Are you sure you want to remove this mediaitem')) {
                var strMediaInputId = $(this).attr('rel');
                var strMediaGuid = $('#' + strMediaInputId).val();
                $('#' + strMediaInputId).val('00000000-0000-0000-0000-000000000000');
                $('#' + strMediaInputId.replace('hidden', 'span')).html('');
                $.get("/ajax/deletemedia.aspx", { id: strMediaGuid });
            }
            return false;
        });
    }

    if ($('input.cancel').length > 0) {
        $('input.cancel').bind("click", function() {
            history.go(-1);
        });
    }

    if ($('a.confirm').length > 0) {
        $('a.confirm').bind("click", function() {
            if (confirm('are you sure want to delete this item'))
                return true
            else
                return false
        });
    }
}

$(document).ready(function() {

    //Initialiseer hier je scripts
    initHomepageFlash();
    initContentFlash();
    initEuropeMapFlash();
    initPopups();
    initValidationMember();
    initValidationCase();
    initValidationClient();
    initValidationLogin();
    initValidationContact();
    initValidationNewsletter();
 
    initForms();

});
