// Console Logging if available
var logger = function (msg) {
    try {
        console.log(msg);
    }
    catch (err) {
    }
}


var PopularList = {
    setupList: function () {       
        $.PeriodicalUpdater('/ajaxutilities?ajaxcommand=pagetrackerweb', {
            method: 'get', // method; get or post
            data: '', // array of values to be passed to the page - e.g. {name: "John", greeting: "hello"}
            minTimeout: 60000, // starting value for the timeout in milliseconds
            maxTimeout: 120000, // maximum length of time between requests
            multiplier: 1.1, // if set to 2, timerInterval will double each time the response hasn't changed (up to maxTimeout)
            type: 'text', // response type - text, xml, json, etc.  See $.ajax config options
            maxCalls: 0, // maximum number of calls. 0 = no limit.
            autoStop: 0// automatically stop requests after this many returns of the same data. 0 = disabled.
        },
        function (data) {
            $('#popular_list').html(data);
        })
    }
};

/**
* Side200200 type advert load
*/
var Side200200Advert = {
    setupAd: function (id) {
        var idName = id;
        $.PeriodicalUpdater('/ajaxutilities', {
            method: 'get', // method; get or post
            data: {
                ajaxcommand: 'advert',
                adtype: 'side200200'
            },
            // array of values to be passed to the page - e.g. {name: "John", greeting: "hello"}
            minTimeout: 61000, // starting value for the timeout in milliseconds
            maxTimeout: 90000, // maximum length of time between requests
            multiplier: 1.1, // if set to 2, timerInterval will double each time the response hasn't changed (up to maxTimeout)
            type: 'text', // response type - text, xml, json, etc.  See $.ajax config options
            maxCalls: 0, // maximum number of calls. 0 = no limit.
            autoStop: 0// automatically stop requests after this many returns of the same data. 0 = disabled.
        },
        function (data) {
            logger("In Side Update: "+data);
            $(idName).html(data);
        })
    }
}


/**
* bottom728100 type advert load
*/
var Bottom728100Advert = {
    setupAd: function (id) {
        var idName = id;
        $.PeriodicalUpdater('/ajaxutilities', {
            method: 'get', // method; get or post
            data: {
                ajaxcommand: 'advert',
                adtype: 'bottom728100'
            },
            // array of values to be passed to the page - e.g. {name: "John", greeting: "hello"}
            minTimeout: 59000, // starting value for the timeout in milliseconds
            maxTimeout: 90000, // maximum length of time between requests
            multiplier: 1.1, // if set to 2, timerInterval will double each time the response hasn't changed (up to maxTimeout)
            type: 'text', // response type - text, xml, json, etc.  See $.ajax config options
            maxCalls: 0, // maximum number of calls. 0 = no limit.
            autoStop: 0// automatically stop requests after this many returns of the same data. 0 = disabled.
        },
        function (data) {
            logger("In Update: "+idName);
            $(idName).html(data);
        })
    }
}


var WR = {
    fetchList: function (wr_list) {
        // Setup pageTrackerUpdater
        $.ajax({
            url: '/ajaxutilities',
            type: 'get',
            data: {
                ajaxcommand: 'winelist',
                listitems: WR.getWRLinks
            },
            success: WR.processWineListXML
        })
    },
    getWRLinks: function () {
        var data = '';
        $('a[href]').each(function () {
            if (data.length > 0)
                data += ",";
            data += "'" + $(this).attr('href').replace(/\/.+\//, '')+"'";
        });
        return data;
    },
    processWineListXML: function (xml) {
        $(xml).find('wine').each(function () {
            var a_hrefKey = $(this).text();
            var a_element = $("a[href$='" + a_hrefKey + "']");
            var a_element_text = $("a[href$='" + a_hrefKey + "']:first").text();
            a_element.toggle();
            a_element.html(a_element_text + " (wr)");
            a_element.toggle();
        })
    }
}

var Captcha = {
    loadCaptcha: function() {
        $('#captchaImg').html('<img src="/captcha" />');
        
    }
}

$(document).ready(function () {
    WR.fetchList();
    //Captcha.loadCaptcha();
    Side200200Advert.setupAd('#advert_right');
    Bottom728100Advert.setupAd('#advert_bottom');
    PopularList.setupList();
});
