(function($) {
    $.adszone = {
        defaults: {
            zoneid: 0,
            pid: 0,
            loadUrl: "/ads/load/"
        },
        // array for checking status of container IDs being loaded
        isLoading: []
    };

    $.fn.extend({
        adszone: function(settings) {
            settings = $.extend({}, $.adszone.defaults, settings);

            // zoneid ve opsiyonel pid
            var url = settings.loadUrl + settings.zoneid + "/" + settings.pid +
                "/" + Math.floor(89999999*Math.random()+10000000) +
                "/" + (new Date().getTime());

            return this.each(function() {
                var instance = $(this);
                //!!! all containers must have an ID
                var instanceID = instance.attr("id");

                // check for reentrance in AJAX
                if ($.adszone.isLoading[instanceID] == null) {
                    // mark the container as being loaded
                    $.adszone.isLoading[instanceID] = true;

                    $.ajax({
                        url: url,
                        type: "GET",
                        error: function() {
                            // mark the container as loaded
                            $.adszone.isLoading[instanceID] = null;
                        },
                        success: function(data, textStatus) {
                            data = data.replace("document.writeln", "");
                            data = eval(data);
                            instance.html(data);

                            // mark the container as loaded
                            $.adszone.isLoading[instanceID] = null;
                        }
                    });
                }
            });
        }
    });
})(jQuery);
