﻿//Function that provides a relative URL for cases of virtual directories.
jQuery.ResolveUrl = function(path) {

    var AppRoot = "";

    //in het geval van localhost wordt approot voor link geplaatst.
    if (document.getElementsByTagName('base')[0].href.split("/")[2].toLowerCase() == "localhost") {
        //Get Application name from Base string
        AppRoot = "/" + document.getElementsByTagName('base')[0].href.split("/")[3];
    }
    return path.replace("~", AppRoot);
};

(function($) {
    $.fn.extend({
        woningAanbodModuleWidget: function(url) {
            return this.each(function() {
                //Creating a reference to the object
                var obj = $(this);

                // Get woninglist
                $.ajax({
                    url: url,
                    cache: false,
                    dataType: 'xml',
                    contentType: 'text/xml',
                    success: parseXml,
                    error: function(xhr, ajaxOptions, thrownError) { alert(xhr.status); alert(thrownError); }
                });

                // Parse XML
                function parseXml(xml) {

                    $(xml).find("woning").each(function() {

                        // Get all information
                        objDiv = $('<div>').addClass('woning');
                        objImage = "<div class=\"img\"><a href=\"" + $(this).find("image").text() + "\"><img src=\"" + $(this).find("image").text() + "\" /></a></div>";
                        //objAdres = "<h4><a href=\"" + $.ResolveUrl("~/Paginaview.aspx?MenuId=Woningaanbod") + "\">" + $(this).find("adres").text() + "</a></h4>";
                        objAdres = "<h4><a href=\"/Paginaview.aspx?MenuId=Woningaanbod\">" + $(this).find("adres").text() + "</a></h4>";
                        objSoort = "<p>" + $(this).find("soort").text() + "</p>";
                        objHuurprijs = "<p class=\"huurprijs\">&euro; " + $(this).find("huurprijs").text() + "</p>";

                        // Wrap all information into DIV
                        objDiv.hide().append(objImage + objSoort + objAdres + objHuurprijs);

                        // Insert to DOM
                        obj.append(objDiv);
                    });

                    setTimer();
                }

                // Set timer
                function setTimer() {
                    if (obj.children('div').length == 0) {
                        // No aanbod
                        obj.append('Er is momenteel geen woningaanbod.');
                    } else {
                        // Make first woning active
                        obj.children('div:first').addClass('activeWon').fadeIn();

                        // Set timer
                        $(document).everyTime(4000, function(i) {
                            var $active = obj.children('div.activeWon');
                            var $next = $active.next().length ? $active.next() : obj.children('div:first');

                            // Animate down
                            $('#divWoningAanbodLoader').addClass('active').animate({ height: '120px' }, 500, function() {
                                $active.removeClass('activeWon').hide();
                                $next.addClass('activeWon').show();

                                $('#divLoaderImg').width(0);
                                $('#divLoaderImg').show().animate({ width: '17px' }, 600, function() {
                                $(this).hide();
                            });

                            $('#divWoningAanbodLoader').fadeTo(500, 0, function() {
                                $(this).removeClass('active');

                                // Reset height and fade
                                $(this).height('1px');
                                $('#divWoningAanbodLoader').fadeTo(0, 1);
                            });

                            });
                        });
                    }
                }
            });
        }
    });
})(jQuery);


