(function() {
    var jQuery;
    var $;
    if(window.hasOwnProperty('fawloaded') && window.fawloaded == true) return;
    window.fawloaded = true;
    if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') {
        var script_tag = document.createElement('script');
        script_tag.setAttribute("type","text/javascript");
        script_tag.setAttribute("src","https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
        if (script_tag.readyState) {
            script_tag.onreadystatechange = function () {
                if (this.readyState == 'complete' || this.readyState == 'loaded') {
                    scriptLoadHandler();
                }
            };
        } else { script_tag.onload = scriptLoadHandler; }
        (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
    } else { jQuery = window.jQuery; fawmain(); }
    function scriptLoadHandler() {
        jQuery = window.jQuery.noConflict(true);
        $ = jQuery;
        fawmain();
    }
    function fawurl(service,param) { return 'https://services.foreningsadministrator.dk/widget/'+service+'?aid=1303&check=c8738e510298cb3ecb11c2ae703e080f'+((param != null && param != undefined) ? param : ''); }
    function fawuid() { return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1)+"-"+Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1); }
    function setupWidget(elm,frameurl,initCallback)
    {
        var uid = fawuid()
        $(elm).attr('id','faw-'+uid).data('uid',uid).html();
        if(frameurl != '')
        {
            var jsonp_url = fawurl(frameurl);
            $.getJSON(jsonp_url, function(data) {
                $('#faw-'+uid).html(data.html);
                initCallback(elm,uid)
            });
        }
        return uid;
    }
    function fawmain() {
        jQuery(document).ready(function($) {
            var css_link = $("", { rel: "stylesheet", type: "text/css", href: fawurl('css') });
            css_link.appendTo('head');
            var fa_link = $("", { rel: "stylesheet", type: "text/css", href: "https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" });
            fa_link.appendTo('head');
            $('.faw-widget').each(function() {
                if($(this).hasClass('faw-calendar')) setupWidget(this,'calendar/frame',initCalendar);
                if($(this).hasClass('faw-login')) setupWidget(this,'login/frame',initLogin);
            });
        });
    }
    /************ LOGIN **************/
    function initLogin(elm,uid)
    {
        var parent = $(elm);
        var url = parent.find('.faw-siteurl').val();
        parent.find('.faw-login').bind('click',function() {
                            loginLogin(elm);
                    });
        parent.find('.faw-forgot').bind('click',function() { parent.find('.faw-login-box').hide(); parent.find('.faw-forgot-box').show(); parent.find('.faw-forgot-error').hide(); });
        parent.find('.faw-forgot-back').bind('click',function() { parent.find('.faw-forgot-succes-box').hide(); parent.find('.faw-login-box').show(); parent.find('.faw-forgot-box').hide(); parent.find('.faw-login-error').hide(); });
        parent.find('.faw-forgot-button').bind('click',function() {
                            loginForgot(elm);
                    });
    }
    function loginForgot(elm)
    {
        var parent = $(elm);
        var url = parent.find('.faw-siteurl').val();
        parent.find('.faw-forgot-error').hide();
        $.post(url+'/adgangskode/forgotservice',{username: parent.find('.faw-forgot-username').val()},function(response) {
            if(response.status == 1) { parent.find('.faw-forgot-box').hide(); parent.find('.faw-forgot-succes-box').show(); }
            else if(response.hasOwnProperty('error') && response.error != "") parent.find('.faw-forgot-error').show().find('td').html(response.error);
            else parent.find('.faw-forgot-error').show().find('td').html("Der opstod en ukendt fejl, prøv igen!");
        },'json');
    }
    function loginLogin(elm)
    {
        var parent = $(elm);
        var url = parent.find('.faw-siteurl').val();
        parent.find('.faw-login-error').hide();
        $.post(url+'/login/loginservice',{username: parent.find('.faw-username').val(),password: parent.find('.faw-password').val()},function(response) {
            if(response.status == 1)
            {
                //document.location = $.trim(parent.find('.faw-site-redirect').val()) != "" ? parent.find('.faw-site-redirect').val() : url;
                parent.find('.loginform').attr('action',url+"/login/loginform").submit();
            }
            else if(response.hasOwnProperty('error') && response.error != "") parent.find('.faw-login-error').show().find('td').html(response.error);
            else parent.find('.faw-login-error').show().find('td').html("Der opstod en ukendt fejl, prøv igen!");
        },'json');
    }
    /************ CALENDAR ***********/
    function initCalendar(elm,uid)
    {
        $(elm).find('.calendar-month').bind('change',fawCalUpdateEvent);
        $(elm).find('.calendar-year').bind('change',fawCalUpdateEvent);
        $(elm).find('.faw-calnext').bind('click',fawCalNext);
        $(elm).find('.faw-calprev').bind('click',fawCalPrev);
        $(elm).find('.cal-back-link').bind('click', function() { fawCalUpdate(elm); return false; })
        fawCalUpdate(elm);
    }
    function fawCalUpdateEvent(event) { fawCalUpdate($(event.target).closest('.faw-widget')); }
    function fawCalUpdate(widget)
    {
        var month = $(widget).find('.calendar-month').val();
        var year = $(widget).find('.calendar-year').val();
        var iea = $(widget).hasClass('faw-calendar-iea') ? $(widget).attr('data-iea') : "non";
        var url = fawurl('calendar/data','&month='+month+'&year='+year+'&iea='+iea);
        $.getJSON(url, function(data) {
            $(widget).find('.calendarcontent').show();
            $(widget).find('.calendardetails').closest('tr').hide();
            $(widget).find('.calendarcontent').html(data.html);
            $(widget).find('.faw-cal-event').bind('click',function() { fawCalDetails(this)});
        });
    }
    function fawCalDetails(details)
    {
        var widget = $(details).closest('.faw-widget');
        widget.find('.calendarcontent').hide();
        widget.find('.calendardetails').closest('tr').show()
        var detailtext = $(details).find('.details').html();
        var text = $(details).find('.text').html();
        var locationtext = $(details).find('.location').html();
        var link = $.trim($(details).find('.link').val());
        var title = $(details).find('.title').html();
        var extradetails = $(details).find('.extradetails').html();
        widget.find('.calendardetails').find('.cal-detail-title').html(title);
        widget.find('.calendardetails').find('.cal-detail-html').html(detailtext);
        if(locationtext == null || locationtext == '') widget.find('.calendardetails').find('.cal-location-html').hide(); else widget.find('.calendardetails').find('.cal-location-html').html(locationtext).show();
        widget.find('.calendardetails').find('.cal-detail-text').html(text);
        widget.find('.calendardetails').find('.cal-detail-details').html(extradetails);
        if(link == "") widget.find('.calendardetails').find('.cal-detail-link').hide();
        else widget.find('.calendardetails').find('.cal-detail-link').show().find('a').attr('href',link);
    }
    function fawCalNext(event)
    {
        var widget = $(event.target).closest('.faw-widget');
        var monthsel = widget.find('.calendar-month');
        var yearsel = widget.find('.calendar-year');
        if(monthsel.val() == "12") {
            monthsel.val("1");
            yearsel.val(parseInt(yearsel.val())+1);
        }
        else monthsel.val(parseInt(monthsel.val())+1);
        fawCalUpdate(widget);
    }
    function fawCalPrev()
    {
        var widget = $(event.target).closest('.faw-widget');
        var monthsel = widget.find('.calendar-month');
        var yearsel = widget.find('.calendar-year');
        if(monthsel.val() == "1") {
            monthsel.val("12");
            yearsel.val(parseInt(yearsel.val())-1);
        }
        else monthsel.val(parseInt(monthsel.val())-1);
        fawCalUpdate(widget);
    }
})();