/*
    Document   : ads_slot_edit
    Created on : Aug 21, 2009, 00:00:17 AM
    Author     : Yehia A.Salam
*/


    jQuery.fn.adrotator = function() {

      return this.each(function() {

                    var rSlotID = jQuery(this).attr("slot");
                    var jrSlot = jQuery(this);
                    //console.info("Fuck I'm here:" + rSlotID);

                    requestAd(jrSlot);
                    jrSlot.everyTime("20s", function(i) {
                       requestAd(jQuery(this));
                    });


      });

      function requestAd(jSlot){

            var SlotID = jSlot.attr("slot");
            var rCurrent = jSlot.attr("current");
            if (!rCurrent) rCurrent = -1;

            jQuery.ajax({
               type: "GET",
               dataType: "xml",
               url: "http://" + location.host + "/ad_server",
               data: "slot=" + SlotID + "&current=" + rCurrent,
               error: function(){
                    //alert('Error loading XML document');
                },
               success: function(xml){
                jQuery(xml).find("ad").each(function(){
                    var jxml_ad = jQuery(this);
                    var xml_ad_content = jxml_ad.find("content").text();
                    var xml_ad_id = jxml_ad.find("id").text();
                    jSlot.html(xml_ad_content);
                    jSlot.attr("current", xml_ad_id);
                });
               }
             });

      }


    };