
(function (w, undefined) {
    var d = w.document;

    w.ImmAds = {
        Opt       : {},
        Default: {
            Width : 0,
            Height: 0,
        
            Age    : 0,
            Sex    : -1,
            Country: "us",

            Domain: "www.indexmultimedia.com",
            Page  : "/",

            Params: "",
            AdUrl : "http://advertising.indexmultimedia.com/AdDisplay.aspx"
        },

        Init: function (opt) {
            ImmAds.Opt = {};
            
            // Function called before each ad is displayed, and overrides default options (if present)
            ImmAds.Opt.Country = (opt.Country !== undefined) ? opt.Country : ImmAds.Default.Country;
            ImmAds.Opt.Domain  = (opt.Domain  !== undefined) ? opt.Domain  : ImmAds.Default.Domain;
            ImmAds.Opt.Page    = (opt.Page    !== undefined) ? opt.Page    : ImmAds.Default.Page;
            ImmAds.Opt.Params  = (opt.Params  !== undefined) ? opt.Params  : ImmAds.Default.Params;

            ImmAds.Opt.Age = ((opt.Age !== undefined) && (opt.Age > 0))   ? opt.Age : ImmAds.Default.Age;
            ImmAds.Opt.Sex = ((opt.Sex !== undefined) && (opt.Sex != -1)) ? opt.Sex : ImmAds.Default.Sex;
            
            ImmAds.Opt.Width  = ((opt.Width  !== undefined) && (opt.Width > 0))  ? opt.Width  : ImmAds.Default.Width;
            ImmAds.Opt.Height = ((opt.Height !== undefined) && (opt.Height > 0)) ? opt.Height : ImmAds.Default.Height;
        },
        
        // START: ADSENSE CONFIGURATION
        GoogleAdServer: function(adClient, adSlot) {
            // setup values
            w.google_ad_client = adClient;
            w.google_ad_slot   = adSlot;

            w.google_ad_width  = ImmAds.Opt.Width;
            w.google_ad_height = ImmAds.Opt.Height;

            w.google_cust_age    = undefined;
            w.google_cust_gender = undefined;           
    
            if (ImmAds.Opt.Age > 12) {
                // A voire...... (mapping age/sex google/IMM)
                if (ImmAds.Opt.Age > 64)
                {
                    w.google_cust_age = 1006; // 65-99
                }
                else if (ImmAds.Opt.Age > 54)
                {
                    w.google_cust_age = 1005; // 55-64
                }
                else if (ImmAds.Opt.Age > 44)
                {
                    w.google_cust_age = 1004; // 45-54
                }
                else if (ImmAds.Opt.Age > 34)
                {
                    w.google_cust_age = 1003; // 35-44
                }
                else if (ImmAds.Opt.Age > 24)
                {
                    w.google_cust_age = 1002; // 26-34
                }
                else if (ImmAds.Opt.Age > 18)
                {
                    w.google_cust_age = 1001; // 18-25
                }
                else if (ImmAds.Opt.Age > 12)
                {
                    w.google_cust_age = 1000; // 13-17
                }
            }
    
            switch (ImmAds.Opt.Sex)
            {
                case 0:
                    w.google_cust_gender = 1; // male
                    break;

                case 1:
                    w.google_cust_gender = 2; // female
                    break;                     
            }
    
            d.write('<scr' + 'ipt src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></scr' + 'ipt>');
        },
        
        // START: SMART CONFIGURATION
        SmartAdServer: function (sasPageid, sasFormatid) {
            var sasTmstp = Math.round(Math.random() * 10000000000);

            if (w.sas_masterflag == 1) {
                w.sas_masterflag = 0;
                w.sas_master     = 'M';
            }
            else {
                w.sas_master = 'S';
            }

            // Start Custom Opt (Age/Sex/Params)
            var sasTargetTmp = [];
            if (ImmAds.Opt.Params != "") {
                sasTargetTmp.push(ImmAds.Opt.Params);
            }

            if (ImmAds.Opt.Age > 0) {
                sasTargetTmp.push("Age=" + ImmAds.Opt.Age);
            }
        
            if (ImmAds.Opt.Sex == 0 || ImmAds.Opt.Sex == 1) {
                var sex = ImmAds.Opt.Sex == 0 ? "H" : "F";
                sasTargetTmp.push("Sex=" + sex);
            }

            if (sasTargetTmp.length > 0) {
                w.sas_target = sasTargetTmp.join(";");
            }
        
            d.write('<scr' + 'ipt src="http://ads.horyzon-media.com/call/pubj/' + sasPageid + '/' + sasFormatid + '/' + w.sas_master + '/' + sasTmstp + '/' + escape(w.sas_target) + '?"></scr' + 'ipt>');                
        }      
    };
})(window);

// Fractured for readability 
function ShowAdvertising (opt) {
    // Initialize Options
    ImmAds.Init(opt);
    
    // START: SMART Related Global Variables
    window.sas_masterflag = 1;
    window.sas_target     = ""; 
    
    // Write Ad
    var url = ImmAds.Default.AdUrl
                .concat("?Width="      , ImmAds.Opt.Width)
                .concat("&amp;Height=" , ImmAds.Opt.Height)
                .concat("&amp;Age="    , ImmAds.Opt.Age)
                .concat("&amp;Sex="    , ImmAds.Opt.Sex)
                .concat("&amp;Country=", ImmAds.Opt.Country)
                .concat("&amp;Domain=" , ImmAds.Opt.Domain)
                .concat("&amp;Page="   , ImmAds.Opt.Page);
    
    document.write('<scr' + 'ipt src="' + url + '"></scr' + 'ipt>');  
};
